user3596749
user3596749

Reputation: 33

Python weave to speed up our code

We would really appreciate any kind of help, because we are driving crazy with our program making it faster using C language.

The values obtained don't change, always are 0,0,0,0

Here is the code, running in Linux:

from scipy import weave

pasa = 0
coorX =  -11.8
coorY = -7.9
INC=0.01296
##def weave_update():

code="""
int i,j, pasa;
double coorX, coorY,INC;

for (i=0; i < 1296;i++){

    yminf = coorY + INC*(i);
    ymaxf = yminf + INC;

    for (j=0; j < 1936;j++){

        xminc = coorX + INC*(j); 
        xmaxc = xminc + INC;
        pasa = 1;
        break;
    }
    if (pasa == 1){
    break;
    }
}
"""
weave.inline(code,['yminf','xminc','xmaxc','ymaxf'],type_converters=weave.converters.blitz,compiler='gcc')
print yminf,xminc,xmaxc,ymaxf

Upvotes: 1

Views: 126

Answers (1)

perimosocordiae
perimosocordiae

Reputation: 17847

Looks like two issues. First, you need to pass in all of the variables that the C code needs access to from python. So, your inline call needs to be:

weave.inline(code, ['coorX','coorY','INC'])

Secondly, you need to return the values you want from the weave code, because modifying them in C doesn't affect their value in Python. Here's one way to do it:

py::tuple ret(4);
ret[0] = yminf;
ret[1] = xminc;
ret[2] = xmaxc;
ret[3] = ymaxf;
return_val = ret;

With these modifications, the following file seems to work correctly:

from scipy import weave

coorX = -11.8
coorY = -7.9
INC = 0.01296

code="""
int i,j, pasa = 0;
double yminf,xminc,xmaxc,ymaxf;

for (i=0; i < 1296;i++){

    yminf = coorY + INC*(i);
    ymaxf = yminf + INC;

    for (j=0; j < 1936;j++){

        xminc = coorX + INC*(j);
        xmaxc = xminc + INC;
        pasa = 1;
        break;
    }
    if (pasa == 1){
    break;
    }
}
py::tuple ret(4);
ret[0] = yminf;
ret[1] = xminc;
ret[2] = xmaxc;
ret[3] = ymaxf;
return_val = ret;
"""
yminf,xminc,xmaxc,ymaxf = weave.inline(code,['coorX','coorY','INC'])
print yminf,xminc,xmaxc,ymaxf

Upvotes: 2

Related Questions