GPU and CPU code: How to do?

조회 수: 22 (최근 30일)
Nycholas Maia
Nycholas Maia 2019년 1월 9일
편집: Biraj Khanal 2022년 1월 3일
I would like to share my MATLAB project with others that does not have any GPU card in your computers, but I want to use the GPU power in my computer.
How can I write ONLY ONE MATLAB code that can be run with and without GPU system?
My main GPU commands are:
  • parfor
  • GPUarray
In C/C++ language, we can write a pre-processor that can be this "magic shift". Is possible to do this in MATLAB?

채택된 답변

OCDER
OCDER 2019년 1월 9일
Depending on how your codes are structured, try to decide early on whether to use gpuArray or regular array. Most Matlab built-in functions will automatically determine and use gpuArrays.
function Output = main(Input)
%Decide early to use gpuArray
if gpuDeviceCount > 0
Input = gpuArray(Input);
end
%Use functions that support either gpuArray or regular arrays as inputs
plot(Input)
Input = myCustonFcn(Input) %custom function that allows gpuArray inputs
%Return as a regular array when done
if existsOnGPU(Input)
Output = gather(Input);
else
Output = Input;
end

추가 답변 (1개)

Joss Knight
Joss Knight 2019년 1월 9일
This is exactly why MATLAB's GPU support is so awesome! You should only need to insert your 'shim' for gpu data, as OCDER describes in their answer, in a very limited number of places. If you are doing Deep Learning, it's even easier: training and inference will automatically run on the GPU if there is one, and the CPU if not.
  댓글 수: 4
Joss Knight
Joss Knight 2020년 6월 23일
Use the option 'ExecutionEnvironment', 'cpu' as input to semanticseg to force CPU execution, or change the 'MiniBatchSize' option to something less (the default is 128) so that your GPU can handle the data.
Biraj Khanal
Biraj Khanal 2022년 1월 3일
편집: Biraj Khanal 2022년 1월 3일
I am trying to compare the performance of a particular function using GPU and CPU.
Can we force any function to use CPU instead of GPU or does it only work with the segmentation function ?

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

카테고리

Help CenterFile Exchange에서 Parallel and Cloud에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by