简体   繁体   English

用boost.python交换numpy数组:pyublas或boost.numpy?

[英]exchange numpy array with boost.python: pyublas or boost.numpy?

I am interfacing a C++ data intense library with Python by py++ / boost.python . 我通过py ++ / boost.python将C ++数据密集库与Python连接起来。 After profiling my program, I find 70% of the run time is spent on code like this: 在对我的程序进行概要分析后,我发现70%的运行时间花费在这样的代码上:

ni = range(v2o.getHits())
tau = np.array([v2o.TofCorrectedTime[i] for i in ni])
q = [v2o.getCharge()[i] for i in ni]

v2o.TofCorrectedTime is typed __array_1_float_2368 from py++. v2o.TofCorrectedTime键入__array_1_float_2368从PY ++。 v2o.getCharge() is typed _impl_details_range_iterator_ from py++, too. v2o.getCharge()也是从py ++中键入的_impl_details_range_iterator_ Size being about 2000, the convertion from these py++ array wrappers to numpy is slow: 大小约为2000,从这些py ++数组包装器到numpy的转换很慢:

In [42]: timeit np.array(v2o.TofCorrectedTime)
100 loops, best of 3: 2.52 ms per loop

In [43]: timeit np.array(v2o.getCharge())
100 loops, best of 3: 4.94 ms per loop

In [44]: timeit np.array([0]*2368)
1000 loops, best of 3: 310 µs per loop

In [45]: timeit np.array(np.zeros(2368))
100000 loops, best of 3: 4.41 µs per loop

I searched the web for a solution. 我在网上搜索了一个解决方案。 The candidates are: 候选人是:

  1. Cython with memoryview Cythonmemoryview
  2. pyublas pyublas
  3. Boost.NumPy Boost.NumPy

Questions and Answers (updated): 问题与解答(更新):

  • Is cython/memoryview easy to be integrated with boost.python and py++? cython / memoryview是否易于与boost.python和py ++集成? I want to keep the rest of the library wrapper. 我想保留库包装的其余部分。

    No. (Jim's answer) 不。(吉姆的回答)

    cython c++ wrapper and boost.python have intrinsicly different infrastructures. cython c ++包装器和boost.python具有内在不同的基础结构。 It's hard for them to talk to each other. 他们很难相互交谈。 (Although in principle, we could teach py++ to output cython code. But that's another story.) (虽然原则上我们可以教py ++输出cython代码。但这是另一个故事。)

    Extending the present wrapper with Boost.NumPy is the most manageable way. 使用Boost.NumPy扩展当前包装是最易于管理的方式。

  • Which one best suites my problem in terms of convertion overhead? 在转换开销方面哪一个最适合我的问题?

    (No definite answer yet.) (还没有确切的答案。)

Thanks 谢谢

(Disclaimer: I'm the primary author of Boost.NumPy.) (免责声明:我是Boost.NumPy的主要作者。)

I'm afraid none of these options are particularly great. 我担心这些选项都不是特别棒。 Here's how I think the pro/con analysis goes: 以下是我认为pro / con分析的方法:

  • Cython has a large number of users and developers, and hence you'll have a lot more support if you go with that option. Cython拥有大量的用户和开发人员,因此如果您使用该选项,您将获得更多支持。 It's not at all integrated with Boost.Python, however, and I think it'd be a tremendous amount of work to make Cython objects talk to Boost.Python, let alone Py++; 但是,它根本没有与Boost.Python集成,我认为使Cython对象与Boost.Python交流是一项巨大的工作,更不用说Py ++了。 you'd probably need to gain a pretty solid understanding of the low-level implementation details of both Cython and Boost.Python to get that going. 你可能需要非常了解Cython和Boost.Python的低级实现细节才能实现这一目标。 You'd probably be better off scrapping your Py++/Boost.Python wrappers if you want to use Cython. 如果你想使用Cython,你可能最好不要废弃你的Py ++ / Boost.Python包装器。

  • Boost.NumPy has a much smaller community, and support resources are hence more limited, but it's much better suited to the code you already have. Boost.NumPy拥有一个更小的社区,因此支持资源更加有限,但它更适合您已有的代码。 Py++ knows nothing about Boost.NumPy, so it won't automatically generate code that uses it (it might be that you could teach Py++ about Boost.NumPy; I'm not familiar enough with Py++ to know), but it's very straightforward to add custom Boost.Python code (and hence Boost.NumPy code) to a Py++ project. Py ++对Boost.NumPy一无所知,因此它不会自动生成使用它的代码(可能是你可以教Py ++关于Boost.NumPy;我对Py ++不太熟悉),但它非常简单将自定义Boost.Python代码(以及Boost.NumPy代码)添加到Py ++项目中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM