Run a function using gpu inside a parfor loop or a for loop
62 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个评论
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 Center 和 File Exchange 中查找有关 Parallel and Cloud 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!