Skip to content

Instantly share code, notes, and snippets.

@webstory
Created March 14, 2018 08:49
Show Gist options
  • Save webstory/f6d02040d8b5101e4f7e34b1fe05a58f to your computer and use it in GitHub Desktop.
Save webstory/f6d02040d8b5101e4f7e34b1fe05a58f to your computer and use it in GitHub Desktop.
Cython with OpenMP
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
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'])]
)
@webstory
Copy link
Author

webstory commented Mar 14, 2018

build:
$ python setup.py build_ext --inplace

inspect:
cython -a cython_np.pyx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment