9
votes

I am running a program which loads 20 GB data to the memory at first. Then I will do N (> 1000) independent tasks where each of them may use (read only) part of the 20 GB data. I am now trying to do those tasks via multiprocessing. However, as this answer says, the entire global variables are copied for each process. In my case, I do not have enough memory to perform more than 4 tasks as my memory is only 96 GB. I wonder if there is any solution to this kind of problem so that I can fully use all my cores without consuming too much memory.

1
So each process needs to process the whole data, but does its own thing i.e no possibility for duplication of effort? In that case, you can try sharing the data in a Manager.dict() here or a Manager.list() and spawn multiple Processes. If they're all doing the same task then you could chunk the data and hand each process its own chunk. I don't think you can usefully spawn more processes than cores though and it seems you want >1000 processes? - roganjosh
It allows all child processes access to a single object, yes, rather than giving them all their own copy. - roganjosh
@whan - no, its the opposite. The child gets a copy-on-write view of the parent memory space. As long as you load the dataset before firing the processes and you don't pass a reference to that memory space in the multiprocessing call (that is, workers should use the global variable directly), then there is no copy. - tdelaney
@tdelaney but that is only on Linux? I could have sworn this kind of thing failed for me on Windows but I was also trying to modify a single object and everything I read indicated that each child got a copy (regardless of OS). Manager was the fix for me, whether I needed read or write access. I'll have to go and play with that approach again on Linux. - roganjosh
@roganjosh - in linux, forked children get a copy-on-write view of the parent. If you modify the data, it won't be seen by the parent. I gave an example in my answer. - tdelaney

1 Answers

6
votes

In linux, forked processes have a copy-on-write view of the parent address space. forking is light-weight and the same program runs in both the parent and the child, except that the child takes a different execution path. As a small exmample,

import os
var = "unchanged"
pid = os.fork()
if pid:
    print('parent:', os.getpid(), var)
    os.waitpid(pid, 0)
else:
    print('child:', os.getpid(), var)
    var = "changed"

# show parent and child views
print(os.getpid(), var)

Results in

parent: 22642 unchanged
child: 22643 unchanged
22643 changed
22642 unchanged

Applying this to multiprocessing, in this example I load data into a global variable. Since python pickles the data sent to the process pool, I make sure it pickles something small like an index and have the worker get the global data itself.

import multiprocessing as mp
import os

my_big_data = "well, bigger than this"

def worker(index):
    """get char in big data"""
    return my_big_data[index]

if __name__ == "__main__":
    pool = mp.Pool(os.cpu_count())
    for c in pool.imap_unordered(worker, range(len(my_big_data)), chunksize=1):
        print(c)

Windows does not have a fork-and-exec model for running programs. It has to start a new instance of the python interpreter and clone all relevant data to the child. This is a heavy lift!