2
votes

Inspired from the tutorial multiprocessing factorial, I have tried to multiprocess a trivial force calculation module. My main concern is the queue.get feature is not retrieving in the same order as expected. for example, instead of giving [5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5], it gives jumbled output based on different processor. 1) How can I append the results from the queue based on the process calling sequence? Should I use pool, map, lock or any such thing? 2)How can I avoid memory sync/overwriting issues?

def mp_worker(istart, iend, x, out_q1, out_q2):
    global_N = len(x)
    outdict1 = []
    outdict2 = []
    k = 0


       for i in range(istart,iend,1):
            temp_FX = 0
            temp_FY = 0
            for j in range(global_N):
                if i != j:
                    temp_FX = temp_FX + (x[j]-x[i])
                    temp_FY = temp_FY + (x[j]-x[i])
            outdict1.append(temp_FX)
            outdict2.append(temp_FY)
            k = k + 1

    out_q1.put(outdict1)
    out_q2.put(outdict2)

def mp_factorizer( nprocs):

    x = mem.x
    FORCE = mem.FORCE
    N = len(FORCE)

    out_q1 = multiprocessing.Queue()
    out_q2 = multiprocessing.Queue()
    chunksize = int(math.ceil(N / float(nprocs)))
    procs = []

    for i in range(nprocs):
        istart = chunksize * i
        iend = chunksize * (i + 1)
        p = multiprocessing.Process(
                target=mp_worker,
                args=(istart, iend, x, out_q1, out_q2))
        procs.append(p)
        p.start()

    # Collect all results into a single result dict. We know how many dicts
    # with results to expect.
    resultdict1 = []
    resultdict2 = []
    for i in range(nprocs):
        resultdict1 = resultdict1 + out_q1.get()
        resultdict2 = resultdict2 + out_q2.get()

    # Wait for all worker processes to finish
    for p in procs:
        p.join()

    return resultdict1
1

1 Answers

1
votes

Items are added to the queues in whatever order the worker processes happen to finish. If you want to force the order, you have to ... force the order ;-)

Process isn't good for this. Their execution is inherently unordered, and the order may very well change from one run to the next.

In this case, probably simplest: first, throw out the queues entirely. End your mp_worker() like so instead:

return outdict1, outdict2

Then use a Pool. There are several ways to use one. The most like what you're already doing looks like:

pool = multiprocessing.Pool(nprocs)
for i in range(nprocs):
    istart = chunksize * i
    iend = chunksize * (i + 1)
    p = pool.apply_async(mp_worker, (istart, iend, x))
    procs.append(p)

resultdict1 = []
resultdict2 = []
for p in procs:
    t1, t2 = p.get()
    resultdict1.extend(t1)
    resultdict2.extend(t2)

pool.close()
pool.join()

Now results are fetched in the same order tasks were passed out; the order has been forced.

Note: switching from + to .extend() isn't logically necessary, but reduces a quadratic-time (in the number of loop iterations) operation to an amortized linear-time operation. That has nothing to do with multiprocessing. somelist = somelist + anotherlist is always better coded as somelist.extend(anotherlist).

About the OS

Here's a guess as to why "it worked" on Windows but not Linux: historically, process creation has been cheaper on Linux than Windows (Windows put much more effort into speeding its threads). That makes it more likely the processes will end on Windows in the same order they started, when the processes are doing about the same amount of work. But they certainly can finish "out of order" on Windows too.

Regardless, Python has no say in any of this: if you need a specific order, you have to force that order.