Advertisement
Guest User

winda

a guest
Nov 17th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.86 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<mpi.h>
  3. #include<string.h>
  4. #include <unistd.h>
  5.  
  6. typedef struct {
  7.     int aktualne;
  8.     int nastepne;
  9.     int poczatkowe;
  10.     int docelowe;
  11.     int stan;
  12. } Data;
  13.  
  14.  
  15. int main(int argc, char **argv)
  16. {
  17.     int floors = 8;
  18.     int rank, size, r;
  19.     int src, dst, tag, i;  
  20.     MPI_Status status;
  21.     MPI_Datatype new_type;
  22.     MPI_Datatype type[5] = { MPI_INT, MPI_INT, MPI_INT, MPI_INT, MPI_INT };
  23.     int blen[5] = { 1, 1, 1, 1, 1};
  24.     MPI_Aint disp[5];
  25.     MPI_Aint base, addr;
  26.     Data tabrecord,tabrecieved;
  27.  
  28.     MPI_Init(&argc, &argv);
  29.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  30.     MPI_Comm_size(MPI_COMM_WORLD, &size);
  31.  
  32.     // if(rank == 0)
  33.     //  printf("MPI_Type_create_struct()\n");
  34.  
  35.     MPI_Get_address(&tabrecord, &base);
  36.     MPI_Get_address(&(tabrecord.aktualne), &addr);
  37.     disp[0] = addr - base;
  38.     MPI_Get_address(&(tabrecord.nastepne), &addr);
  39.     disp[1] = addr - base;
  40.     MPI_Get_address(&(tabrecord.poczatkowe), &addr);
  41.     disp[2] = addr - base;
  42.     MPI_Get_address(&(tabrecord.docelowe), &addr);
  43.     disp[3] = addr - base;
  44.     MPI_Get_address(&(tabrecord.stan), &addr);
  45.     disp[4] = addr - base;
  46.    
  47.     MPI_Type_create_struct(5, blen, disp, type, &new_type);
  48.     MPI_Type_commit(&new_type);
  49.  
  50.     if(rank == 0) {
  51.         tabrecord.aktualne = 0;
  52.         tabrecord.nastepne = 1;
  53.         tabrecord.docelowe = -1;
  54.         tabrecord.poczatkowe = -1;
  55.         tabrecord.stan = 0;
  56.         printf("Winda znajduje sie na %d pietrze w stanie %d\n", tabrecord.aktualne, tabrecord.stan);
  57.         for (r=1; r<size; r++){
  58.             MPI_Bsend(&tabrecord, 1, new_type, r, 0, MPI_COMM_WORLD);
  59.         }
  60.         // poruszanie
  61.         while(1)
  62.         {
  63.             // MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status * status)
  64.             MPI_Recv(&tabrecieved, 1, new_type, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
  65.             if(tabrecieved.poczatkowe != tabrecieved.aktualne) {
  66.                 printf("Winda otrzymala sygnal ze ktos chce jechac z %d do %d\n", tabrecieved.poczatkowe, tabrecieved.docelowe);
  67.             }
  68.             if(tabrecieved.docelowe != -1 && tabrecieved.poczatkowe != -1)
  69.             {
  70.                 tabrecord.stan = 1;
  71.  
  72.                 if(tabrecieved.poczatkowe == tabrecieved.aktualne){
  73.                     tabrecieved.poczatkowe = -1;
  74.                 }
  75.  
  76.                 sleep(0.5);
  77.                 do {
  78.                     if((tabrecieved.poczatkowe == -1 && tabrecieved.docelowe > tabrecieved.aktualne) || (tabrecieved.poczatkowe != -1 && tabrecieved.poczatkowe > tabrecieved.aktualne))
  79.                     {
  80.                         tabrecieved.aktualne++;
  81.                         tabrecieved.nastepne++;
  82.                         printf("Winda jedzie w gore i jest juz na %d pietrze\n", tabrecieved.aktualne);
  83.                     }
  84.                     else if((tabrecieved.poczatkowe == -1 && tabrecieved.docelowe < tabrecieved.aktualne) || (tabrecieved.poczatkowe != -1 && tabrecieved.poczatkowe < tabrecieved.aktualne))
  85.                     {
  86.                         tabrecieved.aktualne--;
  87.                         tabrecieved.nastepne--;
  88.                         printf("Winda jedzie w dol i jest juz na %d pietrze\n", tabrecieved.aktualne);
  89.                     }
  90.                     else
  91.                     {
  92.                         if(tabrecieved.poczatkowe == tabrecieved.aktualne) {
  93.                             tabrecord.poczatkowe = -1;
  94.                         }
  95.                         printf("Winda przyjechala na %d pietro\n", tabrecieved.aktualne);
  96.                         tabrecord.stan = 0;
  97.                         tabrecord.aktualne = tabrecieved.aktualne;
  98.                         tabrecord.nastepne = tabrecieved.nastepne;
  99.                         tabrecord.docelowe = tabrecieved.docelowe;
  100.                        
  101.                         for (r=1; r<size; r++){
  102.                             MPI_Bsend(&tabrecord, 1, new_type, r, 0, MPI_COMM_WORLD);
  103.                         }
  104.                     }
  105.                
  106.  
  107.                 } while(tabrecord.stan != 0);
  108.             }
  109.         }
  110.     } else {
  111.         while(1){
  112.             srand(time(NULL) + rank);
  113.             MPI_Recv(&tabrecieved, 1, new_type, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
  114.             sleep(rank);
  115.  
  116.  
  117.             if(tabrecord.docelowe == tabrecieved.aktualne) {
  118.                 printf("Pasazer %d wysiada na %d pietrze\n",rank, tabrecieved.aktualne);
  119.                 break;
  120.             }
  121.            
  122.             // inicjalizacja pasazera
  123.  
  124.             if(tabrecieved.stan == 0)
  125.             {
  126.                 int pocz = rand() % floors;
  127.                 tabrecord.poczatkowe = pocz;
  128.                 do
  129.                 {
  130.                     tabrecord.docelowe = rand() % floors;
  131.                 } while (tabrecord.poczatkowe == tabrecord.docelowe);
  132.  
  133.                 printf("Pasazer %d, chce jechac z %d do %d i wciska przycisk\n",rank, tabrecord.poczatkowe, tabrecord.docelowe);
  134.                 // int MPI_Bsend(const void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm)
  135.                 MPI_Bsend(&tabrecord, 1, new_type, 0, 0, MPI_COMM_WORLD);
  136.                 sleep(rank);
  137.                
  138.                 MPI_Recv(&tabrecieved, 1, new_type, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
  139.  
  140.                 if(tabrecieved.poczatkowe == -1) {
  141.                     printf("Pasazer %d, wsiada na %d pietrze i bedzie jechac do %d\n",rank, tabrecieved.aktualne, tabrecieved.docelowe);
  142.                     tabrecieved.poczatkowe = tabrecieved.aktualne;
  143.                     MPI_Bsend(&tabrecieved, 1, new_type, 0, 0, MPI_COMM_WORLD);
  144.                 } else if(tabrecord.docelowe == tabrecieved.aktualne) {
  145.                     printf("Pasazer %d wysiada na %d pietrze\n",rank, tabrecieved.aktualne);
  146.                     break;
  147.                 }
  148.             }  
  149.  
  150.         }
  151.     }
  152.  
  153.     MPI_Finalize();
  154.     return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement