使用 carma(犰狳矩阵和 numpy 数组)用 pybind11 包装 c++ 类时出错

Posted

技术标签:

【中文标题】使用 carma(犰狳矩阵和 numpy 数组)用 pybind11 包装 c++ 类时出错【英文标题】:Error by wrapping c++ class with pybind11 using carma (armadillo matrices and numpy arrays) 【发布时间】:2020-09-24 09:55:04 【问题描述】:

我有一个简单的例子,尝试使用carma 和pybind11 来回基于example 的犰狳矩阵和numpy 数组。如果没有 printarma() 函数,代码可以工作,即只需使用 pybind11 进行包装,代码就可以编译,我可以在 python 中运行它。这里的问题似乎与carma有关。代码如下:

#include <ostream>
#include <iostream>
#include <armadillo>
#include <carma/carma.h>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>

namespace py = pybind11;

using namespace std;

class Base

public:
    virtual void test() = 0;
    virtual void printarma() = 0;
;
class Derived: public Base


public:
    void printarma(py::array_t<double> & arr) arma::Mat<double> mat = carma::arr_to_mat<double>(arr);
            std::cout << mat<< std::endl;
    void test() cout << "Test";
;


PYBIND11_MODULE(example,m) 
    py::class_<Base>(m, "Base");

    py::class_<Derived, Base>(m, "Derived")
        .def(py::init<>())
        .def("test", &Derived::test);
        m.def("printarma", &Derived::printarma,py::arg("arr"));
 

我运行命令:

c++ -O3 -Wall -shared -std=c++14 -fPIC -larmadillo `python3 -m pybind11 --includes` abstrakt_test.cpp -o example`python3-config --extension-suffix`

错误是:

In file included from /home/mikanim/anaconda3/envs/pybind/lib/python3.7/site-packages/pybind11/include/pybind11/pybind11.h:47,
                 from /home/mikanim/anaconda3/envs/pybind/lib/python3.7/site-packages/pybind11/include/pybind11/numpy.h:12,
                 from /usr/local/include/carma/carma/converters.h:24,
                 from /usr/local/include/carma/carma/arraystore.h:1,
                 from /usr/local/include/carma/carma.h:1,
                 from abstrakt_test.cpp:18:
/home/mikanim/anaconda3/envs/pybind/lib/python3.7/site-packages/pybind11/include/pybind11/detail/init.h: In instantiation of ‘Class* pybind11::detail::initimpl::construct_or_initialize(Args&& ...) [with Class = Derived; Args = ; typename std::enable_if<(! std::is_constructible<_Tp, _Args>::value), int>::type <anonymous> = 0]’:
/home/mikanim/anaconda3/envs/pybind/lib/python3.7/site-packages/pybind11/include/pybind11/detail/init.h:174:66:   required from ‘static void pybind11::detail::initimpl::constructor<Args>::execute(Class&, const Extra& ...) [with Class = pybind11::class_<Derived, Base>; Extra = ; typename std::enable_if<(! Class::has_alias), int>::type <anonymous> = 0; Args = ]’
/home/mikanim/anaconda3/envs/pybind/lib/python3.7/site-packages/pybind11/include/pybind11/pybind11.h:1148:9:   required from ‘pybind11::class_<type_, options>& pybind11::class_<type_, options>::def(const pybind11::detail::initimpl::constructor<Args ...>&, const Extra& ...) [with Args = ; Extra = ; type_ = Derived; options = Base]’
abstrakt_test.cpp:45:26:   required from here
/home/mikanim/anaconda3/envs/pybind/lib/python3.7/site-packages/pybind11/include/pybind11/detail/init.h:63:64: error: invalid new-expression of abstract class type ‘Derived’
 inline Class *construct_or_initialize(Args &&...args)  return new Classstd::forward<Args>(args)...; 
                                                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
abstrakt_test.cpp:31:7: note:   because the following virtual functions are pure within ‘Derived’:
 class Derived: public Base
       ^~~~~~~
abstrakt_test.cpp:29:18: note:  ‘virtual void Base::printarma()’
     virtual void printarma() = 0;
                  ^~~~~~~~~
In file included from /home/mikanim/anaconda3/envs/pybind/lib/python3.7/site-packages/pybind11/include/pybind11/numpy.h:12,
                 from /usr/local/include/carma/carma/converters.h:24,
                 from /usr/local/include/carma/carma/arraystore.h:1,
                 from /usr/local/include/carma/carma.h:1,
                 from abstrakt_test.cpp:18:
/home/mikanim/anaconda3/envs/pybind/lib/python3.7/site-packages/pybind11/include/pybind11/pybind11.h: In instantiation of ‘void pybind11::cpp_function::initialize(Func&&, Return (*)(Args ...), const Extra& ...) [with Func = pybind11::cpp_function::cpp_function(Return (Class::*)(Arg ...), const Extra& ...) [with Return = void; Class = Derived; Arg = pybind11::array_t<double, 16>&; Extra = pybind11::name, pybind11::scope, pybind11::sibling, pybind11::arg]::<lambda(Derived*, pybind11::array_t<double>&)>; Return = void; Args = Derived*, pybind11::array_t<double, 16>&; Extra = pybind11::name, pybind11::scope, pybind11::sibling, pybind11::arg]’:
/home/mikanim/anaconda3/envs/pybind/lib/python3.7/site-packages/pybind11/include/pybind11/pybind11.h:78:9:   required from ‘pybind11::cpp_function::cpp_function(Return (Class::*)(Arg ...), const Extra& ...) [with Return = void; Class = Derived; Arg = pybind11::array_t<double, 16>&; Extra = pybind11::name, pybind11::scope, pybind11::sibling, pybind11::arg]’
/home/mikanim/anaconda3/envs/pybind/lib/python3.7/site-packages/pybind11/include/pybind11/pybind11.h:819:22:   required from ‘pybind11::module& pybind11::module::def(const char*, Func&&, const Extra& ...) [with Func = void (Derived::*)(pybind11::array_t<double>&); Extra = pybind11::arg]’
abstrakt_test.cpp:47:55:   required from here
/home/mikanim/anaconda3/envs/pybind/lib/python3.7/site-packages/pybind11/include/pybind11/pybind11.h:133:50: error: static assertion failed: The number of argument annotations does not match the number of function arguments
         static_assert(expected_num_args<Extra...>(sizeof...(Args), cast_in::has_args, cast_in::has_kwargs),
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /home/mikanim/anaconda3/envs/pybind/lib/python3.7/site-packages/pybind11/include/pybind11/attr.h:13,
                 from /home/mikanim/anaconda3/envs/pybind/lib/python3.7/site-packages/pybind11/include/pybind11/pybind11.h:44,
                 from /home/mikanim/anaconda3/envs/pybind/lib/python3.7/site-packages/pybind11/include/pybind11/numpy.h:12,
                 from /usr/local/include/carma/carma/converters.h:24,
                 from /usr/local/include/carma/carma/arraystore.h:1,
                 from /usr/local/include/carma/carma.h:1,
                 from abstrakt_test.cpp:18:
/home/mikanim/anaconda3/envs/pybind/lib/python3.7/site-packages/pybind11/include/pybind11/cast.h:1958:57: error: ‘std::enable_if_t<std::is_void<_Res>::value, pybind11::detail::void_type> pybind11::detail::argument_loader<Args>::call(Func&&) && [with Return = void; Guard = pybind11::detail::void_type; Func = pybind11::cpp_function::cpp_function(Return (Class::*)(Arg ...), const Extra& ...) [with Return = void; Class = Derived; Arg = pybind11::array_t<double, 16>&; Extra = pybind11::name, pybind11::scope, pybind11::sibling, pybind11::arg]::<lambda(Derived*, pybind11::array_t<double>&)>&; Args = Derived*, pybind11::array_t<double, 16>&; std::enable_if_t<std::is_void<_Res>::value, pybind11::detail::void_type> = pybind11::detail::void_type]’, declared using local type ‘pybind11::cpp_function::cpp_function(Return (Class::*)(Arg ...), const Extra& ...) [with Return = void; Class = Derived; Arg = pybind11::array_t<double, 16>&; Extra = pybind11::name, pybind11::scope, pybind11::sibling, pybind11::arg]::<lambda(Derived*, pybind11::array_t<double>&)>’, is used but never defined [-fpermissive]
     enable_if_t<std::is_void<Return>::value, void_type> call(Func &&f) && 

提前致谢!

编辑:

没有carma的代码:

c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` abstrakt_test.cpp -o example`python3-config --extension-suffix`



#include <ostream>
#include <iostream>
#include <pybind11/pybind11.h>


namespace py = pybind11;
using namespace std;

class Base

public:
    virtual void test() = 0;
;
class Derived: public Base


public:

    void test() cout << "Test";
;

PYBIND11_MODULE(example,m) 
    py::class_<Base>(m, "Base");

    py::class_<Derived, Base>(m, "Derived")
        .def(py::init<>())
        .def("test", &Derived::test);
 

【问题讨论】:

我没有尝试过这个carma库,但我不确定问题是否与它有关。自从我使用 pybind11 以来已经有很长时间了,但是如果内存没有让我失望,那么只要你有虚拟类(特别是抽象类),你就需要一个蹦床类。也就是说,您需要将py::class_&lt;Base&gt;(m, "Base"); 更改为类似于py::class_&lt;Base, PyBase&gt;(m, "Base"); 的内容,并使用一些pybind11 宏在C++ 中定义PyBase 类。参考pybind11 documentation on this topic。 @darcamo 感谢您的洞察力。如果您要从上面的代码中删除所有带有 carma 和 armadillo 的内容,代码将在 python 中编译和运行。所以我不确定你的意思是什么? Pybind11 需要能够为绑定实例化一个 C++ 类,编译器错误清楚地表明问题是“......以下虚函数在......内是纯函数”。这是您需要蹦床课程的有力指标。它将充当 pybind 可以实例化的具体类,而不是原始的抽象类。此外,您应该在Base 中声明test 方法的绑定,并在Bases 蹦床类实现中使用PYBIND11_OVERLOAD_PURE 听起来比我以前更令人困惑,特别是因为虚拟功能的一切都已经工作了。我使用命令为没有 carma 的情况添加了代码,它可以工作。我不知道为什么仅仅因为我在派生类中有一个新函数就必须更新 test() 函数。 确实令人困惑。但是你绝对需要一个蹦床课,即使这个特定的问题不能仅仅通过这个来解决。我目前无法自己尝试此代码(这就是我没有写出正确答案的原因),但我仍然认为问题出在我所描述的问题上,因为您收到了错误消息。 【参考方案1】:

最后有两件事:基类中的虚函数需要将py::array_t&lt;double&gt; &amp; arr 添加到它,.def 末尾的分号需要与m 之前的.def 一起删除.此代码在 python 中编译和工作:

class Base

public:
    virtual void test() = 0;
    virtual void printarma(py::array_t<double> & arr) = 0;
;
class Derived: public Base


public:
    void printarma(py::array_t<double> & arr) arma::Mat<double> mat = carma::arr_to_mat<double>(arr);
            std::cout << mat<< std::endl;
    void test()  cout << "Test";
;


PYBIND11_MODULE(example,m) 
    py::class_<Base>(m, "Base");

    py::class_<Derived, Base>(m, "Derived")
        .def(py::init<>())
        .def("test", &Derived::test)
        .def("printarma", &Derived::printarma,py::arg("arr"));

【讨论】:

是的,您并没有真正覆盖printarma,因为签名不同。在覆盖的方法中始终添加override 是个好主意。如果你将它添加到一个没有真正覆盖任何东西的方法中,比如在这种情况下,你会得到一个错误。 @darcamo 我很好奇——当 override 已经编译并可以工作时,添加它有什么意义?该错误主要是因为printarma函数内部缺少py::array_t&lt;double&gt; &amp; arr 有时您想覆盖基类中的方法,但您错误地使用了不同的签名。它将毫无问题地编译,但不是覆盖,而是重载。然后使用原始签名调用该方法并使用基类版本(或者如果它是虚拟纯的,如printarma,则会出现编译错误)而不是新版本。通过在派生类版本中添加override,如果发生这种情况并显示明确的错误消息,您会提前收到编译时错误。 @darcamo 感谢您的洞察力。因此,即使它现在可以编译并工作,您认为仍然使用覆盖是一种好习惯? 是的,而且不仅限于此。养成在重写方法时始终使用 override 的习惯。

以上是关于使用 carma(犰狳矩阵和 numpy 数组)用 pybind11 包装 c++ 类时出错的主要内容,如果未能解决你的问题,请参考以下文章

在类中使用犰狳矩阵

用犰狳在 C++ 中实现 numpy.polyfit 和 numpy.polyval

犰狳 - 初始化矩阵并用值填充矩阵

将二维数组转换为犰狳矩阵(垫)对象

在 C++ 中存储大矩阵(犰狳)

从 C 数组(列优先)转换为犰狳矩阵(arma::mat)而不复制