Skip to content Skip to sidebar Skip to footer

How To Implement A Reduce Operation In Python Multiprocessing?

I'm an expert parallel programmer in OpenMP and C++. Now I'm trying to understand parallelism in python and the multiprocessing library. In particular, I'm trying to parallelize th

Solution 1:

You should use shared memory objects in your case:

from random import randint
import multiprocessing as mp

def random_add(x):
    x[randint(0,len(x)-1)]  += 1

if __name__ == "__main__":
    print("Serial")
    x = [0]*8
    for i in range(100):
        random_add(x)
    print(x)

    print("Parallel")
    x = mp.Array('i', range(8))
    processes = [mp.Process(target = random_add, args=(x,)) for i in range(100)]
    for p in processes:
        p.start()
    print(x[:])

I've changed numpy array to ordinal list for the purpose of clearness of code

Post a Comment for "How To Implement A Reduce Operation In Python Multiprocessing?"