How can I specify a minimum compute capability to the mexcuda compiler to compile a mexfunction?

匆匆过客 提交于 2021-02-05 08:19:29

问题


I have a CUDA project in a .cu file that I would like to compile to a .mex file using mexcuda. Because my code makes use of the 64-bit floating point atomic operation atomicAdd(double *, double), which is only supposed for GPU devices of compute capability 6.0 or higher, I need to specify this as a flag when I am compiling.

In my standard IDE, this works fine, but when compiling with mexcuda, this is not working as I would like. In this post on MathWorks, it was suggested to use the following command (edited from the comment by Joss Knight):

mexcuda('-v', 'mexGPUExample.cu', 'NVCCFLAGS=-gencode=arch=compute_60,code=sm_60')

but when I use this command on my file, the verbose option spits out the following line last:

Building with 'NVIDIA CUDA Compiler'.
nvcc -c --compiler-options=/Zp8,/GR,/W3,/EHs,/nologo,/MD - 
gencode=arch=compute_30,code=sm_30 -gencode=arch=compute_50,code=sm_50 - 
gencode=arch=compute_60,code=sm_60 - 
gencode=arch=compute_70,code=\"sm_70,compute_70\"

(and so on), which signals to me that the specified flag was not passed to the nvcc properly. And indeed, compilation fails with the following error:

C:/path/mexGPUExample.cu(35): error: no instance of overloaded function "atomicAdd" matches 
the argument list. Argument types are: (double *, double)

The only other post I could find on this topic was this post on SO, but it is almost three years old and seemed to me more like a workaround - one which I do not understand even after some research, otherwise I would have tried it - rather than a true solution to the problem.

Is there a setting I missed, or can this simply not be done without a workaround?


回答1:


I was able to work my way around this problem after some messing around with the standard xml-files in the MatLab folder. The following steps allowed me to compile using -mexcuda:

-1) Go to the folder C:\Program Files\MATLAB\-version-\toolbox\distcomp\gpu\extern\src\mex\win64, which contains xml-files for different versions of msvcpp;

-2) Make a backup of the file that corresponds to the version you are using. In my case, I made a copy of the file nvcc_msvcpp2017 and named it nvcc_msvcpp2017_old, to always have the original.

-3) Open nvcc_msvcppYEAR with notepad, and scroll to the following block of lines:

COMPILER="nvcc"
COMPFLAGS="--compiler-options=/Zp8,/GR,/W3,/EHs,/nologo,/MD $ARCHFLAGS"
ARCHFLAGS="-gencode=arch=compute_30,code=sm_30 -gencode=arch=compute_50,code=sm_50 -gencode=arch=compute_60,code=sm_60 -gencode=arch=compute_70,code=\"sm_70,compute_70\" $NVCC_FLAGS"
COMPDEFINES="--compiler-options=/D_CRT_SECURE_NO_DEPRECATE,/D_SCL_SECURE_NO_DEPRECATE,/D_SECURE_SCL=0,$MATLABMEX"
MATLABMEX="/DMATLAB_MEX_FILE"
OPTIMFLAGS="--compiler-options=/O2,/Oy-,/DNDEBUG"
INCLUDE="-I"$MATLABROOT\extern\include" -I"$MATLABROOT\simulink\include""
DEBUGFLAGS="--compiler-options=/Z7"

-4) Remove the architectures that will not allow your code to compile, i.e. all the architecture flags below 60 in my case:

ARCHFLAGS="-gencode=arch=compute_60,code=sm_60 -gencode=arch=compute_70,code=\"sm_70,compute_70\" $NVCC_FLAGS"

-5) I was able to compile using mexcuda after this. You do not need to specify any architecture flags in the mexcuda call.

-6) (optional) I suppose you want to revert this change after you are done with the project that required you to make this change, if you want to ensure maximum portability of the code you will compile after this.

Note: you will need administrator permission to make these changes.



来源:https://stackoverflow.com/questions/52741049/how-can-i-specify-a-minimum-compute-capability-to-the-mexcuda-compiler-to-compil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!