Facebook
From Buff Butterfly, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 134
  1. #include <stdio.h>
  2. #include <mpi.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. const int NUMBER = 500;
  8.  
  9. struct User
  10. {
  11.     int id;
  12.     int age;
  13.     char name[25];
  14.     char surname[30];
  15.     float growth;
  16.     float weight;
  17. };
  18.  
  19. void randomUser(struct User* user, int id);
  20.  
  21. int main(int argc, char *argv[])
  22. {
  23.  
  24.     srand(time(NULL));
  25.  
  26.         int npes;
  27.         int myrank;
  28.         double start, end;
  29.    
  30.     MPI_Init(&argc, &argv);
  31.  
  32.         start = MPI_Wtime();
  33.         MPI_Comm_size(MPI_COMM_WORLD, &npes);
  34.         MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
  35.  
  36.     int size, tempSize;
  37.     MPI_Pack_size(2, MPI_INT, MPI_COMM_WORLD, &tempSize);
  38.     size += tempSize;
  39.     MPI_Pack_size(25, MPI_CHAR, MPI_COMM_WORLD, &tempSize);
  40.     size += tempSize;
  41.     MPI_Pack_size(30, MPI_CHAR, MPI_COMM_WORLD, &tempSize);
  42.     size += tempSize;
  43.     MPI_Pack_size(2, MPI_FLOAT, MPI_COMM_WORLD, &tempSize);
  44.     size += tempSize;
  45.     char *buffor = (char*)malloc(NUMBER*size);
  46.  
  47.         if (myrank == 0) {
  48.        
  49.         int position = 0;
  50.         struct User users[NUMBER];
  51.  
  52.         for(int i = 0; i < NUMBER; i++) {
  53.             randomUser(&users[i], i);
  54.             MPI_Pack(&users[i].id, 1, MPI_INT, buffor, NUMBER*size, &position, MPI_COMM_WORLD);
  55.             MPI_Pack(&users[i].age, 1, MPI_INT, buffor, NUMBER*size, &position, MPI_COMM_WORLD);
  56.             MPI_Pack(&users[i].name[0], 25, MPI_CHAR, buffor, NUMBER*size, &position, MPI_COMM_WORLD);
  57.             MPI_Pack(&users[i].surname[0], 30, MPI_CHAR, buffor, NUMBER*size, &position, MPI_COMM_WORLD);
  58.             MPI_Pack(&users[i].growth, 1, MPI_FLOAT, buffor, NUMBER*size, &position, MPI_COMM_WORLD);
  59.             MPI_Pack(&users[i].weight, 1, MPI_FLOAT, buffor, NUMBER*size, &position, MPI_COMM_WORLD);
  60.         }
  61.         MPI_Bsend(buffor, position, MPI_PACKED, 1, 13, MPI_COMM_WORLD);
  62.        
  63.         } else if (myrank == 1) {
  64.         MPI_Status status;
  65.         int position = 0;
  66.  
  67.         MPI_Recv(buffor, NUMBER*size, MPI_PACKED, 0, 13, MPI_COMM_WORLD, &status);
  68.         struct User users[NUMBER];
  69.         for(int i = 0; i < NUMBER; i++) {
  70.             MPI_Unpack(buffor, 500*size, &position, &users[i].id, 1, MPI_INT, MPI_COMM_WORLD);
  71.             MPI_Unpack(buffor, 500*size, &position, &users[i].age, 1, MPI_INT, MPI_COMM_WORLD);
  72.             MPI_Unpack(buffor, 500*size, &position, &users[i].name[0], 25, MPI_CHAR, MPI_COMM_WORLD);
  73.             MPI_Unpack(buffor, 500*size, &position, &users[i].surname[0], 30, MPI_CHAR, MPI_COMM_WORLD);
  74.             MPI_Unpack(buffor, 500*size, &position, &users[i].growth, 1, MPI_FLOAT, MPI_COMM_WORLD);
  75.             MPI_Unpack(buffor, 500*size, &position, &users[i].weight, 1, MPI_FLOAT, MPI_COMM_WORLD);
  76.             // printf("Process 1 received user info from process 0:\nid: %d\nage: %d\nname: %s\nsurname: %s\ngrowth: %.2f\nweight: %.1f\n --------------------------- \n",
  77.             //                                                 users[i].id, users[i].age, users[i].name, users[i].surname, users[i].growth, users[i].weight);
  78.         }
  79.        
  80.        
  81.                
  82.                
  83.         }
  84.  
  85.         end = MPI_Wtime();
  86.         MPI_Finalize();
  87.  
  88.         if (myrank == 0) {
  89.                 printf("end of process %lf\n", end-start);
  90.         }
  91.         return 0;
  92. }
  93.  
  94. void randomUser(struct User* user, int id) {
  95.     user->id = id;
  96.     user->age = (rand() % 30) + 18;
  97.  
  98.     int nameLength = (rand() % 21) + 5;
  99.     for(int i = 0; i < nameLength; i++) {
  100.         user->name[i] = 'a' + (rand() % 26);
  101.     }
  102.     user->name[nameLength] = 0;
  103.  
  104.     int surnameLength = (rand() % 26) + 5;
  105.     for(int i = 0; i < nameLength; i++) {
  106.         user->surname[i] = 'a' + (rand() % 26);
  107.     }
  108.     user->surname[surnameLength] = 0;
  109.     user->growth = (2.0 - 1.3) * (rand() / (double)RAND_MAX) + 1.3;
  110.     user->weight = (120.0 - 40.0) * (rand() / (double)RAND_MAX) + 40.0;
  111. }