2

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.

11
  • It might help if you posted a working example, there are lots of syntax errors that prevent your sample from running Commented Feb 4, 2014 at 17:47
  • Oh, I am sorry. Since it was part of the program, I didn't have a look. I will edit it now. Thanks. Commented Feb 4, 2014 at 17:49
  • Now its working :) Please check Commented Feb 4, 2014 at 18:11
  • Can you provide more of an explanation or raw values for what your expected u_total is? From your description, it sounds like you're expecting (loop_random(x/4) * 4) = loop_random(x), which won't work with total getting initialized to zeros every time. Commented Feb 4, 2014 at 19:49
  • Thank you. Yes, I am expecting (loop_random(x/4) * 4) = loop_random(x). So you think it is happening because I am initializing the total to zero? I will initialize total outside the loop and try. Commented Feb 4, 2014 at 19:53

1 Answer 1

1

Sorry for posting this answer, as it is not an answer, however I cannot post comments yet.

Just wanted to note that using:

from pylab import *

Is considered bad practise. Don't blanket import a bunch of random stuff. Each module should have a longish list of the specific things it needs.

According to the Python Zen:

Explicit is better than implicit.

Can't argue with that :)

Sign up to request clarification or add additional context in comments.

2 Comments

now u can comment, why not delete this and add a comment?
@CharlieParker Can't seem to delete an accepted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.