Well, for the start, XGs_C and XG_C are both Cell arrays. gputimeit() accepts a function handle not cell array. Besides for the first gputimeit (line 20), you are accessing XG_C which is not defined until line 26. So I think you meant XGs_C.
also on line 34 (serial loop or "% CPU" section), you are accesing XG_A and XG_B which I think you wanted to access X_A and X_B.
Well, you can have your code working by changing it to the following:
matrixSize = 4000;
gpuDevice([]);
nGPUs = gpuDeviceCount();
p = gcp;
spmd
gd = gpuDevice;
idx = gd.Index;
disp(['Using GPU ',num2str(idx)]);
end
parfor i = 1:p.NumWorkers
gd = gpuDevice;
XGs = rand(matrixSize,'gpuArray');
XGs_A = XGs * XGs;
XGs_B = XGs / XGs;
XGs_C = @() bsxfun(@times, XGs_A, XGs_B);
wait(gd);
time5GPUs{i} = gputimeit(XGs_C);
end
parfor i = 1:p.NumWorkers
XG = rand(matrixSize,'gpuArray');
XG_A = XG * XG;
XG_B = XG / XG;
XG_C = @() bsxfun(@times, XG_A, XG_B);
time1GPU{i} = gputimeit(XG_C);
end
for i = 1:p.NumWorkers
X = rand(matrixSize);
X_A = X * X;
X_B = X / X;
X_C = @() bsxfun(@times, X_A, X_B);
timeCPU{i} = timeit(X_C);
end
Hope that solves your problem.
Best Answer