Search code examples
matlabgpuparfor

MATLAB arrayfun on GPU with parfor loop


I am doing some heavy computational work using arrayfun with GPU in MATLAB.

My code looks like this

N = 2000;
dp = 0.005;
p1 = [0:dp:1];
p2 = [0:dp:1];
pB = [0:dp:2];
[p1,p2,pB] = meshgrid(p1,p2,pB);
p1 = gpuArray(p1);
p2 = gpuArray(p2);
pB = gpuArray(pB);
A = zeros(N,1);
parfor i = 1:N
       A(i) = arrayfun(@MYFUN,p1,p2,pB);
end

First, I am surprised that for N=2000, the parfor almost takes the same time as the ordinary for loop (when using parfor, it seems that my MATLAB connects to 6 workers). Is that because my laptop only has 1 GPU, so parfor isn't helping?


Solution

  • parfor isn't running on the GPU rather it runs multiple "workers" on the available cores of your computer CPU. In general the GPU cores cannot serve as matlabpool/parpool workers.

    If you have multiple GPU's you can use the parfor combined with spmd in order to run it on the GPU. mathwork answer but this is in case you want to execute a specific function on the GPU.