Cell arrays and multiple GPUs computing
显示 更早的评论
Hi,
I have a cell array of matrices and I have two GPUs, each can process matrices memory-wise.
I want to be able to simultaneously process all mat_list elements on two GPUs. I am wondering if SPMD is suitable for this task? If yes, how can I pass on cell arrays into SPMD in such a way that 2 GPUs process all arrays?
Thank you.
回答(1 个)
Edric Ellis
2015-10-19
You could use spmd or parfor for this, but beware that this will involve making copies of the data onto the GPUs used by the workers. For example, you could do something like this:
matrices = {rand(1000, 'gpuArray'), rand(2000, 'gpuArray')};
spmd
if labindex <= numel(matrices)
myMatrix = matrices{labindex};
myResult = max(myMatrix(:));
else
myResult = NaN;
end
end
The downside of this approach is that all elements of the cell array matrices are copied to each GPU on the workers. For this particular sort of case, parfor would involve fewer copies of data:
matrices = {rand(1000, 'gpuArray'), rand(2000, 'gpuArray')};
parfor idx = 1:numel(matrices)
matrix = matrices{idx};
result(idx) = max(matrix(:));
end
If possible, it's best to build the gpuArray in the body of your spmd or parfor block to avoid making too many copies on the GPU.
类别
在 帮助中心 和 File Exchange 中查找有关 Parallel Computing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!