I was trying to remove a bottleneck by converting a portion of Python code into C++ via scipy.weave.inline, and encountered a bug. Here is my code:
#!/usr/bin/env python2.6
import numpy as np
from scipy import weave
from scipy.weave import converters
def compute_unique_pair_dists(xs, ys, ds):
code = r'''
int n = xs.size();
int idx = 0;
for (int i = 0; i < n; ++i) {
float x = xs(i);
float y = ys(i);
for (int j = 0; j < n - 1 - i; ++j) {
float dx = xs(i+1+j) - x;
float dy = ys(i+1+j) - y;
ds(idx++) = sqrt(dx * dx + dy * dy);
}
}
'''
weave.inline(code, ['xs', 'ys', 'ds'], type_converters=converters.blitz,
compiler='gcc', verbose=2)
def main():
n = 10
xs = np.arange(0, n)
ys = np.arange(0, n)
ds = np.zeros(n * (n + 1) / 2)
compute_unique_pair_dists(xs, ys, ds)
print(ds)
if __name__ == '__main__':
main()
And this is what I get:
running build_ext
running build_src
build_src
building extension "sc_1eb4d019edb4774720031f15557074a811" sources
build_src: building npy-pkg config files
customize UnixCCompiler
customize UnixCCompiler using build_ext
customize UnixCCompiler
customize UnixCCompiler using build_ext
building 'sc_1eb4d019edb4774720031f15557074a811' extension
compiling C++ sources
C compiler: g++ -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -fPIC
compile options: '-I/usr/lib/python2.6/dist-packages/scipy/weave -I/usr/lib/python2.6/dist-packages/scipy/weave/scxx -I/usr/lib/python2.6/dist-packages/scipy/weave/blitz -I/usr/lib/pymodules/python2.6/numpy/core/include -I/usr/include/python2.6 -c'
g++: /home/taro/.python26_compiled/sc_1eb4d019edb4774720031f15557074a811.cpp
In file included from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/applics.h:394,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/vecexpr.h:26,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/vecpick.cc:16,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/vecpick.h:287,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/vector.h:443,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/tinyvec.h:424,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/array-impl.h:38,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/array.h:26,
from /home/taro/.python26_compiled/sc_1eb4d019edb4774720031f15557074a811.cpp:11:
/usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/mathfunc.h: In static member function ‘static long int blitz::_bz_abs::apply(long int)’:
/usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/mathfunc.h:45: error: ‘labs’ is not a member of ‘std’
In file included from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/array/funcs.h:23,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/array/newet.h:23,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/array/et.h:21,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/array-impl.h:2509,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/array.h:26,
from /home/taro/.python26_compiled/sc_1eb4d019edb4774720031f15557074a811.cpp:11:
/usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/funcs.h: In static member function ‘static int blitz::Fn_abs::apply(int)’:
/usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/funcs.h:503: error: call of overloaded ‘abs(int&)’ is ambiguous
/usr/include/c++/4.4/cmath:94: note: candidates are: double std::abs(double)
/usr/include/c++/4.4/cmath:98: note: float std::abs(float)
/usr/include/c++/4.4/cmath:102: note: long double std::abs(long double)
/usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/funcs.h: In static member function ‘static long int blitz::Fn_abs::apply(long int)’:
/usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/funcs.h:524: error: ‘labs’ is not a member of ‘std’
In file included from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/applics.h:394,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/vecexpr.h:26,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/vecpick.cc:16,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/vecpick.h:287,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/vector.h:443,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/tinyvec.h:424,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/array-impl.h:38,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/array.h:26,
from /home/taro/.python26_compiled/sc_1eb4d019edb4774720031f15557074a811.cpp:11:
/usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/mathfunc.h: In static member function ‘static long int blitz::_bz_abs::apply(long int)’:
/usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/mathfunc.h:45: error: ‘labs’ is not a member of ‘std’
In file included from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/array/funcs.h:23,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/array/newet.h:23,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/array/et.h:21,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/array-impl.h:2509,
from /usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/array.h:26,
from /home/taro/.python26_compiled/sc_1eb4d019edb4774720031f15557074a811.cpp:11:
/usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/funcs.h: In static member function ‘static int blitz::Fn_abs::apply(int)’:
/usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/funcs.h:503: error: call of overloaded ‘abs(int&)’ is ambiguous
/usr/include/c++/4.4/cmath:94: note: candidates are: double std::abs(double)
/usr/include/c++/4.4/cmath:98: note: float std::abs(float)
/usr/include/c++/4.4/cmath:102: note: long double std::abs(long double)
/usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/funcs.h: In static member function ‘static long int blitz::Fn_abs::apply(long int)’:
/usr/lib/python2.6/dist-packages/scipy/weave/blitz/blitz/funcs.h:524: error: ‘labs’ is not a member of ‘std’
Traceback (most recent call last):
File "bug.py", line 34, in
main()
File "bug.py", line 29, in main
compute_unique_pair_dists(xs, ys, ds)
File "bug.py", line 22, in compute_unique_pair_dists
compiler='gcc', verbose=2)
File "/usr/lib/python2.6/dist-packages/scipy/weave/inline_tools.py", line 335, in inline
**kw)
File "/usr/lib/python2.6/dist-packages/scipy/weave/inline_tools.py", line 462, in compile_function
verbose=verbose, **kw)
File "/usr/lib/python2.6/dist-packages/scipy/weave/ext_tools.py", line 365, in compile
verbose = verbose, **kw)
File "/usr/lib/python2.6/dist-packages/scipy/weave/build_tools.py", line 288, in build_extension
raise e
scipy.weave.build_tools.CompileError: error: Command "g++ -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -fPIC -I/usr/lib/python2.6/dist-packages/scipy/weave -I/usr/lib/python2.6/dist-packages/scipy/weave/scxx -I/usr/lib/python2.6/dist-packages/scipy/weave/blitz -I/usr/lib/pymodules/python2.6/numpy/core/include -I/usr/include/python2.6 -c /home/taro/.python26_compiled/sc_1eb4d019edb4774720031f15557074a811.cpp -o /tmp/taro/python26_intermediate/compiler_8430959729cb93b29ebb911a70f70625/home/taro/.python26_compiled/sc_1eb4d019edb4774720031f15557074a811.o" failed with exit status 1
Fortunately there is a fix:
The fix for this bug is simple. As per the debian patch linked to below, one needs to add an include for cstdlib above the BZ_NAMESPACE(blitz) call in the following files:
/usr/share/pyshared/scipy/weave/blitz/blitz/blitz.h
/usr/share/pyshared/scipy/weave/blitz/blitz/mathfunc.hhttp://bugs.debian.org/cgi-bin/bugreport.cgi?msg=10;filename=blitz%2B%2B.patch;att=1;bug=455661
This fix works for me by manually editing those files.
The bug appears to be reported a couple years ago but hadn’t been fixed for some reason.