Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ============================================================================
- Name : Berkeley.c
- Author :
- Version :
- Copyright : Your copyright notice
- Description : Hello MPI World in C
- ============================================================================
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include "mpi.h"
- int main(int argc, char* argv[]){
- int my_rank; /* rank of process */
- int p; /* number of processes */
- int source; /* rank of sender */
- int dest; /* rank of receiver */
- int tag=0; /* tag for messages */
- MPI_Status status ; /* return status for receive */
- /* start up MPI */
- MPI_Init(&argc, &argv);
- /* find out process rank */
- MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
- /* find out number of processes */
- MPI_Comm_size(MPI_COMM_WORLD, &p);
- source = 0;
- int difference[p];
- time_t t;
- srand(time(NULL) + my_rank);
- if (my_rank != 0) {
- time_t seconds;
- int diff = rand() % 2000 - 1000;
- seconds = time (NULL);
- seconds -= diff;
- printf("Process - %d\tTime - %ld \n", my_rank, seconds);
- difference[my_rank] = 0;
- MPI_Send(&seconds, 1, MPI_LONG, source, tag, MPI_COMM_WORLD);
- MPI_Barrier(MPI_COMM_WORLD);
- int adjust;
- MPI_Recv(&adjust, 1, MPI_INT, source, tag, MPI_COMM_WORLD, &status);
- printf("%d must adjust time with %d\n", my_rank, adjust);
- } else {
- time_t seconds;
- seconds = time (NULL);
- printf("master time %d\n", seconds);
- long int sec;
- int k;
- for(k = 0; k < p; k++) {
- if(k != source) {
- MPI_Recv(&sec, 1, MPI_LONG, k, tag, MPI_COMM_WORLD, &status);
- int diff = sec - seconds;
- difference[my_rank] = diff;
- printf("Process %d has difference of %d \n", k, diff);
- }
- }
- MPI_Barrier(MPI_COMM_WORLD);
- srand(time(NULL) + my_rank);
- int masterRand = rand() % 1000;
- time_t newTime = time(NULL) + masterRand;
- int masterDiff = newTime - seconds;
- printf("master difference %d\n", masterDiff);
- for(k = 0; k < p; k++) {
- if(k != source) {
- int adjust = difference[k] * (-1) + masterDiff;
- MPI_Send(&adjust, 1, MPI_INT, k, tag, MPI_COMM_WORLD);
- }
- }
- }
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement