3
$\begingroup$

I am currently learning to use CUDA to speed up calculations. The book I am working with uses C code on the CPU to supply certain variables etc.. So I would like to compile the give code in Mathematica. I found an instance for this in the help CUDALink/tutorial/Programming#120112373 chapter Compiling for CUDA.

Needs["CCompilerDriver`"]
Needs["CUDALink`"]

The NVCCComplier is listed after loading the above stated links and the CUDAFunctionLoad is also working. The output of CCompilers[] is:

{{Name->Visual Studio,
Compiler->CCompilerDriver`VisualStudioCompiler`VisualStudioCompiler,
CompilerInstallation->C:\Program Files (x86)\Microsoft Visual Studio 10.0,
CompilerName->Automatic},
{Name->NVIDIA CUDA Compiler,
Compiler->NVCCCompiler,
CompilerInstallation->C:\Users\gbj\AppData\Roaming\Mathematica\
Paclets\Repository\CUDAResources-Win64-10.2.0.3\CUDAToolkit\bin\,
CompilerName->Automatic
}}
testFileName = FileNameJoin[{$CUDALinkPath, "SupportFiles", "cudaDLL.cu"}];
FilePrint[testFileName]

Also works fine and gives the same code than listed in the example. When I try to create a library with

CreateLibrary[{testFileName},"testDLL","Compiler"->NVCCCompiler]

the return is $Failed.

Added:

With ShellOutputFunction->Print as a opition for CreateLibrary I get:

C:\Users\gbj\AppData\Roaming\Mathematica\SystemFiles\LibraryResources\ \Windows-x86-64\Working-goe-nb-0422-7460-5372-11>call "C:\Program \ Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" amd64 Setting environment for using Microsoft Visual Studio 2010 x64 tools. nvcc fatal : Unsupported gpu architecture 'compute_51'

Edit: CUDAInformation[] gives:

{1->{Name->NVS 5200M,Clock Rate->1344000,Compute Capabilities->2.1,GPU Overlap->1,Maximum Block Dimensions->{1024,1024,64},Maximum Grid Dimensions->{65535,65535,65535},Maximum Threads Per Block->1024,Maximum Shared Memory Per Block->49152,Total Constant Memory->65536,Warp Size->32,Maximum Pitch->2147483647,Maximum Registers Per Block->32768,Texture Alignment->512,Multiprocessor Count->2,Core Count->64,Execution Timeout->1,Integrated->False,Can Map Host Memory->True,Compute Mode->Default,Texture1D Width->65536,Texture2D Width->65536,Texture2D Height->65535,Texture3D Width->2048,Texture3D Height->2048,Texture3D Depth->2048,Texture2D Array Width->16384,Texture2D Array Height->16384,Texture2D Array Slices->2048,Surface Alignment->512,Concurrent Kernels->True,ECC Enabled->False,TCC Enabled->False,Total Memory->1073741824}}

CUDAResourcesInformation[] returns

{{Name->CUDAResources,Version->10.2.0.3,BuildNumber->,Qualifier->Win64,WolframVersion->10.*,SystemID->{Windows-x86-64},Description->{ToolkitVersion -> 7.0, MinimumDriver -> 300.0},Category->,Creator->,Publisher->,Support->,Internal->False,Location->C:\Users\gbj\AppData\Roaming\Mathematica\Paclets\Repository\CUDAResources-Win64-10.2.0.3,Context->{},Enabled->True,Loading->Manual,Hash->7326e294f5e53407e2ce826e5b3ce301}}


I have no idea why that is and what to do to make it work. Two questions I have now:

1) Can someone give a hint what to do to make it work?

Maybe someone can test it on his machine and confirm it is working in general.

2) Why is this CreateLibrary compiling a DLL and not a lib which I could load with LibraryFunctionLoad?->answered

My system: Windows 7 (64bit), CUDA 7.5.18 installed, MM 10.3, Microsoft Visual Studio 2010

$\endgroup$
  • 1
    $\begingroup$ It is working for me with Mathematica 10.3.1 on Windows 10 with Visual Studio 2012. Can you compile to C (non-CUDA) with your setup? Also try using the option "ShellOutputFunction" -> Print in CreateLibrary. $\endgroup$ – Simon Woods Jan 2 '16 at 21:24
  • 1
    $\begingroup$ Regarding 2), the DLL is a library which you can load with LibraryFunctionLoad. $\endgroup$ – Simon Woods Jan 2 '16 at 21:25
  • $\begingroup$ Thank you for testing. I am supprised that CUDA compilation works with Visual Studio 2012. I had it also installed but it was recognized by the CUDA toolkit installation (7.5). Which one do you use? The pure c-compilation I need to test, but I have to find some code first. $\endgroup$ – Eisbär Jan 3 '16 at 10:52
  • $\begingroup$ I have toolkit version 6.0 here. To test compiling to C just use something simple like Compile[{x}, x + 1, CompilationTarget -> "C"] $\endgroup$ – Simon Woods Jan 3 '16 at 11:23
  • $\begingroup$ I just tried something more complex and compiled the mandelbulb instance from Mathematica starting with the demo_numerical.c file and CreateLibrary and then doing the LibraryFunctionLoad. That works without issues. So C-compilation does work. $\endgroup$ – Eisbär Jan 3 '16 at 13:11
5
$\begingroup$

I got the same error, but only after updating the CUDA Resources to version 10.2.0.3.

The problem seems to come about due to the inclusion of -gencode=arch=compute_ 51,code=compute_ 51 in the nvcc command line. This is hardcoded into the definition of CUDALink`NVCCCompiler`Private`ResolveCUDAArchitecture when the "CUDAArchitecture" option is Automatic (the default).

You can check this with:

CUDALink`NVCCCompiler`Private`ResolveCUDAArchitecture[CreateLibrary, 1, {}]

-gencode=arch=compute_20,code=compute_20
-gencode=arch=compute_30,code=compute_30
-gencode=arch=compute_35,code=compute_35
-gencode=arch=compute_50,code=compute_50
-gencode=arch=compute_51,code=compute_51
-gencode=arch=compute_52,code=compute_52

The issue is that 5.1 is not one of the supported Compute Capability version numbers. See this or this for example.

There are two workarounds that solve the problem for me:

Workaround 1 - specify the target architecture explicitly with the "CUDAArchitecture" option. For example:

CreateLibrary[{testFileName}, "testDLL", 
 "Compiler" -> NVCCCompiler, "CUDAArchitecture" -> "sm_21"]

Workaround 2 - remove the offending string directly from the definition of CUDALink`NVCCCompiler`Private`ResolveCUDAArchitecture

With[{
   rca = CUDALink`NVCCCompiler`Private`ResolveCUDAArchitecture,
   bad = "  -gencode=arch=compute_51,code=compute_51"},
  DownValues[rca] = DeleteCases[DownValues[rca], bad, -1]];

-> Option for CUDA architecture is here: http://codeyarns.com/2014/03/03/how-to-specify-architecture-to-compile-cuda-code/

$\endgroup$
  • $\begingroup$ Thank you for taking the time. Great work! I was up to this link (section 5) link. For me solution 1 works but CreateLibrary issues an error: >OptionValue::nodef: "Unknown option \!(\"CUDAArchitecture\") for CreateLibrary.". Anyhow, it still creates a working DLL. How did you find about the option CUDAArchitecture? It is not documented (that was were I got stuck). $\endgroup$ – Eisbär Jan 3 '16 at 19:23
  • $\begingroup$ I get the same error. The option is listed in the documentation for NVCCCompiler and clearly works in CreateLibrary even though it isn't recognised as an option. I think CUDALink in general is a bit rough around the edges! $\endgroup$ – Simon Woods Jan 3 '16 at 20:18
  • $\begingroup$ @entropi99 Wouldn't docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html, sections 3.2.7 and 5.4ff, be a better, more authoritative reference? I voted to approve your edit because the intention seemed helpful and the OP can always override; and I would like you to feel welcome to participate on the site. But edits really should be more substantial. Perhaps it would have been better left as a comment? $\endgroup$ – Michael E2 Mar 9 '16 at 12:32
  • $\begingroup$ Hi Michael, Thanks for that. I couldn't comment at that time (need at least 50 reps to able to comment). The link from nvidia has a lot of information which may confuse a bit. But I agree with you. $\endgroup$ – entropi99 Mar 14 '16 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.