views:

763

answers:

2

CUDA has an option to compile code in emulation mode, which is supported in the .rules file they provide.

I have C# .NET 3.5 SP1 code that calls a native dll, using DllImport, the native dll is compiled via VS2008 using nvcc and its function is to transfer memory from and to CUDA and to invoke CUDA kernels.

When the CUDA kernels are correct, everything runs fine, but when there is a bug, I can only step in to the code until the title of the kernels and see the parameters they receive. (I enabled debugging native code in the startup-project's debug options.)

I have tried compiling with emulation mode, however I get the CUDA error "mixed device execution" when calling the CUDA memcopy host-->device. I tried switching the alloc+dealloc+memcopy with their equivalent non-CUDA versions, but then the same error occurs when invoking the kernels.

What did I do wrong in my attempt to using the debug-emulation mode?

P.S. I tried this on Vista x64 SP1 + VS2008, with the same solution complied in both x86 and x64, neither worked in emulation mode, both worked in non-emulation mode.

+1  A: 

From the CUDA Programming Guide p44:

When compiling an application in this mode (using the -deviceemu option), the device code is compiled for and runs on the host, allowing the programmer to use the host’s native debugging support to debug the application as if it were a host application. The preprocessor macro DEVICE_EMULATION is defined in this mode. All code for an application, including any libraries used, must be compiled consistently either for device emulation or for device execution. Linking code compiled for device emulation with code compiled for device execution causes the following runtime error to be returned upon initialization: cudaErrorMixedDeviceExecution.

Do you just have a single DLL which you've recompiled for EMU or are there other DLLs which aren't for example the CUDA utils library?

This works as I'd expect on Win7 x64 compiling Debug|x86 with EMU enabled.

Here are the compiler and linker settings I'm using:

 "C:\Program Files\CUDA\bin64\nvcc.exe"   -m32 -arch sm_10 -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin" -deviceemu -D_DEVICEEMU   -Xcompiler "/EHsc /W3 /nologo /Od /Zi   /MT  "  -maxrregcount=32  --compile -o "int\x86\Debug\NBody.DomainModel.Native.cu.obj" "c:\Src\NBody\trunk\NBody.DomainModel.Native\NBody.DomainModel.Native.vcproj"

/VERBOSE /OUT:"c:\Src\NBody\trunk\NBody.DomainModel.Native\bin\x86\Debug\NBody.DomainModel.Native.dll" /INCREMENTAL /NOLOGO /LIBPATH:"C:\Program Files\CUDA\lib64\..\lib" /DLL /MANIFEST /MANIFESTFILE:"int\x86\Debug\NBody.DomainModel.Native.dll.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /NODEFAULTLIB:"libcmt" /DEBUG /ASSEMBLYDEBUG /PDB:"c:\Src\NBody\trunk\NBody.DomainModel.Native\bin\x86\Debug\NBody.DomainModel.Native.pdb" /DYNAMICBASE /FIXED:No /NXCOMPAT /MACHINE:X86 /ERRORREPORT:PROMPT cudart.lib
Ade Miller
I have a project full of C# dlls with only one native/CUDA dll.
Danny Varod
That's what I have too. C# application calling into a C++/CLI DLL which in turn calls some CUDA code. What compile options do you have set in the CUDA build rules? For example is optimization disabled?
Ade Miller
I am using the standard CUDA 2.3 .rules file's options, in debug mode (no optimization). The only change I made was routing the compiler/linker to different CUDA Toolkit/SDK folders in x86 and in x64 so I can compile in both.
Danny Varod
My C++ DLL isn't even CLI enabled.
Danny Varod
I added my compiler and linker settings. It might help to compare them with yours. You could also look in the debug modules window and see what libraries actually got loaded as it seems like somehow you've got a non-EMU one.
Ade Miller
Hi, I compared the settings and found no differences !?Is there not supposed to be any difference in the code (any #ifdef _DEVICEEMU)?I also double checked all the files in the project - they all have the same settings as the project.
Danny Varod
Nope. No ifdefs required. Stupid question... as I'm running out of ideas. You're building x86 right? If you build x64 and debug from VS then mixed mode debugging isn't supported and none of your (C++/CUDA) breakpoints will be hit.
Ade Miller
Yep x86, I manage to step in the native dll all the way to the title of the kernel (I get to see the parameters). When I change the project's configuration to emulation-debug it doesn't work.
Danny Varod
A: 

The issue was caused by a missing build event to copy the SDK emulation DLLS (only the TOOLKIT DLLs were copied) and a few regular libs instead of emulation libs.

Danny Varod