-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheLider.c
107 lines (100 loc) · 2.62 KB
/
eLider.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <time.h>
int calcularId(int r,int cont, int id);
void calculoLider(int id, int v[2],int r,int soylider,int cont);
void main(int argc, char* argv[]){
int id=0, idMayor=0, vueltas=0;
int lider[2];
int p,r,n;
int soylider=0;
srand(time(NULL));
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD,&p);
MPI_Comm_rank(MPI_COMM_WORLD,&r);
while(vueltas!=2){
switch(r){
case 0:
id=calcularId(r,vueltas,id);
if(vueltas==0){
lider[0]=id;
lider[1]=r;
}
else if(vueltas==1){
MPI_Recv(&lider,2,MPI_INT,7,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
}
MPI_Send(&lider,2,MPI_INT,1,0,MPI_COMM_WORLD);
break;
case 1:
MPI_Recv(&lider,2,MPI_INT,0,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
id=calcularId(r,vueltas,id);
calculoLider(id,lider,r,soylider,vueltas);
MPI_Send(&lider,2,MPI_INT,2,0,MPI_COMM_WORLD);
break;
case 2:
MPI_Recv(&lider,2,MPI_INT,1,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
id=calcularId(r,vueltas,id);
calculoLider(id,lider,r,soylider,vueltas);
MPI_Send(&lider,2,MPI_INT,3,0,MPI_COMM_WORLD);
break;
case 3:
MPI_Recv(&lider,2,MPI_INT,2,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
id=calcularId(r,vueltas,id);
calculoLider(id,lider,r,soylider,vueltas);
MPI_Send(&lider,2,MPI_INT,4,0,MPI_COMM_WORLD);
break;
case 4:
MPI_Recv(&lider,2,MPI_INT,3,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
id=calcularId(r,vueltas,id);
calculoLider(id,lider,r,soylider,vueltas);
MPI_Send(&lider,2,MPI_INT,5,0,MPI_COMM_WORLD);
break;
case 5:
MPI_Recv(&lider,2,MPI_INT,4,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
id=calcularId(r,vueltas,id);
calculoLider(id,lider,r,soylider,vueltas);
MPI_Send(&lider,2,MPI_INT,6,0,MPI_COMM_WORLD);
break;
case 6:
MPI_Recv(&lider,2,MPI_INT,5,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
id=calcularId(r,vueltas,id);
calculoLider(id,lider,r,soylider,vueltas);
MPI_Send(&lider,2,MPI_INT,7,0,MPI_COMM_WORLD);
break;
case 7:
MPI_Recv(&lider,2,MPI_INT,6,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
id=calcularId(r,vueltas,id);
calculoLider(id,lider,r,soylider,vueltas);
MPI_Send(&lider,2,MPI_INT,0,0,MPI_COMM_WORLD);
break;
}
vueltas++;
}
MPI_Finalize();
}
int calcularId(int r,int cont,int id){
if(cont==0){
r=r+20;
return (rand()%r);
}
else return id;
}
void calculoLider(int id, int v[2],int r,int soylider,int cont){
if(cont==0){
if(id>v[0]){
v[0]=id;
v[1]=r;
soylider=1;
}
}
else{
if(id!=v[0]){
soylider=0;
printf("Soy proceso: %d, y el Rango del Lider es: %d\n",r,v[1]);
}
else{
printf("Soy proceso: %d, y Soy el Lider\n",r);
}
}
}