I've been browsing for some time but couldn't find any constructive answer that I could comprehend.
How should I paralellize the following code:
import random
import math
import numpy as np
import sys
import multiprocessing
boot = 20#number of iterations to be performed
def myscript(iteration_number):
#stuff that the code actually does
def main(unused_command_line_args):
for i in xrange(boot):
myscript(i)
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))
or where can I read about it? I'm not really sure how to search for it even.
EDIT: Because this problem arised directly due to applying the answer to the first version of this question, I will try to glue it here. In the minimal working example (MWE) there's a place in the myscript definition where I generate some random numbers, then perform some operations on them, and fnally write the output to a file. When this code is un-parallelized, it works correct. However, if it's parallel (I'm testing it on a 2-core machine, and have two threads at a time), when I want to perform 4 iterations (boot) I get twice the same output (e.g., among four outputs I get only two distinct numbers, not four as expected).
How to fix it?
MWE:
import random
import math
import numpy as np
import multiprocessing as mp
from multiprocessing import Pool
boot = 4
RRpoints = 278
def myscript(iteration_number):
RRfile_name = "outputRR%d.txt" % iteration_number
with open(RRfile_name, "w") as RRf:
col1 = np.random.uniform(0 , 1 , RRpoints)
col2 = np.random.uniform(0 , 1 , RRpoints)
sph1 = [i * 2 * math.pi for i in col1]
sph2 = [math.asin(2 * i - 1) for i in col2]
corr = []
for k in xrange(0 , RRpoints):
h = 0
mltp = sph1[k] * sph2[k]
h += mltp
RRf.write("%s\n" % h)
x = xrange(boot)
p = mp.Pool()
y = p.imap(myscript, x)
list(y)
Aucun commentaire:
Enregistrer un commentaire