Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<stdio.h>
- #include<mpi.h>
- using namespace std;
- class Pouso{
- private:
- int codigo;
- int origem;
- int horarioChegada;
- int tempoVoo;
- public:
- Pouso(int codigo, int origem, int horarioChegada, int tempoVoo)
- {
- this->codigo = codigo;
- this->origem = origem;
- this->horarioChegada = horarioChegada;
- this->tempoVoo = tempoVoo;
- }
- //getters
- int getCodigo()
- {
- return(this->codigo);
- }
- int getOrigem()
- {
- return(this->origem);
- }
- int getHorarioChegada()
- {
- return(this->horarioChegada);
- }
- int getTempoVoo()
- {
- return(this->tempoVoo);
- }
- void setHorarioChegada(int horarioChegada)
- {
- this->horarioChegada = horarioChegada;
- }
- };
- class Decolagem{
- private:
- int codigo;
- int destino;
- int horarioPartida;
- int tempoVoo;
- public:
- Decolagem(int codigo, int destino, int horarioPartida, int tempoVoo)
- {
- this->codigo = codigo;
- this->destino = destino;
- this->horarioPartida = horarioPartida;
- this->tempoVoo = tempoVoo;
- }
- //getters
- int getCodigo()
- {
- return(this->codigo);
- }
- int getDestino()
- {
- return(this->destino);
- }
- int getHorarioPartida()
- {
- return(this->horarioPartida);
- }
- int getTempoVoo()
- {
- return(this->tempoVoo);
- }
- void setHorarioPartida(int horarioPartida)
- {
- this->horarioPartida = horarioPartida;
- }
- };
- class Aeroporto{
- private:
- static int StaticCodigo;
- int codigo;
- vector<Pouso> pousos;
- int numeroPousos;
- vector<Decolagem> decolagens;
- int numeroDecolagens;
- int relogio;
- vector<int> qtdMensagensEnviar;
- vector<int> qtdMensagensReceber;
- public:
- Aeroporto(int codigo, int comm_size)
- {
- //string cod;
- //char *fileName= "entrada";
- this->StaticCodigo++;
- this->codigo = codigo;
- this->relogio = 0;
- numeroPousos = 0;
- numeroDecolagens = 0;
- qtdMensagensReceber.assign(comm_size, 0);
- qtdMensagensEnviar.assign(comm_size, 0);
- /*cod = to_string(codigo);
- strcat(fileName, cod.c_str())
- FILE *f;
- f = fopen(fileName, "r");*/
- }
- //getters
- int getCodigo()
- {
- return(this->codigo);
- }
- void getDecolagem()
- {
- vector<Decolagem>::iterator i;
- cout << "Decolagens Destino Horario_Partida Tempo_Voo\n";
- for(i = (this->decolagens).begin(); i < (this->decolagens).end(); i++)
- {
- cout << i->getCodigo()<< "\t " << i->getDestino() << "\t " << i->getHorarioPartida() << "\t\t " << i->getTempoVoo() << endl;
- }
- }
- void getPouso()
- {
- vector<Pouso>::iterator i;
- cout << "Pousos Origem Horario_Chegada Tempo_Voo\n";
- for(i = (this->pousos).begin(); i < (this->pousos).end(); i++)
- {
- cout << i->getCodigo()<< "\t " << i->getOrigem() << "\t " << i->getHorarioChegada() << "\t\t " << i->getTempoVoo() << endl;
- }
- }
- //setters
- void SetDecolagem()
- {
- int n, j = 0,destino, horarioPartida, tempoVoo;
- Decolagem *aux;
- cout << "Informe o numero de Decolagens: ";
- cin >> this->numeroDecolagens;
- n = this->numeroDecolagens;
- do
- {
- n = n / 10;
- j++;
- }while(n > 0);
- for(int i = 0;i < this->numeroDecolagens; i++)
- {
- cout << "Informe o Destino: ";
- cin >> destino;
- cout << "Informe a horada da Partdia: ";
- cin >> horarioPartida;
- cout << "Informe o tempo de voo: ";
- cin >> tempoVoo;
- aux = new Decolagem(this->codigo * j * 10 + (i+1) , destino, horarioPartida, tempoVoo);
- (this->decolagens).push_back(*aux);
- }
- }
- void setPousos(int aux[])
- {
- Pouso novo(aux[0], aux[1], aux[2], aux[3]);
- pousos.push_back(novo);
- this->numeroPousos += 1;
- }
- //functions
- void aviso(int comm_size, int rank) //avisa quantas mensagens o processo rank vai enviar aos outros procesos
- {
- //vector<int> qtdMensagens(comm_size, 0);
- vector<Decolagem>::iterator i;
- int tag = 99;
- for(i = this->decolagens.begin(); i < this->decolagens.end(); i++)
- {
- qtdMensagensEnviar[i->getDestino() - 1] += 1;
- }
- /*for(int j = 0; j < comm_size; j++)
- {
- cout << " qtdMensagensEnviar[" << j << "] " << qtdMensagensEnviar[j] << endl;
- }*/
- for(int j = 0; j < comm_size; j++)
- {
- if(j != rank)
- {
- //cout << "Enviando para " << j << " " << qtdMensagensEnviar[j] << endl;
- MPI_Send(&qtdMensagensEnviar[j], 1, MPI_INT, j, tag, MPI_COMM_WORLD);
- }
- }
- }
- void entendido(int comm_size, int rank)
- {
- //vector<int> qtdMensagens(comm_size, 0);
- int tag = 99;
- //cout << "recebendo do " << rank << endl;
- MPI_Recv(&qtdMensagensReceber[rank], 1, MPI_INT, rank, tag, MPI_COMM_WORLD,MPI_STATUS_IGNORE);
- //cout << "qtdMensagensReceber[" << rank << "] " << qtdMensagensReceber[rank] << endl;
- /*for(int i = 0; i < comm_size; i++)
- {
- if(i != rank)
- {
- cout << "recebendo do " << i << endl;
- MPI_Recv(&qtdMensagensReceber[i], 1, MPI_INT, i, tag, MPI_COMM_WORLD,MPI_STATUS_IGNORE);
- cout << "qtdMensagensReceber[" << i << "] " << qtdMensagensReceber[i] << endl;
- }
- }*/
- }
- void enviar(int comm_size)
- {
- int rank = this->getCodigo() - 1;
- int tag = 99, aux[4];
- vector<Decolagem>::iterator it;
- for(int i = 0; i < comm_size; i++)
- {
- if(i != rank)
- {
- for(it = this->decolagens.begin(); it < this->decolagens.end(); it++)
- {
- if((it->getDestino() - 1) == i)
- {
- //cout << "Enviando para " << it->getDestino() << endl;
- aux[0] = it->getCodigo();
- aux[1] = rank + 1;
- aux[2] = it->getHorarioPartida() + it->getTempoVoo();
- aux[3] = it->getTempoVoo();
- MPI_Send(aux, 4, MPI_INT, i, tag, MPI_COMM_WORLD);
- }
- }
- }
- }
- cout << endl;
- }
- void receber(int comm_size, int i)
- {
- //int rank = this->getCodigo() - 1;
- int aux[4], tag = 99;
- for(int j = 0; j < qtdMensagensReceber[i]; j++)
- {
- //cout << "Recebendo do " << i + 1<< endl;
- MPI_Recv(aux, 4, MPI_INT, i, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- setPousos(aux);
- }
- cout << endl;
- /* for(int i = 0; i < comm_size; i++)
- {
- if(i != rank)
- {
- for(int j = 0; j < qtdMensagensReceber[i]; j++)
- {
- cout << "Recebendo do " << i + 1<< endl;
- MPI_Recv(aux, 4, MPI_INT, i, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- setPousos(aux);
- }
- }
- }*/
- }
- void imprimir()
- {
- cout << "Codigo: " << endl;
- getPouso();
- getDecolagem();
- cout << endl;
- }
- void tratar()
- {
- vector<Pouso>::iterator i, j;
- vector<Decolagem>::iterator k, z;
- int offset = 1;
- for(i = this->pousos.begin(); i < this->pousos.end(); i++)
- {
- for(j = this->pousos.begin(); j < this->pousos.end(); j++)
- {
- if(i != j)
- {
- if(i->getHorarioChegada() == j->getHorarioChegada())
- {
- if(i->getTempoVoo() > j->getTempoVoo())
- {
- for(int n = 0; n < this->pousos.size(); n++)
- {
- if(pousos[n].getHorarioChegada() == j->getHorarioChegada() + offset)
- {
- n = -1;
- offset++;
- }
- }
- j->setHorarioChegada(j->getHorarioChegada() + offset);
- }
- else
- {
- for(int n = 0; n < this->pousos.size(); n++)
- {
- if(pousos[n].getHorarioChegada() == i->getHorarioChegada() + offset)
- {
- n = -1;
- offset++;
- }
- }
- i->setHorarioChegada(i->getHorarioChegada() + offset);
- }
- }
- }
- offset = 1;
- }
- }
- offset = 1;
- for(k = this->decolagens.begin(); k < this->decolagens.end(); k++)
- {
- for(i = this->pousos.begin(); i < this->pousos.end(); i++)
- {
- if(i->getHorarioChegada() == k->getHorarioPartida())
- {
- for(int n = 0; n < this->pousos.size(); n++)
- {
- if(pousos[n].getHorarioChegada() == k->getHorarioPartida() + offset)
- {
- n = -1;
- offset++;
- }
- }
- k->setHorarioPartida(k->getHorarioPartida() + offset);
- }
- offset = 1;
- }
- }
- offset = 1;
- for(k = this->decolagens.begin(); k < this->decolagens.end(); k++)
- {
- for(z = this->decolagens.begin(); z < this->decolagens.end(); z++)
- {
- if(k != z)
- {
- if(k->getHorarioPartida() == z->getHorarioPartida())
- {
- if(k->getTempoVoo() > z->getTempoVoo())
- {
- for(int i = 0; i < this->pousos.size(); i++)
- {
- if(pousos[i].getHorarioChegada() == z->getHorarioPartida() + offset)
- {
- i = -1;
- offset++;
- }
- }
- z->setHorarioPartida(z->getHorarioPartida() + offset);
- }
- else
- {
- for(int i = 0; i < this->pousos.size(); i++)
- {
- if(pousos[i].getHorarioChegada() == k->getHorarioPartida() + offset)
- {
- i = -1;
- offset++;
- }
- }
- k->setHorarioPartida(k->getHorarioPartida() + offset);
- }
- }
- }
- offset = 1;
- }
- }
- }
- };
- int Aeroporto::StaticCodigo = 0;
- int main()
- {
- int rank, comm_size;
- MPI_Init(NULL, NULL);
- MPI_Comm_size (MPI_COMM_WORLD, &comm_size) ;
- MPI_Comm_rank (MPI_COMM_WORLD, &rank) ;
- Aeroporto a(rank + 1, comm_size);
- for(int i = 0; i < comm_size; i++)
- {
- if(rank == i)
- {
- cout << "new my rank " << rank << " my codigo " << a.getCodigo() << endl;
- a.SetDecolagem();
- }
- }
- for(int i = 0; i < comm_size; i++)
- {
- if(rank == i)
- {
- a.aviso(comm_size, rank);
- }
- else
- {
- a.entendido(comm_size, i);
- }
- }
- for(int i = 0; i < comm_size; i++)
- {
- if(rank == i)
- {
- a.enviar(comm_size);
- }
- else
- {
- a.receber(comm_size, i);
- }
- }
- for(int i = 0; i < comm_size; i++)
- {
- if(rank == i)
- {
- a.tratar();
- a.imprimir();
- }
- }
- MPI_Finalize();
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement