cython

Cython C-level interface of package: *.pxd files are not found

依然范特西╮ 提交于 2020-02-04 01:46:38
问题 In a nutshell I try to compile a cython extension called extension2 that cimports a file extension from a self-created package. When building extension2 , I get the error that extension.pxd is not found though this file is exactly at the sepcified path. Details I am building two packages involving cython, a package A and a package B that depends on A . A is a subpacke of a namespace package nsp . That is, the folder structure looks as follows: ├── nsp │ └── A | ├── extension.pxd | ├──

reload module with pyximport?

风流意气都作罢 提交于 2020-01-31 15:23:06
问题 I have a python program that loads quite a bit of data before running. As such, I'd like to be able to reload code without reloading data. With regular python, importlib.reload has been working fine. Here's an example: setup.py: from distutils.core import setup from distutils.extension import Extension from Cython.Build import cythonize extensions = [ Extension("foo.bar", ["foo/bar.pyx"], language="c++", extra_compile_args=["-std=c++11"], extra_link_args=["-std=c++11"]) ] setup( name="system2

Using Cython to expose functionality to another application

╄→尐↘猪︶ㄣ 提交于 2020-01-31 03:59:07
问题 I have this C++ code that shows how to extend a software by compiling it to a DLL and putting it in the application folder: #include <windows.h> #include <DemoPlugin.h> /** A helper function to convert a char array into a LPBYTE array. */ LPBYTE message(const char* message, long* pLen) { size_t length = strlen(message); LPBYTE mem = (LPBYTE) GlobalAlloc(GPTR, length + 1); for (unsigned int i = 0; i < length; i++) { mem[i] = message[i]; } *pLen = length + 1; return mem; } long __stdcall

time.time() not working to run while loop for predetermined time in Cython

痞子三分冷 提交于 2020-01-30 04:59:09
问题 I had a Python module which included a while loop which was supposed to run for a fixed amount of time. I did this by adding a constant to the output of time.time() and running until time.time() was greater than that variable. This did not present any issues, but the same thing is not working for me in Cython. Now I'm getting wildly off timings. Just for a minimal example demonstrating this: import time cdef float wait_time = 3 def slow(): cdef float end_time = time.time() + wait_time while

Using typedef'd struct in cython

会有一股神秘感。 提交于 2020-01-25 04:25:11
问题 I have the following definition in the header file dcm.h: typedef struct { double alpha; double gamma; double tau; } ThetaDCM; I want to import it in cython, so I have: cdef extern from "dcm.h": ctypedef struct ThetaDCM: np.float64_t alpha np.float64_t gamma np.float64_t tau Now I want to allocate memory to an array of ThetaDCM's. I have the following: cdef ThetaDCM *c_theta = <ThetaDCM *> malloc(nt * nb * sizeof(ThetaDCM)) free(c_theta) This did not compile and reported the following error:

Having thread-local arrays in cython so that I can resize them?

情到浓时终转凉″ 提交于 2020-01-25 02:46:04
问题 I have an interval-treeish algorithm I would like to run in parallel for many queries using threads. Problem is that then each thread would need its own array, since I cannot know in advance how many hits there will be. There are other questions like this, and the solution suggested is always to have an array of size (K, t) where K is output length and t is number of threads. This does not work for me as K might be different for each thread and each thread might need to resize the array to

Cython example with more than one class

孤者浪人 提交于 2020-01-24 22:21:27
问题 I ask your help, because I'm trying to run a cython example a bit more complex than the one with one class that is possible to find in many tutorials (e.g. this guide ). I haven't found any "more advanced" tutorial, so I hope this question will be useful also for people that are trying to learn it a bit more in depth. I will write here the steps I took, hoping that someone will tell me where is my mistake. I have a Rectangle c++ class (I put here just the .h file to make it shorter): #ifndef

Combining Cython with MKL

微笑、不失礼 提交于 2020-01-23 18:13:06
问题 I want to use some functions of the MKL-library in my cython-code. Thus I wrote the function #include <stdlib.h> #include <complex.h> #include <stdbool.h> #define MKL__Complex16 double _Complex #include <mkl.h> #include <mkl_cblas.h> #include <mkl_blas.h> #include <mkl_lapack.h> #include <mkl_lapacke.h> inline void scalarMult(const double _Complex *a, const int a_len, double _Complex *b, const int b_len, const double z) { if(a_len != b_len) return; if(z == 1) memcpy(b, a, a_len); else cblas

Cython conditional compile based on external value

冷暖自知 提交于 2020-01-22 16:28:50
问题 I try to conditionally compile (or generate) to c code from a Cython pxd. I read that I can DEF to define aa value and IF to conditionally generate based on its value, but how can I get this value to get from outside of the pxd file? Specifically these two cases are interesting for me now: give some command-line define to Cython, preferrably through the Cython.Distutils setuptools way the extern-ed C header file defines some value, and conditionally define using Cython based on this value

How to apply decorators to Cython cpdef functions

﹥>﹥吖頭↗ 提交于 2020-01-22 12:34:10
问题 I've been playing around with Cython lately and I came across this error when applying a decorator to a Cython function Cdef functions/classes cannot take arbitrary decorators Here is the code I was tinkering with: import functools def memoize(f): computed = {} @functools.wraps(f) def memoized_f(main_arg, *args, **kwargs): if computed.get(main_arg): return computed[main_arg] computed[main_arg] = f(main_arg, *args, **kwargs) return computed[main_arg] return memoized_f @memoize cpdef int fib