This repository has been archived by the owner on Jun 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
projet.c
285 lines (217 loc) · 6.19 KB
/
projet.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "projet.h"
// private
int masterFunction(MPI_Comm masterComm, MPI_Comm comm, int nbTask, int chunkMin, int *resultArray);
int slaveFunction(MPI_Comm masterComm, MPI_Comm comm, int nbTask, int (*pointerWork)(int));
// printing functions
void display(int nb);
void displayArray(int *array, int count);
int verbose = 1;
void mpiForScheduled(int nbTask, int chunkMin, int (*pointerWork)(int), int *resultArray)
{
srand(time(NULL));
int rank = 0;
int index = 0;
int size = 0;
MPI_Comm newComm;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// check the minimum chunk
if (chunkMin <= 0)
{
chunkMin = 1;
}
// master / slave model
MPI_Comm_split(MPI_COMM_WORLD, 0 == rank, 0, &newComm);
if (0 == rank)
{
masterFunction(MPI_COMM_WORLD, newComm, nbTask, chunkMin, resultArray);
}
else
{
slaveFunction(MPI_COMM_WORLD, newComm, nbTask, pointerWork);
}
}
int masterFunction(MPI_Comm masterComm, MPI_Comm comm, int nbTask, int chunkMin, int *resultArray)
{
int i, j, k, size;
int nbMaster;
int nbSlave;
char buf[256];
MPI_Status status;
int *taskArray = (int*) malloc(sizeof(int));
int taskIndex = 0;
int nbRest = nbTask;
int chunk = chunkMin;
int resultRestCount = nbRest;
MPI_Comm_size(masterComm, &size);
MPI_Comm_size(comm, &nbMaster);
nbSlave = size - nbMaster;
vprintf("Number of processors: %d\n", size);
vprintf("Number of slaves: %d\n", nbSlave);
int resultCount = 0;
int resultIndex = 0;
// either we still have to work or chunk is not equal to zero
while (nbRest || chunk != 0)
{
// slave is free
MPI_Recv(&resultCount, 1, MPI_INT, MPI_ANY_SOURCE, 0, masterComm, &status);
if (0 == resultCount)
{
vprintf("Slave %d is free\n", status.MPI_SOURCE);
}
else
{
vprintf("Slave %d sent this result size %d\n", status.MPI_SOURCE, resultCount);
MPI_Recv(&resultIndex, 1, MPI_INT, status.MPI_SOURCE, 0, masterComm, &status);
vprintf("Result index %d\n", resultIndex);
// ready to receive
int *resArray = (int*) calloc(resultCount, sizeof(int));
MPI_Recv(resArray, resultCount, MPI_INT, status.MPI_SOURCE, 0, MPI_COMM_WORLD, &status);
// getting the array up to day
for (k = 0 ; k < resultCount ; k++)
{
resultArray[resultIndex] = resArray[k];
resultIndex++;
}
resultRestCount -= resultCount;
free(resArray);
}
chunk = nbRest / nbSlave;
if ((0 == chunk) && (nbRest <= chunkMin))
{
chunk = nbRest;
}
else if (chunk < chunkMin)
{
chunk = chunkMin;
}
if (chunk)
{
// send chunk size
MPI_Send(&chunk, 1, MPI_INT, status.MPI_SOURCE, 0, masterComm);
int *tmpTaskArray = (int*) realloc(taskArray, chunk * sizeof(int));
if (!tmpTaskArray)
{
vprintf("Reallocation failed\n");
return 1;
}
taskArray = tmpTaskArray;
resultIndex = taskIndex;
MPI_Send(&resultIndex, 1, MPI_INT, status.MPI_SOURCE, 0, masterComm);
// sending a chunk to a free slave
for (j = 0 ; j < chunk ; j++)
{
taskArray[j] = taskIndex;
taskIndex++;
}
vprintf("Task array\n");
displayArray(taskArray, chunk);
vprintf("\n");
MPI_Send(taskArray, chunk, MPI_INT, status.MPI_SOURCE, 0, masterComm);
nbRest = nbRest - chunk;
}
}
// receving the pending result
while (resultRestCount > 0)
{
MPI_Recv(&resultCount, 1, MPI_INT, MPI_ANY_SOURCE, 0, masterComm, &status);
if (resultCount != 0)
{
MPI_Recv(&resultIndex, 1, MPI_INT, status.MPI_SOURCE, 0, masterComm, &status);
// ready for receiving
int *resArray = (int*) calloc(resultCount, sizeof(int));
MPI_Recv(resArray, resultCount, MPI_INT, status.MPI_SOURCE, 0, MPI_COMM_WORLD, &status);
// getting the array up to day
for (k = 0 ; k < resultCount ; k++)
{
resultArray[resultIndex] = resArray[k];
resultIndex++;
resultRestCount--;
}
free(resArray);
}
}
for (i = 1 ; i <= nbSlave ; i++)
{
MPI_Send(&chunk, 1, MPI_INT, i, 0, masterComm);
}
for (i = 1 ; i <= nbSlave ; i++)
{
// slave is out
MPI_Recv(buf, 256, MPI_CHAR, i, 0, masterComm, &status);
vprintf("%s", buf);
vprintf("\n");
}
free(taskArray);
}
int slaveFunction(MPI_Comm masterComm, MPI_Comm comm, int nbTask, int (*pointerWork)(int))
{
int i;
int *taskArray = (int*) malloc(sizeof(int));
int chunkSize = -1;
char buf[256];
int rank;
MPI_Status status;
int zero = 0;
int resultIndex = 0;
MPI_Comm_rank(comm, &rank);
// slave is free
MPI_Send(&zero, 1, MPI_CHAR, 0, 0, masterComm);
while (chunkSize != 0)
{
// receiving chunk size
MPI_Recv(&chunkSize, 1, MPI_INT, 0, 0, masterComm, &status);
vprintf("Slave %d received a chunk size: %d\n", rank, chunkSize);
if (chunkSize != 0)
{
// slave receiving the index
MPI_Recv(&resultIndex, 1, MPI_INT, 0, 0, masterComm, &status);
int *tmpTaskArray = (int*) realloc(taskArray, chunkSize * sizeof(int));
if (!tmpTaskArray)
{
vprintf("Reallocation failed\n");
return 1;
}
taskArray = tmpTaskArray;
MPI_Recv(taskArray, chunkSize, MPI_INT, 0, 0, masterComm, &status);
display(rank);
vprintf("Slave %d received some tasks\n", rank);
int *resultArray = (int*) calloc(chunkSize, sizeof(int));
for (i = 0 ; i < chunkSize ; i++)
{
int result = (*pointerWork)(taskArray[i]);
resultArray[i] = result;
}
// sending some info to the master
// sending the size
MPI_Send(&chunkSize, 1, MPI_INT, 0, 0, masterComm);
// sending the index
MPI_Send(&resultIndex, 1, MPI_INT, 0, 0, masterComm);
// sending the results
MPI_Send(resultArray, chunkSize, MPI_INT, 0, 0, masterComm);
free(resultArray);
}
}
// send a signal to master
display(rank); // for display
sprintf(buf, "Slave %d is out\n", rank);
MPI_Send(buf, strlen(buf) + 1, MPI_CHAR, 0, 0, masterComm);
free(taskArray);
return 0;
}
void display(int nb)
{
int i;
for (i = 0 ; i <= nb ; i++)
{
vprintf(" ");
}
}
void displayArray(int *array, int count)
{
int i = 0;
vprintf("Array:\n");
for (i = 0 ; i < count ; i++)
{
vprintf("%d\n", array[i]);
}
}