Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<mpi.h>
- #include<string.h>
- #include <unistd.h>
- typedef struct {
- int aktualne;
- int nastepne;
- int poczatkowe;
- int docelowe;
- int stan;
- } Data;
- int main(int argc, char **argv)
- {
- int floors = 8;
- int rank, size, r;
- int src, dst, tag, i;
- MPI_Status status;
- MPI_Datatype new_type;
- MPI_Datatype type[5] = { MPI_INT, MPI_INT, MPI_INT, MPI_INT, MPI_INT };
- int blen[5] = { 1, 1, 1, 1, 1};
- MPI_Aint disp[5];
- MPI_Aint base, addr;
- Data tabrecord,tabrecieved;
- MPI_Init(&argc, &argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- MPI_Comm_size(MPI_COMM_WORLD, &size);
- // if(rank == 0)
- // printf("MPI_Type_create_struct()\n");
- MPI_Get_address(&tabrecord, &base);
- MPI_Get_address(&(tabrecord.aktualne), &addr);
- disp[0] = addr - base;
- MPI_Get_address(&(tabrecord.nastepne), &addr);
- disp[1] = addr - base;
- MPI_Get_address(&(tabrecord.poczatkowe), &addr);
- disp[2] = addr - base;
- MPI_Get_address(&(tabrecord.docelowe), &addr);
- disp[3] = addr - base;
- MPI_Get_address(&(tabrecord.stan), &addr);
- disp[4] = addr - base;
- MPI_Type_create_struct(5, blen, disp, type, &new_type);
- MPI_Type_commit(&new_type);
- if(rank == 0) {
- tabrecord.aktualne = 0;
- tabrecord.nastepne = 1;
- tabrecord.docelowe = -1;
- tabrecord.poczatkowe = -1;
- tabrecord.stan = 0;
- printf("Winda znajduje sie na %d pietrze w stanie %d\n", tabrecord.aktualne, tabrecord.stan);
- for (r=1; r<size; r++){
- MPI_Bsend(&tabrecord, 1, new_type, r, 0, MPI_COMM_WORLD);
- }
- // poruszanie
- while(1)
- {
- // MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status * status)
- MPI_Recv(&tabrecieved, 1, new_type, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
- if(tabrecieved.poczatkowe != tabrecieved.aktualne) {
- printf("Winda otrzymala sygnal ze ktos chce jechac z %d do %d\n", tabrecieved.poczatkowe, tabrecieved.docelowe);
- }
- if(tabrecieved.docelowe != -1 && tabrecieved.poczatkowe != -1)
- {
- tabrecord.stan = 1;
- if(tabrecieved.poczatkowe == tabrecieved.aktualne){
- tabrecieved.poczatkowe = -1;
- }
- sleep(0.5);
- do {
- if((tabrecieved.poczatkowe == -1 && tabrecieved.docelowe > tabrecieved.aktualne) || (tabrecieved.poczatkowe != -1 && tabrecieved.poczatkowe > tabrecieved.aktualne))
- {
- tabrecieved.aktualne++;
- tabrecieved.nastepne++;
- printf("Winda jedzie w gore i jest juz na %d pietrze\n", tabrecieved.aktualne);
- }
- else if((tabrecieved.poczatkowe == -1 && tabrecieved.docelowe < tabrecieved.aktualne) || (tabrecieved.poczatkowe != -1 && tabrecieved.poczatkowe < tabrecieved.aktualne))
- {
- tabrecieved.aktualne--;
- tabrecieved.nastepne--;
- printf("Winda jedzie w dol i jest juz na %d pietrze\n", tabrecieved.aktualne);
- }
- else
- {
- if(tabrecieved.poczatkowe == tabrecieved.aktualne) {
- tabrecord.poczatkowe = -1;
- }
- printf("Winda przyjechala na %d pietro\n", tabrecieved.aktualne);
- tabrecord.stan = 0;
- tabrecord.aktualne = tabrecieved.aktualne;
- tabrecord.nastepne = tabrecieved.nastepne;
- tabrecord.docelowe = tabrecieved.docelowe;
- for (r=1; r<size; r++){
- MPI_Bsend(&tabrecord, 1, new_type, r, 0, MPI_COMM_WORLD);
- }
- }
- } while(tabrecord.stan != 0);
- }
- }
- } else {
- while(1){
- srand(time(NULL) + rank);
- MPI_Recv(&tabrecieved, 1, new_type, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
- sleep(rank);
- if(tabrecord.docelowe == tabrecieved.aktualne) {
- printf("Pasazer %d wysiada na %d pietrze\n",rank, tabrecieved.aktualne);
- break;
- }
- // inicjalizacja pasazera
- if(tabrecieved.stan == 0)
- {
- int pocz = rand() % floors;
- tabrecord.poczatkowe = pocz;
- do
- {
- tabrecord.docelowe = rand() % floors;
- } while (tabrecord.poczatkowe == tabrecord.docelowe);
- printf("Pasazer %d, chce jechac z %d do %d i wciska przycisk\n",rank, tabrecord.poczatkowe, tabrecord.docelowe);
- // int MPI_Bsend(const void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm)
- MPI_Bsend(&tabrecord, 1, new_type, 0, 0, MPI_COMM_WORLD);
- sleep(rank);
- MPI_Recv(&tabrecieved, 1, new_type, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
- if(tabrecieved.poczatkowe == -1) {
- printf("Pasazer %d, wsiada na %d pietrze i bedzie jechac do %d\n",rank, tabrecieved.aktualne, tabrecieved.docelowe);
- tabrecieved.poczatkowe = tabrecieved.aktualne;
- MPI_Bsend(&tabrecieved, 1, new_type, 0, 0, MPI_COMM_WORLD);
- } else if(tabrecord.docelowe == tabrecieved.aktualne) {
- printf("Pasazer %d wysiada na %d pietrze\n",rank, tabrecieved.aktualne);
- break;
- }
- }
- }
- }
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement