--sas

apr1-gatherv.c

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