I'm trying to generate python bindings for a dummy class which needs to be compiled with a cuda enabled compiler. I'm using cmake 3.12.0, pybind11 v2.2.3 and nvcc 7.5.17. Compilation fails because options like -flto
and -fno-fat-lto-objects
are passed directly to nvcc, which doesn't recognize them.
Here is a (minimal) example:
Cuda Code:
//Adder.hpp
#include <thrust/host_vector.h>
struct Adder {
thrust::host_vector<float> a_h;
thrust::host_vector<float> b_h;
thrust::host_vector<float> r_h;
int N;
Adder(int N);
void set_a(float const * const in);
void set_b(float const * const in);
void calc();
void calc_gpu();
};
//Adder.cu
#include "Adder.hpp"
#include <thrust/device_vector.h>
Adder::Adder(int N): N(N),a_h(N),b_h(N),r_h(N) {}
void Adder::set_a(float const * const in) {
for (int i=0; i<N; ++i) {
a_h[i] = in[i];
}
}
void Adder::set_b(float const * const in) {
for (int i=0; i<N; ++i) {
b_h[i] = in[i];
}
}
void Adder::calc() {
for (int i=0; i<N; ++i) {
r_h[i] = a_h[i]+b_h[i];
}
}
void Adder::calc_gpu() {
thrust::device_vector<float> a_d(a_h);
thrust::device_vector<float> b_d(b_h);
thrust::device_vector<float> r_d(r_h);
thrust::transform(a_d.begin(), a_d.end(), b_d.begin(), r_d.begin(),thrust::plus<float>());
r_h = r_d;
}
Binding Code:
#include "Adder.hpp"
#include "lib/include/pybind11/pybind11.h"
#include "lib/include/pybind11/numpy.h"
#include <stdexcept>
namespace py = pybind11;
void bind_Adder(py::module& m) {
py::class_<Adder>(m,"Adder","Module docstring")
.def(py::init<int>(), py::arg("N"), "Init Adder")
.def(
"set_a"
, [](Adder& self, py::array_t<float, py::array::c_style | py::array::forcecast> in) {
py::buffer_info ai = in.request();
if (ai.ndim!=1 || ai.shape[0]!=self.N || ai.strides[0]!=sizeof(float)) {
throw std::runtime_error("Shape of given numpy array must be (N,)! Type must be float.");
}
self.set_a(static_cast<float const * const>(ai.ptr));
}
, py::arg("in")
, "Set a."
)
.def(
"set_b"
, [](Adder& self, py::array_t<float, py::array::c_style | py::array::forcecast> in) {
py::buffer_info ai = in.request();
if (ai.ndim!=1 || ai.shape[0]!=self.N || ai.strides[0]!=sizeof(float))
throw std::runtime_error("Shape of given numpy array must be (N,)! Type must be float.");
self.set_b(static_cast<float const * const>(ai.ptr));
}
, py::arg("in")
, "Set b."
)
.def(
"get_r"
, [](Adder& self, py::array_t<float> x) {
auto r = x.mutable_unchecked<1>();
for (ssize_t i = 0; i < r.shape(0); i++) {
r(i) = self.r_h[i];
}
}
, py::arg("x").noconvert())
.def("calc", &Adder::calc, "Calculate on CPU.")
.def("calc_gpu", &Adder::calc_gpu, "Calculate on GPU.");
}
PYBIND11_MODULE(dummy, m) {
bind_Adder(m);
}
CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 3.11)
project(dummy LANGUAGES CXX CUDA)
set(PYBIND11_CPP_STANDARD -std=c++11)
add_subdirectory(lib/pybind11)
pybind11_add_module(dummy
Adder.pybind.cpp
Adder.cu
)
Building with e.g. cmake ../src && make -VERBOSE=1
fails. While the object file for Adder.pybind.cpp
is generated succesfully. The compilation of Adder.cu
fails with:
/usr/bin/nvcc -Ddummy_EXPORTS -I/home/user/projects/pybind11_cuda_cmake/lib/pybind11/include -I/home/user/.anaconda3/include/python3.6m -Xcompiler=-fPIC -std=c++11 -flto -fno-fat-lto-objects -x cu -c /home/user/projects/pybind11_cuda_cmake/Adder.cu -o CMakeFiles/dummy.dir/Adder.cu.o
nvcc fatal : Unknown option 'flto'
I tried to disable automatic propagation to nvcc, but with no luck.
set(CUDA_PROPAGATE_HOST_FLAGS FALSE)
set(CUDAFLAGS "-Xcompiler -fPIC -Xcompiler -flto -Xcompiler=-fvisibility=hidden")
Does anybody know how to make this work?
pybind11_add_module
. Justfind_package(PythonLibs...)
andtarget_link_libraries(your_target ${PYTHON_LIBRARIES}
is sufficient. This way you can easily usecuda_add_library
without any issues. – Chromo