How do I multiply matrices using multiple GPU?
Show older comments
Hi All,
I'm new to Matlab, so apologies for any basic mistakes.
I'm trying to run a multiplication of matrices on multiple GPUs to then compare the computation time with running the same code on 1 GPU and again on the CPU. The machine has 5 GPUs, and the code is this:
matrixSize = 4000;
gpuDevice([]);
nGPUs = gpuDeviceCount();
parpool('local', nGPUs);
p = gcp;
spmd
gd = gpuDevice;
idx = gd.Index;
disp(['Using GPU ',num2str(idx)]);
end
% 5 GPUs
parfor i = 1:p.NumWorkers
gd = gpuDevice;
XGs{i} = rand(matrixSize,'gpuArray');
XGs_A{i} = XGs{i} * XGs{i};
XGs_B{i} = XGs{i} / XGs{i};
XGs_C{i} = @() bsxfun(@times, XGs_A{i}, XGs_B{i});
wait(gd);
end
time5GPUs = gputimeit(XG_C)
% 1 GPU
parfor i = 1:p.NumWorkers
XG{i} = rand(matrixSize,'gpuArray');
XG_A{i} = XG{i} * XG{i};
XG_B{i} = XG{i} / XG{i};
XG_C{i} = @() bsxfun(@times, XG_A{i}, XG_B{i});
end
time1GPU = gputimeit(XG_C)
% CPU
for i = 1:p.NumWorkers
X{i} = rand(matrixSize);
X_A{i} = X{i} * X{i};
X_B{i} = X{i} / X{i};
X_C{i} = @() bsxfun(@times, XG_A{i}, XG_B{i});
end
timeCPU = timeit(X_C)
When I run it, the error I get is
Error: The variable XGs_A in a parfor cannot be classified.
See Parallel for Loops in MATLAB, "Overview".
How can I solve this problem? And is there a better way to do this?
Accepted Answer
More Answers (0)
Categories
Find more on GPU Computing in MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!