Advertisement
--sas

apr1-gather-gatherv.c

Apr 1st, 2022
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <mpi.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include "l17.h"
  6.  
  7. int main(int argc, char** argv) {
  8.     int rc;
  9.     rc = MPI_Init(&argc, &argv);
  10.     L17_CHECK_ERROR(MPI_Init, rc);
  11.  
  12.     int size;
  13.     rc = MPI_Comm_size(MPI_COMM_WORLD, &size);
  14.     L17_CHECK_ERROR(MPI_Comm_size, rc);
  15.  
  16.     int rank;
  17.     rc = MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  18.     L17_CHECK_ERROR(MPI_Comm_rank, rc);
  19.  
  20.     srand(rank + 1);
  21.     int sendcount = rand() % 7 + 1;
  22.  
  23.     double* sendbuf;
  24.     sendbuf = (double*) malloc(sendcount * sizeof(double));
  25.     if (sendbuf == NULL) {
  26.         printf("Error. Couldn't allocate memory.");
  27.         MPI_Abort(MPI_COMM_WORLD, 1);
  28.     }
  29.  
  30.     int i;
  31.     for (i = 0; i < sendcount; ++i)
  32.         sendbuf[i] = sqrt(rank);
  33.  
  34.     int* recvcounts;
  35.     if (rank == 0) {
  36.         recvcounts = (int*) malloc(size * sizeof(int));
  37.         if (recvcounts == NULL) {
  38.             printf("Error. Couldn't allocate memory.");
  39.             MPI_Abort(MPI_COMM_WORLD, 3);
  40.         }
  41.     }
  42.  
  43.     rc = MPI_Gather(&sendcount, 1, MPI_INT, recvcounts, 1, MPI_INT, 0, MPI_COMM_WORLD);
  44.     L17_CHECK_ERROR(MPI_Gather, rc);
  45.  
  46.     int* displs;
  47.     if (rank == 0) {
  48.         displs = (int*) malloc(size * sizeof(int));
  49.         if (displs == NULL) {
  50.             printf("Error. Couldn't allocate memory.");
  51.             MPI_Abort(MPI_COMM_WORLD, 4);
  52.         }
  53.  
  54.         displs[0] = 0;
  55.         for (i = 1; i < size; ++i)
  56.             displs[i] = displs[i - 1] + recvcounts[i - 1];
  57.     }
  58.  
  59.     double* recvbuf;
  60.     int recvbufsize;
  61.  
  62.     if (rank == 0) {
  63.         recvbufsize = displs[size - 1] + recvcounts[size - 1];
  64.         recvbuf = (double*) malloc(recvbufsize * sizeof(double));
  65.         if (recvbuf == NULL) {
  66.             printf("Error. Couldn't allocate memory.");
  67.             MPI_Abort(MPI_COMM_WORLD, 2);
  68.         }
  69.     }
  70.  
  71.     rc = MPI_Gatherv(sendbuf, sendcount, MPI_DOUBLE, recvbuf, recvcounts, displs, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  72.     L17_CHECK_ERROR(MPI_Gatherv, rc);
  73.  
  74.     free(sendbuf);
  75.  
  76.     if (rank == 0) {
  77.         FILE* outputp = fopen("apr1-gather-gatherv.dat", "w");
  78.         if (outputp == NULL) {
  79.             printf("Error. Couldn't open the file for output values.");
  80.             MPI_Abort(MPI_COMM_WORLD, 5);
  81.         }
  82.  
  83.         for (i = 0; i < size; ++i) {
  84.             int j;
  85.             for (j = 0; j < recvcounts[i]; ++j)
  86.                 fprintf(outputp, "%f ", recvbuf[displs[i] + j]);
  87.             fprintf(outputp, "\n");
  88.         }
  89.  
  90.         free(displs);
  91.         free(recvcounts);
  92.         fclose(outputp);
  93.         free(recvbuf);
  94.     }
  95.  
  96.     MPI_Finalize();
  97.  
  98.     return 0;
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement