I am working on some code that does pretty hard numerical work on a lot (from tens to hundreds of thousands of numerical integrations) a lot of problems. Fortunately, these integrations are awkwardly parallel, so itβs easy to use Pool.map () to split work across multiple cores.
Now I have a program that has this basic workflow:
#!/usr/bin/env python from multiprocessing import Pool from scipy import * from my_parser import parse_numpy_array from my_project import heavy_computation
Since X, param_1, and param_2 are hardcoded and initialized in exactly the same way for each process in the pool, this all works great. Now that my code is working for me, I would like to make the file name, param_1 and param_2 entered by the user at runtime, rather than hard-coded.
It should be noted that X, param_1 and param_2 do not change as work progresses. Since I do not change them, I could do something like this at the beginning of the program:
import sys X = parse_numpy_array(sys.argv[1]) param_1 = float(sys.argv[2]) param_2 = float(sys.argv[3])
And this will do the trick, but since most users of this code run the code from Windows computers, I would rather not follow the path of the command line arguments.
What I would like to do is something like this:
X, param_1, param_2 = None, None, None def init(x,p1, p2) X = x param_1 = p1 param_2 = p2 if __name__=='__main__': filename = raw_input("Filename> ") param_1 = float(raw_input("Parameter 1: ")) param_2 = float(raw_input("Parameter 2: ")) X = parse_numpy_array(filename) pool = Pool(initializer = init, initargs = (X, param_1, param_2,)) arglist = linspace(0.0,1.0,100) results = Pool.map(do_work,arglist)
But of course this fails, and X / param_1 / param_2 is still None when the call to pool.map occurs. I'm new to multiprocessing, so I'm not sure why the initializer call fails. Is there a way to do what I want to do? Is there a better way to do this? I also looked at using shared data, but from my understanding of documentation that only works with ctypes that don't include numpy arrays. Any help with this would be greatly appreciated.