Here is the sample program for multiprocessing using python. I see the memory usage by each process is ~2 to 3 times higher than the memory each process is supposed to use. If I calculate with just one process, the memory used is ~1.3 times more and it goes higher with the number of processes.
For example, for an array of 1000*1000*1000 with float64, it should use the memory of 8Gb, but I see the memory goes upto 25Gb with 8 processors running in parallel! But I read that multiprocessing uses shared memory. So I am not sure where the memory is leaking. Here is the code :
#To use the code, please take care of your RAM.
#If you have higher RAM, kindly try for the bigger arrays to see the difference clearly.
from numpy import *
import multiprocessing as mp
a = arange(0, 2500, 5)
b = arange(0, 2500, 5)
c = arange(0, 2500, 5)
a0 = 540. #random values
b0 = 26.
c0 = 826.
def rand_function(a, b, c, a0, b0, c0):
Nloop = 100.
def loop(Nloop, out):
res_total = zeros((500, 500, 500), dtype = 'float')
n = 1
while n <= Nloop:
rad = sqrt((a-a0)**2 + (b-b0)**2 + (c-c0)**2)
res_total = res_total + rad
n +=1
out.put(res_total)
out = mp.Queue()
jobs = []
Nprocs = mp.cpu_count()
print "No. of processors : ", Nprocs
for i in range(Nprocs):
p = mp.Process(target = loop, args=(Nloop/Nprocs, out))
jobs.append(p)
p.start()
final_result = zeros((500,500,500), dtype = 'float')
for i in range(Nprocs):
final_result = final_result + out.get()
p.join()
test = rand_function(a,b,c,a0, b0, c0)
Can anyone please tell me where the memory is leaking? And how to overcome that? Thank you very much in advance.