一个具有多个函数的函数的python多重处理

2024-09-30 16:37:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我的函数类似于蒙特卡罗积分法确定pi值。这个函数基本上是在任意位置插入一个分子,然后估计能量。我将函数中的for循环转换为使用多处理模块在多个核心上操作。但是我从随机数发生器得到了相似的值和所有过程的能量值。看起来它运行了多次函数,但报告了相似的结果。在

用户定义函数的列表。为了简单起见,没有给出细节。在

def createRandomValuesforRotationTranslation(boxSpace):
def rotateTranslateMolec(randomValues,molec):
def makemolgroups(newMolec,peg):
def steric_closmol_clashes_vdw2(boxMolecs,ResID):

循环运行

^{pr2}$

然后我将for循环转换为:

def paralle_distances(nReplicates): 
    count = 0
    throws = 0
    for i in range(nReplicates):
        throws += 1
        randomvalues = createRandomValuesforRotationTranslation(boxSpace)
        newMolec = rotateTranslateMolec(randomvalues,rna_mol)
        boxMolecs = makemolgroups(newMolec,peg)
        output = steric_closmol_clashes_vdw2(boxMolecs,ResID)
        count += output
        ratio = count /throws
        # binomial distribution method V_free = (number of accepted/total)Vbox 
        V_free = (count/throws)*output_vol
        p = count/throws
        std_binom = sqrt(throws*p*(1-p))
        error_binom = (output_vol/throws)*std_binom
        error_binom_fraction =  error_binom/V_free
        if i % 1 == 0:
            print("STEPS %d: BINOMIAL ERROR T.VOLUME %s: ERROR F.VOLUME %s: ESTIMATED VOLUME %s:" %(i, error_binom,error_binom_fraction,ratio))
    return

import multiprocessing as mp
pool = mp.Pool(processes=4)
results = [pool.apply_async(paralle_distances, args=(x,)) for x in range(1,5)]

我只在这里打印随机位置值。在

(( 242.281, -50.4288, -7.54141 ), ( -0.679886, 0.674784, 0.287092 ), 201.097 degree)
(( 242.281, -50.4288, -7.54141 ), ( -0.679886, 0.674784, 0.287092 ), 201.097 degree)
(( 242.281, -50.4288, -7.54141 ), ( -0.679886, 0.674784, 0.287092 ), 201.097 degree)
(( 157.376, 67.453, -132.227 ), ( 0.0216526, 0.765258, 0.64336 ), 16.5297 degree)
(( 157.376, 67.453, -132.227 ), ( 0.0216526, 0.765258, 0.64336 ), 16.5297 degree)
(( 242.281, -50.4288, -7.54141 ), ( -0.679886, 0.674784, 0.287092 ), 201.097 degree)

非常感谢!在


Tags: 函数freeforoutputdefcounterror能量
1条回答
网友
1楼 · 发布于 2024-09-30 16:37:26

我的直觉反应是建议使用传递给每个线程的值x作为随机数生成器的种子,确保每个线程中都有一个单独的生成器实例。如果您使用的生成器不能保证线程安全,则这可能是您的问题。在

相关问题 更多 >