Run a function using gpu inside a parfor loop or a for loop

조회 수: 19 (최근 30일)
Yi Xien Yap
Yi Xien Yap 2022년 8월 15일
댓글: Walter Roberson 2022년 8월 15일
I have a code that work perfectly on parfor, and it significantly improve the speed of the simulation. Then the next step for me is to try the process in the GPU. For example, I have a parfor loop or a for loop trying to execute 50 iteration, and the huge processing is done in a function "signalProcessController" to return the output. I would need to speed up the function execution using a gpu not sure how it is done or is this even possible? The only thing I found is the gpuArray.
%Sample code
iCellNum = 1;
activeUeNums = 1;
for i = 1 : 50
% parfor ttiNum = 1 : 50
[timeDomainSignal{i}] = signalProcessController(iCellNum, i, activeUeNums);
end

채택된 답변

Walter Roberson
Walter Roberson 2022년 8월 15일
Notice in particular "Create a parallel pool with as many workers as GPUs available". Provided that you do not have more workers than GPUs then each worker will automatically be allocated a different GPU. Avoid manually selecting a GPU device -- or at least avoid selecting the same GPU on multiple workers, as the communications with the GPU has to be reset each time the device is selected.
It is possible to mix workers that use the GPU with workers that do not use the GPU, so that you can keep your other cores busy even though they have no access to GPU. You will need to figure out whether the worker has access to a GPU and if not then refrain from using gpu functions and variables.
  댓글 수: 2
Yi Xien Yap
Yi Xien Yap 2022년 8월 15일
My CPU has 4 cores meaning I have 4 workers, and I only have one GPU. If I understand you correctly, for example, I can have one GPU assign for worker one, and the rest of the 3 workers will process on CPU?
Walter Roberson
Walter Roberson 2022년 8월 15일
You would have to specifically program that.
IDs = fetchOutput(parfevalOnAll(@()getCurrentTask().Id,1));
ID_for_GPU = IDs(1);
parfor ttiNum = 1 : 50
thisID = getCurrentTask().Id;
if thisID == ID_for_GPU
proceed with GPU code here
else
proceed with non-GPU code here
end
end
the GPU code would use gpuArray and so on, and the non-GPU code would not.
In some cases you might be able to do something like
parfor ttiNum = 1 : 50
thisID = getCurrentTask().Id;
if thisID == ID_for_GPU
initialize some variables as gpuArray
else
initialize variables non-gpuArray
end
common code that uses the variables
if thisID == ID_for_GPU
gather() results back into the workspace
end
common code that creates final outputs
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by