cython

Cython: prange is repeating not parallelizing

落爺英雄遲暮 提交于 2021-02-10 04:06:23
问题 I have the following simple Cython function for a parallel reduction: # cython: boundscheck = False # cython: initializedcheck = False # cython: wraparound = False # cython: cdivision = True # cython: language_level = 3 from cython.parallel import parallel, prange cpdef double simple_reduction(int n, int num_threads): cdef int i cdef int sum = 0 for i in prange(n, nogil=True, num_threads=num_threads): sum += 1 return sum Which horrifyingly returns the following: In [3]: simple_reduction(n=10,

NumPy: how to left join arrays with duplicates

冷暖自知 提交于 2021-02-09 07:36:57
问题 To use Cython, I need to convert df1.merge(df2, how='left') (using Pandas ) to plain NumPy , while I found numpy.lib.recfunctions.join_by(key, r1, r2, jointype='leftouter') doesn't support any duplicates along key . Is there any way to solve it? 回答1: Here's a stab at a pure numpy left join that can handle duplicate keys: import numpy as np def join_by_left(key, r1, r2, mask=True): # figure out the dtype of the result array descr1 = r1.dtype.descr descr2 = [d for d in r2.dtype.descr if d[0]

NumPy: how to left join arrays with duplicates

寵の児 提交于 2021-02-09 07:36:51
问题 To use Cython, I need to convert df1.merge(df2, how='left') (using Pandas ) to plain NumPy , while I found numpy.lib.recfunctions.join_by(key, r1, r2, jointype='leftouter') doesn't support any duplicates along key . Is there any way to solve it? 回答1: Here's a stab at a pure numpy left join that can handle duplicate keys: import numpy as np def join_by_left(key, r1, r2, mask=True): # figure out the dtype of the result array descr1 = r1.dtype.descr descr2 = [d for d in r2.dtype.descr if d[0]

Porting cython files from python2 to python3 with 2to3

China☆狼群 提交于 2021-02-08 19:47:10
问题 I have a python package which was developed under python2.7, but I need to port it to python3.6 . I use cython in some parts of the code, hence the package has both .py and .pyx files. I tried the 2to3 command, but I got an error that I couldn't neither understand nor solve. Example: I have the following test.pyx file # cython: profile=False cimport cython @cython.boundscheck(False) @cython.wraparound(False) @cython.profile(False) cpdef sillyfunction(): print 'Thank you for your kind help'

CythonGSL/ Using GSL on Windows via Cython

妖精的绣舞 提交于 2021-02-08 07:51:47
问题 Machine Configuration: My config is windows 7 x64, with python 2.7 and cython 0.18 (all 64 bit ) installed. I also have MS C++ 2008 Visual Studio installed. I have the GSL binaries and have Path pointed to the GSL\Bin. I am using MS VS 2008 + SDK 7.0 to compile cython .pyx files. I use the SDK 7.0 command prompt to compile to c and cython. Problem However at compilation time I get a LINK error 2019, which indicates the header files in the GSL folder are not being found . My Attempts To

CythonGSL/ Using GSL on Windows via Cython

≡放荡痞女 提交于 2021-02-08 07:51:28
问题 Machine Configuration: My config is windows 7 x64, with python 2.7 and cython 0.18 (all 64 bit ) installed. I also have MS C++ 2008 Visual Studio installed. I have the GSL binaries and have Path pointed to the GSL\Bin. I am using MS VS 2008 + SDK 7.0 to compile cython .pyx files. I use the SDK 7.0 command prompt to compile to c and cython. Problem However at compilation time I get a LINK error 2019, which indicates the header files in the GSL folder are not being found . My Attempts To

Going faster than struct.pack with cython

时光总嘲笑我的痴心妄想 提交于 2021-02-08 05:15:17
问题 I'm trying to do better than struct.pack . Taking a specific case of packing integeres, via the answer to this question, I have the following to pack a list of ints in pack_ints.pyx : # cython: language_level=3, boundscheck=False import cython @cython.boundscheck(False) @cython.wraparound(False) def pack_ints(int_col): int_buf = bytearray(4*len(int_col)) cdef int[::1] buf_view = memoryview(int_buf).cast('i') idx: int = 0 for idx in range(len(int_col)): buf_view[idx] = int_col[idx] return int

What are differences between Cython's language_level 3 and 3str?

六眼飞鱼酱① 提交于 2021-02-07 12:58:42
问题 In the upcoming Cython 3.0 version, 3str language_level (which was introduced with Cython 0.29) becomes the new default instead of the current default 2 , i.e. if language_level is not set (how to set), we get the following warning: FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: /home/ed/mygithub/cython/foo.pyx tree = Parsing.p_module(s, pxd, full_module_name) But what are the differences between 3str and 3

How to type generator function in Cython?

孤人 提交于 2021-02-07 11:38:21
问题 If I have a generator function in Python, say: def gen(x): for i in range(x): yield(i ** 2) How do I declare that the output data type is int in Cython? Is it even worth while? Thanks. Edit: I read mentions of (async) generators being implemented in the changelog: http://cython.readthedocs.io/en/latest/src/changes.html?highlight=generators#id23 However there is no documentation about how to use them. Is it because they are supported but there is no particular advantage in using them with

How to use 128 bit integers in Cython

£可爱£侵袭症+ 提交于 2021-02-07 05:56:12
问题 On my 64 bit computer the long long type has 64 bits. print(sizeof(long long)) # prints 8 I need to use 128 bit integers and luckily GCC supports these. How can I use these within Cython? The following doesn't work. Compiling foo.pyx containing just cdef __int128_t x = 0 yields $ cython foo.pyx Error compiling Cython file: ------------------------------------------------------------ ... cdef __int128_t x = 0 ^ ------------------------------------------------------------ foo.pyx:2:5: '__int128