OpenMP 和 Python [英] OpenMP and Python

查看:35
本文介绍了OpenMP 和 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.实现粗略并行化所需的时间非常少.有没有办法在 Python 中复制此功能?

The reason I use OpenMP is because a serial code can be converted to a working parallel code with a few !$OMPs 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 扩展
  • 无堆栈 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆