Numpy dot product very slow using ints

后端 未结 2 1937
一生所求
一生所求 2020-12-30 04:43

sorry for so many questions. I am running Mac OSX 10.6 on Intel core 2 Duo. I am running some benchmarks for my research and I have run into another thing that baffles me. <

2条回答
  •  不知归路
    2020-12-30 05:00

    Using int vs float data types causes different code paths to be executed:

    The stack trace for float looks like this:

    (gdb) backtr
    #0  0x007865a0 in dgemm_ () from /usr/lib/libblas.so.3gf
    #1  0x007559d5 in cblas_dgemm () from /usr/lib/libblas.so.3gf
    #2  0x00744108 in dotblas_matrixproduct (__NPY_UNUSED_TAGGEDdummy=0x0, args=(, ), 
    kwargs=0x0) at numpy/core/blasdot/_dotblas.c:798
    #3  0x08088ba1 in PyEval_EvalFrameEx ()
    ...
    

    ..while the stack trace for int looks like this:

    (gdb) backtr
    #0  LONG_dot (ip1=0xb700a280 "\t", is1=4, ip2=0xb737dc64 "\a", is2=4000, op=0xb6496fc4 "", n=1000, __NPY_UNUSED_TAGGEDignore=0x85fa960)
    at numpy/core/src/multiarray/arraytypes.c.src:3076
    #1  0x00659d9d in PyArray_MatrixProduct2 (op1=, op2=, out=0x0)
    at numpy/core/src/multiarray/multiarraymodule.c:847
    #2  0x00742b93 in dotblas_matrixproduct (__NPY_UNUSED_TAGGEDdummy=0x0, args=(, ), 
    kwargs=0x0) at numpy/core/blasdot/_dotblas.c:254
    #3  0x08088ba1 in PyEval_EvalFrameEx ()
    ...
    

    Both calls lead to dotblas_matrixproduct, but it appears that the float call stays in the BLAS library (probably accessing some well-optimized code), while the int call gets kicked back out to numpy's PyArray_MatrixProduct2.

    So this is either a bug or BLAS just doesn't support integer types in matrixproduct (which seems rather unlikely).

    Here's an easy and inexpensive workaround:

    af = a.astype(float)
    np.dot(af, af).astype(int)
    

提交回复
热议问题