Use GPU for the matlab code
Show older comments
I have computations on structs in my code with a label identifier and other numerical values. What I could understand is I can use gpuArray only for numerical computations but whenever it encounter a logical operation, it doesnt work. Is there a way out of this problem?
I am trying to run code available at: https://in.mathworks.com/matlabcentral/fileexchange/42853-deep-neural-network
Can anyone please help?
10 Comments
Walter Roberson
on 8 Jul 2021
Please expand on "whenever it encounters a local operation" and "it does not work"?
Virtualkeeda
on 8 Jul 2021
Joss Knight
on 8 Jul 2021
Can you post the actual output from the MATLAB command window so we can see the error and the call stack?
For what it's worth, there are no gpuArrays in that struct, and neither of the fields are numeric or logical data. Maybe you tried to construct a gpuArray using one of those things, or perhaps you passed the whole struct to gpuArray?
Virtualkeeda
on 8 Jul 2021
Virtualkeeda
on 8 Jul 2021
Edited: Virtualkeeda
on 8 Jul 2021
Walter Roberson
on 8 Jul 2021
dbn.rbm{nrbm}.W = gpuArray(linearMapping( Hall{nrbm-1}, OUT ));
H = gpuArray(sigmoid( bsxfun(@plus, V * dnn.W, dnn.b ) ));
I would have expected you to gpuArray() the data at an earlier step ? You would then not gupArray() around the calculation -- any calculation done on data that is gpuArray will automatically become gpuArray itself.
Virtualkeeda
on 8 Jul 2021
Joss Knight
on 9 Jul 2021
Isn't your input data in variables like dnn.b, dnn.W, Hall{i} and others? Plus all you seem to have done in the above is to move one set of network parameters to the GPU, rather than all of them. Isn't rbm a struct array containing many variables, all of which need to be gpuArray objects?
It's probably best to start with the documentation: https://www.mathworks.com/help/parallel-computing/run-matlab-functions-on-a-gpu.html
The basic principle is that if the data at the input to a function is a gpuArray object, that function will run on the GPU. Use the MATLAB debugger to check the datatype of the data about to be used on each line of your code, and make sure it is a gpuArray. You can use the isgpuarray() function.
You should also make sure your data is declared as single precision, so for instance replace
gpuArray(randn(dimV, dimH) * 0.1);
with
randn(dimV, dimH, 'single', 'gpuArray') * 0.1;
Note how I also create the data directly on the GPU rather than on the CPU and then moving it, as well as performing the scalar multiply on the GPU rather than the host.
Virtualkeeda
on 9 Jul 2021
Virtualkeeda
on 10 Jul 2021
Answers (0)
Categories
Find more on Parallel and Cloud in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


