-
Notifications
You must be signed in to change notification settings - Fork 1
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
Comments
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
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
andconcurrent.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-exampleIn 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 thempi4py
offers afutures-esque
interface similar to Python3'sconcurrent.futures
: https://mpi4py.readthedocs.io/en/stable/mpi4py.futures.htmlThe text was updated successfully, but these errors were encountered: