You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Use GPU for the matlab code
6 views (last 30 days)
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
Sorry for the typo mistake..
So whenever the gpuArray encounters logical operation... As in the operations are mostly performing on a struct with two fields. 1st one with some label, like 'bbdbn', or 'dbn' and 2nd field with 3x1 cell on which all the major computation takes place. Please see the screenshot of the struct.
gpuArray gives an error 'GPU arrays support only fundamental numeric or logical data types.'
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/678088/image.png)
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
Thanks @Joss Knight for the response. I will get back to you with detailed explanation with example within 24 hours. Please hold on to this.
Virtualkeeda
on 8 Jul 2021
Edited: Virtualkeeda
on 8 Jul 2021
So, here is the whole structure of the struct:
- bbdn (consists of a type and a struct named rbm)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/678578/image.png)
2. bbdbn.rbm (consists of 3 inner struct)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/678583/image.png)
3. bbdbn.rbm{1,1}: consists of a type and 3 internal matrics W,b, & c (i have used gpuArray so they are showing like that now)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/678588/image.png)
The opertaions are performing on these W,b, and c. You were right, earlier I was passing the whole struct to gpuArray, which was wrong and hence I was getitng the error. Now, I used gpuArray on the matrix operations such as:
dbn.rbm{nrbm}.W = gpuArray(linearMapping( Hall{nrbm-1}, OUT ));
H = gpuArray(sigmoid( bsxfun(@plus, V * dnn.W, dnn.b ) ));
There were many such operations.
But still the code is running as same as previous. Also, after using gpuArray I can see 0% GPU utilization. Am I missing something to install for matlab or for the gpu? Here is my gpuDevice details:
Name: 'Quadro T2000'
Index: 1
ComputeCapability: '7.5'
SupportsDouble: 1
DriverVersion: 11
ToolkitVersion: 10.1000
MaxThreadsPerBlock: 1024
MaxShmemPerBlock: 49152
MaxThreadBlockSize: [1024 1024 64]
MaxGridSize: [2.1475e+09 65535 65535]
SIMDWidth: 32
TotalMemory: 4.2950e+09
AvailableMemory: 3.1927e+09
MultiprocessorCount: 16
ClockRateKHz: 1785000
ComputeMode: 'Default'
GPUOverlapsTransfers: 1
KernelExecutionTimeout: 1
CanMapHostMemory: 1
DeviceSupported: 1
DeviceSelected: 1
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
Yes Ok.
So, this is what I did now. At the starting initialization, I used gpuArray as:
rbm.W = gpuArray(randn(dimV, dimH) * 0.1);
rbm.b = gpuArray(zeros(1, dimH));
rbm.c = gpuArray(zeros(1, dimV));
I tested on 1 epoch and few layer. Still, gpu Utlilization 0 % and same old time consumption.
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
Yes, the actual input data is in dnn.W,dnn.b etc.
Also, yes rbm is an struct containing a struct and a identifier type with labels like 'DNN', 'BBDBN' etc... But I think I cannot pass this rbm struct to gpuArray.
I guess, I will again have to go through the documentation on gpuArray.
Virtualkeeda
on 10 Jul 2021
Hello @Joss Knight, I could now use the gpuArray successfully. Thanks for your help and suggestions.
Answers (0)
See Also
Categories
Find more on GPU Computing 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)