I am working with a serial code and trying to parrallize it
I am working with a serial code and trying to parrallize it. The code is suppose to implement the sieve of Eratosthenes and find the primes up to a given limit. The code that I implemented does not work, can anyone see what the problem is? Below is a snapshot of the guidelines i must follow.
#include <cstdlib>
 #include <cstdio>
 #include <sys/time.h>
 #include <mpi.h> //looks in the include directory
 
 static bool isPrime(long val)
 {
 if (val < 2) return false;
 for (long i = 2; i * i <= val; i++) {
     if ((val % i) == 0) return false;
 }
 return true;
 }
 int main(int argc, char *argv[])
 {
    int comm_sz;
    int my_rank;
MPI_Init(NULL,NULL); //initializes MPI
 MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
 MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
 
 if (my_rank == 0){
 printf(\"Prime Sieve v1.0 [MPI]\ \"); //changed \"serial\" to \"MPI\"
 }
 // check command line
 if (argc != 2) {fprintf(stderr, \"usage: %s maximum\ \", argv[0]); exit(-1);}
 const long top = atol(argv[1]);
 if (top < 23) {fprintf(stderr, \"error: maximum must be at least 23\ \"); exit(-1);}
 printf(\"computing prime numbers up to but not including %ld\ \", top);
const int my_start = my_rank * (long)top / comm_sz;
 const int my_end = (my_rank+1) * (long)top / comm_sz;
// allocate array
 bool* array = new bool[top];
// start time
 MPI_Barrier(MPI_COMM_WORLD);//barrier implemented before time start
 timeval start, end;
 gettimeofday(&start, NULL);
// initialize array
 for (long i = 2 + my_rank; (i*i) < top; i+=comm_sz) { //assigned in a cylic
     array[i] = true;
 }
// remove multiples
 for (long i = my_start; (i * i) < my_end; i++) { // just finished splitting array between processes, now
     long j = i * i;                                //I need to reduce into final array in the process 0
     while (j < top) {
       array[j] = false;
       j += i;
     }
 }
 // initiallize new array
 bool* array2 = new bool[top];
     for (long i = 2 + my_rank; (i*i) < top; i+=comm_sz) { //assigned in a cylic
     array[i] = true;
// reduction
 for (long i = my_start; i <= my_end; i++){
 // if (array[i] == true){
       MPI_Reduce(&array[i],&array2[i],1,MPI::BOOL,MPI_LXOR,0,MPI_COMM_WORLD);
  }
 
 // end time
 gettimeofday(&end, NULL);
 double runtime = end.tv_sec + end.tv_usec / 1000000.0 - start.tv_sec - start.tv_usec / 1000000.0;
 printf(\"compute time: %.4f s\ \", runtime);
// print part of result from array2
 if (my_rank == 0){
 for (long i = 2; i < 10; i++) {
     if (array2[i]) {
       printf(\" %ld\", i);
     }
 }
 printf(\" ...\");
 for (long i = top - 10; i < top; i++) {
     if (array2[i]) {
       printf(\" %ld\", i);
     }
 }
 printf(\"\ \");
// verify result
 if (top < 10000000) {
     for (long i = 2; i < top; i++) {
       if (array2[i] != isPrime(i)) {
         fprintf(stderr, \"ERROR: wrong answer for %ld\ \ \", i);
         exit(-1);
       }
     }
 }
 }
 delete [] array2;
 delete [] array;
 MPI_Finalize(); //finalizes mpi
 return 0;
 }
 }
Solution
/*MpI program*/
#include <cstdlib>
 #include <cstdio>
 #include <sys/time.h>
 #include <mpi.h>
static bool isPrime(long val)
 {
 if (val < 2) return false;
 for (long i = 2; i * i <= val; i++) {
 if ((val % i) == 0) return false;
 }
 return true;
 }
int main(int argc, char *argv[])
 {
 int comm_sz;
 int my_rank;
 MPI_Init(NULL,NULL); //initializes MPI
 MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
 MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
if (my_rank == 0){
 printf(\"Prime Sieve v1.0 [MPI]\ \"); //changed \"serial\" to \"MPI\"
 }
 m=n/p
 if(myrank.Lt.(n-p*a))Then
 m=n+1;
 End if
 if(myrank.Eq.0)then
 left=MPI_PROC_Null
 Else
 left=myrank-1
 End if
 if(myrank.Eq.p-1)thenright=MPI_PROC_Null
 elseright=anyrank+1
 Endif
 }



