MATLAB Answers

GPU and CPU code: How to do?

조회 수: 78(최근 30일)
Nycholas Maia
Nycholas Maia 9 Jan 2019
댓글: Joss Knight 23 Jun 2020
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?

  댓글 수: 0

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

채택된 답변

OCDER
OCDER 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);
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

  댓글 수: 0

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

추가 답변(1개)

Joss Knight
Joss Knight 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.

  댓글 수: 3

Nycholas Maia
Nycholas Maia 10 Jan 2019
thank you!!
Constantino Carlos Reyes-Aldasoro
How can you swap between GPU and CPU? I was running a U-Net training on a single CPU, it took ages but worked well, then I enabled the GPU by updating the nvidia driver, it was faster but before finishing there was an error of the memory whilst calling semantic segmentation:
net = trainNetwork(trainingData,layers,opts);
C = semanticseg(currentData,net);
Error using nnet.internal.cnngpu.convolveBiasReluForward2D
Out of memory on device. To view more detail about
available memory on the GPU, use 'gpuDevice()'. If the
problem persists, reset the GPU by calling 'gpuDevice(1)'.
Error in
nnet.internal.cnn.layer.util.ConvolutionReLUGPUStrategy/forward
(line 16)
Z =
nnet.internal.cnngpu.convolveBiasReluForward2D(
...
Error in nnet.internal.cnn.layer.ConvolutionReLU/predict
(line 144)
Z = this.ExecutionStrategy.forward( X, ...
Error in nnet.internal.cnn.DAGNetwork/activations (line
571)
outputActivations =
thisLayer.predict(XForThisLayer);
Error in DAGNetwork/calculateActivations (line 86)
YBatch = predictNetwork.activations({X},
layerIndex, layerOutputIndex);
Error in DAGNetwork/activationsSeries (line 239)
Y = this.calculateActivations(X, layerID, 1,
varargin{:});
Error in SeriesNetwork/activations (line 673)
Y =
this.UnderlyingDAGNetwork.activationsSeries(X,
layerID, varargin{:});
Error in semanticseg>iClassifyImagePixels (line 441)
allScores = activations(net, X, params.PixelLayerID,
...
Error in semanticseg (line 248)
[Lroi, scores, allScores] = iClassifyImagePixels(Iroi,
net, params);
Error in segmentationHeLaUnet (line 194)
C =
semanticseg(currentData,net);
This is my gpu:
>> gpuDevice()
ans =
CUDADevice with properties:
Name: 'GeForce 930MX'
Index: 1
ComputeCapability: '5.0'
SupportsDouble: 1
DriverVersion: 11
ToolkitVersion: 10
MaxThreadsPerBlock: 1024
MaxShmemPerBlock: 49152
MaxThreadBlockSize: [1024 1024 64]
MaxGridSize: [2.1475e+09 65535 65535]
SIMDWidth: 32
TotalMemory: 2.1475e+09
AvailableMemory: 1.5761e+09
MultiprocessorCount: 3
ClockRateKHz: 1019500
ComputeMode: 'Default'
GPUOverlapsTransfers: 1
KernelExecutionTimeout: 1
CanMapHostMemory: 1
DeviceSupported: 1
DeviceSelected: 1
>>
I know it is not a great GPU, how can I switch to do the segmentation on the CPU (which was working) instead of the GPU (which runs out of memory)?
Joss Knight
Joss Knight 23 Jun 2020
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.

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

Community Treasure Hunt

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

Start Hunting!

Translated by