Making python bindings for a project with CUDA kernels. #401
-
Hi everyone! I am currently working on a project that requires me to build python bindings. For performance reasons I decided that nanobind would be a great option for this. However, I am running into a bit of an issue with compiling the CUDA kernels that are included in the project. My CMakeLists.txt looks like this: cmake_minimum_required(VERSION 3.15...3.27)
project(my_project LANGUAGES CXX CUDA)
enable_language(CUDA)
# Warn if the user invokes CMake directly
if (NOT SKBUILD)
message(WARNING <standard warning is here>)
endif()
# Try to import all Python components potentially needed by nanobind
find_package(Python 3.11
REQUIRED COMPONENTS Interpreter Development.Module
OPTIONAL_COMPONENTS Development.SABIModule
)
find_package(nanobind CONFIG REQUIRED)
find_package(CUDAToolkit REQUIRED)
set(MY_PROJECT_SOURCES
# Source files go here
src/pch.cpp
src/types.cpp
src/logging.cpp
src/display.cpp
src/main.cpp
)
set(MY_PROJECT_INCLUDES
# Include folders go here
Dependencies/x64/argparse/include
Dependencies/x64/CImg/include
Dependencies/x64/cuFFTDx/include
Dependencies/x64/fftw/include
Dependencies/x64/glm/include
Dependencies/x64/libpng/include
Dependencies/x64/zlib/include
${CUDAToolkit_INCLUDE_DIRS}
)
set(MY_PROJECT_DEPENDENCIES
"${CMAKE_SOURCE_DIR}/Dependencies/x64/fftw/lib/libfftw3-3.lib"
"${CMAKE_SOURCE_DIR}/Dependencies/x64/fftw/lib/libfftw3f-3.lib"
"${CMAKE_SOURCE_DIR}/Dependencies/x64/libpng/lib/libpng16.lib"
"${CMAKE_SOURCE_DIR}/Dependencies/x64/libpng/lib/libpng16_static.lib"
"${CMAKE_SOURCE_DIR}/Dependencies/x64/zlib/lib/zlib.lib"
"${CMAKE_SOURCE_DIR}/Dependencies/x64/zlib/lib/zlibstatic.lib"
CUDA::cufft
CUDA::cudart
#${CUDAToolkit_LIBRARY_DIR}
)
add_library(MY_PROJECT_CUDA_KERNELS STATIC my-project/src/kernels.cu)
set_target_properties(MY_PROJECT_CUDA_KERNELS
PROPERTIES
POSITION_INDEPENDENT_CODE ON
CUDA_VISIBILITY_PRESET "hidden"
CUDA_SEPARABLE_COMPILATION ON
)
target_link_libraries(MY_PROJECT_CUDA_KERNELS PRIVATE ${MY_PROJECT_DEPENDENCIES} ${Python_LIBRARIES})
target_include_directories(MY_PROJECT_CUDA_KERNELS PRIVATE ${MY_PROJECT_INCLUDES})
add_library(MY_PROJECT_LIBRARY STATIC ${MY_PROJECT_SOURCES})
set_target_properties(MY_PROJECT_LIBRARY
PROPERTIES
CXX_STANDARD 17
POSITION_INDEPENDENT_CODE ON
CUDA_VISIBILITY_PRESET "hidden"
CUDA_SEPARABLE_COMPILATION ON
)
target_link_libraries(MY_PROJECT_LIBRARY PRIVATE ${MY_PROJECT_DEPENDENCIES} ${MY_PROJECT_CUDA_KERNELS})
target_include_directories(MY_PROJECT_LIBRARY PRIVATE ${MY_PROJECT_INCLUDES})
nanobind_add_module(my_project_ext
STABLE_ABI
NB_STATIC
src/my_project_ext.cpp
${MY_PROJECT_SOURCES}
src/kernels.cu
)
target_link_libraries(my_project_ext PRIVATE ${MY_PROJECT_DEPENDENCIES} ${MY_PROJECT_LIBRARY})
target_include_directories(my_project _ext PRIVATE ${MY_PROJECT_INCLUDES})
install(TARGETS my_project_ext LIBRARY DESTINATION my_project) In #include "pch.h"
#include <nanobind/nanobind.h>
namespace nb = nanobind;
using namespace nb::literals;
int add( int i1, int i2 ) {
return i1 + i2;
}
NB_MODULE(my_project_ext, m) {
m.def( "add", &add, "a"_a, "b"_a = 1,
"This function adds two numbers and increments if only one is provided." );
nb::class_<Main>(m, "Main").def(nb::init<int, int, int, const std::string&>());
} Unfortunately, I am getting quite an extensive error that I am not sure how to fix:
Is there anyone who has made python bindings with a CUDA kernel before who is capable of providing some insight or maybe sharing how they did it? I have been stuck on this problem for...longer than I would like to admit really 😅 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 14 replies
-
Could it be something like your projected containing spaces, and one of the include directives not having quotes? |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
You need Process Monitor: https://learn.microsoft.com/en-us/sysinternals/downloads/procmon