Muhammad Sajid Iqbal
Muhammad Sajid Iqbal

Reputation: 93

python multiprocessing to a function with multiple functions

My function resembles with the determination of pi value using Monte Carlo integral method. The function basically insert a molecule in random position and estimate the energy. I convert my for loop in a function to operate over multiple cores using multiprocessing module. however I got the similar values from random number generator and energy values for all processes. It looks like it ran the function multiple times but reporting similar results.

list of user defined functions. There is no details given for simplicity.

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

Running a for loop;

nReplicates = 100 
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))

Then I convert the for loop as ;

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)]

I printed the random position values only here.

(( 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)

thanks a lot!

Upvotes: 0

Views: 620

Answers (1)

John
John

Reputation: 1819

My gut reaction is to suggest using the value x passed to each thread as the seed for the random number generator, making sure you have a separate instance of the generator in each thread. This could be your problem if the generator you are using is not guaranteed to be thread safe.

Upvotes: 1

Related Questions