Question

I'm trying to make my first simulation using multiple processes, right now each simulation takes about an hour to complete. For that im using the multiprocessing import.

All the code works fine when i run simulation() inside a for loop. When the program is run all the workers in the pool start doing the same task, so i end up with 4 copies of the same, but when they have finished they start over again and again. Whats more, when the pool is created the whole program is run from line 1 four times simultaneously, instead of just running the simulation.

The code below is almost entirely a copy-paste from an example in the multiprocessing documentation: http://docs.python.org/library/multiprocessing.html

I'm working on a windows 7, and i'll try running it on a linux machine as soon as i can, but could somebody explain me what's going wrong here.

Thank you very much

pd. windows 7(64bits) + python 2.7.1

....

code where: simulation, pulse1, pulse2, sol_ref, model, algorithm and i are defined. Also 
where the various imports are made. Also some print statements.

...


if __name__=='__main__':
    freeze_support()

    def calculate(func, args):
        result = func(*args)
        return '%s says that %s%s = %s' % (
            multiprocessing.current_process().name,
            func.__name__, args, result
            )


    PROCESSES = 4
    print 'Creating pool with %d processes\n' % PROCESSES
    pool = Pool(PROCESSES)
    print 'pool = %s' % pool
    print

    TASKS = [(simulation, (pulse1,pulse2,sol_ref,model,algorithm,i)) for i in delta_tau]

    results = [pool.apply_async(calculate, t) for t in TASKS]
    for r in results:
        print '\t', r.get()
    print

EDIT: Errror Log

The lines that print the number of points in the simulation come from the code before the if main statement. In this code the simulation is run once inside a for loop to check that it does work correctly and gives the expected results.

C:\Users\HP\Desktop\experiment>python amplifier_parallel.py
Number of z points:  169   dist:  20.0  mm  dz:  118.343195266  um
Number of t points:  8192.0   window span:  69.0  ps  dt:  8.4228515625  fs

delta_tau:  -6.0
delta_tau:  -3.85714285714
delta_tau:  -1.71428571429
delta_tau:  0.428571428571
delta_tau:  2.57142857143
delta_tau:  4.71428571429
delta_tau:  6.85714285714
delta_tau:  9.0
Simulation Time: 43.1258663953 seconds
Creating pool with 4 processes

pool = <multiprocessing.pool.Pool object at 0x063E66B0>

        Number of z points:  169   dist:  20.0  mm  dz:  118.343195266  um
Number of t points:  8192.0   window span:  69.0  ps  dt:  8.4228515625  fs
...3 more times...

delta_tau:  -6.0
...3more times
delta_tau:  -3.85714285714
... 3 more tiemes
delta_tau:  -1.71428571429
... 3 more tiemes
delta_tau:  0.428571428571
... 3 more tiemes
delta_tau:  2.57142857143
... 3 more tiemes3
delta_tau:  4.71428571429
... 3 more tiemes
delta_tau:  6.85714285714
... 3 more tiemes
delta_tau:  9.0
... 3 more tiemes
Simulation Time: 63.3316095785 seconds
... 3 more times with slightly different times, depending on the worker who did the job
Process PoolWorker-4:
Traceback (most recent call last):
  File "C:\Python27\lib\multiprocessing\process.py", line 232, in _bootstrap
    self.run()
  File "C:\Python27\lib\multiprocessing\process.py", line 88, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Python27\lib\multiprocessing\pool.py", line 59, in worker
    task = get()
  File "C:\Python27\lib\multiprocessing\queues.py", line 352, in get
    return recv()
AttributeError: 'module' object has no attribute 'calculate'
Number of z points:  169   dist:  20.0  mm  dz:  118.343195266  um
Number of t points:  8192.0   window span:  69.0  ps  dt:  8.4228515625  fs

Process PoolWorker-3:
Traceback (most recent call last):
  File "C:\Python27\lib\multiprocessing\process.py", line 232, in _bootstrap
    self.run()
  File "C:\Python27\lib\multiprocessing\process.py", line 88, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Python27\lib\multiprocessing\pool.py", line 59, in worker
    task = get()
  File "C:\Python27\lib\multiprocessing\queues.py", line 352, in get
    return recv()
AttributeError: 'module' object has no attribute 'calculate'
delta_tau:  -6.0
... this kind of stuff goes on for ever until i control-C

No correct solution

OTHER TIPS

Define calculate() outside of if __name__=="__main__" block. Child processes don't see it on Windows.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top