How do you cast an "int" to an "int*" array in a .pyx file?

404 views
Skip to first unread message

thomas...@gmail.com

unread,
Dec 16, 2017, 1:31:08 AM12/16/17
to cython-users
I'm trying to use Cython to call some CUDA code. What I want to do is first use regular Python to copy data to the GPU, and then pass pointers to the data to a Cython method, which will then call some CUDA code to work on the data.

I'm having trouble figuring out how to cast an "int" representing the memory address of the data that is on the GPU to an "int*" array in Cython. Here is what I have in my .pyx file now:

import numpy as np
import ctypes

cimport numpy as np


cdef extern from './solve_sparse_system.h':
void solve_sparse_system_cg(int *device_I, int *device_J, float *device_V, float *device_X, float *device_B) nogil


def solve_sparse_system(np.ndarray[np.int, ndim=1] device_I,
np.ndarray[np.int, ndim=1] device_J,
np.ndarray[np.float32_t, ndim=1] device_V,
np.ndarray[np.float32_t, ndim=1] device_X,
np.ndarray[np.float32_t, ndim=1] device_B):

solve_sparse_system_cg(&device_I[0], &device_J[0], &device_V[0], &device_X[0], &device_B[0])




When I try to compile this I get two errors saying " Cannot take address of Python object" for the "device_I" and "device_J" arguments on the last line. Since I don't get compile errors for the arrays that consist of floats, I think the problem is that there is some issue with the "int" datatype. How can I convert the "int" representing the address of the data into an "int*" here?

thomas...@gmail.com

unread,
Dec 20, 2017, 2:36:54 AM12/20/17
to cython-users
I was able to find a workaround by simply passing the address as a "long long" straight through the Cython code and then casting it to the appropriate pointer type in the CUDA code. For example like this:

int *device_I = (int*)device_I_address;


Reply all
Reply to author
Forward
0 new messages