# calling CUDA library functions using MathematicaGPULibrary (WGL)

Recently I want implement a sparse linear solver in Mathematica with cuda, and I found the following resource: calling CUDA library functions. As it shows, I tried to use CreateLibrary function to export the example Mathematica\10.4\SystemFiles\Links\CUDALink\CSource\addTwo.cu to a dll file. However, I faced link errors.

tmpxft_ 00002fa4_ 00000000-28_addTwo.obj : error LNK2019: unresolved external symbol CUDA_Runtime_getDeviceMemoryAsMInteger referenced in function "int __cdecl iAddTwo(struct st_WGL_Memory_p_t *,struct st_WGL_Memory_p_t *)" (?iAddTwo@@YAHPEAUst_WGL_Memory_p_t@@0@Z)

where CUDA_Runtime_getDeviceMemoryAsMInteger in my opinion is get device pointer from a wgl object which wrap a cudamemory data. So the pointer can be used in traditional cuda c/c++ functions. The errors imply that it possibly is lacking a lib file implementing CUDA_Runtime_getDeviceMemoryAsMInteger, but I only found the statement in wgl_cuda_runtime.h shows #define CUDA_Runtime_getDeviceMemoryAsMInteger(mem) ((mint *) CUDA_Runtime_getDeviceMemory(mem)). I also replaced CUDA_Runtime_getDeviceMemoryAsMInteger with (mint *) CUDA_Runtime_getDeviceMemory in the cu file, but it showed the similar problem that ...unresolved external symbol CUDA_Runtime_getDeviceMemory referenced in.... Could someone help me?

• I just recheck my code on V10.3 under Linux. It works fine. You know, WGL is undocumented so it can be broken in any future version. Did you try LibraryLink method? It is enough in many case because you can use global variables and arrays inside your library (they are stored until LibraryUnload). – ybeltukov Apr 8 '16 at 1:54
• @ybeltukov, the link errors imply that the lmplement files don't exist in the code. I also checked the source file and couldn't find CUDA_RUNTIME_XX functions anywhere. In addition, I installed 9.0 at another windows 7 computer and faced same problem. I think I have to choose the LibraryLink method, although it has disadvantages. Thanks @ybeltukov! – sejabs Apr 8 '16 at 3:07
• @ybeltukov, what version of cuda packlet do you use? I doubt the newest 10.2.0.3 may be a beta product and don't include the CUDA_Runtime_xx functions. – sejabs Apr 8 '16 at 14:52
• I updated to 10.2.0.3 from 10.0.0.1. Now the code runs only if I change "libCUDALink_Single.so" to "libCUDALink_Double.so" in CreateLibrary. Could you check it? (see also the corresponding remark in my answer). – ybeltukov Apr 11 '16 at 17:44