Evolution of a Parallel Program
Serial Implementation - C
We use a simple iterative method to test for primality for each
integer in a range. Two functions, one that takes as argument an
array of two integers, the range, and tests each integer within. The
other, a function that tests an integer for least divisors.
int isPrime(int testNum) {
int i;
if(testNum < 2) return 0;
if(testNum == 2) return 1;
for( i=2; i < sqrt(testNum)+1; i++)
if (!(testNum % i)) /* remainder 0 indicates a divisor */
return 0;
return 1; /* no divisors, a prime */
}
void findPrimes (int Params[]) {
const int lbound = Params[0];
const int ubound = Params[1];
int i, prime_count = 0;
for(i = lbound; i < ubound; i++) {
if (isPrime(i)) prime_count++;
}
}
Control of the program is composed of setting a default range [0..100]
and reading possible command line arguments specifying a different
range.
int main(int argc, char* argv[]) {
int i;
int Range[2] = {0, 100};
if (argc == 3) { /* default range [0..100] */
for (i = 1; i
The complete source for the original program can be found as
prime.c. The
program can be compiled and run with something like: [here is the
makefile]
klamath:[prime]> make prime
gcc -g -Wall -pg prime.c -o prime -lm
klamath:[prime]> prime 0 10000
There are 1229 primes. [0 .. 1.0e+04]