cython

Singleton is not working in Cython

我与影子孤独终老i 提交于 2019-12-23 17:19:12
问题 This is how i define Singleton. class Singleton(type): _instances = {} def __call__(cls, *args, **kwargs): if cls not in cls._instances: cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) return cls._instances[cls] Then I have my classes defined as: class MyClass(object): __metaclass__ = Singleton def __init__(self): pass a = MyClass() b = MyClass() "a is b" will return True However, cdef class MyCythonClass(object): __metaclass__ = Singleton def __cinit__(self): pass c =

Cython: have sequence of Extension Types as attribute of another Extension Type with access to cdef methods

半世苍凉 提交于 2019-12-23 16:53:33
问题 Suppose I defined the following Cython class cdef class Kernel: cdef readonly double a def __init__(self, double a): self.a = a cdef public double GetValue(self, double t): return self.a*t Now I'd like to define another Extension Type that has a sequence of Kernels as attribute. Something like: cdef class Model: cdef readonly Kernel[:] kernels cdef unsigned int n_kernels def __init__(self, Kernel[:] ker): self.kernels = ker self.n_kernels = ker.shape[0] cdef double Run(self, double t): cdef

Optimizing point - circle distance method

浪子不回头ぞ 提交于 2019-12-23 16:51:23
问题 I'm implementing a RANSAC algorithm for circle detection in images. I profiled the execution and I get: 13699392 function calls in 799.981 seconds Random listing order was used ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 {time.time} 579810 0.564 0.000 0.564 0.000 {getattr} 289905 2.343 0.000 8.661 0.000 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/linalg/blas.py:226(_get_funcs) 579810 0.124 0.000 0

How to compile my python code in cython with external python libs like pybrain

瘦欲@ 提交于 2019-12-23 16:30:06
问题 I need more perfomance running my neural network, so I thinked that building it with cython will be good idea. I am building my code like this: from distutils.core import setup from Cython.Build import cythonize setup( ext_modules = cythonize("my_code.pyx") ) But will it build external python files that I use? Like pybrain, skimage and PIL in my case. If not, how to force cython to build them. 回答1: No, external python files will not be cythonized and compiled unless you specifically add them

Cython - Memoryview of a dynamic 2D C++Array

空扰寡人 提交于 2019-12-23 15:55:04
问题 The Goal: Get a Memoryview from a 2D C++ char array using Cython. A little background: I have a native C++ library which generates some data and returns it via a char** to the Cython world. The array is initialized and operated in the library about like this: struct Result_buffer{ char** data_pointer; int length = 0; Result_buffer( int row_capacity) { data_pointer; = new char*[row_capacity]; return arr; } // the actual data is appended row by row void append_row(char* row_data) { data_pointer

How to call a multi-threaded C function in Cython?

百般思念 提交于 2019-12-23 13:27:26
问题 I have a question about how to call a multi-threaded C function in Cython. Do I need to release/acquire the GIL before/after I do the multi-threaded stuff in the C function? Or can I just use it like a normal C function? Should I follow the directions here for general Python extensions? 回答1: You should have looked down a few sections. http://docs.python.org/c-api/init.html#non-python-created-threads 来源: https://stackoverflow.com/questions/11687960/how-to-call-a-multi-threaded-c-function-in

I've mangled Cython badly, it's performing worse than pure Python. Why?

不问归期 提交于 2019-12-23 13:01:00
问题 I'm rather new to Python and absolutely ignorant of C (unfortunately) so I am struggling to properly understand some aspects of working with Cython. After profiling a Python program and discovering that it was just a couple of loops that were hogging most of the time, I decided to look into dumping them into Cython. Initially, I just let Cython interpret the Python as it was, and the result was a (remarkable!) ~2x speed boost. Cool! From the Python main, I pass the function two 2-D arrays ("a

Prepare C-based Cython package to publish on pypi

☆樱花仙子☆ 提交于 2019-12-23 12:29:31
问题 I'm going to create C library and I would like to create Python wrapper using Cython. Right now I have mylib.a file compiled and bundled (C files) and I want to wrap methods from my library in Cython. I successfully created .pyx and .pxd files and I can build it using python setup.py build_ext command. My problem appears when I try publish it on pypi. If I run my setup.py , create wheel and publish it I can download it from pypi - but I can't run import mylib . I read a lot online tutorials

cython function output slightly different from python function output

﹥>﹥吖頭↗ 提交于 2019-12-23 12:08:35
问题 I have converted a python function into a cython equivalent by adding types to some variables. However, the cython function to produces slightly different output than the original python function. I've learnt some of the reasons for this difference in this post Cython: unsigned int indices for numpy arrays gives different result But even with what I've learnt in this post I still can't get the cython function to produce the same results as the python one. So I have put together 4 functions

Auto Conversion of Structs to Dicts in Cython

醉酒当歌 提交于 2019-12-23 12:07:34
问题 So, if you've got a header file. %%file test.h struct mystruct{ int i; int j; }; And then you wrap it in Cython: cdef extern from "test.h" nogil: struct mystruct: int i int j And some function that returns back out to Py: def spit_out_dict(): return mystruct(5,10) Cython correctly automatically generates a dict wrapper. However, when I wrap the original C header in a namespace, I haven't been able to get get Cython to still generate the dict wrapper correctly, something along these lines: %