Advertisement
AlexCioata

Berkeley

Dec 9th, 2016
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.22 KB | None | 0 0
  1. /*
  2.  ============================================================================
  3.  Name        : Berkeley.c
  4.  Author      :
  5.  Version     :
  6.  Copyright   : Your copyright notice
  7.  Description : Hello MPI World in C
  8.  ============================================================================
  9.  */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <time.h>
  14. #include "mpi.h"
  15.  
  16. int main(int argc, char* argv[]){
  17.     int  my_rank; /* rank of process */
  18.     int  p;       /* number of processes */
  19.     int source;   /* rank of sender */
  20.     int dest;     /* rank of receiver */
  21.     int tag=0;    /* tag for messages */
  22.     MPI_Status status ;   /* return status for receive */
  23.    
  24.     /* start up MPI */
  25.    
  26.     MPI_Init(&argc, &argv);
  27.    
  28.     /* find out process rank */
  29.     MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  30.    
  31.     /* find out number of processes */
  32.     MPI_Comm_size(MPI_COMM_WORLD, &p);
  33.    
  34.     source = 0;
  35.     int difference[p];
  36.     time_t t;
  37.     srand(time(NULL) + my_rank);
  38.  
  39.     if (my_rank != 0) {
  40.         time_t seconds;
  41.         int diff = rand() % 2000 - 1000;
  42.         seconds = time (NULL);
  43.         seconds -= diff;
  44.         printf("Process - %d\tTime - %ld \n", my_rank, seconds);
  45.         difference[my_rank] = 0;
  46.         MPI_Send(&seconds, 1, MPI_LONG, source, tag, MPI_COMM_WORLD);
  47.  
  48.         MPI_Barrier(MPI_COMM_WORLD);
  49.  
  50.         int adjust;
  51.         MPI_Recv(&adjust, 1, MPI_INT, source, tag, MPI_COMM_WORLD, &status);
  52.  
  53.         printf("%d must adjust time with %d\n", my_rank, adjust);
  54.     } else {
  55.         time_t seconds;
  56.         seconds = time (NULL);
  57.         printf("master time %d\n", seconds);
  58.         long int sec;
  59.         int k;
  60.         for(k = 0; k < p; k++) {
  61.             if(k != source) {
  62.                 MPI_Recv(&sec, 1, MPI_LONG, k, tag, MPI_COMM_WORLD, &status);
  63.                 int diff = sec - seconds;
  64.                 difference[my_rank] = diff;
  65.                 printf("Process %d has difference of %d \n", k, diff);
  66.             }
  67.         }
  68.  
  69.         MPI_Barrier(MPI_COMM_WORLD);
  70.  
  71.         srand(time(NULL) + my_rank);
  72.         int masterRand = rand() % 1000;
  73.         time_t newTime = time(NULL) + masterRand;
  74.         int masterDiff = newTime - seconds;
  75.         printf("master difference %d\n", masterDiff);
  76.  
  77.         for(k = 0; k < p; k++) {
  78.             if(k != source) {
  79.                 int adjust = difference[k] * (-1) + masterDiff;
  80.                 MPI_Send(&adjust, 1, MPI_INT, k, tag, MPI_COMM_WORLD);
  81.             }
  82.         }
  83.     }
  84.  
  85.     MPI_Finalize();
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement