<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 函数

        时间:2023-10-17

          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 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:通过3d派对库回调成员函数 下一篇:使用 boost::python 将回调从 python 传递到 C++

                  相关文章

                  <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'>

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