-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaiores.cpp
More file actions
115 lines (91 loc) · 3.02 KB
/
maiores.cpp
File metadata and controls
115 lines (91 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "mpi.h"
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void geraVetor(int vet[], int n){//Gera vetor con n valores aleatórios.
for(int i=0; i<n; ++i){
vet[i] = rand()%1000;
}
}
void encontraMaiores(int vet[], int inicio, int fim, int &pmaior, int &smaior){
smaior = pmaior = 0;
for(int i=inicio; i<fim; ++i){
if(vet[i]>=pmaior){
smaior = pmaior;
pmaior = vet[i];
}
if( vet[i]>=smaior && vet[i]<pmaior ){
smaior = vet[i];
}
}
}
void imprimeVetor(int vet[], int n){
for(int i=0; i<n; ++i){
cout<<vet[i]<<" ";
}
cout<<endl;
}
int main( int argc, char **argv)
{
int rank, inicio, fim, passo, rest, pmaior, smaior, n;
int *vet;
MPI_Status status;
srand(322);
MPI_Init(&argc, &argv);
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
if (rank == 0) {
int p1maior, s1maior, p2maior, s2maior, aux;
cout<<"Quantidade: "; cin>>n;//Quantidade de elementos
//Envia quantidade de elementos para os outros nós
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
//Aloca vetor
vet = new int[n];
//Gera n valores aleatórios
geraVetor(vet, n);
imprimeVetor(vet, n);
//Divisão de intervalos para cada nó
passo = n/2;
rest = n%2;
inicio = 0;
fim = passo;
//Envia intervalo e vetor para o primeiro nó
MPI_Send( &inicio, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
MPI_Send( &fim, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
MPI_Send( vet, n, MPI_INT, 1, 0, MPI_COMM_WORLD);
inicio = inicio+passo;
fim = fim + passo + rest;
//Envia intervalo e vetor para o segundo nó
MPI_Send( &inicio, 1, MPI_INT, 2, 0, MPI_COMM_WORLD);
MPI_Send( &fim, 1, MPI_INT, 2, 0, MPI_COMM_WORLD);
MPI_Send( vet, n, MPI_INT, 2, 0, MPI_COMM_WORLD);
//Recebe maiores valores encontrados pelo primeiro nó
MPI_Recv( &p1maior, 1, MPI_INT, 1, 0, MPI_COMM_WORLD,&status );
MPI_Recv( &s1maior, 1, MPI_INT, 1, 0, MPI_COMM_WORLD,&status );
//Recebe maiores valores encontrados pelo segundo nó
MPI_Recv( &p2maior, 1, MPI_INT, 2, 0, MPI_COMM_WORLD,&status );
MPI_Recv( &s2maior, 1, MPI_INT, 2, 0, MPI_COMM_WORLD,&status );
//Econtra os 2 maiores dentre os 4 recebidos
pmaior = p1maior >= p2maior ? p1maior : p2maior;
smaior = s1maior >= s2maior ? s1maior : s2maior;
p1maior >= p2maior ? (smaior = smaior >= p2maior ? smaior : p2maior) : (smaior = smaior >= p1maior ? smaior : p1maior);
cout<<"1 - "<<pmaior<<"\n2 - "<<smaior<<endl;
}else{
//Recebe quantidade de elementos e aloca vetor
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
vet = new int[n];
geraVetor(vet, n);
//Recebe intervalos e valores do vetor
MPI_Recv( &inicio, 1, MPI_INT,0,0,MPI_COMM_WORLD,&status );
MPI_Recv( &fim, 1, MPI_INT,0,0,MPI_COMM_WORLD,&status );
//Encontra os dois maiores do intervalo
encontraMaiores(vet, inicio, fim, pmaior, smaior);
//Envia para o nó principal os valores encontrados
MPI_Send( &pmaior, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
MPI_Send( &smaior, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
// cout<<"2: "<<pmaior<<" "<<smaior<<endl;
}
MPI_Finalize();
return 0;
}