-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoordinator.cpp
203 lines (165 loc) · 6.03 KB
/
coordinator.cpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <mpi.h>
#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <GL/gl.h>
#include <GL/glu.h>
unsigned int microseconds = 3000;
using namespace std;
int PlateauToString(int nbLig, int nbCol, double **plateau){
for (int i = 0; i < nbLig; i++)
{
for(int j = 0; j < nbCol; j++){
printf("| %.3lf ", plateau[i][j] );
}
printf("|\n");
}
}
SDL_Renderer* FenetreGraphique_creer (int largeurDeLaFenetre, int hauteurDeLaFenetre, double xMin,
double xMax, double yMin, double yMax) {
SDL_Init (SDL_INIT_VIDEO);
SDL_Window* displayWindow;
SDL_RendererInfo displayRendererInfo;
SDL_Renderer* displayRenderer;
SDL_CreateWindowAndRenderer (largeurDeLaFenetre, hauteurDeLaFenetre, SDL_WINDOW_OPENGL,
&displayWindow, &displayRenderer);
SDL_GetRendererInfo(displayRenderer, &displayRendererInfo);
glViewport (0, 0, (GLsizei) largeurDeLaFenetre, (GLsizei) hauteurDeLaFenetre);
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
glOrtho (xMin, xMax, yMin, yMax, 1, -1);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );
return displayRenderer;
}
void FenetreGraphique_initialiser () {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
//glOrtho (-3, 3, -3, 3, -1.0, 1.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
}
void FenetreGraphique_rendre (SDL_Renderer* displayRenderer) {
SDL_RenderPresent (displayRenderer);
SDL_Delay(800);
}
void FenetreGraphique_fermer () {
SDL_Quit();
}
void FenetreGrahique_dessinerRectangle (double x1, double y1, double x2, double y2,
float fondR, float fondV, float fondB,
float formeR, float formeV, float formeB) {
glColor3f (fondR, fondV ,fondB);
glBegin (GL_QUADS);
glVertex2f (x1, y1);
glVertex2f (x2, y1);
glVertex2f (x2, y2);
glVertex2f (x1, y2);
glEnd ();
glColor3f (formeR, formeV ,formeB);
glBegin (GL_LINE_LOOP);
glVertex2f (x1, y1);
glVertex2f (x2, y1);
glVertex2f (x2, y2);
glVertex2f (x1, y2);
glEnd ();
}
/*
int main(int argc, char *argv[]) {
SDL_Renderer* displayRenderer = FenetreGraphique_creer (800, 600, -2, 2, -2, 2);
FenetreGraphique_initialiser ();
FenetreGrahique_dessinerRectangle (-0.8, -0.8, 0.8, 0.8, 1, 0, 1, 0, 1, 0);
FenetreGraphique_rendre (displayRenderer);
FenetreGraphique_fermer ();
return 0;
}
*/
int main( int argc, char *argv[] ) {
// Propriétés
double temperatureAmbiante;
char endSignal = 'c';
int myrank, N, nbLig, nbCol;
double **plateau;
// Initialisation communication
MPI_Comm parent;
MPI_Status etat;
MPI_Init (&argc, &argv);
MPI_Comm_get_parent (&parent);
MPI_Comm_rank (MPI_COMM_WORLD,&myrank);
// IHM
SDL_Renderer* displayRenderer = FenetreGraphique_creer (800, 600, -2, 2, -2, 2);
if (parent == MPI_COMM_NULL) {
printf ("Fils %d : Coordinateur : Pas de pere !\n", myrank);
} else {
// Reception de la largeur du plateau
MPI_Recv(&nbCol, 1, MPI_INT, 0, 0, parent, &etat);
// Reception de la longueur du plateau
MPI_Recv(&nbLig, 1, MPI_INT, 0, 0, parent, &etat);
//printf("Le Coordinateur a reçu la longueur %d du plateau\n", nbLig);
//FenetreGrahique_dessinerRectangle (-(0.95/(nbCol)), -(0.95/(nbLig)), (0.95/(nbCol/2)), (0.95/(nbLig/2)), 1, 0, 1, 0, 1, 0);
// Calcul du nombre d'esclave
N = nbCol*nbLig;
// Reception de la temperature ambiante
MPI_Recv(&temperatureAmbiante, 1, MPI_DOUBLE, 0, 0, parent, &etat);
//printf ("Fils %d : Coordinateur : La temperature ambiante recue est de %.3lf !\n", myrank, temperatureAmbiante);
plateau = new double *[nbLig];
for(int i = 0; i < nbLig; i++)
plateau[i] = new double[nbCol];
FenetreGraphique_rendre (displayRenderer);
FenetreGraphique_initialiser ();
double xSize=1.6;
double ySize=1.6;
for (int iteration=0; iteration <10; iteration++){
// Envoi de la temperature ambiante aux esclaves 10 fois
for (int i=0; i < N; i++) {
//printf ("Coordinateur envoie temp ambiante vers %d\n", (i+1));
MPI_Send(&temperatureAmbiante, 1, MPI_DOUBLE, i+1, 0, MPI_COMM_WORLD);
}
// Pour chaques esclaves
//Attente de la reception de la température de tous les esclaves
int nEsclave = 0;
for (int i=0; i < nbLig; i++) {
for (int j=0; j < nbCol; j++) {
nEsclave++;
double temperature;
//printf ("Coordinateur recoit ack de %d\n", nEsclave;
MPI_Recv(&temperature, 1, MPI_DOUBLE, nEsclave, 0, MPI_COMM_WORLD, &etat);
printf ("Coordinateur : La temperature de %d est de %.3lf !\n", nEsclave, temperature);
plateau[i][j] = temperature;
}
}
PlateauToString(nbLig,nbCol,plateau);
FenetreGraphique_initialiser ();
for (int i=0; i < nbLig; i++) {
for (int j=0; j < nbCol; j++) {
if (plateau[i][j]>75.0)
FenetreGrahique_dessinerRectangle (-xSize/2+(xSize/nbCol)*j, -ySize/2+(ySize/nbLig)*(i+1), -xSize/2+(xSize/nbCol)*(j+1), -ySize/2+(ySize/nbLig)*i, 1, 0, 0, 0.5, 0.5, 0.5);
else if (plateau[i][j]>50.0)
FenetreGrahique_dessinerRectangle (-xSize/2+(xSize/nbCol)*j, -ySize/2+(ySize/nbLig)*(i+1), -xSize/2+(xSize/nbCol)*(j+1), -ySize/2+(ySize/nbLig)*i, 0.75, 0, 0.33, 0.5, 0.5, 0.5);
else if (plateau[i][j]>25.0)
FenetreGrahique_dessinerRectangle (-xSize/2+(xSize/nbCol)*j, -ySize/2+(ySize/nbLig)*(i+1), -xSize/2+(xSize/nbCol)*(j+1), -ySize/2+(ySize/nbLig)*i, 0.5, 0, 0.66, 0.5, 0.5, 0.5);
else if (plateau[i][j]>0.0)
FenetreGrahique_dessinerRectangle (-xSize/2+(xSize/nbCol)*j, -ySize/2+(ySize/nbLig)*(i+1), -xSize/2+(xSize/nbCol)*(j+1), -ySize/2+(ySize/nbLig)*i, 0.25, 0, 1, 0.5, 0.5, 0.5);
}
}
FenetreGraphique_rendre (displayRenderer);
}
// Envoi du message de fin de simulation au maitre
printf ("Fils %d : Coordinateur : Envoi vers le pere !\n", myrank);
MPI_Send(&endSignal, 1, MPI_CHAR, 0, 0, parent);
for(int i = 0; i < nbLig; i++)
delete[] plateau[i];
delete[] plateau;
//usleep(microseconds);*/
}
printf ("Fils %d : Coordinateur : fin !\n", myrank);
MPI_Finalize();
FenetreGraphique_fermer ();
return 0;
}