MATLAB: Parfor on GPU

gpuparfor

I want to run two functions in parallel on a GPU. For this i want to use pafor(eg: for ii=1 fun1 and ii=2 fun2).
Can variables on GPU be used for parfor operations on GPU?

Best Answer

Yes, you can do this. Whether you get much benefit depends on whether you have multiple GPUs in your system (under some circumstances, a single GPU might actually suffice if you have enough CPU work to keep things busy).
You might wish to do something like
spmd
gpuDevice( 1 + mod( labindex - 1, gpuDeviceCount ) )
end
before you go any further (if you have multiple supported GPUs)
After that, gpuArrays can be passed into and out from PARFOR loops with no further modification. The following example shows this - but note that this is a proof of concept - it performs very badly because you're operating on scalar elements of the gpuArray.
g = gpuArray(1:10);
parfor ii=1:numel(g)
x(ii) = 1/g(ii);
end
Related Question