如何使用Numba加速此功能?

时间:2018-07-15 11:35:23

标签: python gpu jit numba numba-pro

我试图使用Numba优化此功能,但是我无法做到。我认为这没有可以加速的代码部分。如果有人可以为我提供优化版本的帮助,我的程序将变得异常火爆。请告知是否需要任何数据集或其他信息。当我直接对此应用@jit时,它不起作用。

def c_a(x, y, z, counter, p_l):
    # start = time.time()
    if counter == 1:
        l = x
        m = y
        n = z


        path = "c_r.pdb"
        global r_a_t


        p = Bio.PDB.PDBParser() 
        structure = p.get_structure('mSN1', path)
        c_r = [a.get_coord() for a in structure.get_atoms()]   
        lengthnew = len(c_r)


        m_d = np.array([-45, -45, -45])


        a_s_r = np.zeros((128, 128, 128), np.complex)
        for i in range(0, lengthnew):
            x = int(math.floor((c_r[i][0] - m_d[0]) / 1.2))
            y = int(math.floor((c_r[i][1] - m_d[1]) / 1.2))
            z = int(math.floor((c_r[i][2] - m_d[2]) / 1.2))
            with open("Ei.txt", 'r') as ei_values:
                for row in ei_values:
                    s_v = row.split()
                    if s_v[0] == r_a_t[i] :
                        a_s_r[x, y, z] = np.complex(s_v[1])


        n_n = lambda x, y, z : [(x2, y2, z2) for x2 in range(x - 5, x + 6)
                                   for y2 in range(y - 5, y + 6)
                                   for z2 in range(z - 5, z + 6)
                                   if (-1 < x < X and
                                       -1 < y < Y and
                                       -1 < z < Z and
                                       (x != x2 or y != y2 or z != z2) and
                                       (0 <= x2 < X) and
                                       (0 <= y2 < Y) and
                                       (0 <= z2 < Z) and
                                       ((( abs(x - x2)) ** 2 + (abs(y - y2)) ** 2 + (abs(z - z2)) ** 2  ) <= 25))]  
        m = n_n(l, m, n)
        result = 0
        for i in range(0, len(m)):
            a = m[i][0]
            b = m[i][1]
            c = m[i][2]
            result = result + a_s_r[a][b][c]
        return result

    else:
        l = x
        m = y
        n = z
        path = p_l


        global l_a_t


        p = Bio.PDB.PDBParser() 
        structure = p.get_structure('mSN1', path)
        c_l = [a.get_coord() for a in structure.get_atoms()]   
        lengthnew = len(c_l)


        m_d = np.array([-45, -45, -45])


        a_s_l = np.zeros((128, 128, 128), np.complex)
        for i in range(0, lengthnew):
            x = int(math.floor((c_l[i][0] - m_d[0]) / 1.2))
            y = int(math.floor((c_l[i][1] - m_d[1]) / 1.2))
            z = int(math.floor((c_l[i][2] - m_d[2]) / 1.2))
            with open("E.txt", 'r') as e_v:
                for row in e_v:
                    s_v = row.split()
                    if s_v[0] == l_a_t[i] :
                        a_s_l[x, y, z] = np.complex(s_v[1])


        n_n = lambda x, y, z : [(x2, y2, z2) for x2 in range(x - 5, x + 6)
                                       for y2 in range(y - 5, y + 6)
                                       for z2 in range(z - 5, z + 6)
                                       if (-1 < x < X and
                                           -1 < y < Y and
                                           -1 < z < Z and
                                           (x != x2 or y != y2 or z != z2) and
                                           (0 <= x2 < X) and
                                           (0 <= y2 < Y) and
                                           (0 <= z2 < Z) and
                                           (((abs(x - x2)) ** 2 + (abs(y - y2)) ** 2 + (abs(z - z2)) ** 2  ) <= 25))]  
        m = n_n(l, m, n)
        result = 0
        for i in range(0, len(m)):
            a = m[i][0]
            b = m[i][1]
            c = m[i][2]
            result = result + a_s_l[a][b][c]
        # print "c_a : ", time.time() - start    
        return result

1 个答案:

答案 0 :(得分:0)

已解决。

在函数外进行了所有文件读取步骤,因为它们已经执行了很多次。它提高了 70倍

只需将 lambda 函数保留在函数中,因为它们取决于 x y z