From the archive · source updated November 5, 2016
threadPartialSieve
Four threads split the sieve’s factor-marking passes, crossing out multiples in one shared array. A college-era experiment Vincent is still proud of.
void *threadPartialSieve(void *rank) {
long unsigned int i, j;
// run sieve
for (i = (long unsigned int) rank+1; i < sqrt(globalN); i+=numberOfThreads) {
for (j = i; j < globalN; j+=(i+1)) {
if (i > (long unsigned int) rank+1 && (i+1)%2 == 0) {break;}
if (j > i) {
isPrimeArray[j] = FALSE;
} // end if
} // end for (j)
} // end for (i)
} // end threadPartialSieve
Complete function, preserved as written. The caller, shared variables, and thread setup are in the full source.
This historical experiment has unsynchronized shared writes and includes 1 in its output. Today’s list uses a separate JavaScript Web Worker.
View the original source ↗