Run a function using gpu inside a parfor loop or a for loop
62 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Yi Xien Yap
el 15 de Ag. de 2022
Comentada: Walter Roberson
el 15 de Ag. de 2022
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 comentarios
Respuesta aceptada
Walter Roberson
el 15 de Ag. de 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 comentarios
Walter Roberson
el 15 de Ag. de 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
Más respuestas (0)
Ver también
Categorías
Más información sobre Parallel and Cloud en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!