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;
}
}

Parallelize the FOR loop that removes the multiples in the sieve code using a cyclic assignment of iterations to processes. Follow these steps when writing the MPI code 1. Base your code on the serial code from Project l 2. Make the code print \"MPI\" instead of \"serial\". 3. Include the MPI header file. 4. Initialize and finalize the MPI library. 5. Every process is allowed to read in the parameters from the command line. 6. All normal program output needs to be produced by process 0 exclusively 7. All processes must allocate the full Boolean array. In addition, process 0 must allocate a second Boolean array of the same size. 8. Execute a barrier just before the start time is taken. 9. very process has to initialize its entire array. 10. Change the FOR loop header to \"for (long i- 2 my rank; (i i) top; i comm sz)\" to obtain a cyclic assignment. 11. Reduce the results of the local arrays into the second array that only process 0 has. Use \"MPI :BOOL\" as the data type and the correct operator from Slide Cho4.46. 12. Make sure the verification code checks the proper array 13. Your code must run correctly for different numbers of processes, i e., it must produce the same result as the serial code and pass the verification. 4. Free all dynamically allocated memory before normal program termination. The timed code section should be the same as in the serial code except it also includes the reduc- tion. Dynamic memory allocation and freeing should not be timed, nor should the MPI initializa- tion or finalization.

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
}

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.
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.
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.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site