Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include Non-Trivial Python Examples #14

Open
MorganRodgers opened this issue Aug 27, 2018 · 1 comment
Open

Include Non-Trivial Python Examples #14

MorganRodgers opened this issue Aug 27, 2018 · 1 comment

Comments

@MorganRodgers
Copy link
Contributor

Our R example is a great comparison of several methods to do parallel processing in R. I would love to see something similar for Python. One quick way to add a useful example would be to include a comparison of Python3's standard library classes concurrent.futures.ThreadPoolExecutor and concurrent.futures.ProcessPoolExecutor. These two classes allow Python to take better advantage of a single compute node. Their performance differences could be highlighted with a comparison using the prime number checking example: https://docs.python.org/3/library/concurrent.futures.html#processpoolexecutor-example

In order to take advantage of multiple nodes Python users can take advantage of MPI (at least on Owens) using mpi4py. MPI offers a number of options, but the mpi4py offers a futures-esque interface similar to Python3's concurrent.futures: https://mpi4py.readthedocs.io/en/stable/mpi4py.futures.html

@MorganRodgers
Copy link
Contributor Author

MorganRodgers commented Aug 27, 2018

Something like:

#!/usr/bin/env python
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
import math
from time import time


def is_prime(n):
  if n % 2 == 0:
    return False

  sqrt_n = int(math.floor(math.sqrt(n)))
  for i in range(3, sqrt_n + 1, 2):
    if n % i == 0:
      return False
  return True

def main():
  PRIMES = list(range(1_000_000))
  start = time()
  with ProcessPoolExecutor() as executor:
    primes = executor.map(is_prime, PRIMES)
  print('Calculation using ProcessPoolExecutor with n processes', time() - start)

  start = time()
  with ThreadPoolExecutor() as executor:
    primes = executor.map(is_prime, PRIMES)
  print('Calculation using ThreadPoolExecutor with n threads', time() - start)

# MPI Example here...

if __name__ == '__main__':
  main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant