[Bf-blender-cvs] [7eeeaec6da3] master: Cycles: use direct linking for oneAPI backend

Xavier Hallade noreply at git.blender.org
Fri Oct 7 09:54:38 CEST 2022


Commit: 7eeeaec6da33971ab7805c9a4bfd5f4e186273d1
Author: Xavier Hallade
Date:   Thu Oct 6 18:35:51 2022 +0200
Branches: master
https://developer.blender.org/rB7eeeaec6da33971ab7805c9a4bfd5f4e186273d1

Cycles: use direct linking for oneAPI backend

This is a minimal set of changes, allowing a lot of cleanup that can
happen afterward as it allows sycl method and objects to be used outside
of kernel.cpp.

Reviewed By: brecht, sergey

Differential Revision: https://developer.blender.org/D15397

===================================================================

M	build_files/cmake/platform/platform_unix.cmake
M	build_files/cmake/platform/platform_win32.cmake
M	intern/cycles/device/CMakeLists.txt
M	intern/cycles/device/oneapi/device.cpp
M	intern/cycles/device/oneapi/device_impl.cpp
M	intern/cycles/device/oneapi/device_impl.h
D	intern/cycles/device/oneapi/dll_interface.h
M	intern/cycles/device/oneapi/queue.cpp
M	intern/cycles/device/oneapi/queue.h
M	intern/cycles/kernel/CMakeLists.txt
D	intern/cycles/kernel/device/oneapi/dll_interface_template.h
M	intern/cycles/kernel/device/oneapi/kernel.cpp
M	intern/cycles/kernel/device/oneapi/kernel.h
M	source/creator/CMakeLists.txt

===================================================================

diff --git a/build_files/cmake/platform/platform_unix.cmake b/build_files/cmake/platform/platform_unix.cmake
index 35103ec441b..0aab46b1250 100644
--- a/build_files/cmake/platform/platform_unix.cmake
+++ b/build_files/cmake/platform/platform_unix.cmake
@@ -335,10 +335,18 @@ if(WITH_CYCLES_DEVICE_ONEAPI)
     set(LEVEL_ZERO_ROOT_DIR ${CYCLES_LEVEL_ZERO})
   endif()
 
-  set(CYCLES_SYCL ${LIBDIR}/dpcpp CACHE PATH "Path to DPC++ and SYCL installation")
+  set(CYCLES_SYCL ${LIBDIR}/dpcpp CACHE PATH "Path to oneAPI DPC++ compiler")
   if(EXISTS ${CYCLES_SYCL} AND NOT SYCL_ROOT_DIR)
     set(SYCL_ROOT_DIR ${CYCLES_SYCL})
   endif()
+  file(GLOB _sycl_runtime_libraries
+    ${SYCL_ROOT_DIR}/lib/libsycl.so
+    ${SYCL_ROOT_DIR}/lib/libsycl.so.[0-9]
+    ${SYCL_ROOT_DIR}/lib/libsycl.so.[0-9].[0-9].[0-9]-[0-9]
+    ${SYCL_ROOT_DIR}/lib/libpi_level_zero.so
+  )
+  list(APPEND PLATFORM_BUNDLED_LIBRARIES ${_sycl_runtime_libraries})
+  unset(_sycl_runtime_libraries)
 endif()
 
 if(WITH_OPENVDB)
diff --git a/build_files/cmake/platform/platform_win32.cmake b/build_files/cmake/platform/platform_win32.cmake
index 4778ddebea6..0f7f04203c6 100644
--- a/build_files/cmake/platform/platform_win32.cmake
+++ b/build_files/cmake/platform/platform_win32.cmake
@@ -952,5 +952,17 @@ endif()
 set(ZSTD_INCLUDE_DIRS ${LIBDIR}/zstd/include)
 set(ZSTD_LIBRARIES ${LIBDIR}/zstd/lib/zstd_static.lib)
 
-set(LEVEL_ZERO_ROOT_DIR ${LIBDIR}/level_zero)
-set(SYCL_ROOT_DIR ${LIBDIR}/dpcpp)
+if(WITH_CYCLES_DEVICE_ONEAPI)
+  set(LEVEL_ZERO_ROOT_DIR ${LIBDIR}/level_zero)
+  set(CYCLES_SYCL ${LIBDIR}/dpcpp CACHE PATH "Path to oneAPI DPC++ compiler")
+  if(EXISTS ${CYCLES_SYCL} AND NOT SYCL_ROOT_DIR)
+    set(SYCL_ROOT_DIR ${CYCLES_SYCL})
+  endif()
+  file(GLOB _sycl_runtime_libraries
+    ${SYCL_ROOT_DIR}/bin/sycl.dll
+    ${SYCL_ROOT_DIR}/bin/sycl[0-9].dll
+    ${SYCL_ROOT_DIR}/bin/pi_level_zero.dll
+  )
+  list(APPEND PLATFORM_BUNDLED_LIBRARIES ${_sycl_runtime_libraries})
+  unset(_sycl_runtime_libraries)
+endif()
diff --git a/intern/cycles/device/CMakeLists.txt b/intern/cycles/device/CMakeLists.txt
index 24855d795d1..e5467121497 100644
--- a/intern/cycles/device/CMakeLists.txt
+++ b/intern/cycles/device/CMakeLists.txt
@@ -142,7 +142,6 @@ set(SRC
   ${SRC_DUMMY}
   ${SRC_MULTI}
   ${SRC_OPTIX}
-  ${SRC_ONEAPI}
   ${SRC_HEADERS}
 )
 
@@ -188,7 +187,22 @@ if(WITH_CYCLES_DEVICE_METAL)
   )
 endif()
 if (WITH_CYCLES_DEVICE_ONEAPI)
+  if(WIN32)
+    set(cycles_kernel_oneapi_lib ${CMAKE_CURRENT_BINARY_DIR}/../kernel/cycles_kernel_oneapi.lib)
+  else()
+    set(cycles_kernel_oneapi_lib ${CMAKE_CURRENT_BINARY_DIR}/../kernel/libcycles_kernel_oneapi.so)
+  endif()
+  list(APPEND LIB
+    ${SYCL_LIBRARY}
+    ${cycles_kernel_oneapi_lib}
+  )
   add_definitions(-DWITH_ONEAPI)
+  list(APPEND SRC
+    ${SRC_ONEAPI}
+  )
+  list(APPEND INC_SYS
+    ${SYCL_INCLUDE_DIR}
+  )
 endif()
 
 if(WITH_OPENIMAGEDENOISE)
diff --git a/intern/cycles/device/oneapi/device.cpp b/intern/cycles/device/oneapi/device.cpp
index 4aa307e9300..f303ab41627 100644
--- a/intern/cycles/device/oneapi/device.cpp
+++ b/intern/cycles/device/oneapi/device.cpp
@@ -19,62 +19,12 @@
 
 CCL_NAMESPACE_BEGIN
 
-#ifdef WITH_ONEAPI
-static OneAPIDLLInterface oneapi_dll;
-#endif
-
-#ifdef _WIN32
-#  define LOAD_ONEAPI_SHARED_LIBRARY(path) (void *)(LoadLibrary(path))
-#  define LOAD_ONEAPI_SHARED_LIBRARY_ERROR() GetLastError()
-#  define FREE_SHARED_LIBRARY(handle) FreeLibrary((HMODULE)handle)
-#  define GET_SHARED_LIBRARY_SYMBOL(handle, name) GetProcAddress((HMODULE)handle, name)
-#elif __linux__
-#  define LOAD_ONEAPI_SHARED_LIBRARY(path) dlopen(path, RTLD_NOW)
-#  define LOAD_ONEAPI_SHARED_LIBRARY_ERROR() dlerror()
-#  define FREE_SHARED_LIBRARY(handle) dlclose(handle)
-#  define GET_SHARED_LIBRARY_SYMBOL(handle, name) dlsym(handle, name)
-#endif
-
 bool device_oneapi_init()
 {
 #if !defined(WITH_ONEAPI)
   return false;
 #else
 
-  string lib_path = path_get("lib");
-#  ifdef _WIN32
-  lib_path = path_join(lib_path, "cycles_kernel_oneapi.dll");
-#  else
-  lib_path = path_join(lib_path, "cycles_kernel_oneapi.so");
-#  endif
-  void *lib_handle = LOAD_ONEAPI_SHARED_LIBRARY(lib_path.c_str());
-
-  /* This shouldn't happen, but it still makes sense to have a branch for this. */
-  if (lib_handle == NULL) {
-    LOG(ERROR) << "oneAPI kernel shared library cannot be loaded: "
-               << LOAD_ONEAPI_SHARED_LIBRARY_ERROR();
-    return false;
-  }
-
-#  define DLL_INTERFACE_CALL(function, return_type, ...) \
-    (oneapi_dll.function) = reinterpret_cast<decltype(oneapi_dll.function)>( \
-        GET_SHARED_LIBRARY_SYMBOL(lib_handle, #function)); \
-    if (oneapi_dll.function == NULL) { \
-      LOG(ERROR) << "oneAPI shared library function \"" << #function \
-                 << "\" has not been loaded from kernel shared  - disable oneAPI " \
-                    "library disable oneAPI implementation due to this"; \
-      FREE_SHARED_LIBRARY(lib_handle); \
-      return false; \
-    }
-#  include "kernel/device/oneapi/dll_interface_template.h"
-#  undef DLL_INTERFACE_CALL
-
-  VLOG_INFO << "oneAPI kernel shared library has been loaded successfully";
-
-  /* We need to have this oneapi kernel shared library during all life-span of the Blender.
-   * So it is not unloaded because of this.
-   * FREE_SHARED_LIBRARY(lib_handle); */
-
   /* NOTE(@nsirgien): we need to enable JIT cache from here and
    * right now this cache policy is controlled by env. variables. */
   /* NOTE(hallade) we also disable use of copy engine as it
@@ -109,17 +59,10 @@ bool device_oneapi_init()
 #endif
 }
 
-#if defined(_WIN32) || defined(__linux__)
-#  undef LOAD_SYCL_SHARED_LIBRARY
-#  undef LOAD_ONEAPI_SHARED_LIBRARY
-#  undef FREE_SHARED_LIBRARY
-#  undef GET_SHARED_LIBRARY_SYMBOL
-#endif
-
 Device *device_oneapi_create(const DeviceInfo &info, Stats &stats, Profiler &profiler)
 {
 #ifdef WITH_ONEAPI
-  return new OneapiDevice(info, oneapi_dll, stats, profiler);
+  return new OneapiDevice(info, stats, profiler);
 #else
   (void)info;
   (void)stats;
@@ -165,7 +108,7 @@ static void device_iterator_cb(const char *id, const char *name, int num, void *
 void device_oneapi_info(vector<DeviceInfo> &devices)
 {
 #ifdef WITH_ONEAPI
-  (oneapi_dll.oneapi_iterate_devices)(device_iterator_cb, &devices);
+  OneapiDevice::iterate_devices(device_iterator_cb, &devices);
 #else  /* WITH_ONEAPI */
   (void)devices;
 #endif /* WITH_ONEAPI */
@@ -175,10 +118,10 @@ string device_oneapi_capabilities()
 {
   string capabilities;
 #ifdef WITH_ONEAPI
-  char *c_capabilities = (oneapi_dll.oneapi_device_capabilities)();
+  char *c_capabilities = OneapiDevice::device_capabilities();
   if (c_capabilities) {
     capabilities = c_capabilities;
-    (oneapi_dll.oneapi_free)(c_capabilities);
+    free(c_capabilities);
   }
 #endif
   return capabilities;
diff --git a/intern/cycles/device/oneapi/device_impl.cpp b/intern/cycles/device/oneapi/device_impl.cpp
index dd0622a5bd5..2df605fa047 100644
--- a/intern/cycles/device/oneapi/device_impl.cpp
+++ b/intern/cycles/device/oneapi/device_impl.cpp
@@ -8,7 +8,7 @@
 #  include "util/debug.h"
 #  include "util/log.h"
 
-#  include "kernel/device/oneapi/kernel.h"
+#  include "kernel/device/oneapi/globals.h"
 
 CCL_NAMESPACE_BEGIN
 
@@ -19,26 +19,19 @@ static void queue_error_cb(const char *message, void *user_ptr)
   }
 }
 
-OneapiDevice::OneapiDevice(const DeviceInfo &info,
-                           OneAPIDLLInterface &oneapi_dll_object,
-                           Stats &stats,
-                           Profiler &profiler)
+OneapiDevice::OneapiDevice(const DeviceInfo &info, Stats &stats, Profiler &profiler)
     : Device(info, stats, profiler),
       device_queue_(nullptr),
       texture_info_(this, "texture_info", MEM_GLOBAL),
       kg_memory_(nullptr),
       kg_memory_device_(nullptr),
-      kg_memory_size_(0),
-      oneapi_dll_(oneapi_dll_object)
+      kg_memory_size_(0)
 {
   need_texture_info_ = false;
 
-  oneapi_dll_.oneapi_set_error_cb(queue_error_cb, &oneapi_error_string_);
+  oneapi_set_error_cb(queue_error_cb, &oneapi_error_string_);
 
-  /* OneAPI calls should be initialized on this moment. */
-  assert(oneapi_dll_.oneapi_create_queue != nullptr);
-
-  bool is_finished_ok = oneapi_dll_.oneapi_create_queue(device_queue_, info.num);
+  bool is_finished_ok = create_queue(device_queue_, info.num);
   if (is_finished_ok == false) {
     set_error("oneAPI queue initialization error: got runtime exception \"" +
               oneapi_error_string_ + "\"");
@@ -50,7 +43,7 @@ OneapiDevice::OneapiDevice(const DeviceInfo &info,
   }
 
   size_t globals_segment_size;
-  is_finished_ok = oneapi_dll_.oneapi_kernel_globals_size(device_queue_, globals_segment_size);
+  is_finished_ok = kernel_globals_size(device_queue_, globals_segment_size);
   if (is_finished_ok == false) {
     set_error("oneAPI constant memory initialization got runtime exception \"" +
               oneapi_error_string_ + "\"");
@@ -59,27 +52,27 @@ OneapiDevice::OneapiDevice(const DeviceInfo &info,
     VLOG_DEBUG << "Successfully created global/constant memory segment (kernel globals object)";
   }
 
-  kg_memory_ = oneapi_dll_.oneapi_usm_aligned_alloc_host(device_queue_, globals_segment_size, 16);
-  oneapi_dll_.oneapi_usm_memset(device_queue_, kg_memory_, 0, globals_segment_size);
+  kg_memory_ = usm_aligned_alloc_host(device_queue_, globals_segment_size, 16);
+  usm_memset(device_queue_, kg_memory_, 0, globals_segment_size);
 
-  kg_memory_device_ = oneapi_dll_.oneapi_usm_alloc_device(device_queue_, globals_segment_size);
+  kg_memory_device_ = usm_alloc_device(device_queue_, globals_segment_size);
 
   kg_memory_size_ = globals_segment_size;
 
-  max_memory_on_device_ = oneapi_dll_.oneapi_get_memcapacity(device_queue_);
+  max_memory_on_device_ = get_memcapacity();
 }
 
 OneapiDevice::~OneapiDevice()
 {
   texture_info_.free();
-  oneapi_dll_.oneapi_usm_free(device_queue_, kg_memory_);
-  oneapi_dll_.oneapi_usm_free(device_queue_, kg_memory_device_);
+  usm_free(device_queue_, kg_memory_);
+  usm_free(device_queue_, kg_memory_device_);
 
   for (ConstMemMap::iterator mt = const_mem_map_.begin(); mt != const_mem_map_.end(); mt++)
     delete mt->second;
 
   if (device

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list