使用numba来加速二进制处理

时间:2017-06-30 16:23:44

标签: numba

我有一个包含不同类型信息的二进制文件。我知道类型,并编写了一个解析脚本来提取信息:

import numba as nb 
import numpy as np

def fread(fid, nelements, dtype):
     if dtype is np.str:
         dt = np.uint8  # WARNING: assuming 8-bit ASCII for np.str!
     else:
         dt = dtype

     data_array = np.fromfile(fid, dt, nelements)
     data_array.shape = (nelements, 1)

     return data_array


fid = open('./binary_information.bts', 'rb');
nffc = 3
fileFmt = 'int16'


nz    = fread( fid, 1, np.int32);        # the number of grid points vertically, INT(4)
ny    = fread( fid, 1, np.int32);        # the number of grid points laterally, INT(4)

nPts        = ny*nz;
nv          = nffc*nPts;               # the size of one time step
velocity    = np.zeros([int(s) for s in (nt,nffc,ny,nz)])
for it in range(1, nt):
  ip = 1
  v_cnt = fread( fid, int(nv), fileFmt )
  for iz in range(1, nz):
    for iy in range(1, ny):
       for k in range(1, nffc):
          velocity[it,k,iy,iz] = ( v_cnt[ip] - Voffset[k])/Vslope[k]
          ip = ip + 1

我的for循环花费了大量时间。我想用numba加快速度。我试图这样做,并得到一个奇怪的错误,发布在下面。有什么方法可以加快numba的阅读过程吗?这里出了什么问题?为什么会引发这个range问题?

@nb.jit(nopython=True)
def main(velocity, v_cnt):
  ip = 1
  for iz in xrange(1, nz):
     for iy in xrange(1, ny):
        for k in xrange(1, nffc):
           velocity[it,k,iy,iz] = ( v_cnt[ip] - Voffset[k])/Vslope[k]
           ip = ip + 1
  return velocity

for it in range(nt):
  ip = 0
  v_cnt = fread( fid, int(nv), fileFmt )
  velocity = main(velocity, v_cnt)



Traceback (most recent call last):
  File "bts_read.py", line 66, in <module>
    velocity = main(velocity, v_cnt)
  File "/usr/local/lib/python2.7/site-packages/numba/dispatcher.py", line 330, in _compile_for_args
    raise e
numba.errors.TypingError: Caused By:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/numba/compiler.py", line 235, in run
    stage()
  File "/usr/local/lib/python2.7/site-packages/numba/compiler.py", line 449, in stage_nopython_frontend
    self.locals)
  File "/usr/local/lib/python2.7/site-packages/numba/compiler.py", line 805, in type_inference_stage
    infer.propagate()
  File "/usr/local/lib/python2.7/site-packages/numba/typeinfer.py", line 767, in propagate
    raise errors[0]
TypingError: Invalid usage of Function(<built-in function range>) with parameters (int64, readonly array(int32, 2d, C))
Known signatures:
 * (int32,) -> range_state_int32
 * (int32, int32) -> range_state_int32
 * (int32, int32, int32) -> range_state_int32
 * (int64,) -> range_state_int64
 * (int64, int64) -> range_state_int64
 * (int64, int64, int64) -> range_state_int64
 * (uint64,) -> range_state_uint64
 * (uint64, uint64) -> range_state_uint64
 * (uint64, uint64, uint64) -> range_state_uint64
File "bts_read.py", line 56
[1] During: resolving callee type: Function(<built-in function range>)
[2] During: typing of call at bts_read.py (56)

Failed at nopython (nopython frontend)
Invalid usage of Function(<built-in function range>) with parameters (int64, readonly array(int32, 2d, C))
Known signatures:
 * (int32,) -> range_state_int32
 * (int32, int32) -> range_state_int32
 * (int32, int32, int32) -> range_state_int32
 * (int64,) -> range_state_int64
 * (int64, int64) -> range_state_int64
 * (int64, int64, int64) -> range_state_int64
 * (uint64,) -> range_state_uint64
 * (uint64, uint64) -> range_state_uint64
 * (uint64, uint64, uint64) -> range_state_uint64
File "bts_read.py", line 56
[1] During: resolving callee type: Function(<built-in function range>)
[2] During: typing of call at bts_read.py (56)

0 个答案:

没有答案