Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41073 > unrolled thread

Re: Advice regarding multiprocessing module

Started byOscar Benjamin <oscar.j.benjamin@gmail.com>
First post2013-03-11 15:58 +0000
Last post2013-03-11 15:58 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Advice regarding multiprocessing module Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-03-11 15:58 +0000

#41073 — Re: Advice regarding multiprocessing module

FromOscar Benjamin <oscar.j.benjamin@gmail.com>
Date2013-03-11 15:58 +0000
SubjectRe: Advice regarding multiprocessing module
Message-ID<mailman.3193.1363017562.2939.python-list@python.org>
On 11 March 2013 14:57, Abhinav M Kulkarni <amkulkar@uci.edu> wrote:
> Hi Jean,
>
> Below is the code where I am creating multiple processes:
>
> if __name__ == '__main__':
>     # List all files in the games directory
>     files = list_sgf_files()
>
>     # Read board configurations
>     (intermediateBoards, finalizedBoards) = read_boards(files)
>
>     # Initialize parameters
>     param = Param()
>
>     # Run maxItr iterations of gradient descent
>     for itr in range(maxItr):
>         # Each process analyzes one single data point
>         # They dump their gradient calculations in queue q
>         # Queue in Python is process safe
>         start_time = time.time()
>         q = Queue()
>         jobs = []
>         # Create a process for each game board
>         for i in range(len(files)):
>             p = Process(target=TrainGoCRFIsingGibbs, args=(intermediateBoards[i], finalizedBoards[i], param, q))

Use a multiprocessing.Pool for this, rather than creating one process
for each job. e.g.:

p = Pool(4) # 1 process for each core
results = []
for ib, fb in zip(intermediateBoards, finalizedBoards):
    results.append(p.apply_async(TrainGoCRFIsingGibbs, args=(ib, fb, param, q)))
p.close()
p.join()

# To retrieve the return values
for r in results:
    print(r.get())

This will distribute your jobs over a fixed number of processes. You
avoid the overhead of creating and killing processes and the process
switching that occurs when you have more processes than cores.

>             p.start()
>             jobs.append(p)
>         # Blocking wait for each process to finish
>         for p in jobs:
>             p.join()
>         elapsed_time = time.time() - start_time
>         print 'Iteration: ', itr, '\tElapsed time: ', elapsed_time
>
> As you recommended, I'll use the profiler to see which part of the code is
> slow.

Do this without using multiprocessing first. Loosely you can hope that
multiprocessing would give you a factor of 4 speedup but no more. You
haven't reported a comparison of times with/without multiprocessing so
it's not clear that that is the issue.


Oscar

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web