-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproject_MPI.c
More file actions
363 lines (308 loc) · 8.38 KB
/
project_MPI.c
File metadata and controls
363 lines (308 loc) · 8.38 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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Jacob Mills
#include "mpi.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
////////////////////////////////////////////////////////////////////////////////
// Program main
////////////////////////////////////////////////////////////////////////////////
#define INPUTFOLDER "inputs"
#define MAXTEXTS 20
#define MAXPATTERNS 20
#define BUFSIZE 100000
#define MASTER 0
#define JOBSIZE 768
// Reading/writing files
void outOfMemory()
{
fprintf (stderr, "Out of memory\n");
exit (0);
}
void readFromFile (FILE *f, char **data, int *length)
{
int ch;
int allocatedLength;
char *result;
int resultLength = 0;
allocatedLength = 0;
result = NULL;
ch = fgetc (f);
while (ch >= 0)
{
resultLength++;
if (resultLength > allocatedLength)
{
allocatedLength += 10000;
result = (char *) realloc (result, sizeof(char)*allocatedLength);
if (result == NULL)
outOfMemory();
}
result[resultLength-1] = ch;
ch = fgetc(f);
}
*data = result;
*length = resultLength;
}
int readTexts(char **textData, int *textLength)
{
int i;
for (i = 0; i < MAXTEXTS; i++)
{
FILE *f;
char fileName[1000];
#ifdef DOS
sprintf (fileName, "%s\\text%d.txt", INPUTFOLDER, i);
#else
sprintf (fileName, "%s/text%d.txt", INPUTFOLDER, i);
#endif
f = fopen(fileName, "r");
if (f == NULL)
return i;
readFromFile (f, &textData[i], &textLength[i]);
fclose (f);
}
return MAXTEXTS;
}
int readPatterns(char **patternData, int *patternLength)
{
int i;
for (i = 0; i < MAXPATTERNS; i++)
{
FILE *f;
char fileName[1000];
#ifdef DOS
sprintf (fileName, "%s\\pattern%d.txt", INPUTFOLDER, i);
#else
sprintf (fileName, "%s/pattern%d.txt", INPUTFOLDER, i);
#endif
f = fopen (fileName, "r");
if (f == NULL)
return i;
readFromFile (f, &patternData[i], &patternLength[i]);
fclose (f);
}
return MAXPATTERNS;
}
int readControl(int controlData[][3])
{
FILE *f;
char fileName[1000];
#ifdef DOS
sprintf (fileName, "%s\\control.txt", INPUTFOLDER);
#else
sprintf (fileName, "%s/control.txt", INPUTFOLDER);
#endif
f = fopen (fileName, "r");
if (f == NULL)
return 0;
int r;
int i = 0;
while ((r = fscanf(f, "%d %d %d", &controlData[i][0], &controlData[i][1], &controlData[i][2])) != EOF)
{
if (r == 3)
i++;
}
fclose (f);
return i;
}
void writeOutputFile(char buf[])
{
FILE *f;
char fileName[1000];
sprintf (fileName, "result_MPI.txt");
f = fopen(fileName, "a+");
if (f == NULL)
return;
fprintf(f, buf);
fclose(f);
buf[0] = '\0';
}
// Only master should call this
void writeToBuffer(char buf[], int textIndex, int patternIndex, int matchPoint)
{
if (strlen(buf) > BUFSIZE - 20)
writeOutputFile(buf);
sprintf(buf+strlen(buf), "%d %d %d\n", textIndex, patternIndex, matchPoint);
}
// Find pattern at given text position and the positions following for lengthToCheck
int checkTextForPatternAtPosition(char text[], char pattern[], int textIndex, int textLength, int patternLength, int lengthToCheck, int results[JOBSIZE])
{
int i;
int found = 0;
for (i = 0; i < lengthToCheck; i++)
{
if (textLength - (textIndex + i) < patternLength)
break;
int k = textIndex + i;
int j = 0;
while (text[k] == pattern[j] && j < patternLength)
{
k++;
j++;
}
if (j == patternLength)
{
results[found++] = textIndex + i;
}
}
if (found < JOBSIZE)
results[found] = -1; // Use this to indicate to master where array results end.
return found;
}
void masterLoop(int numtasks, int numberOfTests, int controlData[][3], int* textLength, int* patternLength, char* buf)
{
// Loop through each test
int testNumber;
for (testNumber = 0; testNumber < numberOfTests; testNumber++)
{
int mode = controlData[testNumber][0];
int textIndex = controlData[testNumber][1];
int patternIndex = controlData[testNumber][2];
// If text is shorter than pattern, it's not gonna be found.
if (textLength[textIndex] < patternLength[patternIndex])
{
sprintf(buf+strlen(buf), "%d %d %d\n", textIndex, patternIndex, -1);
continue;
}
int found = -1;
int activeThreads = 0;
int i;
int checkData[4] = {textIndex, patternIndex, 0, JOBSIZE}; // {text#, pattern#, text index to check, length of text to check}
// Send a job to all the threads to start off with
for (i = 1; i < numtasks; i++)
{
if (checkData[2] <= textLength[textIndex] - patternLength[patternIndex]) // Unless the text is shorter than the number of threads
{
MPI_Send(&checkData, 4, MPI_INT, i, 0, MPI_COMM_WORLD); // A
checkData[2]+=checkData[3];
activeThreads++;
}
else
break;
}
int done = 0;
// Loop over each character in the text until the test is complete
while (!done && activeThreads > 0)
{
int results[JOBSIZE];
MPI_Status s;
MPI_Recv(&results, JOBSIZE, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &s); // B
activeThreads--;
// If a thread has found an occurrence of the pattern
if (results[0] != -1)
{
// Mode 0 - find first instance
if (mode == 0)
{
found = results[0];
// Wait for remaining active threads to return, in case they find an earlier result
while (activeThreads > 0)
{
MPI_Recv(&results, JOBSIZE, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &s); // B
if (results[0] != -1 )
{
//int minVal = min(results);
int minVal = results[0];
if (minVal < found)
found = minVal;
}
activeThreads--;
}
// Record the result and exit this test by setting done
sprintf(buf+strlen(buf), "%d %d %d\n", textIndex, patternIndex, found);
done = 1;
}
// Mode 1 - find all instances
else
{
// Record the result and set found
found = results[0];
int i = 0;
while (i < JOBSIZE && results[i] != -1)
sprintf(buf+strlen(buf), "%d %d %d\n", textIndex, patternIndex, results[i++]);
}
}
// Thread did not find pattern (mode 0) and/or still text left to be analysed (mode 0/1)
if (!done && checkData[2] <= textLength[textIndex] - patternLength[patternIndex])
{
MPI_Send(&checkData, 4, MPI_INT, s.MPI_SOURCE, 0, MPI_COMM_WORLD); // A
checkData[2] += JOBSIZE;
activeThreads++;
}
}
// If no recorded found, pattern not present.
if (found == -1)
{
sprintf(buf+strlen(buf), "%d %d %d\n", textIndex, patternIndex, -1);
}
}
// All tests complete - tell slaves to stop
int done = 1;
MPI_Request request;
int i;
for (i = 1; i < numtasks; i++)
{
MPI_Isend(&done, 1, MPI_INT, i, 1, MPI_COMM_WORLD, &request); // X
}
}
void slaveLoop(int rank, char** textData, char** patternData, int* textLength, int* patternLength)
{
// Set up the receive for the flag from MASTER declaring the end
int done = 0;
int doneFlag = 0;
MPI_Request request;
MPI_Status status;
MPI_Irecv(&done, 1, MPI_INT, MASTER, 1, MPI_COMM_WORLD, &request); // X
while (!doneFlag)
{
// Await new work from master
MPI_Request r;
MPI_Status s;
int newWorkFlag = 0;
int checkData[4];
MPI_Irecv(&checkData, 4, MPI_INT, MASTER, 0, MPI_COMM_WORLD, &r); // A
while(!newWorkFlag)
{
MPI_Test(&request, &doneFlag, &status); // X
if (doneFlag)
return;
MPI_Test(&r, &newWorkFlag, &s); // A
}
int results[JOBSIZE];
int numFound = checkTextForPatternAtPosition(textData[checkData[0]], patternData[checkData[1]], checkData[2], textLength[checkData[0]], patternLength[checkData[1]], checkData[3], results);
MPI_Send(&results, JOBSIZE, MPI_INT, MASTER, 0, MPI_COMM_WORLD); // B
// Check if master says we're done
MPI_Test(&request, &doneFlag, &status); // X
}
}
int main(int argc, char**argv)
{
// Read in the text pattern and control for each thread (aim of keeping bcasts etc to as few as possible)
char *textData[MAXTEXTS];
int textLength[MAXTEXTS];
char *patternData[MAXPATTERNS];
int patternLength[MAXPATTERNS];
int controlData[800][3];
int numberOfTests = readControl(controlData);
int numberOfTexts = readTexts(textData, textLength);
int numberOfPatterns = readPatterns(patternData, patternLength);
int rank;
int numtasks;
char buf[BUFSIZE];
sprintf(buf, "");
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == MASTER)
{
masterLoop(numtasks, numberOfTests, controlData, textLength, patternLength, buf);
writeOutputFile(buf);
}
else
{
slaveLoop(rank, textData, patternData, textLength, patternLength);
}
MPI_Finalize();
}