我试图使用python(和Numpy)通过有限元来求解导数。当我顺序解决没有问题,但for非常大,需要太长时间。然后,我尝试用共享内存实现multiprocessing
,但我有一些问题。我在一个有4个内核的机器上运行上面的代码。代码中心运行良好,但顺序代码慢得多,为什么?我做得不好?
cores = multiprocessing.cpu_count()
n = 40 #array size
deriv2_base = multiprocessing.Array(ctypes.c_double, n)
temp_base = multiprocessing.Array(ctypes.c_double, n)
deriv2 = np.ctypeslib.as_array(deriv2_base.get_obj())
temp = np.ctypeslib.as_array(temp_base.get_obj())
temp[-1] = const['T0']
def calculo(i, def_param=(deriv2, temp)):
pos_ini = i*blockDim + 1/(1 + i)
pos_fin = (i + 1)*blockDim - (i+1)/cores
deriv2[pos_ini:pos_fin] = (temp[pos_ini-1:pos_fin-1] -2*temp[pos_ini:pos_fin] + temp[pos_ini+1:pos_fin+1]) / delta_z**2
temp[pos_ini:pos_fin] = temp[pos_ini:pos_fin] + kappa*deriv2[pos_ini:pos_fin] * delta_t_seg
if __name__ == '__main__':
pool = multiprocessing.Pool(processes = cores)
for j in xrange(i_steps):
pool.map(calculo, range(cores))
print temp
坦克!