<tfoot id='3L1DV'></tfoot>
  • <legend id='3L1DV'><style id='3L1DV'><dir id='3L1DV'><q id='3L1DV'></q></dir></style></legend>

      • <bdo id='3L1DV'></bdo><ul id='3L1DV'></ul>
    1. <small id='3L1DV'></small><noframes id='3L1DV'>

      <i id='3L1DV'><tr id='3L1DV'><dt id='3L1DV'><q id='3L1DV'><span id='3L1DV'><b id='3L1DV'><form id='3L1DV'><ins id='3L1DV'></ins><ul id='3L1DV'></ul><sub id='3L1DV'></sub></form><legend id='3L1DV'></legend><bdo id='3L1DV'><pre id='3L1DV'><center id='3L1DV'></center></pre></bdo></b><th id='3L1DV'></th></span></q></dt></tr></i><div id='3L1DV'><tfoot id='3L1DV'></tfoot><dl id='3L1DV'><fieldset id='3L1DV'></fieldset></dl></div>

        从 C++ 调用 Cython 函数

        call Cython function from C++(从 C++ 调用 Cython 函数)

          1. <tfoot id='aFnUY'></tfoot>

              <bdo id='aFnUY'></bdo><ul id='aFnUY'></ul>
                <tbody id='aFnUY'></tbody>
                <i id='aFnUY'><tr id='aFnUY'><dt id='aFnUY'><q id='aFnUY'><span id='aFnUY'><b id='aFnUY'><form id='aFnUY'><ins id='aFnUY'></ins><ul id='aFnUY'></ul><sub id='aFnUY'></sub></form><legend id='aFnUY'></legend><bdo id='aFnUY'><pre id='aFnUY'><center id='aFnUY'></center></pre></bdo></b><th id='aFnUY'></th></span></q></dt></tr></i><div id='aFnUY'><tfoot id='aFnUY'></tfoot><dl id='aFnUY'><fieldset id='aFnUY'></fieldset></dl></div>

                  <small id='aFnUY'></small><noframes id='aFnUY'>

                  <legend id='aFnUY'><style id='aFnUY'><dir id='aFnUY'><q id='aFnUY'></q></dir></style></legend>
                1. 本文介绍了从 C++ 调用 Cython 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 C++ 库,它有一个 Python 包装器(用 SWIG 编写).该库允许执行小的用户定义代码(回调),例如向量上的元素操作.IE.你可以做任何任意的二进制函数,而不仅仅是一个 +.现在,这是通过接受二进制函数的可调用 Python 对象并调用它来完成的.它可以工作,但比不必在每次迭代中上下反弹到 Python 的代码慢约 80 倍.

                  I have a C++ library that has a Python wrapper (written with SWIG). This library allows executing small user-defined code (a callback), such as element-wise operations on a vector. I.e. instead of just a + you can do whatever arbitrary binary function. Right now this is accomplished by accepting a callable Python object for the binary function and calling it. It works, but is about 80 times slower than code that doesn't have to bounce up and down into Python at every iteration.

                  我如何编写/构建/导入 Cython 函数并将其传递到我的 C++ 库中,以便它可以被 C++ 库直接调用?

                  How would I write/build/import a Cython function could be passed into my C++ library so that it can be called directly by the C++ library?

                  如果我只是坚持使用 C,那么我会写一些类似

                  If I just stuck to C then I would write something like

                  EWise(double (*callback)(double, double))
                  

                  EWise 然后会 callback(10, 20); 等等.我希望 callback 用 Cython 编写,使用用户想要的任何名称,并且必须以某种方式通过 Python 将指向它的指针传递给我的 C++ 库.这就是我不清楚的地方.

                  EWise would then callback(10, 20); or such. I want callback to be written in Cython, using whatever name the user wants, and a pointer to it has to be passed to my C++ library through Python somehow. That somehow is where I'm unclear.

                  推荐答案

                  cython 的诀窍在于使用关键字 public

                  The trick with cython is in using the keyword public

                  cdef public double cython_function( double value, double value2 ):
                      return value + value2
                  

                  然后命令 cythonize 将创建标题 > 您可以包括在内.或者,您可以自己创建标题:

                  Then the command cythonize <your_file.pyx> along with <your_file.c> will create header <your_file.h> that you can include. Alternatively, you can create the header yourself:

                  #ifdef __cplusplus {
                  extern "C"
                  #endif
                  
                  double cython_function( double value, double value2 );
                  
                  #ifdef __cplusplus
                  }
                  #endif
                  

                  更新:

                  然后通过 Python 的一些覆盖,您可以使用 ctypes 的回调机制

                  Then with a little overlay from Python you can use ctypes's callback mechanism

                  func_type = CFUNCTYPE(c_double, c_double, c_double)
                  
                  your_library.set_callback_function ( func_type(user_modules.cython_function) )
                  

                  这篇关于从 C++ 调用 Cython 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)

                  <i id='Jl4XH'><tr id='Jl4XH'><dt id='Jl4XH'><q id='Jl4XH'><span id='Jl4XH'><b id='Jl4XH'><form id='Jl4XH'><ins id='Jl4XH'></ins><ul id='Jl4XH'></ul><sub id='Jl4XH'></sub></form><legend id='Jl4XH'></legend><bdo id='Jl4XH'><pre id='Jl4XH'><center id='Jl4XH'></center></pre></bdo></b><th id='Jl4XH'></th></span></q></dt></tr></i><div id='Jl4XH'><tfoot id='Jl4XH'></tfoot><dl id='Jl4XH'><fieldset id='Jl4XH'></fieldset></dl></div>
                      <bdo id='Jl4XH'></bdo><ul id='Jl4XH'></ul>

                          <small id='Jl4XH'></small><noframes id='Jl4XH'>

                              <tbody id='Jl4XH'></tbody>
                          1. <legend id='Jl4XH'><style id='Jl4XH'><dir id='Jl4XH'><q id='Jl4XH'></q></dir></style></legend>
                            <tfoot id='Jl4XH'></tfoot>