Advertisement
Guest User

Untitled

a guest
Jan 26th, 2013
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. from __future__ import print_function
  2.  
  3. import multiprocessing
  4. import time
  5.  
  6. """
  7. Date: 26/01/2013
  8.  
  9. Bug explication
  10. ---------------
  11. Why when we import a module with .c compiled module,
  12. multiprocessing (pool.imap and map) don't use all cores.
  13.  
  14. This bug exists on latest Ubuntu 64bits (both kernels 3.5 and 3.8rc4). It does
  15. not exist for Windows Seven 32 bits (Virtualbox). Not tested on Mac OS X.
  16.  
  17. Python 2.7.3
  18. """
  19.  
  20.  
  21. def compute(args):
  22.     """
  23.    Dummy computations
  24.    """
  25.     M = 350
  26.     ss = [0] * M
  27.     for a in xrange(1, M):
  28.         a2 = a * a
  29.         for b in xrange(1, M - a):
  30.             s = a2 + b * b
  31.             for c in xrange(1, M - a - b):
  32.                 if s == c * c:
  33.                     ss[a + b + c] += 1
  34.     return args[0]
  35.  
  36.  
  37. def main():
  38.     pool = multiprocessing.Pool(processes=5)
  39.     pool.imap_unordered(compute, range(20))
  40.  
  41.     pool.close()
  42.     pool.join()
  43.  
  44. if __name__ == '__main__':
  45.     t = time.time()
  46.     main()
  47.     print("* Time using all cores: %f" % (time.time() - t))
  48.  
  49.     # Here we import numpy but it works with module with .c compiled module
  50.     # Same behaviour as numpy for: skimage, scipy, tables and pandas
  51.     print ("* Import a module containing .c compiled module (here we use numpy)")
  52.     import numpy
  53.  
  54.     t = time.time()
  55.     main()
  56.     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