Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- import multiprocessing
- import time
- """
- Date: 26/01/2013
- Bug explication
- ---------------
- Why when we import a module with .c compiled module,
- multiprocessing (pool.imap and map) don't use all cores.
- This bug exists on latest Ubuntu 64bits (both kernels 3.5 and 3.8rc4). It does
- not exist for Windows Seven 32 bits (Virtualbox). Not tested on Mac OS X.
- Python 2.7.3
- """
- def compute(args):
- """
- Dummy computations
- """
- M = 350
- ss = [0] * M
- for a in xrange(1, M):
- a2 = a * a
- for b in xrange(1, M - a):
- s = a2 + b * b
- for c in xrange(1, M - a - b):
- if s == c * c:
- ss[a + b + c] += 1
- return args[0]
- def main():
- pool = multiprocessing.Pool(processes=5)
- pool.imap_unordered(compute, range(20))
- pool.close()
- pool.join()
- if __name__ == '__main__':
- t = time.time()
- main()
- print("* Time using all cores: %f" % (time.time() - t))
- # Here we import numpy but it works with module with .c compiled module
- # Same behaviour as numpy for: skimage, scipy, tables and pandas
- print ("* Import a module containing .c compiled module (here we use numpy)")
- import numpy
- t = time.time()
- main()
- print("* Time using only one core (check it with 'htop' command on Linux system: %f" % (time.time() - t))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement