Last active
February 12, 2021 00:48
-
-
Save Megaf/e27382c12ffc8d49925ffb96c39674e1 to your computer and use it in GitHub Desktop.
multi process version of the prime number counter from https://www.youtube.com/watch?v=hGyJTcdfR1E
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing as mp | |
import time | |
#max number to look up to | |
max_number = 10000 | |
#four processes per cpu | |
num_processes = mp.cpu_count() * 1 | |
def chunks(seq, chunks): | |
size = len(seq) | |
start = 0 | |
for i in range(1, chunks + 1): | |
stop = i * size // chunks | |
yield seq[start:stop] | |
start = stop | |
def calc_primes(numbers): | |
num_primes = 0 | |
primes = [] | |
#Loop through each number, then through the factors to identify prime numbers | |
for candidate_number in numbers: | |
found_prime = True | |
for div_number in range(2, candidate_number): | |
if candidate_number % div_number == 0: | |
found_prime = False | |
break | |
if found_prime: | |
primes.append(candidate_number) | |
num_primes += 1 | |
return num_primes | |
def main(): | |
#Record the test start time | |
start = time.time() | |
pool = mp.Pool(num_processes) | |
#0 and 1 are not primes | |
parts = [[x] for x in range(1, max_number)] | |
#run the calculation | |
results = pool.map(calc_primes, parts) | |
total_primes = sum(results) | |
pool.close() | |
#Once all numbers have been searched, stop the timer | |
end = round(time.time() - start, 2) | |
#Display the results, uncomment the last to list the prime numbers found | |
print('Find all primes up to: ' + str(max_number) + ' using ' + str(num_processes) + ' processes.') | |
print('Time elasped: ' + str(end) + ' seconds') | |
print('Number of primes found ' + str(total_primes)) | |
if __name__ == "__main__": | |
main() |
Hi,
I got your comment notification, but the comment is gone. I'll look into.
It's my first time working with the multiprocess module. The figures my script returns look fine to me and also my cores are all busy when running the script.
I'll look at your differences later this evening.
Thanks for the heads up.
Hi,
I got your comment notification, but the comment is gone. I'll look into.
It's my first time working with the multiprocess module. The figures my script returns look fine to me and also my cores are all busy when running the script.
I'll look at your differences later this evening.Thanks for the heads up.
So, the changes I made make it use all cores when using a single node.
I can't test it yet when using multiple nodes.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Edited following advice from Simon Pratt https://www.youtube.com/watch?v=hGyJTcdfR1E&lc=UgzO5UL2JbefjznbliN4AaABAg.9JMH3io4d6I9JNdFYWmuii