Cycles: Add option to directly link against CUDA libraries
authorSergey Sharybin <sergey.vfx@gmail.com>
Thu, 14 Jan 2016 07:24:09 +0000 (12:24 +0500)
committerSergey Sharybin <sergey.vfx@gmail.com>
Thu, 14 Jan 2016 07:27:22 +0000 (12:27 +0500)
The main purpose of such linking is to make Blender compatible with
NVidia's debuggers and profilers which are doing some LD_PRELOAD
magic to intercept some function calls. Such magic conflicts with
our CUDA wrangler magic and causes segmentation faults.

The option is disabled by default, so there's no affect on any of
artists.

In order to make Blender linked directly against CUDA library use
the WITH_CUDA_DYNLOAD CMake option (it's marked as advanced).

CMakeLists.txt
build_files/cmake/macros.cmake
extern/CMakeLists.txt
intern/cycles/app/CMakeLists.txt
intern/cycles/cmake/external_libs.cmake
intern/cycles/device/CMakeLists.txt
intern/cycles/device/device_cuda.cpp
source/blenderplayer/CMakeLists.txt

index 7d9d515d1e5500079b1761177a85855362367f1f..73d219e0e1c113719b5b950dc23103026dd28325 100644 (file)
@@ -399,6 +399,9 @@ mark_as_advanced(WITH_CYCLES_LOGGING)
 mark_as_advanced(WITH_CYCLES_DEBUG)
 mark_as_advanced(WITH_CYCLES_WERROR)
 
+option(WITH_CUDA_DYNLOAD "Dynamically load CUDA libraries at runtime" ON)
+mark_as_advanced(WITH_CUDA_DYNLOAD)
+
 # LLVM
 option(WITH_LLVM                                       "Use LLVM" OFF)
 if(APPLE)
@@ -2906,7 +2909,9 @@ if(WITH_BLENDER OR WITH_PLAYER)
 elseif(WITH_CYCLES_STANDALONE)
        add_subdirectory(intern/cycles)
        add_subdirectory(extern/clew)
-       add_subdirectory(extern/cuew)
+       if(WITH_CUDA_DYNLOAD)
+               add_subdirectory(extern/cuew)
+       endif()
        if(NOT WITH_SYSTEM_GLEW)
                add_subdirectory(extern/glew)
        endif()
index cacc741966f723cafce69fc18afc3305fb76c3df..a8e17c3b5ef20b86fd35ab98307bc611bf008538 100644 (file)
@@ -478,7 +478,11 @@ function(setup_liblinks
        # We put CLEW and CUEW here because OPENSUBDIV_LIBRARIES dpeends on them..
        if(WITH_CYCLES OR WITH_COMPOSITOR OR WITH_OPENSUBDIV)
                target_link_libraries(${target} "extern_clew")
-               target_link_libraries(${target} "extern_cuew")
+               if(WITH_CUDA_DYNLOAD)
+                       target_link_libraries(${target} "extern_cuew")
+               else()
+                       target_link_libraries(${target} ${CUDA_CUDA_LIBRARY})
+               endif()
        endif()
 
        #system libraries with no dependencies such as platform link libs or opengl should go last
index 632b68ef0742184b971f144255b7884c459093c8..82b13cc114bc6ccb465416b84d673a6be0debaf7 100644 (file)
@@ -74,7 +74,9 @@ endif()
 
 if(WITH_CYCLES OR WITH_COMPOSITOR OR WITH_OPENSUBDIV)
        add_subdirectory(clew)
-       add_subdirectory(cuew)
+       if(WITH_CUDA_DYNLOAD)
+               add_subdirectory(cuew)
+       endif()
 endif()
 
 if(WITH_MOD_BOOLEAN)
index 95bc517ae005c998615e7a44e51146258e0846ce..41233287279a26a71875cbe5307bc3a1a3c81f9d 100644 (file)
@@ -29,9 +29,14 @@ set(LIBRARIES
        ${TIFF_LIBRARY}
        ${PTHREADS_LIBRARIES}
        extern_clew
-       extern_cuew
 )
 
+if(WITH_CUDA_DYNLOAD)
+       list(APPEND LIBRARIES extern_cuew)
+else()
+       list(APPEND LIBRARIES ${CUDA_CUDA_LIBRARY})
+endif()
+
 if(WITH_CYCLES_OSL)
        list(APPEND LIBRARIES cycles_kernel_osl)
 endif()
index 56ab8bed6e85e2bc30d30a9dfeca5e15d65690fd..fc75efa77558558bf3a9e5c7896394536e2eab51 100644 (file)
@@ -37,7 +37,7 @@ endif()
 ###########################################################################
 # CUDA
 
-if(WITH_CYCLES_CUDA_BINARIES)
+if(WITH_CYCLES_CUDA_BINARIES OR NOT WITH_CUDA_DUNLOAD)
        find_package(CUDA) # Try to auto locate CUDA toolkit
        if(CUDA_FOUND)
                message(STATUS "CUDA nvcc = ${CUDA_NVCC_EXECUTABLE}")
index b0fa283c1d8809a9506944799e3085f53f449f75..220b16bb45bacd9d000f2fb21c732664758a936e 100644 (file)
@@ -11,10 +11,17 @@ set(INC
 
 set(INC_SYS
        ${GLEW_INCLUDE_DIR}
-       ../../../extern/cuew/include
        ../../../extern/clew/include
 )
 
+if(WITH_CUDA_DYNLOAD)
+       list(APPEND INC ../../../extern/cuew/include)
+       add_definitions(-DWITH_CUDA_DYNLOAD)
+else()
+       list(APPEND INC_SYS ${CUDA_TOOLKIT_INCLUDE})
+       add_definitions(-DCYCLES_CUDA_NVCC_EXECUTABLE="${CUDA_NVCC_EXECUTABLE}")
+endif()
+
 set(SRC
        device.cpp
        device_cpu.cpp
index 5c9ca3454c62ad0fec92bf5e00b27243d3de90c8..88f1a86d6ac0941ee565616f0e4f30b0153460f9 100644 (file)
 
 #include "buffers.h"
 
-#include "cuew.h"
+#ifdef WITH_CUDA_DYNLOAD
+#  include "cuew.h"
+#else
+#  include "util_opengl.h"
+#  include <cuda.h>
+#  include <cudaGL.h>
+#endif
 #include "util_debug.h"
 #include "util_logging.h"
 #include "util_map.h"
 
 CCL_NAMESPACE_BEGIN
 
+#ifndef WITH_CUDA_DYNLOAD
+
+/* Transparently implement some functions, so majority of the file does not need
+ * to worry about difference between dynamically loaded and linked CUDA at all.
+ */
+
+namespace {
+
+const char *cuewErrorString(CUresult result)
+{
+       /* We can only give error code here without major code duplication, that
+        * should be enough since dynamic loading is only being disabled by folks
+        * who knows what they're doing anyway.
+        *
+        * NOTE: Avoid call from several threads.
+        */
+       static string error;
+       error = string_printf("%d", result);
+       return error.c_str();
+}
+
+const char *cuewCompilerPath(void)
+{
+       return CYCLES_CUDA_NVCC_EXECUTABLE;
+}
+
+int cuewCompilerVersion(void)
+{
+       return (CUDA_VERSION / 100) + (CUDA_VERSION % 100 / 10);
+}
+
+}  /* namespace */
+#endif  /* WITH_CUDA_DYNLOAD */
+
 class CUDADevice : public Device
 {
 public:
@@ -1100,6 +1140,7 @@ public:
 
 bool device_cuda_init(void)
 {
+#ifdef WITH_CUDA_DYNLOAD
        static bool initialized = false;
        static bool result = false;
 
@@ -1133,6 +1174,9 @@ bool device_cuda_init(void)
        }
 
        return result;
+#else  /* WITH_CUDA_DYNLOAD */
+       return true;
+#endif /* WITH_CUDA_DYNLOAD */
 }
 
 Device *device_cuda_create(DeviceInfo& info, Stats &stats, bool background)
index f630befdf5fa28b70726b9392e211feae1667542..e8c710e68ae5b84ad9ce0984e163cac700ae234a 100644 (file)
@@ -255,5 +255,9 @@ setup_liblinks(blenderplayer)
 # We put CLEW and CUEW here because OPENSUBDIV_LIBRARIES dpeends on them..
 if(WITH_CYCLES OR WITH_COMPOSITOR OR WITH_OPENSUBDIV)
        target_link_libraries(blenderplayer "extern_clew")
-       target_link_libraries(blenderplayer "extern_cuew")
+       if(WITH_CUDA_DYNLOAD)
+               target_link_libraries(blenderplayer "extern_cuew")
+       else()
+               target_link_libraries(${target} ${CUDA_CUDA_LIBRARY})
+       endif()
 endif()