Alex, I found your interesting Metal implementation of prime number generation through Reddit.
I've cloned and run it and it works well until the number of primes gets large when the test you have included to compare CPU and GPU prime-generation generates an error.
The first error is for a false prime generated by the GPU (the false primes are all generated by the GPU). This is the claim that 17884441 is prime when in fact it it 4229^2. Subsequently all numbers that are the squares of primes are falsely identified as primes.
The problem comes from the upper bound at about line 33 in your gpuTest/computePrimeNumbers.metal file where the last number is not tested; hence when the only divisor of a number is its prime square-root, it seems to be a prime but isn't.
This fixes it just by adding 2 to force the top number to be tested:
for (UIntType i = 3; i <= sqrt((FloatType)num) + 2; i+=2)
I notice that you also use a different Type in the CPU version with Double rather than FloatType so maybe that would also work but I haven't checked.
Hope this is helpful.
Incidentally, my MacBook generates primes up to 1,000,000,000 on its AMD Radeon 5500M GPU in about 10 secs; the CPU takes forever (or maybe hangs) and I haven't run it to completion with that many primes.
The code doesn't seem to exit smoothly, but that's another matter.
pudepiedj
Alex, I found your interesting Metal implementation of prime number generation through Reddit.
I've cloned and run it and it works well until the number of primes gets large when the test you have included to compare CPU and GPU prime-generation generates an error.
The first error is for a false prime generated by the GPU (the false primes are all generated by the GPU). This is the claim that 17884441 is prime when in fact it it 4229^2. Subsequently all numbers that are the squares of primes are falsely identified as primes.
The problem comes from the upper bound at about line 33 in your gpuTest/computePrimeNumbers.metal file where the last number is not tested; hence when the only divisor of a number is its prime square-root, it seems to be a prime but isn't.
This fixes it just by adding 2 to force the top number to be tested:
for (UIntType i = 3; i <= sqrt((FloatType)num) + 2; i+=2)
I notice that you also use a different Type in the CPU version with Double rather than FloatType so maybe that would also work but I haven't checked.
Hope this is helpful.
Incidentally, my MacBook generates primes up to 1,000,000,000 on its AMD Radeon 5500M GPU in about 10 secs; the CPU takes forever (or maybe hangs) and I haven't run it to completion with that many primes.
The code doesn't seem to exit smoothly, but that's another matter.
pudepiedj