Getting "invalid device function" when compiling with device emulation.

I’m trying to use CUDA with cmake (v 2.8) on my Mac (OSX 10.6). So far it works fine, I created a small sample just to try it out (see below). However when I switch on emulation mode, it cannot invoke the CUDA kernel anymore and I get the following error message:

Cuda error: kernel invocation: invalid device function .

I’m not sure if it’s a problem with cmake or CUDA on my Mac…

I already posted this on stackoverflow [1], but nobody responded so far. The post on stackoverflow includes the CMakeLists.txt and the source files. I’m also wondering about the debug options on OSX, cuda-gdb does not seem to be supported so I have to rely on emulation mode?

Thx for answers!

[1] http://stackoverflow.com/questions/2676477…-emulation-mode

It looks like this is a problem with the FindCUDA.cmake script (which I maintain). There is a new cudartemu library which showed up in 3.0 which I didn’t notice and which you need to link against instead of the original cudart. I’ll go ahead and update the FindCUDA.cmake script with this new feature, but in the meantime, you can adjust the library that CUDA_CUDART_LIBRARY points to from ccmake or from the cmake-gui. Just have it point to libcudartemu.dylib instead of libcudart.dylib.

Another option is you could add the following to your CMakeLists.txt file just before cuda_add_executable(test). I tested this and it works on my mac (OSX 10.5, CUDA 3.0, CMake 2.8.0). Note that this may not work with future versions of CUDA where emulation is completely removed.

if(CUDA_VERSION VERSION_GREATER "2.3")

  find_library_local_first(CUDA_CUDARTEMU_LIBRARY cudartemu "\"cudartemu\" library")

  if(CUDA_BUILD_EMULATION)

	set(CUDA_LIBRARIES ${CUDA_CUDARTEMU_LIBRARY})

	if(APPLE)

	  # We need to add the path to cudart to the linker using rpath, since the

	  # library name for the cuda libraries is prepended with @rpath.

	  get_filename_component(_cuda_path_to_cudart "${CUDA_CUDARTEMU_LIBRARY}" PATH)

	  if(_cuda_path_to_cudart)

		list(APPEND CUDA_LIBRARIES -Wl,-rpath "-Wl,${_cuda_path_to_cudart}")

	  endif()

	endif()

	# Add cuda library to the link line only if it is found.

	if (CUDA_CUDA_LIBRARY)

	  set(CUDA_LIBRARIES ${CUDA_LIBRARIES} ${CUDA_CUDA_LIBRARY})

	endif(CUDA_CUDA_LIBRARY)

	mark_as_advanced(

	  CUDA_CUDARTEMU_LIBRARY

	  )

  endif()

endif()

Thanks a lot for your quick answer! I quickly tested it and it seems to work. I don’t understand however why the emulation mode will be removed. Will you still be able to use emulation (maybe with an additional library) in future CUDA versions? Also nvcc-gdb is not available for OSX right now, so the only debugging option I have is to use emulation, I think…

I don’t know the details of the CUDA product roadmap, so I can’t tell you when emulation mode will go away.

I also don’t know anything about cuda-gdb for Mac, though I know there are implementations of printf available for CUDA. I don’t know right off hand where the code might be found, though.

Wish I could be of more help.

You might try a new thread about the cuda-gdb for Mac or search the forum.

Thanks a lot for the provided CMakeFiles changes, JBigler! :) It also solved my problem and now, the emulation mode runs just fine! :)

Thanks a lot for the provided CMakeFiles changes, JBigler! :) It also solved my problem and now, the emulation mode runs just fine! :)

You’re welcome! By the way, this is fixed in the next release of CMake (2.8.3) scheduled to be released soon. Also, the emulation libraries were removed in CUDA 3.1. They simply aren’t there anymore.

You’re welcome! By the way, this is fixed in the next release of CMake (2.8.3) scheduled to be released soon. Also, the emulation libraries were removed in CUDA 3.1. They simply aren’t there anymore.