在python线程中调用TBB'intertra

编程入门 行业动态 更新时间:2024-10-27 16:25:28
在python线程中调用TBB'intertra_for'(Calling TBB 'parallel_for' in python thread)

我有一个用于python的自定义c ++模块,它公开了函数,其中一些函数使用TBB(tbb21_015oss)来加速处理。

到目前为止,在Win32环境中从Python(2.6.2)调用TBB加速函数时没有问题。

但是现在,我从Python线程(使用threading.Thread类创建)调用这样的函数时遇到问题 - 在调用相同的函数时,从主Python线程工作正常。

调用该函数会导致应用程序崩溃,并显示以下消息:

First-chance exception at 0x03522e96 in python.exe: 0xC0000005: Access violation reading location 0x000000c8.

TBB.dll基地址是0x03510000,根据MSVC 2005调试器,崩溃发生在TBB的'parallel_for.h'代码中,显然是在static void start_for::run( const Range& range, const Body& body, const Partitioner& partitioner )执行:

start_for& a = *new(task::allocate_root(context)) start_for(range,body,const_cast<Partitioner&>(partitioner));

似乎有一个空指针被解除引用。

FWIW,TBB通过使用延迟初始化机制进行初始化:

// at 'global' scope tbb::task_scheduler_init g_tbbinit(tbb::task_scheduler_init::deferred); ... // in a function g_tbbinit.initialize();

有没有办法让这项工作? 例如,TBB是否需要特定的初始化以允许从“自定义”线程调用它?

I have a custom c++ module for python that exposes functions, some of which use TBB (tbb21_015oss) to accelerate processing.

Until now, I had no problem when calling a TBB-accelerated function from Python (2.6.2) in a Win32 environment.

But now, I have a problem when calling such a function from a Python thread (created by using threading.Thread class) - while calling the same function, from the main Python thread works fine.

Calling the function lead to application crash, with the following message :

First-chance exception at 0x03522e96 in python.exe: 0xC0000005: Access violation reading location 0x000000c8.

TBB.dll base address is 0x03510000, and according to MSVC 2005 debugger, the crash happens in code from TBB's 'parallel_for.h', apparently in static void start_for::run( const Range& range, const Body& body, const Partitioner& partitioner ) when executing :

start_for& a = *new(task::allocate_root(context)) start_for(range,body,const_cast<Partitioner&>(partitioner));

It seems that there's a NULL pointer being dereferenced.

FWIW, TBB is initialized by using the deferred initialization mechanism :

// at 'global' scope tbb::task_scheduler_init g_tbbinit(tbb::task_scheduler_init::deferred); ... // in a function g_tbbinit.initialize();

Is there way to make this work ? For example, does TBB need specific initialization to allow it being called from a 'custom' thread ?

最满意答案

对于TBB 2.1,使用TBB的每个外部线程应首先创建自己的task_scheduler_init对象; 单个全局对象不起作用。

TBB的后续版本放宽了这一要求; 现在,如果不存在,则隐式创建这种特定于线程的初始化对象。

您可以尝试使用更新版本机械替换tbb.dll对您有所帮助。 使用最新版本的TBB重新编译上述C ++模块将会更好。 重新设计模块以分别为每个调用它的线程初始化TBB(这样它也适用于旧版本),如果设计约束不被禁止,也可能有意义。

With TBB 2.1, each external thread that uses TBB should first create its own task_scheduler_init object; a single global object will not work.

Later versions of TBB relaxed this requirement; now such a thread-specific initialization object is created implicitly if absent.

You might try whether mechanical replacement of tbb.dll with a more recent version will help you. Recompiling the mentioned C++ module with a recent version of TBB will be even better. Reworking the module to initialize TBB separately for each thread that invokes it (so that it works with an older version too), if not prohibited by design constraints, might make sense as well.

更多推荐

本文发布于:2023-08-04 21:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422033.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   python   intertra   TBB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!