How to use something like openMP in Cython?

Basically, I have a problem that pretty much duplicates the parallel, and I think that I got into the scope of how quickly I can do this with simple python and multiprocessing, so now I'm trying to go to a lower level via Cython and, hope openMP.

In short, I wonder how can I use openMP with Cython, or will I have to wrap some raw C code and load / bind to it via Cython?

Or can I compile Cython to C code, and then change the C code to add openMP to pragmas, and then compile it into a library and load in Python?

+4
source share
4 answers

According to the cython wiki, the developers thought of various options, but I do not believe that they have implemented anything.

If your problem is awkwardly parallel, and you already have a solution for multiprocessing, why not just get every workflow to call cython code instead of python code?

+1
source

This question is from 3 years ago, and Cython currently has features available that support the OpenMP server. See, for example, documentation here . One very handy feature is prange . This is one example of how you can implement a (more naive) dot function using prange .

Remember to compile passing the argument "/opemmp" C compiler.

 import numpy as np cimport numpy as np import cython from cython.parallel import prange ctypedef np.double_t cDOUBLE DOUBLE = np.float64 def mydot(np.ndarray[cDOUBLE, ndim=2] a, np.ndarray[cDOUBLE, ndim=2] b): cdef np.ndarray[cDOUBLE, ndim=2] c cdef int i, M, N, K c = np.zeros((a.shape[0], b.shape[1]), dtype=DOUBLE) M = a.shape[0] N = a.shape[1] K = b.shape[1] for i in prange(M, nogil=True): multiply(&a[i,0], &b[0,0], &c[i,0], N, K) return c @cython.wraparound(False) @cython.boundscheck(False) @cython.nonecheck(False) cdef void multiply(double *a, double *b, double *c, int N, int K) nogil: cdef int j, k for j in range(N): for k in range(K): c[k] += a[j]*b[k+j*K] 
+7
source

If anyone came across this question:

Now there is direct support for OpenMP in cython via the cython.parallel module, see http://docs.cython.org/src/userguide/parallelism.html

+3
source

I have no experience with OpenMP, but you might be lucky to try zeromq (python bindings enabled):

easy_install pyzmq

0
source

Source: https://habr.com/ru/post/1337191/


All Articles