Chiel ten Brinke | 17 Nov 16:31 2014
Picon

Call gcc builtin in cython?

Hello everyone,

I want to call the gcc builtin function `__builtin_ffs` inside a cython sourcefile (`.pyx`), e.g. like

    cpdef int ffs(long b):
       
return __builtin_ffs(b);


Is it possible? If so, how?


Kind regards,

Chiel

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Martin Bammer | 15 Nov 18:38 2014
Picon

Why so complicated?

Hi,

when I look into the code generated by Cython I'm wondering why it is so complicated and bloated.

As an example please have a look at the code below:

static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int(int value) {
    const int neg_one = (int) -1, const_zero = 0;
    const int is_unsigned = neg_one > const_zero;
    if (is_unsigned) {
        if (sizeof(int) < sizeof(long)) {
            return PyInt_FromLong((long) value);
        } else if (sizeof(int) <= sizeof(unsigned long)) {
            return PyLong_FromUnsignedLong((unsigned long) value);
        } else if (sizeof(int) <= sizeof(unsigned long long)) {
            return PyLong_FromUnsignedLongLong((unsigned long long) value);
        }
    } else {
        if (sizeof(int) <= sizeof(long)) {
            return PyInt_FromLong((long) value);
        } else if (sizeof(int) <= sizeof(long long)) {
            return PyLong_FromLongLong((long long) value);
        }
    }
    {
        int one = 1; int little = (int)*(unsigned char *)&one;
        unsigned char *bytes = (unsigned char *)&value;
        return _PyLong_FromByteArray(bytes, sizeof(int),
                                     little, !is_unsigned);
    }
}

IMHO all the code above can be reduced to just 1 line if Cython would evaluate the sizeof and if instructions at compile time.
I think the code size, compile time and run time would get reduced.

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Kaushik Ghose | 14 Nov 20:26 2014
Picon

__Pyx_RefNannySetupContext

Hi Folks,

In Cython generated c source what does the __Pyx_RefNannySetupContext and __Pyx_RefNannyDeclarations do?

I was looking at a simple cdef-d function:

cdef int add(int x, int y):
  return x + y

cdef int zip_add(int *x, int *y, int l):
  cdef int s = 0
  while l > 0:
    s += add(x[l], y[l])
    l -= 1

And the add function is translated to:

static int __pyx_f_2v1_add(int __pyx_v_x, int __pyx_v_y) {
  int __pyx_r;
  __Pyx_RefNannyDeclarations
  __Pyx_RefNannySetupContext("add", 0);

  /* "v1.pyx":2
 * cdef int add(int x, int y):
 *   return x + y             # <<<<<<<<<<<<<<
 * 
 * cdef int zip_add(int *x, int *y, int l):
 */
  __pyx_r = (__pyx_v_x + __pyx_v_y);
  goto __pyx_L0;

  /* "v1.pyx":1
 * cdef int add(int x, int y):             # <<<<<<<<<<<<<<
 *   return x + y
 * 
 */

  /* function exit code */
  __pyx_L0:;
  __Pyx_RefNannyFinishContext();
  return __pyx_r;

It would appear to me that the RefNanny context is an unnecessary overhead in this case. Is there an option to get rid of it?

many thanks
-Kaushik

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Yuxiang Wang | 14 Nov 17:32 2014
Picon

Which gcc is used?

Hi all,

I have a probably stupid question wrt. which gcc is used.

In Win 7 64-bit, I have two mingw installed, and I can check which will be called first using the `where` command:


X:\WorkFolder\DataAnalysis\Learn\LearnCython>where gcc
X:\WinPython3\python-3.4.2.amd64\Scripts\gcc.bat
X:\WinPython3\tools\mingw32\bin\gcc.exe
c:\cygwin64\bin\gcc.exe


So, it seems like X:\WinPython3\python-3.4.2.amd64\Scripts\gcc.bat will be used, because it is earlier in the PATH.

However, when I use the distutils to compile my Cython code, the second got used:

X:\WorkFolder\DataAnalysis\Learn\LearnCython>python setup_helloworld.py build_ext --inplace
Compiling helloworld.pyx because it changed.
Cythonizing helloworld.pyx
running build_ext
building 'helloworld' extension
X:\WinPython3\python-3.4.2.amd64\..\tools\mingw32\bin\gcc.exe -mdll -O -DMS_WIN64 -Wall -IX:\WinPython3\python-3.4.2.amd64\include -IX:\WinPython3\python-3.4.2.amd64\include -c helloworld.c -o build\temp.win-amd64-3.4\Release\helloworld.o
writing build\temp.win-amd64-3.4\Release\helloworld.def
X:\WinPython3\python-3.4.2.amd64\..\tools\mingw32\bin\gcc.exe -shared -s build\temp.win-amd64-3.4\Release\helloworld.o build\temp.win-amd64-3.4\Release\helloworld.def -LX:\WinPython3\python-3.4.2.amd64\libs -LX:\WinPython3\python-3.4.2.amd64\PCbuild\amd64 -lpython34 -lmsvcr100 -o X:\WorkFolder\DataAnalysis\Learn\LearnCython\helloworld.pyd


Could anyone please give me a hint...? Is it that gcc.exe got higher priority compared to gcc.bat?

Shawn





--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jonathan Buck | 14 Nov 16:34 2014
Picon

Cython partial differentiation

I have a python script where, as part of an evolutionary optimization algorithm, I'm evaluating partial derivatives many thousands of times. I've done a line by line profile, and this partial derivative calculation is taking up the majority of the run time. I'm using scipy.optimize.approx_fprime to calculate the partial derivatives, and I tried to rewrite it in cython without much success.

The line by line profile is below. My cythonized version of scipy.optimize.approx_fprime is simply called approx_fprime.

Line # Hits Time Per Hit % Time Line Contents ============================================================== 84 <at> profile 100 1500 14889652 9926.4 25.3 df1 = approx_fprime(inp_nom,evaluate1,epsilon) 101 1500 14939889 9959.9 25.4 df2 = scipy.optimize.approx_fprime(inp_upp,evaluate1,epsilon)

Below is my cython file.

import numpy as np cimport numpy as np cimport cython <at> cython.boundscheck(False) # turn of bounds-checking for entire function def approx_fprime(np.ndarray xk, f, double epsilon, *args): # From scipy.optimize.approx_fprime f0 = f(*((xk,) + args)) cdef np.ndarray grad = np.zeros((len(xk),), float) cdef np.ndarray ei = np.zeros((len(xk),), float) cdef np.ndarray d = epsilon * ei for k in xrange(len(xk)): ei[k] = 1.0 grad[k] = (f(*((xk + d,) + args)) - f0) / d[k] ei[k] = 0.0 return grad

I've tried to put in all the relevant type declarations and ensure that it plays nicely with numpy. Ultimately, though, the proof is in the pudding, as they say. This version is just not really any faster than the scipy version. The function only has a few variables, so it's not a huge computation and there's probably only room for an incremental improvement in one iteration. However, the function gets called over and over because this is used in an evolutionary optimization algorithm, and so I'm expecting/hoping that an incremental performance gain multiplied many times over will have a big payoff.

Could a cython expert out there take a look at this code and help me figure out if I'm on the right track, or is this a case where there's just not much performance to be gained?

Thank you!

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
1989lzhh | 14 Nov 07:40 2014
Picon

Can we remove the cdef keyword from definition statement?

hi,
I find remove "cdef" from definition statement will not cause confusion. It will be c syntax and make the
definition more clean. Can anybody tell me why we need cdef?

Regards,
Liu zhenhai

--

-- 

--- 
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hai Nguyen | 13 Nov 01:32 2014
Picon

Changing C++ template class's name

Hi everyone,

I am trying to wrap a C++ library for Python. One of cpp classes has template and I want to change its' name to new name (like "vector" to "_vector"). 

I was trying to follow the class template in this link (http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html) and just changed "cdef cppclass _vector[T] "vector[T]".

However I got this error: 

cdef extern from "<vector>" namespace "std":
    cdef cppclass _vector[T] "vector[T]":
                            ^
------------------------------------------------------------

test_vector_template_2.pyx:4:29: Syntax error in C++ class definition

Doesn't Cython support this (yet)? And if "No", can you please tell me how to overcome?

Best
Hai

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
George Coles | 12 Nov 16:06 2014
Picon

defining extension type using cython.inline?

Hi,
  I want to create a cpdef class that corresponds to a row of a pandas dataframe, or a csv file. It should be accessible from python or cython. So the definition might look like this:

cdef class Foo:
   cpdef public int bar
   

I tried to create this in python code, using cython.inline:

str = """
cdef class foo:
    cdef public int bar
"""
res = Cython.inline(str)

I get an error that says Error Compiling Cython file:

...
    cdef public int bar

def __invoke():

then a path to an actual file on my c: drive that contains the definition in my string, with legal indentation, but then at the bottom of the file, 
def __invoke() at a "zero" level of indentation. 

Is my attempt to define an extension type at runtime doomed? 

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Espen | 11 Nov 20:29 2014
Picon

Vector STL versus more traditional interface to C/C++

I hope some of you can give me some pointers. I discovered it was possible to use a STL vector interface from this post:

http://blog.birving.com/2014/05/passing-numpy-arrays-between-python-and.html

I've been using the more traditional way (defined later in the example), but making a multidimensional interface is not so straightforward. I like the STL vectors and would like to use this more exclusively in the future when I need some work done in C++ or when I need to link inn code that runs the STL vectors. Tried to follow the example above, however it does not seem the data is not returned properly (or assigned) from interface.cpp using the STL vector interface. I suspect it is not as easy as I initially thought and put together a small example:

running python run.py yields:
vector: [0 0 0 0 0 0 0 0 0 0]
traditional: [1 0 0 0 0 0 0 0 0 0]


run.py:
import patchwork
import numpy as np
somearray=np.zeros(10,dtype='intc')
patchwork.python_function_vector(somearray)
print "vector:", somearray
somearray=np.zeros(10,dtype='intc')
patchwork.python_function_traditional(somearray)
print "traditional:", somearray


interface.cpp:
#include <vector>
void cpp_function_vector(std::vector<int> &);
void cpp_function_traditiona(int *);

void cpp_function_vector(std::vector<int> &somearray) {
        somearray[0]=1;
}

void cpp_function_traditional(int *somearray) {
        somearray[0]=1;
}


patchwork.pyx:
import cython
import numpy as np
cimport numpy as np
from libcpp.vector cimport vector

cdef extern from "interface.cpp":
        void cpp_function_vector(vector[int] &)
        void cpp_function_traditional(int *)

def python_function_vector(somearray not None):
        cpp_function_vector(somearray)

def python_function_traditional(np.ndarray[int,ndim=1,mode="c"] somearray not None):
        cpp_function_traditional(&somearray[0])

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
sulabh tiwari | 9 Nov 19:31 2014
Picon

how to use c code in cython

Hi guys i am new to cython and i am trying to use C code in cython
example.c


#include <stdlib.h>
#include <stdio.h>
#include <math.h>

double* reverseCoeffs(double* coeffs, int N)
{
    double *revCoeffs = (double*) malloc(7 * sizeof(double));

    if (N < 7)
    {
        coeffs = (double *) realloc(coeffs, 7 * sizeof(double));
        while (N < 7) coeffs[N++] = (double) 0;
    }

    // The first coefficient is the linear part
    revCoeffs[0] = 1 / coeffs[0];
    revCoeffs[1] = -coeffs[1] / pow(coeffs[0], 3);
    revCoeffs[2] = (2 * pow(coeffs[1], 2) - coeffs[0] * coeffs[2]) / pow(coeffs[0], 5);



    free(coeffs);

    return revCoeffs;
}

this works perfectly fine in C but when i use it in cython it throws an error
 example.pyx

import numpy as np
cimport numpy as np
cdef extern from "reverseCoeffs.c":
     double* reverseCoeffs(double* coeffs, int N)

def cisr(np.ndarray[np.double_t,ndim=1] x, int N):
    cdef double* x_data = <double *>x.data
    cdef np.ndarray[np.double_t, ndim=1] res
    res = reverseCoeffs(x_data, N)
    return res

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Wiktor Grębla | 9 Nov 18:04 2014
Picon

Mingw-w64 -municode and Cython on MS Windows.

Hi guys.

First of all, thanks for such a great tool.

I had an idea which finally led me to MS Windows and for a few days I was struggling with correct handling of Windows command line arguments and filenames. Generally it was a good thing as I went through lots of documentation but as it turned out, -municode flag added to gcc (mingw-w64) solved all my problems with UTF-16 Windows console. 

I've no intention to bother anybody and generally I don't care too much about MS Windows tools, but I've to admit that MSYS and Mingw-w64 is a great toolchain and it'd be worth to mention about this somwhere in Cython documentation:

http://sourceforge.net/p/mingw-w64/wiki2/Unicode%20apps/

Shortly speaking: mingw-gcc 4.8.1 doesn't have -municode flag in 32-bit release,  mingw-w64 supports it and int wmain(int argc, wchar_t **argv) generated by Cython's --embed option works like a charm and possibly a lot of unicode-related problems are solved as well.

Cheers,
W.

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gmane