/******* ** ** To compile for ** pcc parsieve.pc -sync mpc -arch mpi -o parsieve.par ** Adapted for PARSEC 10-9-97 by Monnica Terwilliger ** *******/ /********************************************************************** ** ** This is a parallel version of the sieve.pc program. ** An END message is added to detect termination condition. ** The program terminates when the last sieve receives an END message. ** ** **********************************************************************/ /********************************************************************** ** ** Sieve of Eratosthenes ** ** This program implements the Sieve Of Eratosthenes algorithm to ** generate prime numbers from a sequence of consecutive natural ** numbers beginning with the smallest prime, 2. ** ** The entities used: ** (1) sieve: Each instance of sieve entity stands ** for a prime number. ** (2) driver: Initiate the program and generate the ** sequence of consecutive natural numbers. ** **********************************************************************/ #include #include entity sieve(int); message Number { int number; }; message End {}; /********** ** ** SIEVE: The first message contains the prime number ** represented by the current instance of sieve entity. ** The entity creates another instance of sieve and then ** enters a loop. In the loop. the entity accepts all ** subsequent numbers from its creator and pass only ** those number not a multiple of its prime number. ** **********/ entity sieve(int myno) { ename next_sieve; int myprime; receive (Number number) { myprime = number.number; } or receive (End signal) { pc_exit(1); } pc_printf("Sieve number %d is for prime number %d\n", myno, myprime); next_sieve = new sieve( myno+1 ) at (myno); for(;;) { receive (Number number) /* A new number!! */ { if(number.number % myprime) /* Multiple of myprime? */ send number to next_sieve; /* If not, pass to the */ } /* next sieve entity. */ or receive (End end) { send end to next_sieve; } } } /********** ** ** DRIVER: Ask for how many natural numbers to generate, create the ** first sieve entity, and then generate the sequence of ** consecutive natural numbers and send to the first sieve. ** **********/ entity driver(int argc, char **argv) { int i, n; ename first; n = 300; if(argc > 1) n = atoi(argv[1]); first = new sieve(1) ; for(i = 2 ; i <= n; ++i) { send Number { i } to first ; }; send End {} to first ; }