Cell arrays and multiple GPUs computing

11 次查看(过去 30 天)
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
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.
  1 个评论
Mehdi Ravanbakhsh
Mehdi Ravanbakhsh 2015-10-20
Thanks Edric. I tested the second approach and it worked well. In your first approach, you used labindex to interate but what if my cell array has more than 20 elements? Matlab help is saying that labindex represents the number for workers. I have 2 GPUs and each has 6 workers so it can handle at max 12 cell elements. Is there any way around this?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Parallel Computing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by