ここで私のPythonコードを並列化して助けを得たいと願っていますが、私はしばらくそれに苦労しています。完了までに2〜3時間、コードは以下の通りです。数値計算のためのマルチプロセッシングPython関数
import numpy as np
from scipy.constants import Boltzmann, elementary_charge as kb, e
import multiprocessing
from functools import partial
Tc = 9.2
x = []
g= []
def Delta(T):
'''
Delta(T) takes a temperature as an input and calculates a
temperature dependent variable based on Tc which is defined as a
global parameter
'''
d0 = (pi/1.78)*kb*Tc
D0 = d0*(np.sqrt(1-(T**2/Tc**2)))
return D0
def element_in_sum(T, n, phi):
D = Delta(T)
matsubara_frequency = (np.pi * kb * T) * (2*n + 1)
factor_d = np.sqrt((D**2 * cos(phi/2)**2) + matsubara_frequency**2)
element = ((2 * D * np.cos(phi/2))/ factor_d) * np.arctan((D * np.sin(phi/2))/factor_d)
return element
def sum_elements(T, M, phi):
'''
sum_elements(T,M,phi) is the most computationally heavy part
of the calculations, the larger the M value the more accurate the
results are.
T: temperature
M: number of steps for matrix calculation the larger the more accurate the calculation
phi: The phase of the system can be between 0- pi
'''
X = list(np.arange(0,M,1))
Y = [element_in_sum(T, n, phi) for n in X]
return sum(Y)
def KO_1(M, T, phi):
Iko1Rn = (2 * np.pi * kb * T /e) * sum_elements(T, M, phi)
return Iko1Rn
def main():
for j in range(1, 92):
T = 0.1*j
for i in range(1, 314):
phi = 0.01*i
pool = multiprocessing.Pool()
result = pool.apply_async(KO_1,args=(26000, T, phi,))
g.append(result)
pool.close()
pool.join()
A = max(g);
x.append(A)
del g[:]
私のアプローチは、マルチプロセッシング・プールにKO1機能を試してみて、送信することでしたが、私はどちらかPickling
エラーまたはtoo many files open
、すべてのヘルプは大歓迎されます、そしてマルチプロセッシングは間違ったアプローチであるならば、私はすべてのガイドを大好きです。
マルチスレッドの代わりに、コードのランタイムを減らすことができますが、ループの代わりに適切な配列操作を使用すると、かなり単純な数学が必要になると思われます。 (私が間違っているなら私を修正してください)。このコードはCのプログラムマーによって書かれたようです) 次の項目を確認してください:http://technicaldiscovery.blogspot.de/2011/06/speeding-up-python-numpy-cython-and.html http:// scipy -cookbook.readthedocs.io/items/PerformancePython.html – dnalow
Iol私は賞賛されています:D @nnnnowしかし、これはプログラミングに慣れていない化学者志向のエンジニアだった人によって書かれましたが、私は間違いなくナンシーだけど、ここでそれをどうやって実装するかについてはあまり気にしていなかった。 – user3191569