0

First, I know this question appears similar to this one but they are different. I'm struggling trying to pass int (int32) numpy array to C++ via Cython without copying. The files:

doit.cpp:

#include "doit.h"
void run(int *x) {}

doit.h:

#ifndef _DOIT_H_
#define _DOIT_H_
void run(int *);
#endif

q.pyx:

cimport numpy as np
import numpy as np

cdef extern from "doit.h":
    void run(int* X)

def pyrun(np.ndarray[np.int_t, ndim=1] X):
    X = np.ascontiguousarray(X)
    run(&X[0])

I compile with Cython. The error is:

Error compiling Cython file:
------------------------------------------------------------
...
cdef extern from "doit.h":
    void run(int* X)

def pyrun(np.ndarray[np.int_t, ndim=1] X):
    X = np.ascontiguousarray(X)
    run(&X[0])
       ^
------------------------------------------------------------

py_cpp/q.pyx:9:8: Cannot assign type 'int_t *' to 'int *'

However, if I replace all occurrences of int to double (e.g. int *x to double *x, int_t to double_t), then all errors are gone.

How to solve the problem? Thanks in advance.

2
  • 1
    int_t maps to long which is 8bytes on Linux but 4bytes on Windows. It is better to use np.int32_t which would be the same on all platforms Commented Jun 2, 2022 at 5:29
  • See also stackoverflow.com/a/72471220/5769463 Commented Jun 2, 2022 at 5:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.