2

I am new to multi-core programming. The following program using only one core. How can I make it to run on multiple cores (I have 4 cores).

simDict={}

def sim(outer,inner, ...):
    val= /*do some math*/
    simDict[...]=val

def foo():
   for outer in xrange(0, limit):
      for inner in xrange(outer, limit):
          sim(outer,inner, ...)
foo()

2 Answers 2

5

Easy:

from multiprocessing import Pool

p = Pool()

def do_inner(outer, limit):
    for inner in xrange(outer, limit):
        sim(outer, inner, ...)

def foo():
    p.map(do_inner, xrange(limit))

foo()

This employs multiprocessing.Pool to create a pool of worker processes.

Sign up to request clarification or add additional context in comments.

Comments

1

For your problem I would use a queue then I would stick with the consumer/producer problem and go from there. Unless there is some data dependency between your threads then you will need to get into some more advanced locking mechanisms and have to protect against far more threading pitfalls.

Producer/Consumer concept: http://en.wikipedia.org/wiki/Producer-consumer_problem And just for reference: http://docs.python.org/library/multiprocessing.html

multiprocessing may be helpful to use as well as the global interpreter lock can slow things down in some specific cases if just threads are used.

2 Comments

He explicitly wants to run on multiple cores, and the algorithm looks CPU-bound. Therefore, multiprocessing is the only choice if he wants to stay with pure-Python programming.
Agreed, why I put the link to the multiprocessing at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.