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

93 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 15 Aug 2022
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 Comments
Yi Xien Yap
Yi Xien Yap on 15 Aug 2022
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 on 15 Aug 2022
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

Sign in to comment.

More Answers (0)

Categories

Find more on Parallel Computing Fundamentals in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!