How to use multiple GPUs asynchronously

I have two GPUs connected to my computer. I've explored on Mathworks, but can't find an easy way to use both GPUs in my code (there seems to be a 'parfor' and 'spmd' method, but neither seem to work for me). I would like to have each GPU run asynchronously as in:
x = gpuArray();
y1 = gpuFunction1(x);
y2 = gpuFunction2(x);
where gpuFunction1 runs on the first GPU and gpuFunction2 runs on the second GPU. Is there an easy way to do this? Thank you.

 채택된 답변

Edric Ellis
Edric Ellis 2014년 11월 13일

1 개 추천

If you have 2 GPUs, you could use SPMD like this:
parpool('local', 2); % ensure the pool is of size 2
spmd
% set each worker to use a GPU device based
% on their "labindex"
gpuDevice(labindex);
x = gpuArray(...);
if labindex == 1
y1 = gpuFunction1(x);
elseif labindex == 2
y2 = gpuFunction2(x);
end
end
% Note that y1 and y2 are 'Composite', so we must
% use indexing to retrieve the values
y1 = y1{1};
y2 = y2{2};
Note that the SPMD block approach shown is still synchronous with respect to the client. If you want the GPU work to be asynchronous with respect to the client, you need to use either batch (without a parallel pool open), or parfeval (with a parallel pool open).
Here's how you might use parfeval:
% setup as before
parpool('local', 2);
spmd, gpuDevice(labindex); end
% now invoke asynchronous work
x = gpuArray(...);
% Here the "1" is the number of outputs from gpuFunction#
f1 = parfeval(@gpuFunction1, 1, x);
f2 = parfeval(@gpuFunction2, 1, x);
% The MATLAB client is now idle, you can check to see if the
% work is done by examining the state:
disp(f1.State)
% Or, you can wait for the results - "fetchOutputs" blocks
% until the results are ready.
result1 = fetchOutputs(f1);
result2 = fetchOutputs(f2);

댓글 수: 4

Jonathan
Jonathan 2015년 4월 30일
편집: Jonathan 2015년 4월 30일
When using parfeval in this way, are f1 and f2 gpuArrays or regular variables stored in system memory? Is it possible to have gpuArrays residing on different GPUs accessible to the client?
Edric Ellis
Edric Ellis 2015년 4월 30일
편집: Edric Ellis 2015년 4월 30일
In the example above, f1 and f2 are parallel.Future objects returned by parfeval. The results that they return from fetchOutputs can be gpuArray if you wish (i.e. if that's what gpuFunction1 returns), or you could arrange for your functions to gather things before returning.
Srinidhi Ganeshan
Srinidhi Ganeshan 2019년 1월 24일
편집: Srinidhi Ganeshan 2019년 1월 24일
% setup as before
parpool('local', 2);
spmd, gpuDevice(labindex); disp(gpuDevice); end
% now invoke asynchronous work
x = gpuArray(...);
disp(gpuDevice);
% Here the "1" is the number of outputs from gpuFunction#
f1 = parfeval(@gpuFunction1, 1, x);
disp(gpuDevice);
f2 = parfeval(@gpuFunction2, 1, x);
disp(gpuDevice);
% The MATLAB client is now idle, you can check to see if the
% work is done by examining the state:
% Or, you can wait for the results - "fetchOutputs" blocks
% until the results are ready.
result1 = fetchOutputs(f1);
result2 = fetchOutputs(f2);
Inside spmd, I tried checking gpuDevice, it shows labindex=1,labindex 2, which is gpuDevice1 and gpuDevice2. But when I try, printing gpuDevice inbetween the lines to check if both gpuDevices are working, it always shows 2, which means gpuDevice 2 is only ON throughout the code and gpuDevice is idle. Why is that gpuDevice 1 is idle ? @Edric Ellis
There's a problem with your code. It should be:
spmd, currGpu = gpuDevice(labindex); disp(currGpu); end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Parallel Computing Fundamentals에 대해 자세히 알아보기

질문:

2014년 11월 12일

댓글:

2019년 6월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by