GPU and CPU code: How to do?
49 views (last 30 days)
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:
In C/C++ language, we can write a pre-processor that can be this "magic shift". Is possible to do this in MATLAB?
OCDER on 9 Jan 2019
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);
%Use functions that support either gpuArray or regular arrays as inputs
Input = myCustonFcn(Input) %custom function that allows gpuArray inputs
%Return as a regular array when done
Output = gather(Input);
Output = Input;
More Answers (1)
Joss Knight on 9 Jan 2019
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.