How to pass on a different sized matrix through GPU Arrayfun
이전 댓글 표시
I wrote the following program:
x1 = parallel.gpu.GPUArray.linspace(low,high,N)';
x2 = parallel.gpu.GPUArray.linspace(low,high,N)';
[xx1,xx2] = ndgrid(x1,x2);
[y1,y2] = arrayfun(@fun, xx1,xx2, Vmatrix, constant);
I want y1 and y2 to be output whose elements corresponding to each element of xx1 and xx2. But in order to calculate each element of y1 and y2 I also need to use Vmatrix which is a matrix.
In order to pass on Vmatrix that is a different size from matrix xx1, can I define the following?
Vcell=cell(size(xx1));
and for each cell element=Vmatrix. In this way the Vcell will have the same dim of xx1 and xx2, and write:
[y1,y2] = arrayfun(@fun, xx1,xx2, Vcell, constant);
Thank you!
답변 (1개)
Edric Ellis
2013년 11월 6일
Unfortunately, you cannot pass cell arrays to the GPU. What you might be able to do is instead use a nested function handle to pass in your matrix through the "up-level workspace". Here's an example:
function example
% Define a matrix and a vector
matrix = magic(5);
gpuVector = gpuArray.colon(1, 10);
% Nested function definition for use with arrayfun
% which accesses "matrix"
function out = nestedFcn(in)
out = in;
for i = 1:5
for j = 1:5
% Remember we must use only scalar operations
% within arrayfun on the GPU
out = out + matrix(i, j);
end
end
end
% Call arrayfun.
arrayfun(@nestedFcn, gpuVector)
end
카테고리
도움말 센터 및 File Exchange에서 GPU Computing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!