Skip to content

Instantly share code, notes, and snippets.

@Megaf
Last active February 12, 2021 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Megaf/e27382c12ffc8d49925ffb96c39674e1 to your computer and use it in GitHub Desktop.
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
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()
@Megaf
Copy link
Author

Megaf commented Feb 5, 2021

Edited following advice from Simon Pratt https://www.youtube.com/watch?v=hGyJTcdfR1E&lc=UgzO5UL2JbefjznbliN4AaABAg.9JMH3io4d6I9JNdFYWmuii

"... changing that line to a simpler parts = [[x] for x in range(1, max_number)] gives much better results. Because we are now loading up the individual cores properly, we also need to change line 8 to remove the * 4, because we can only run one process on each core..."

@adsworth
Copy link

adsworth commented Feb 5, 2021

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.

@Megaf
Copy link
Author

Megaf commented Feb 12, 2021

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