我正在尝试生成一个虚拟类的Python绑定,需要使用支持CUDA的编译器进行编译。我正在使用CMake 3.12.0、Pybind11 v2.2.3和nvcc 7.5.17。编译失败,因为像
这里是一个(最小化的)示例:
CUDA代码:
绑定码:
-flto
和-fno-fat-lto-objects
这样的选项直接传递给nvcc,而nvcc无法识别它们。这里是一个(最小化的)示例:
CUDA代码:
//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;
}
绑定码:
#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
)
使用例如 cmake ../src && make -VERBOSE=1
构建失败。虽然生成了 Adder.pybind.cpp
的目标文件,但是编译 Adder.cu
失败,错误信息如下:
/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'
我尝试禁用自动传播到nvcc,但没有成功。
set(CUDA_PROPAGATE_HOST_FLAGS FALSE)
set(CUDAFLAGS "-Xcompiler -fPIC -Xcompiler -flto -Xcompiler=-fvisibility=hidden")
有谁知道如何让这个工作起来吗?
pybind11_add_module
。只需使用find_package(PythonLibs...)
和target_link_libraries(your_target ${PYTHON_LIBRARIES})
即可。这样,你就可以轻松地使用cuda_add_library
而不会出现任何问题。 - Patwie