Roy
Roy

Reputation: 43

Cython int ** and int * types

I am trying to wrap some C code with Cython, but I am running into a error that I don't understand, and despite a lot of searching I cannot seem to find anything on it. Here is my c code

void cssor(double *U, int m, int n, double omega, double tol, int maxiters, int *info){
double maxerr, temp, lcf, rcf;
int i, j, k;
lcf = 1.0 - omega;
rcf = 0.25 * omega;
for (k =0; k < maxiters ; k ++){
    maxerr = 0.0;
    for (j =1; j < n-1; j++) {
        for (i =1; i < m-1; i++) {
            temp = U[i*n+ j];
            U[i*n+j] = lcf * U[i*n+j] + rcf * (U[i*n+j-1] + U [i*n+j+1] + U [(i-1)*n + j] + U [(i+1)*n+j]);
            maxerr = fmax(fabs(U[i*n+j] - temp), maxerr);
        }
    }
    if(maxerr < tol){break;}
}
if (maxerr < tol) {*info =0;}
else{*info =1;}

}

My .pyx file is

    cdef extern from "cssor.h":
        void cssor(double *U, int m, int n, double omega, double tol, int maxiters, int *info)

    cpdef cyssor(double[:, ::1] U, double omega, double tol, int maxiters, int *info):
        cdef int n, m
        m = U.shape[0]
        n = U.shape[1]
        cssor(&U[0, 0], m, n, omega, tol, maxiters, &info)

However, when I try to run the associated setup file I get an error referring to maxiters in the last line of the code that says:

Cannot assign type 'int **' to type 'int *'

Can you tell me how to fix this?

Roy Roth

Upvotes: 1

Views: 1321

Answers (1)

Serdalis
Serdalis

Reputation: 10489

The problem comes from here:

cpdef cyssor(double[:, ::1] U, double omega, double tol, int maxiters, int *info):
    cdef int n, m
    m = U.shape[0]
    n = U.shape[1]
    cssor(&U[0, 0], m, n, omega, tol, maxiters, &info)

You declare info as type int*. But you then pass it into the cssor function as a reference to an int*, making it an int**.

The correct code is:

cssor(&U[0, 0], m, n, omega, tol, maxiters, info)

Upvotes: 4

Related Questions