使用cmake使用cpp和cuda源构建pybind11模块

时间:2018-08-18 10:46:43

标签: compiler-errors cmake cuda nvcc pybind11

我正在尝试为虚拟类生成python绑定,该虚拟类需要使用启用了cuda的编译器进行编译。我正在使用cmake 3.12.0,pybind11 v2.2.3和nvcc 7.5.17。编译失败,因为诸如-flto-fno-fat-lto-objects之类的选项直接传递给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")

有人知道如何进行这项工作吗?

1 个答案:

答案 0 :(得分:1)

更新:一个(可能是)更清洁的版本,使用target_set_properties并因此让cmake整理出实际的编译器/链接器标志。

cmake_minimum_required(VERSION 3.12)
project(dummy LANGUAGES CXX CUDA)

set(PYBIND11_CPP_STANDARD -std=c++11)
add_subdirectory(lib/pybind11)

add_library(dummycu STATIC
    Adder.cu
)

set_target_properties(dummycu PROPERTIES 
    POSITION_INDEPENDENT_CODE ON
    CUDA_VISIBILITY_PRESET "hidden"
    # CUDA_SEPARABLE_COMPILATION ON
)


add_library(dummy MODULE
    Adder.pybind.cpp
)

set_target_properties(dummy PROPERTIES 
    CXX_VISIBILITY_PRESET "hidden"
    INTERPROCEDURAL_OPTIMIZATION TRUE
    PREFIX "${PYTHON_MODULE_PREFIX}"
    SUFFIX "${PYTHON_MODULE_EXTENSION}"
)

target_link_libraries(dummy PRIVATE dummycu)
target_link_libraries(dummy PRIVATE pybind11::module)

我们还可以编译并链接一个目标中的所有内容。 这可能也可以用于修改pybind11Tools.cmake中的pybind11_add_module
由于pybind11无条件地为其module目标设置了可见性标志,因此该标志与不带-Xcompiler的nvcc一起使用,如果不想更改pybind11 CMakeLists.txt或pybind11Tools,则需要进行一些改动。 cmake。可能最好为module目标和/或pybind11_add_module函数编写自定义版本。

cmake_minimum_required(VERSION 3.12)
project(dummy LANGUAGES CXX CUDA)

set(PYBIND11_CPP_STANDARD -std=c++11)
add_subdirectory(lib/pybind11)

##pybind11 sets -fvisibility=hidden in INTERFACE_COMPILE_OPTIONS on it's module target
get_target_property(modifacecopts module INTERFACE_COMPILE_OPTIONS)
list(REMOVE_ITEM modifacecopts "-fvisibility=hidden")
set_target_properties(module PROPERTIES INTERFACE_COMPILE_OPTIONS "${modifacecopts}")

add_library(dummy MODULE
    Adder.pybind.cpp
    Adder.cu
)

set_target_properties(dummy PROPERTIES 
    POSITION_INDEPENDENT_CODE ON
    CUDA_VISIBILITY_PRESET "hidden"
    CXX_VISIBILITY_PRESET "hidden"
    INTERPROCEDURAL_OPTIMIZATION TRUE
    PREFIX "${PYTHON_MODULE_PREFIX}"
    SUFFIX "${PYTHON_MODULE_EXTENSION}"
)

target_link_libraries(dummy PRIVATE pybind11::module)