Aaaaa988

gavno

Nov 14th, 2021
714
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <mpi.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. struct Vector_clock {
  7.   int rank;
  8.   int nprocs;
  9.   int *vc_mas;
  10.   MPI_Comm comm;
  11. };
  12.  
  13. struct Vector_clock *vc_create(MPI_Comm comm)
  14. {
  15.   struct Vector_clock *vc = malloc(sizeof(*Vector_clock));
  16.   if (!vc) return NULL;
  17.   vc->comm = comm;
  18.   MPI_Comm_size(comm, &vc->nprocs);
  19.   MPI_Comm_rank(comm, &vc->rank);
  20.   vc->vc_mas = malloc(sizeof(*(vc->vc_mas)) * vc->nprocs);
  21.   if (!vc->vc_mas) {
  22.     free(vc);
  23.    
  24.     return NULL;
  25.   }
  26.   for (int i = 0; i < vc->nprocs; i++)
  27.     vc->vc_mas[i] = 0;
  28.  
  29.   return vc;
  30. }
  31.  
  32. void vc_free(struct Vector_clock *vc)
  33. {
  34.   free(vc->vc_mas);
  35.   free(vc);
  36. }
  37.  
  38. void vc_print_clock(Vector_clock *vc)
  39. {
  40.   printf("\nrank %d clock: ", vc->rank);
  41.   for (int i = 0; i < vc->vc_mas[i]; i++) printf("%d ", vc->vc_mas[i]);
  42.   printf("\n");
  43. }
  44.  
  45. void vc_event(Vector_clock *vc, char *event)
  46. {
  47.   vc->vc_mas[vc->rank]++;
  48.   printf("rank %d event '%s' time %d\n", vc->rank, event, vc->vc_mas[vc->rank]);
  49. }
  50.  
  51. int ds_send(Vector_clock *vc, void *buf, int count, MPI_Datatype datatype, int dest, int tag)
  52. {
  53.   int userbuf_size, clockbuf_size, packbuf_size;
  54.   MPI_Pack_size(count, datatype, vc->comm, &userbuf_size);
  55.   MPI_Pack_size(vc->nprocs, MPI_INT, vc->comm, &clockbuf_size);
  56.   packbuf_size = userbuf_size + clockbuf_size;
  57.   char *outbuf = malloc(sizeof(*buf) * packbuf_size);
  58.   if (NULL == outbuf) {
  59.       fprintf(stderr, "error: no enough memory\n");
  60.       MPI_Abort(ds->comm, EXIT_FAILURE);
  61.   }
  62.   int position = 0;
  63.   MPI_Pack(buf, count, datatype, outbuf, packbuf_size, &position, ds->comm);
  64.   MPI_Pack(ds->vc_mas, ds->nprocs, MPI_INT, outbuf, packbuf_size, &position, ds->comm);
  65.   MPI_Send(outbuf, position, MPI_PACKED, dest, tag, ds->comm);
  66.   free(outbuf);
  67. }
  68.  
  69. int ds_recv(ds_t *ds, void *buf, int count, MPI_Datatype datatype, int src, int tag)
  70. {
  71.   int userbuf_size, clockbuf_size, packbuf_size, comm_size;
  72.   MPI_Pack_size(count, datatype, ds->comm, &userbuf_size);
  73.   MPI_Pack_size(ds->nprocs, MPI_INT, ds->comm, &clockbuf_size);
  74.   packbuf_size = userbuf_size + clockbuf_size;
  75.   char *inbuf = malloc(sizeof(*buf) * packbuf_size);
  76.   int *vc = malloc(sizeof(*vc) * ds->nprocs);
  77.   if (NULL == inbuf || NULL == vc) {
  78.       fprintf(stderr, "error: no enough memory\n");
  79.       MPI_Abort(ds->comm, EXIT_FAILURE);
  80.   }
  81.   MPI_Recv(inbuf, packbuf_size, MPI_PACKED, src, tag, ds->comm, MPI_STATUS_IGNORE);
  82.   int position = 0;
  83.   MPI_Unpack(inbuf, packbuf_size, &position, buf, count, datatype, ds->comm);
  84.   MPI_Unpack(inbuf, packbuf_size, &position, vc, ds->nprocs, MPI_INT, ds->comm);
  85.   free(inbuf);
  86.   for (int i = 0; i < ds->nprocs; i++) {
  87.       ds->vc_mas[i] = vc[i] > ds->vc_mas[i] ? vc[i] : ds->vc_mas[i];
  88.   }
  89.   ds->vc_mas[ds->rank]++;
  90.   free(vc);
  91. }
  92.  
  93. int main(int argc, char **argv)
  94. {
  95.   MPI_Comm comm = MPI_COMM_WORLD;
  96.   int rank, buf;
  97.   MPI_Init(&argc, &argv);
  98.   MPI_Comm_rank(comm, &rank);
  99.  
  100.   ds_t *ds = ds_create(comm);
  101.  
  102.   if (rank != 0)
  103.         ds_recv(ds, &buf, 1, MPI_INT, (rank - 1)/2, 0)
  104.   else
  105.     buf = rank;
  106.  
  107.     ds_event(ds, "event1");
  108.     ds_event(ds, "event2");
  109.     ds_event(ds, "event3");
  110.    
  111.     if (ds->nprocs > rank * 2 + 1) ds_send(ds, &buf, 1, MPI_INT, rank * 2 + 1, 0);
  112.     if (ds->nprocs > rank * 2 + 2) ds_send(ds, &buf, 1, MPI_INT, rank * 2 + 2, 0);
  113.    
  114.     ds_free(ds);
  115.   MPI_Finalize();
  116.  
  117.   return 0;
  118. }
  119.  
RAW Paste Data