Advertisement
Guest User

Untitled

a guest
May 21st, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. s#include <mpi.h>
  2. #include <time.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #define PROC_MASTER 0
  7.  
  8. typedef struct double_int{
  9.     double val;
  10.     int proc_id;
  11. }double_int;
  12.  
  13. int main(int argc, char **argv)
  14. {
  15.     int n_proc, proc_id;
  16.  
  17.     double *x = (double *)malloc(10*sizeof(double));
  18.  
  19.     MPI_Init(&argc, &argv);
  20.  
  21.     MPI_Comm_size(MPI_COMM_WORLD, &n_proc);
  22.     MPI_Comm_rank(MPI_COMM_WORLD, &proc_id);
  23.  
  24.     double_int num;
  25.     double_int num_min;
  26.  
  27.     srand(time(NULL));
  28.    
  29.     int i;
  30.     for(i = 0; i < 1000; i++){
  31.         if(i == 20 && proc_id == 2){
  32.             MPI_Finalize();
  33.             exit(EXIT_SUCCESS);
  34.         }
  35.         if(proc_id == PROC_MASTER){
  36.             num.val = rand()/(double)RAND_MAX;
  37.             num.proc_id = proc_id;
  38.         }
  39.         else if(proc_id == 1){
  40.             num.val = rand()/(double)RAND_MAX;
  41.             num.proc_id = proc_id;
  42.         }
  43.         else if(proc_id == 2){
  44.             num.val = rand()/(double)RAND_MAX;
  45.             num.proc_id = proc_id;
  46.         }
  47.         else if(proc_id == 3){
  48.                     num.val = rand()/(double)RAND_MAX;
  49.                     num.proc_id = proc_id;
  50.         }
  51.        
  52.         MPI_Allreduce(&num, &num_min, 1, MPI_DOUBLE_INT, MPI_MINLOC, MPI_COMM_WORLD);
  53.  
  54.         if(proc_id == PROC_MASTER){
  55.             printf("iter %d: ricevuto f = %f dal processo %d\n", i, num_min.val, num_min.proc_id);
  56.         }
  57.         MPI_Bcast(x, 10, MPI_DOUBLE, num_min.proc_id, MPI_COMM_WORLD);
  58.     }
  59.  
  60.     MPI_Finalize();
  61.  
  62.     exit(EXIT_SUCCESS);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement