The goal of this program is to calculate the loop_random
for the loop num
. I want to calculate 4 random different num/4
with 4 processors in parallel and in the end summing to give the u_total
. But the problem is though I see all the 4 processors in action, the end result is calculated for 4 times the same set of num/4
. But I want to calculate loop_random
for 4 different random arrays.
Here is the snippet of the part of the code I am working on. .
from numpy import *
import multiprocessing as mp
from multiprocessing import Process, Queue
num = 5000
def loop_random(num, out):
n = 1
total = zeros(((100.,100.,100.)), dtype='float')
while n<= num:
x, y , z = random.rand(100), random.rand(100), random.rand(100) #some random numbers
result = x**2 + y**2 + z**2
total = total + result
n += 1
out.put(total)
if __name__=='__main__':
q1 = mp.Queue()
q2 = mp.Queue()
q3 = mp.Queue()
q4 = mp.Queue()
p1 = mp.Process(target = loop_random, args=(num/4, q1))
p2 = mp.Process(target = loop_random, args=(num/4, q2))
p3 = mp.Process(target = loop_random, args=(num/4, q3))
p4 = mp.Process(target = loop_random, args=(num/4, q4))
p1.start()
p2.start()
p3.start()
p4.start()
u_total_proc1 = q1.get()
u_total_proc2 = q2.get()
u_total_proc3 = q3.get()
u_total_proc4 = q4.get()
p1.join()
p2.join()
p3.join()
p4.join()
u_total = u_total_proc1 + u_total_proc2 + u_total_proc3 + u_total_proc4
print u_total
Any suggestions are welcome. The problem is more about is this the right way of multiprocessing? Does each Queue calculate the same result and not stored or each processor calculates different results and stored? Thanks a lot for your help. Please do correct me if there is something wrong in what I said.