OpenMP和Python [英] OpenMP and Python
问题描述
我有为共享内存机器(在C和FORTRAN中)编码OpenMP的经验,可以执行简单的任务,例如矩阵加法,乘法等(仅看它如何与LAPACK竞争).我知道OpenMP足以执行简单的任务,而无需查看文档.
I have experience in coding OpenMP for Shared Memory machines (in both C and FORTRAN) to carry out simple tasks like matrix addition, multiplication etc. (Just to see how it competes with LAPACK). I know OpenMP enough to carry out simple tasks without the need to look at documentation.
最近,我转向项目的Python,除了绝对的基础知识之外,我对Python没有任何经验.
Recently, I shifted to Python for my projects and I don't have any experience with Python beyond the absolute basics.
我的问题是:
在Python中使用OpenMP的最简单方法是什么?最简单的说,我的意思是在程序员方面花费最少的工作(即使这是以增加系统时间为代价的)?
What is the easiest way to use OpenMP in Python? By easiest, I mean the one that takes least effort on the programmer side (even if it comes at the expense of added system time)?
我使用OpenMP的原因是因为串行代码可以转换为工作的并行代码,并且周围散布着几个!$OMP
.实现 rough 并行化所需的时间非常短.有什么办法可以在Python中复制此功能?
The reason I use OpenMP is because a serial code can be converted to a working parallel code with a few !$OMP
s scattered around. The time required to achieve a rough parallelization is fascinatingly small. Is there any way to replicate this feature in Python?
通过浏览SO,我可以找到:
From browsing around on SO, I can find:
- C扩展名
- StackLess Python
还有更多吗?哪个最符合我的问题?
Are there more? Which aligns best with my question?
推荐答案
由于GIL,没有必要在CPython中将线程用于CPU密集型任务.您需要进行多处理(示例)或使用在计算过程中释放GIL的C扩展,例如一些numpy函数,示例.
Due to GIL there is no point to use threads for CPU intensive tasks in CPython. You need either multiprocessing (example) or use C extensions that release GIL during computations e.g., some of numpy functions, example.
您可以轻松地在Cython中编写使用多个线程的C扩展,示例.
You could easily write C extensions that use multiple threads in Cython, example.
这篇关于OpenMP和Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!