cython

cython openmp single, barrier

点点圈 提交于 2019-12-24 03:42:54
问题 I'm trying to use openmp in cython. I need to do two things in cython: i) use the #pragma omp single{} scope in my cython code. ii) use the #pragma omp barrier{} Does anyone know how to do this in cython? Here are more details. I have a nogil cdef-function my_fun() which I call in an omp for-loop: from cython.parallel cimport prange cimport openmp cdef int i with nogil: for i in prange(10,schedule='static', num_threads=10): my_func(i) Inside my_func I need to place a barrier to wait for all

Cython-compiled *.pyd causes python interpreter to crash

蓝咒 提交于 2019-12-24 03:29:09
问题 I am compiling some modules with Cython into *.pyd files that get imported by a Python program. I have tested all of my modules and they all work with the Python program. At least, they all worked until today, when I tried to run the program and the interpreter crashed upon trying to load one of the *.pyd files. It crashed with the Windows message "python.exe has stopped working". I have been able to identify the *.pyd file that causes this, because by removing it I can start up my program.

When profiling Cython Code, what is `stringsource`?

两盒软妹~` 提交于 2019-12-24 02:33:40
问题 I have a heavy Cython function that I'm trying to optimize. I am profiling per this following tutorial http://docs.cython.org/src/tutorial/profiling_tutorial.html. My profile output looks like this: ncalls tottime percall cumtime percall filename:lineno(function) 1 7.521 7.521 18.945 18.945 routing_cython_core.pyx:674(resolve_flat_regions_for_drainage) 6189250 4.964 0.000 4.964 0.000 stringsource:323(__cinit__) 6189250 2.978 0.000 7.942 0.000 stringsource:618(memoryview_cwrapper) 6009849 0

Calling Cython from c++ with not built-in types

寵の児 提交于 2019-12-24 02:24:14
问题 i'm trying to extend functionality of my lib written in C++ with Python and Cython. I have class MyClass in C++ wich is essential for my lib. And i'm using a lot it's wrapper class PyMyClass in Python. So i want to use functions (with PyMyClass as argument) from C++. How i can accomplish this? I imagine it something like this: cdef public my_func(MyClass class): *cast MyClass to PyMyClass* other_py_func(PyMyClass) myclass.h namespace classes { class MyClass { public: explicit MyClass(int x,

cython C code compilation fails with typed memoryviews

跟風遠走 提交于 2019-12-24 02:06:33
问题 The following code compiles without issues in cython: cdef class Double: cdef double v def __init__(self, double v): self.v = v cdef double incr(self) nogil: self.v += 1 return self.v cdef int f(Double[:] xs): cdef int i, N N = xs.shape[0] for i in range(N): xs[i].incr() return 0 However, when I try to compile the generated C code, the gcc compiler stops with an error: test.c: In function '__pyx_f_4test_f': test.c:1491:55: error: 'PyObject {aka struct _object}' has no member named '__pyx_vtab

cython C code compilation fails with typed memoryviews

天涯浪子 提交于 2019-12-24 02:06:14
问题 The following code compiles without issues in cython: cdef class Double: cdef double v def __init__(self, double v): self.v = v cdef double incr(self) nogil: self.v += 1 return self.v cdef int f(Double[:] xs): cdef int i, N N = xs.shape[0] for i in range(N): xs[i].incr() return 0 However, when I try to compile the generated C code, the gcc compiler stops with an error: test.c: In function '__pyx_f_4test_f': test.c:1491:55: error: 'PyObject {aka struct _object}' has no member named '__pyx_vtab

Cython: storing unicode in numpy array

假装没事ソ 提交于 2019-12-24 01:19:38
问题 I'm new to cython, and I've been having a re-ocurring problem involving encoding unicode inside of a numpy array. Here's an example of the problem: import numpy as np cimport numpy as np cpdef pass_array(np.ndarray[ndim=1,dtype=np.unicode] a): pass cpdef access_unicode_item(np.ndarray a): cdef unicode item = a[0] Example errors: In [3]: unicode_array = np.array([u"array",u"of",u"unicode"],dtype=np.unicode) In [4]: pass_array(unicode_array) ValueError: Does not understand character buffer

Remove compile args in Cython

穿精又带淫゛_ 提交于 2019-12-24 01:18:37
问题 I want to compile my python project with cython. I created this setup.py file : from setuptools import setup, find_packages from Cython.Build import cythonize recursive_tree = [file for file in glob.iglob("sample/**/*.py", recursive=True)] setup( name = 'sample', version = sample.__version__, packages = find_packages(), author = "42", description = "Cython Sample", include_package_data = True, ext_modules = cythonize( recursive_tree, nthreads=2, exclude="setup.py", build_dir = "out", ), ) In

Using C++ STL maps in Cython

北城以北 提交于 2019-12-24 00:45:49
问题 I'm trying to use a map in a Cython class but the Cython compiler raises an error. Here is an example demonstrating my problem and the error reported by Cython. Cython file pyx from libcpp.map cimport map from libcpp.utility cimport pair from libcpp.string cimport string cdef class MyDict: cdef: map[string, int] components def __setitem__(self, key, value): self.components[key] = value def __getitem__(self, key): return self.components[key] Python file from pyximport import install install()

Making a vectorized numpy function behave like a ufunc

我怕爱的太早我们不能终老 提交于 2019-12-24 00:05:35
问题 Let's suppose that we have a Python function that takes in Numpy arrays and returns another array: import numpy as np def f(x, y, method='p'): """Parameters: x (np.ndarray) , y (np.ndarray), method (str) Returns: np.ndarray""" z = x.copy() if method == 'p': mask = x < 0 else: mask = x > 0 z[mask] = 0 return z*y although the actual implementation does not matter. We can assume that x and y will always be arrays of the same shape, and that the output is of the same shape as x . The question is