How to pass on a different sized matrix through GPU Arrayfun
1 view (last 30 days)
Show older comments
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!
0 Comments
Answers (1)
Edric Ellis
on 6 Nov 2013
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
See Also
Categories
Find more on GPU Computing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!