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. I continue to get this error message and would like to know what could be the fix?
#include
#include
#include
#include //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
Solution 1.
It may happen if you have already installed openMPI and not only MPICH2, then you should execute the below commands as root:
root~# update-alternatives --config mpirun
There are 2 choices for the alternative mpirun (providing /usr/bin/mpirun).
Selection | Path | Priority | Status
*0 | /usr/bin/mpirun.openmpi | 50 | auto mode
1 | /usr/bin/mpirun.mpich2 | 40 | manual mode
2 | /usr/bin/mpirun.openmpi | 50 | manual mode
Press enter to keep the current choice[*], or type selection number: 1
Then you should select the MPICH version, as above, to run normally.
Solution 2.
Put the full path to tau_exec in the command line. It is possible that your PATH is not the same on all the nodes. Then , it would not be able to locate the executable anywhere where the path isn\'t correct.
Note:
Don\'t remane the output file. Keep the default a.out as the name of output file.


