-
Notifications
You must be signed in to change notification settings - Fork 0
/
grafo.c
91 lines (80 loc) · 1.86 KB
/
grafo.c
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
#include <stdio.h>
#include <mpi.h>
int numeroDeTarefas = 8;
int matrizVizinhanca[8][8] = {
{0,1,1,0,0,0,0,0},
{1,0,1,1,1,0,0,0},
{1,1,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,1},
{0,0,0,0,0,1,0,1},
{0,0,0,0,0,1,1,0},
};
int contaNumeroDeVizinhos(int myRank)
{
int i;
int contador = 0;
for(i=0; i < numeroDeTarefas; i++)
if(matrizVizinhanca[myRank][i] == 1)
contador++;
return contador;
}
int finaliza(int contador[], int numeroDeVizinhos, int myRank)
{
int i, todosiguaisa0=1;
if(contador[myRank] != numeroDeVizinhos) return 0;
else return 1;
}
int main(int argc, char** argv)
{
int i, j;
int numeroDeVizinhos;
int myRank;
int source;
int tag = 50;
int pai[numeroDeTarefas];
int contador[numeroDeTarefas];
int reached[numeroDeTarefas];
int id;
int origem;
MPI_Status status;
MPI_Init (&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
numeroDeVizinhos = contaNumeroDeVizinhos(myRank);
for (i=0; i<numeroDeTarefas; i++)
{
pai[i] = 0;
contador[i] = 0;
reached[i] = 0;
}
reached[myRank] = 1;
for (i=0; i<numeroDeTarefas; i++)
{
if(matrizVizinhanca[myRank][i] == 1)
MPI_Send(&myRank, 1, MPI_INT, i, tag, MPI_COMM_WORLD);
}
while (!finaliza(contador, numeroDeVizinhos, myRank))
{
MPI_Recv(&id, 1, MPI_INT, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &status);
origem = status.MPI_SOURCE;
contador[id]++;
if(reached[id]==0)
{
reached[id]=1;
pai[id]=origem;
for(j=0; j<numeroDeTarefas; j++)
if(matrizVizinhanca[myRank][j]==1 && j!=pai[id])
MPI_Send(&id, 1, MPI_INT, j, tag, MPI_COMM_WORLD);
}
if(contador[id] == numeroDeVizinhos)
if(pai[id]!=0)
MPI_Send(&id, 1, MPI_INT, pai[id], tag, MPI_COMM_WORLD);
}
printf("Processo %d: Elementos conectados: ", myRank);
for(i=0; i<numeroDeTarefas; i++)
if(contador[i] != 0) printf("%d", i);
printf("\n");
fflush(stdout);
MPI_Finalize();
}