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