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.

Kevin
  • 143
  • 1
  • 7
  • 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 – ead Jun 02 '22 at 05:29
  • See also https://stackoverflow.com/a/72471220/5769463 – ead Jun 02 '22 at 05:51

0 Answers0