简体   繁体   English

使用scipy.weave.inline将大整数从Python传递到C / C ++

[英]Passing large integers from Python to C/C++ with scipy.weave.inline

I found that a portion of my code written in python could be done faster in C. I used scipy.weave.inline to do this. 我发现用python编写的代码的一部分可以在C语言中更快地完成。我使用scipy.weave.inline来做到这一点。 seek_pos, one of the integers that I needed to pass into my C code, was (at times) larger than what could be represented by a 32 bit long. seek_pos是我需要传递到C代码中的整数之一,(有时)大于32位长所表示的整数。 I could cout seek_pos and get the correct value (maybe 2.3 billion) but when doing other things with it, such as using it as the offset in fseek or fseeko64, it would act as though it were -1.9 billion (or whatever value you'd get from wrapping past the range of positive long ints and around into the negative long ints). 我可以找到seek_pos并获得正确的值(也许是23亿美元),但是当用它来做其他事情时,例如将其用作fseek或fseeko64中的偏移量,它的作用就好像是-19亿美元(或任何其他值d可以绕开正长整数的范围,然后再绕到负长整数)。

MY WORKAROUND was to break the large integer down in python y=seek_pos/N, x=seek_pos%N, pass in those smaller numbers and rebuild the larger number in C, seek_pos_off = Y*N+X. 我的解决方法是在python y = seek_pos / N,x = seek_pos%N中分解较大的整数,传入较小的数字,然后在C中重建较大的数字,seek_pos_off = Y * N + X。 I"m new to both Weave and C/C++. My code works now but I think this is a pretty ridiculous way of getting there. Maybe I could have specified a premade or made a custom type converter for weave.inline, but how to do this was not clear to me. 我对Weave和C / C ++都是陌生的。我的代码现在可以运行,但是我认为这是到达那里的一种非常荒谬的方式。也许我可以为weave.inline指定一个预制的或定制的类型转换器,但是如何这对我来说还不清楚。

If someone can suggest a better way of doing this, I'd appreciate it, but if not, I wanted to post this anyway so that someone dealing with the same problem might at the very least find my work around when searching. 如果有人可以提出一种更好的方法,我将不胜感激,但是如果没有,我还是想发布此信息,以便处理相同问题的人至少可以在搜索时找到我的工作。

Here's the relevant portion of my code 这是我代码的相关部分

vtrace = numpy.zeros(len_trace, dtype='short')
c_code = '''
using namespace std;
const char * cc_fpath = filepath.c_str();
FILE * infile;
infile = fopen(cc_fpath, "r");
long seek_pos_off;
long long_multiplier;
long_multiplier = seek_pos_multiplier;
long long_adder;
long_adder = seek_pos_adder;
seek_pos_off = long_multiplier * 2000000000 + long_adder;
fseek(infile, seek_pos_off, SEEK_SET);
for (int n=0; n<len_trace; n++) {
    fread(vtrace+n, data_bytes_per_channel, 1, infile);
    fseek(infile, skip_bytes, SEEK_CUR);
}
fclose(infile);

return_val = 0;
'''
filepath = str(filepath)
seek_pos = int(data_start_pos_in_bytes + start_byte)
seek_pos_multiplier = seek_pos/2000000000
seek_pos_adder = seek_pos%2000000000
weave.inline(c_code, ['vtrace', 'filepath', 'seek_pos_multiplier', 
        'seek_pos_adder', 'len_trace', 'data_bytes_per_channel', 
        'skip_bytes'], headers=['<typeinfo>'])

Have you tried 你有没有尝试过

seek_pos = long(data_start_pos_in_bytes + start_byte)

instead of 代替

seek_pos = int(data_start_pos_in_bytes + start_byte)

That may be it. 可能就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM