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

thoughts on a multithreaded version #61

Open
teub opened this issue Apr 9, 2023 · 3 comments
Open

thoughts on a multithreaded version #61

teub opened this issue Apr 9, 2023 · 3 comments

Comments

@teub
Copy link

teub commented Apr 9, 2023

O(1) is great but what if I have more than 1 core, is there any way of making use of them as well ?

I thought about duplicating the code is_prime_1() is_prime_2()... is_prime_n(), n being the number of cores, but that doesn't sound very optimized. I'm afraid of potential racing conditions as well.

Anyway the multithread question led me to wonder if your original code could maybe even be further optimized, let me know if I should open a separate issue :
another approach completely would be to have one function per number to test; instead of calling is_prime(42), you would call is_prime_42() { return false; }.
It's slightly less convenient than calling is_prime(42) because you need to have a switch case on the value, but it has one less parameter so it sounds like it should be faster for each call what do you think.

@Acters
Copy link

Acters commented Apr 10, 2023

You can set each core to only handle multiples of each number except for multiples of 1. This way you can cover almost all the numbers. while having one core handle the numbers skipped by your other cores. probable max multithreaded efficiency use case will be to have about 10 cores dedicated to crunching numbers, after that multithreading will be too complicated to work.

This way you have one core handling any missed numbers, then second core handling multiples of 2, third core handling multiples of 3, fourth core handling multiples of 4, …and so on... up to core nine handling multiples of 9. This way all numbers get tested. Unfortunately some numbers will be tested more than once by different cores.

@Acters
Copy link

Acters commented Apr 10, 2023

your worries of A race condition occurring will only be problematic when two or more threads that can access shared data and try to change it at the same time to different values. This does not need to be a problem as both threads should come to the same conclusion/write the same value.

@yoifox
Copy link
Contributor

yoifox commented Apr 10, 2023

cuda implementation uses more than 1000000 threads

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

3 participants