Benjamin_ballintyn asked . 2021-06-01

Specifying gpuArray input for codegen

Hi,
I am trying to accelerate some simulation code I have using my GPU. I am trying to do this by first creating a bunch of gpuArrays to hold the relevant variables and then passing these arrays to a function (updateNet) which I want to turn into a mex file using GPU Coder. For different simulations, the gpuArrays will be different sizes but for a particular simulation they will all be the same size. Since GPU Coder does not support variable sized gpuArray inputs I have written a function that takes as input an integer and then specifies that as the size of all of the gpuArray inputs for this call to codegen.
 
function [] = compile_easySim(N)
cfg = coder.gpuConfig('mex');
cfg.GpuConfig.CompilerFlags = '--fmad=false';
cfg.GenerateReport = true;

ARGS = cell(23,1);
ARGS{1} = coder.typeof(gpuArray(single(0)),[N 1],[0 0]); % V (membrane Voltage)
ARGS{2} = coder.typeof(gpuArray(single(0)),[N 1],[0 0]); % Gref (refractory conductance)
ARGS{3} = coder.typeof(gpuArray(single(0)),[N 1],[0 0]); % dGref (refractory conductance change on spike)
ARGS{4} = coder.typeof(gpuArray(single(0)),[N 1],[0 0]); % tau_ref (refractory time_constant)
ARGS{5} = coder.typeof(gpuArray(single(0)),[N 1],[0 0]); % Vth (spike threshold)
ARGS{6} = coder.typeof(gpuArray(single(0)),[N 1],[0 0]); % VsynE (excitatory synaptic reversal potential)
ARGS{7} = coder.typeof(gpuArray(single(0)),[N 1],[0 0]); % VsynI (inhibitory synaptic reversal potential)
ARGS{8} = coder.typeof(gpuArray(single(0)),[N 1],[0 0]); % GsynE (total excitatory synaptic conductance)
ARGS{9} = coder.typeof(gpuArray(single(0)),[N 1],[0 0]); % GsynI (total inhibitory synaptic conductance)
ARGS{10} = coder.typeof(gpuArray(single(0)),[N 1], [0 0]); % maxGsynE
ARGS{11} = coder.typeof(gpuArray(single(0)),[N 1], [0 0]); % maxGsynI
ARGS{12} = coder.typeof(gpuArray(single(0)),[N N],[0 0]); % dGsyn (synaptic strength matrix)
ARGS{13} = coder.typeof(gpuArray(single(0)),[N 1],[0 0]); % tau_synE (excitatory synaptic decay time constant)
ARGS{14} = coder.typeof(gpuArray(single(0)),[N 1],[0 0]); % tau_synI (inhibitory synaptic decay time constant)
ARGS{15} = coder.typeof(gpuArray(single(0)),[N 1],[0 0]); % Cm (membrane capacitance)
ARGS{16} = coder.typeof(gpuArray(single(0)),[N 1],[0 0]); % Gl (leak conductance)
ARGS{17} = coder.typeof(gpuArray(single(0)),[N 1],[0 0]); % El (leak reversal potential)
ARGS{18} = coder.typeof(gpuArray(single(0)),[N 1],[0 0]); % Ek
ARGS{19} = coder.typeof(gpuArray(single(0)),[N 1],[0 0]); % dth
ARGS{20} = coder.typeof(gpuArray(single(0)),[N 1],[0 0]); % Iapp
ARGS{21} = coder.typeof(single(0),[1],[0]); % dt
ARGS{22} = coder.typeof(gpuArray(false),[N 1],[0 0]); % ecells
ARGS{23} = coder.typeof(gpuArray(false),[N 1],[0 0]); % icells
codegen updateNet -args ARGS -nargout 5

However, when I do this I get the following error:

Use of CODER.TYPEOF to represent GPU inputs is supported only with GPU Coder.
Use help codegen for more information on using this command.

This confuses me since I thought I was using GPU Coder. Does GPU Coder only refer to the app GUI where you must manually specify each input?

For reference the output of coder.checkGpuInstall is:

coder.checkGpuInstall
Compatible GPU           : PASSED 
CUDA Environment         : PASSED 
	Runtime   : PASSED 
	cuFFT     : PASSED 
	cuSOLVER  : PASSED 
	cuBLAS    : PASSED 
cuDNN Environment        : FAILED (Unable to find the 'NVIDIA_CUDNN' environment variable. Set 'NVIDIA_CUDNN' to point to the root directory of a NVIDIA cuDNN installation.)
Basic Code Generation    : PASSED 
Basic Code Execution     : PASSED 
ans = 
  struct with fields:

                 gpu: 1
                cuda: 1
               cudnn: 0
            tensorrt: 0
        basiccodegen: 1
       basiccodeexec: 1
         deepcodegen: 0
        deepcodeexec: 0
    tensorrtdatatype: 0
           profiling: 0

The only thing that fails is cuDNN but since I'm not trying to do deep learning this shouldn't cause problems.

gpu coder , gpuarray , simulation , codegen

Expert Answer

Kshitij Singh answered . 2024-05-17 20:10:12

Okay, I was able to fix this problem by getting gpucoder to produce the code that the app used to compile my function.
  1. Install the GPU coder interface for deep learning libraries.
  2. use the GPU Coder app to compile your code.
  3. run
 
 
gpucoder -script yourscript.m -tocode yourgpucoderproject.prj
replacing the .m and .prj files with the names of you files.
This will output a script called yourscript.m with the code the app used. Now you can turn this script into a function that takes in the sizes of the arrays it should expect.
 
The critical difference between what I was doing and what the GPU Coder app does is it uses
 
coder.typeof(single(0), [N 1], 'Gpu', true)
to signal a gpuArray input. NOWHERE in the documentation is this syntax shown or explained.


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.