销毁 Openmp 中的线程 (C++)
是否可以销毁 OpenMP 创建的线程?当程序启动时,只有一个线程。在并行化部分之后,由于存在线程池,因此仍保留多个线程。并行部分运行后有什么方法可以销毁这个池吗?
我问是因为我在动态库中使用 OpenMP,并且在线程运行时无法关闭库句柄(程序将出现段错误)。
谢谢
更多解释: 我将所有并行化代码放入模块(共享库)中。然后,我加载该模块并向其传递一个从抽象基类派生的类。然后该模块并行“运行”该类。这样,我就可以不使用并行化、OpenMP、MPI 或其他任何东西,并且可以在运行时甚至即时更改并行方案。但 OpenMP 不会破坏线程,当需要手动 dlclose 库时,它会出现段错误,因为资源是从线程下面破坏的(我相信)。现在让程序完成而不关闭库可能是可以的,但是将来可能仍然会出现想要手动关闭库的情况(考虑动态更改方案)。希望这是有道理的:) 谢谢
Is it possible to destroy the threads created by OpenMP? When the program starts, there is only the one thread. After the parallelized section multiple threads remain since there is a thread pool. Is there any way to destroy this pool after the parallel section is run?
I ask because I'm using OpenMP in a dynamic library, and the library handle cannot be closed while the threads are running (the program will segfault).
Thanks
More explanation:
I'm putting all parallelization code into modules (shared libraries). I then load the module and pass it a class derived from an abstract base class. The module then 'runs' that class in parallel. This way, I can use no parallelization, OpenMP, MPI, or anything else and can change the parallel scheme at run time or even on the fly. But OpenMP doesn't destroy the threads, and when it comes time to manually dlclose the library, it will segfault since the resources are destroyed from underneath the thread (I believe). Letting the program finish without closing the library is probably ok for now, but wanting to manually close the library may still come up in the future (think changing scheme on the fly). Hope this makes sense :) Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前,OpenMP 规范没有为用户提供任何控制线程何时被销毁的能力。您所说的内容非常有趣,并且在任何 OpenMP 语言委员会讨论规范的会议上都没有被提及。您能否提供更多有关您正在做的事情以及问题所在的信息?将这一讨论提交给委员会将会有所帮助。
编辑添加 3/7 -
好的 - 这是一个似乎有效的简单示例,它可能会解决您的问题:
虽然主程序 (prog.c) 中没有 OpenMP 代码,但我编译了它使用 -fopenmp 标志。这意味着将在调用实际使用 OpenMP 的共享库之前设置 OpenMP 环境。这似乎解决了执行 dlclose 的问题,因为环境仍然存在。它还允许首次使用 OpenMP 获得的线程保留下来以供后续使用。这是否可以解决您的问题(或者这仅适用于这个简单的示例)?
At this point in time, the OpenMP specification doesn't give the user any ability to control when threads are destroyed. What you are saying is very interesting and hasn't been brought up during any of the OpenMP language committee meetings to discuss the specification. Can you give more information about what you are doing and what the problem is? It would be helpful in bringing this discussion to the committee.
Edit added 3/7 -
Okay - here is a simple example that seems to work and it might get around your problem:
While there is no OpenMP code in the main program (prog.c), I compiled it using the -fopenmp flag. This means that the OpenMP environment will be set up before the call to the shared library that is actually using OpenMP. This seems to get around the problem with doing a dlclose, since the environment is still there. It also allows the threads gotten by the first use of OpenMP to stay around for subsequent use. Does this work for getting around your problem (or does this only work for this simple example)?