Created
March 14, 2018 08:49
-
-
Save webstory/f6d02040d8b5101e4f7e34b1fe05a58f to your computer and use it in GitHub Desktop.
Cython with OpenMP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from cython.parallel import prange | |
import numpy as np | |
cimport numpy as np | |
def calculate_z(int maxiter, double complex[:] zs, double complex[:] cs): | |
""" Calculate output list using jullia update rules.""" | |
cdef unsigned int i, length | |
cdef double complex z, c | |
cdef int[:] output = np.empty(len(zs), dtype=np.int32) | |
length = len(zs) | |
with nogil: | |
for i in prange(length, schedule="guided"): | |
z = zs[i] | |
c = cs[i] | |
output[i] = 0 | |
while output[i] < maxiter and (z.real * z.real + i.imag * z.imag) < 4: | |
z = z * z + c | |
output[i] += 1 | |
return output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from distutils.core import setup | |
from distutils.extension import Extension | |
from Cython.Distutils import build_ext | |
setup( | |
cmdclass = {'build_ext': build_ext}, | |
ext_modes = [Extension("calculate", | |
["cython_np.pyx"], | |
extra_compile_args=['-fopenmp'], | |
extra_link_args=['-fopenmp'])] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
build:
$ python setup.py build_ext --inplace
inspect:
cython -a cython_np.pyx