MATLAB Answers

Generating CUDA using GPU Coder

5 views (last 30 days)
AlexRD
AlexRD on 26 May 2021
Answered: Chao Luo on 5 Aug 2021
Hi,
I've been writing my own code for a CNN, and i came across a problem that would be interesting to solve with CUDA.
So basically, if you want to perform convolution across multiple channels/kernels, you got 3 choices:
  1. Generate a big matrix of convolution by indexing it. (Also known as im2col method)
  2. Transform your data using FFTs, perform elementwise multiplication on a for loop across multiple channels/kernels
  3. Use the convn function on a for loop across multiple channels/kernels
I have used the big matrix convolution method so far, but it has the drawback of needing a lot of memory. So i decided to try generating CUDA code to perform these convolutions across multiple channels in parallel, and wrote this test function:
function result = convolve(image, kernel)
numKernels = single(size(kernel, 3));
numSamples = single(size(image, 3));
mapSize = single(size(image,1) - size(kernel,1) + 1);
result = single(zeros(mapSize, mapSize, numKernels*numSamples));
for i=1:numKernels
result(:,:,((i-1)*numSamples +1):i*numSamples) = single(convn(image, kernel(:,:,i), 'valid'));
end
end
When i try generating CUDA code for it, but get some errors that are kind of hard to debug. The code compiles when i select CPU test, for example. But it breaks on GPU tests. If i proceed with the CPU test, and then try to compile it using different parameters for compilation for the GPU, it doesn't work either. I have tried several different combinations for configs, but nothing seems to work i always get the same error:
[1/11] nvcc -c -rdc=true -Wno-deprecated-gpu-targets -Xcompiler "/wd 4819" -Xcompiler "/MD" -Xcudafe "--display_error_number --diag_suppress=2381 --diag_suppress=unsigned_compare_with_zero --diag_suppress=useless_type_qualifier_on_return_type" -D_GNU_SOURCE -DMATLAB_MEX_FILE -Xcompiler "/source-charset:utf-8" -I "." -I "C:\Users\Alex\Google Drive\Desktop\Active\AI\Neural-Net\tests" -I ".\interface" -I "C:\Workspace\MATLAB\extern\include" -I "." -DMW_CUDA_ARCH=350 -DMODEL=convolve_mex -DMW_NEEDS_VERSION_H -arch sm_35 -I "C:\Workspace\MATLAB/simulink/include" -I "C:\Workspace\MATLAB/toolbox/shared/simtargets" convolve_terminate.cu -o build\win64\convolve_terminate.obj
FAILED: build/win64/convolve_terminate.obj 
nvcc -c -rdc=true -Wno-deprecated-gpu-targets -Xcompiler "/wd 4819" -Xcompiler "/MD" -Xcudafe "--display_error_number --diag_suppress=2381 --diag_suppress=unsigned_compare_with_zero --diag_suppress=useless_type_qualifier_on_return_type" -D_GNU_SOURCE -DMATLAB_MEX_FILE -Xcompiler "/source-charset:utf-8" -I "." -I "C:\Users\Alex\Google Drive\Desktop\Active\AI\Neural-Net\tests" -I ".\interface" -I "C:\Workspace\MATLAB\extern\include" -I "." -DMW_CUDA_ARCH=350 -DMODEL=convolve_mex -DMW_NEEDS_VERSION_H -arch sm_35 -I "C:\Workspace\MATLAB/simulink/include" -I "C:\Workspace\MATLAB/toolbox/shared/simtargets" convolve_terminate.cu -o build\win64\convolve_terminate.obj
CreateProcess failed: The system cannot find the file specified.
[2/11] nvcc -c -rdc=true -Wno-deprecated-gpu-targets -Xcompiler "/wd 4819" -Xcompiler "/MD" -Xcudafe "--display_error_number --diag_suppress=2381 --diag_suppress=unsigned_compare_with_zero --diag_suppress=useless_type_qualifier_on_return_type" -D_GNU_SOURCE -DMATLAB_MEX_FILE -Xcompiler "/source-charset:utf-8" -I "." -I "C:\Users\Alex\Google Drive\Desktop\Active\AI\Neural-Net\tests" -I ".\interface" -I "C:\Workspace\MATLAB\extern\include" -I "." -DMW_CUDA_ARCH=350 -DMODEL=convolve_mex -DMW_NEEDS_VERSION_H -arch sm_35 -I "C:\Workspace\MATLAB/simulink/include" -I "C:\Workspace\MATLAB/toolbox/shared/simtargets" convolve.cu -o build\win64\convolve.obj
FAILED: build/win64/convolve.obj 
nvcc -c -rdc=true -Wno-deprecated-gpu-targets -Xcompiler "/wd 4819" -Xcompiler "/MD" -Xcudafe "--display_error_number --diag_suppress=2381 --diag_suppress=unsigned_compare_with_zero --diag_suppress=useless_type_qualifier_on_return_type" -D_GNU_SOURCE -DMATLAB_MEX_FILE -Xcompiler "/source-charset:utf-8" -I "." -I "C:\Users\Alex\Google Drive\Desktop\Active\AI\Neural-Net\tests" -I ".\interface" -I "C:\Workspace\MATLAB\extern\include" -I "." -DMW_CUDA_ARCH=350 -DMODEL=convolve_mex -DMW_NEEDS_VERSION_H -arch sm_35 -I "C:\Workspace\MATLAB/simulink/include" -I "C:\Workspace\MATLAB/toolbox/shared/simtargets" convolve.cu -o build\win64\convolve.obj
CreateProcess failed: The system cannot find the file specified.
[3/11] nvcc -c -rdc=true -Wno-deprecated-gpu-targets -Xcompiler "/wd 4819" -Xcompiler "/MD" -Xcudafe "--display_error_number --diag_suppress=2381 --diag_suppress=unsigned_compare_with_zero --diag_suppress=useless_type_qualifier_on_return_type" -D_GNU_SOURCE -DMATLAB_MEX_FILE -Xcompiler "/source-charset:utf-8" -I "." -I "C:\Users\Alex\Google Drive\Desktop\Active\AI\Neural-Net\tests" -I ".\interface" -I "C:\Workspace\MATLAB\extern\include" -I "." -DMW_CUDA_ARCH=350 -DMODEL=convolve_mex -DMW_NEEDS_VERSION_H -arch sm_35 -I "C:\Workspace\MATLAB/simulink/include" -I "C:\Workspace\MATLAB/toolbox/shared/simtargets" convolve_initialize.cu -o build\win64\convolve_initialize.obj
FAILED: build/win64/convolve_initialize.obj 
nvcc -c -rdc=true -Wno-deprecated-gpu-targets -Xcompiler "/wd 4819" -Xcompiler "/MD" -Xcudafe "--display_error_number --diag_suppress=2381 --diag_suppress=unsigned_compare_with_zero --diag_suppress=useless_type_qualifier_on_return_type" -D_GNU_SOURCE -DMATLAB_MEX_FILE -Xcompiler "/source-charset:utf-8" -I "." -I "C:\Users\Alex\Google Drive\Desktop\Active\AI\Neural-Net\tests" -I ".\interface" -I "C:\Workspace\MATLAB\extern\include" -I "." -DMW_CUDA_ARCH=350 -DMODEL=convolve_mex -DMW_NEEDS_VERSION_H -arch sm_35 -I "C:\Workspace\MATLAB/simulink/include" -I "C:\Workspace\MATLAB/toolbox/shared/simtargets" convolve_initialize.cu -o build\win64\convolve_initialize.obj
CreateProcess failed: The system cannot find the file specified.
[4/11] nvcc -c -rdc=true -Wno-deprecated-gpu-targets -Xcompiler "/wd 4819" -Xcompiler "/MD" -Xcudafe "--display_error_number --diag_suppress=2381 --diag_suppress=unsigned_compare_with_zero --diag_suppress=useless_type_qualifier_on_return_type" -D_GNU_SOURCE -DMATLAB_MEX_FILE -Xcompiler "/source-charset:utf-8" -I "." -I "C:\Users\Alex\Google Drive\Desktop\Active\AI\Neural-Net\tests" -I ".\interface" -I "C:\Workspace\MATLAB\extern\include" -I "." -DMW_CUDA_ARCH=350 -DMODEL=convolve_mex -DMW_NEEDS_VERSION_H -arch sm_35 -I "C:\Workspace\MATLAB/simulink/include" -I "C:\Workspace\MATLAB/toolbox/shared/simtargets" MWCUBLASUtils.cpp -o build\win64\MWCUBLASUtils.obj
FAILED: build/win64/MWCUBLASUtils.obj 
nvcc -c -rdc=true -Wno-deprecated-gpu-targets -Xcompiler "/wd 4819" -Xcompiler "/MD" -Xcudafe "--display_error_number --diag_suppress=2381 --diag_suppress=unsigned_compare_with_zero --diag_suppress=useless_type_qualifier_on_return_type" -D_GNU_SOURCE -DMATLAB_MEX_FILE -Xcompiler "/source-charset:utf-8" -I "." -I "C:\Users\Alex\Google Drive\Desktop\Active\AI\Neural-Net\tests" -I ".\interface" -I "C:\Workspace\MATLAB\extern\include" -I "." -DMW_CUDA_ARCH=350 -DMODEL=convolve_mex -DMW_NEEDS_VERSION_H -arch sm_35 -I "C:\Workspace\MATLAB/simulink/include" -I "C:\Workspace\MATLAB/toolbox/shared/simtargets" MWCUBLASUtils.cpp -o build\win64\MWCUBLASUtils.obj
CreateProcess failed: The system cannot find the file specified.
[5/11] nvcc -c -rdc=true -Wno-deprecated-gpu-targets -Xcompiler "/wd 4819" -Xcompiler "/MD" -Xcudafe "--display_error_number --diag_suppress=2381 --diag_suppress=unsigned_compare_with_zero --diag_suppress=useless_type_qualifier_on_return_type" -D_GNU_SOURCE -DMATLAB_MEX_FILE -Xcompiler "/source-charset:utf-8" -I "." -I "C:\Users\Alex\Google Drive\Desktop\Active\AI\Neural-Net\tests" -I ".\interface" -I "C:\Workspace\MATLAB\extern\include" -I "." -DMW_CUDA_ARCH=350 -DMODEL=convolve_mex -DMW_NEEDS_VERSION_H -arch sm_35 -I "C:\Workspace\MATLAB/simulink/include" -I "C:\Workspace\MATLAB/toolbox/shared/simtargets" convolve_data.cu -o build\win64\convolve_data.obj
FAILED: build/win64/convolve_data.obj 
nvcc -c -rdc=true -Wno-deprecated-gpu-targets -Xcompiler "/wd 4819" -Xcompiler "/MD" -Xcudafe "--display_error_number --diag_suppress=2381 --diag_suppress=unsigned_compare_with_zero --diag_suppress=useless_type_qualifier_on_return_type" -D_GNU_SOURCE -DMATLAB_MEX_FILE -Xcompiler "/source-charset:utf-8" -I "." -I "C:\Users\Alex\Google Drive\Desktop\Active\AI\Neural-Net\tests" -I ".\interface" -I "C:\Workspace\MATLAB\extern\include" -I "." -DMW_CUDA_ARCH=350 -DMODEL=convolve_mex -DMW_NEEDS_VERSION_H -arch sm_35 -I "C:\Workspace\MATLAB/simulink/include" -I "C:\Workspace\MATLAB/toolbox/shared/simtargets" convolve_data.cu -o build\win64\convolve_data.obj
CreateProcess failed: The system cannot find the file specified.
[6/11] nvcc -c -rdc=true -Wno-deprecated-gpu-targets -Xcompiler "/wd 4819" -Xcompiler "/MD" -Xcudafe "--display_error_number --diag_suppress=2381 --diag_suppress=unsigned_compare_with_zero --diag_suppress=useless_type_qualifier_on_return_type" -D_GNU_SOURCE -DMATLAB_MEX_FILE -Xcompiler "/source-charset:utf-8" -I "." -I "C:\Users\Alex\Google Drive\Desktop\Active\AI\Neural-Net\tests" -I ".\interface" -I "C:\Workspace\MATLAB\extern\include" -I "." -DMW_CUDA_ARCH=350 -DMODEL=convolve_mex -DMW_NEEDS_VERSION_H -arch sm_35 -I "C:\Workspace\MATLAB/simulink/include" -I "C:\Workspace\MATLAB/toolbox/shared/simtargets" rt_nonfinite.cu -o build\win64\rt_nonfinite.obj
FAILED: build/win64/rt_nonfinite.obj 
nvcc -c -rdc=true -Wno-deprecated-gpu-targets -Xcompiler "/wd 4819" -Xcompiler "/MD" -Xcudafe "--display_error_number --diag_suppress=2381 --diag_suppress=unsigned_compare_with_zero --diag_suppress=useless_type_qualifier_on_return_type" -D_GNU_SOURCE -DMATLAB_MEX_FILE -Xcompiler "/source-charset:utf-8" -I "." -I "C:\Users\Alex\Google Drive\Desktop\Active\AI\Neural-Net\tests" -I ".\interface" -I "C:\Workspace\MATLAB\extern\include" -I "." -DMW_CUDA_ARCH=350 -DMODEL=convolve_mex -DMW_NEEDS_VERSION_H -arch sm_35 -I "C:\Workspace\MATLAB/simulink/include" -I "C:\Workspace\MATLAB/toolbox/shared/simtargets" rt_nonfinite.cu -o build\win64\rt_nonfinite.obj
CreateProcess failed: The system cannot find the file specified.
[7/11] nvcc -c -rdc=true -Wno-deprecated-gpu-targets -Xcompiler "/wd 4819" -Xcompiler "/MD" -Xcudafe "--display_error_number --diag_suppress=2381 --diag_suppress=unsigned_compare_with_zero --diag_suppress=useless_type_qualifier_on_return_type" -D_GNU_SOURCE -DMATLAB_MEX_FILE -Xcompiler "/source-charset:utf-8" -I "." -I "C:\Users\Alex\Google Drive\Desktop\Active\AI\Neural-Net\tests" -I ".\interface" -I "C:\Workspace\MATLAB\extern\include" -I "." -DMW_CUDA_ARCH=350 -DMODEL=convolve_mex -DMW_NEEDS_VERSION_H -arch sm_35 -I "C:\Workspace\MATLAB/simulink/include" -I "C:\Workspace\MATLAB/toolbox/shared/simtargets" cpp_mexapi_version.cpp -o build\win64\cpp_mexapi_version.obj
FAILED: build/win64/cpp_mexapi_version.obj 
nvcc -c -rdc=true -Wno-deprecated-gpu-targets -Xcompiler "/wd 4819" -Xcompiler "/MD" -Xcudafe "--display_error_number --diag_suppress=2381 --diag_suppress=unsigned_compare_with_zero --diag_suppress=useless_type_qualifier_on_return_type" -D_GNU_SOURCE -DMATLAB_MEX_FILE -Xcompiler "/source-charset:utf-8" -I "." -I "C:\Users\Alex\Google Drive\Desktop\Active\AI\Neural-Net\tests" -I ".\interface" -I "C:\Workspace\MATLAB\extern\include" -I "." -DMW_CUDA_ARCH=350 -DMODEL=convolve_mex -DMW_NEEDS_VERSION_H -arch sm_35 -I "C:\Workspace\MATLAB/simulink/include" -I "C:\Workspace\MATLAB/toolbox/shared/simtargets" cpp_mexapi_version.cpp -o build\win64\cpp_mexapi_version.obj
CreateProcess failed: The system cannot find the file specified.
[8/11] nvcc -c -rdc=true -Wno-deprecated-gpu-targets -Xcompiler "/wd 4819" -Xcompiler "/MD" -Xcudafe "--display_error_number --diag_suppress=2381 --diag_suppress=unsigned_compare_with_zero --diag_suppress=useless_type_qualifier_on_return_type" -D_GNU_SOURCE -DMATLAB_MEX_FILE -Xcompiler "/source-charset:utf-8" -I "." -I "C:\Users\Alex\Google Drive\Desktop\Active\AI\Neural-Net\tests" -I ".\interface" -I "C:\Workspace\MATLAB\extern\include" -I "." -DMW_CUDA_ARCH=350 -DMODEL=convolve_mex -DMW_NEEDS_VERSION_H -arch sm_35 -I "C:\Workspace\MATLAB/simulink/include" -I "C:\Workspace\MATLAB/toolbox/shared/simtargets" interface\_coder_convolve_mex.cu -o build\win64\_coder_convolve_mex.obj
FAILED: build/win64/_coder_convolve_mex.obj 
nvcc -c -rdc=true -Wno-deprecated-gpu-targets -Xcompiler "/wd 4819" -Xcompiler "/MD" -Xcudafe "--display_error_number --diag_suppress=2381 --diag_suppress=unsigned_compare_with_zero --diag_suppress=useless_type_qualifier_on_return_type" -D_GNU_SOURCE -DMATLAB_MEX_FILE -Xcompiler "/source-charset:utf-8" -I "." -I "C:\Users\Alex\Google Drive\Desktop\Active\AI\Neural-Net\tests" -I ".\interface" -I "C:\Workspace\MATLAB\extern\include" -I "." -DMW_CUDA_ARCH=350 -DMODEL=convolve_mex -DMW_NEEDS_VERSION_H -arch sm_35 -I "C:\Workspace\MATLAB/simulink/include" -I "C:\Workspace\MATLAB/toolbox/shared/simtargets" interface\_coder_convolve_mex.cu -o build\win64\_coder_convolve_mex.obj
CreateProcess failed: The system cannot find the file specified.
[9/11] nvcc -c -rdc=true -Wno-deprecated-gpu-targets -Xcompiler "/wd 4819" -Xcompiler "/MD" -Xcudafe "--display_error_number --diag_suppress=2381 --diag_suppress=unsigned_compare_with_zero --diag_suppress=useless_type_qualifier_on_return_type" -D_GNU_SOURCE -DMATLAB_MEX_FILE -Xcompiler "/source-charset:utf-8" -I "." -I "C:\Users\Alex\Google Drive\Desktop\Active\AI\Neural-Net\tests" -I ".\interface" -I "C:\Workspace\MATLAB\extern\include" -I "." -DMW_CUDA_ARCH=350 -DMODEL=convolve_mex -DMW_NEEDS_VERSION_H -arch sm_35 -I "C:\Workspace\MATLAB/simulink/include" -I "C:\Workspace\MATLAB/toolbox/shared/simtargets" interface\_coder_convolve_api.cu -o build\win64\_coder_convolve_api.obj
FAILED: build/win64/_coder_convolve_api.obj 
nvcc -c -rdc=true -Wno-deprecated-gpu-targets -Xcompiler "/wd 4819" -Xcompiler "/MD" -Xcudafe "--display_error_number --diag_suppress=2381 --diag_suppress=unsigned_compare_with_zero --diag_suppress=useless_type_qualifier_on_return_type" -D_GNU_SOURCE -DMATLAB_MEX_FILE -Xcompiler "/source-charset:utf-8" -I "." -I "C:\Users\Alex\Google Drive\Desktop\Active\AI\Neural-Net\tests" -I ".\interface" -I "C:\Workspace\MATLAB\extern\include" -I "." -DMW_CUDA_ARCH=350 -DMODEL=convolve_mex -DMW_NEEDS_VERSION_H -arch sm_35 -I "C:\Workspace\MATLAB/simulink/include" -I "C:\Workspace\MATLAB/toolbox/shared/simtargets" interface\_coder_convolve_api.cu -o build\win64\_coder_convolve_api.obj
CreateProcess failed: The system cannot find the file specified.
ninja: fatal: ReadFile: The handle is invalid.
How exactly do i proceed here?
Thanks!

Answers (1)

Chao Luo
Chao Luo on 5 Aug 2021
Hi AlexRD,
It looks like nvcc is not found. Since 19a, it uses the nvcc shipping with MALTAB, so you don't have to install NVIDIA CUDA toolkit to compile the generated code for MEX target. If your MATLAB is older than 19a, could you check if the CUDA toolkit is installed and setup properly? If your MATLAB is 19a or newer, could you check if you can find nvcc under MATLAB_ROOT\sys\cuda\win64\cuda\bin? MATLAB_ROOT is where your MATLAB is installed.
Thanks,
Chao

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!