samedi 26 septembre 2015

Random method gives the same number in MPI program if called multiple times

I am writing a MPI program to generate random number and call MPI_Reduce() to get the max number. The random method returns the same number for the same process for different call. Here is my code

void printArray(float *array, int size, int rank){
 int index;
 printf("Rank: %d, elements: ", rank);
 for(index=0; index<size; index++){
      printf("%f \t",array[index]);
 }

 printf("\n");
}
int main (int argc, char* argv[]) {

     int rank, root, size, index;
     int N = 2;
     float rcvBuffer; 
     float localmax;

     MPI_Status status;  
     MPI_Init (&argc, &argv);      /* starts MPI */
     MPI_Comm_rank (MPI_COMM_WORLD, &rank);        /* get current process id */
     MPI_Comm_size (MPI_COMM_WORLD, &size);




     root = 0;
     float *array = (float *)malloc(sizeof(float)*N);

     for(index=0; index<N; index++) {
          // i hv to create between 0 and 1. so need to modify
          srand(rank); // to generate different seed value
          localmax = random()/100000000;
          array[index] = localmax;
          MPI_Reduce(&localmax, &rcvBuffer, 1, MPI_FLOAT, MPI_MAX, root, MPI_COMM_WORLD);

          if(rank==root) {
          printf("Process: %d MPI_MAX result is %f \n", rank, rcvBuffer);
       }
     }
     printArray(array, N, rank);

     MPI_Finalize();

     return 0;
}

here is the output:

Rank: 3, elements: 12.000000    12.000000
Rank: 2, elements: 15.000000    15.000000
Rank: 1, elements: 18.000000    18.000000
Process: 0 MPI_MAX result is 18.000000
Process: 0 MPI_MAX result is 18.000000
Rank: 0, elements: 18.000000    18.000000

The program is returning exactly the same value for localmax for all iteration. I am not sure why it is happening. Somehow I doubt the MPI_Reduce() call in side the for loop. So basically I want to know two things 1. Why random() is not returning the random value each time? 2. is it fine to call MPI_Reduce() multiple times in the for loop?




Aucun commentaire:

Enregistrer un commentaire