Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- #include <time.h>
- #include <mpi.h>
- ////////////////////////////////////////////////////////////////////////////////
- // Program main
- ////////////////////////////////////////////////////////////////////////////////
- char *textData;
- int textLength;
- char *patternData[8];
- int patternLength;
- int patternCount;
- clock_t c0, c1;
- time_t t0, t1;
- 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 readData (i)
- {
- FILE *f;
- char fileName[1000];
- #ifdef DOS
- sprintf (fileName, "inputs\\text.txt");
- #else
- sprintf (fileName, "inputs/text.txt");
- #endif
- f = fopen (fileName, "r");
- if (f == NULL)
- return 0;
- readFromFile (f, &textData, &textLength);
- fclose (f);
- printf("Read pattern number %d \n", i);
- #ifdef DOS
- sprintf(fileName, "inputs\\pattern%d.txt", i);
- #else
- sprintf(fileName, "inputs/pattern%d.txt", i);
- #endif
- f = fopen(fileName, "r");
- if (f == NULL)
- return 0;
- readFromFile(f, &patternData[i], &patternLength);
- fclose(f);
- return 1;
- }
- int hostMatch(long *comparisons, int index)
- {
- int i,j,k, lastI;
- char *pattern = patternData[index];
- i=0;
- j=0;
- k=0;
- lastI = textLength-patternLength;
- *comparisons=0;
- while (i<=lastI && j<patternLength)
- {
- (*comparisons)++;
- if (textData[k] == pattern[j])
- {
- k++;
- j++;
- }
- else
- {
- i++;
- k=i;
- j=0;
- }
- }
- if (j == patternLength)
- return i;
- else
- return -1;
- }
- void processData(int i)
- {
- unsigned int result;
- long comparisons;
- printf("Text length = %d\n", textLength);
- printf("Pattern %d length = %d\n", i, patternLength);
- result = hostMatch(&comparisons, i);
- if (result == -1)
- printf("Pattern %d not found\n", i);
- else
- printf("Pattern %d found at position %d\n", i, result);
- printf("# comparisons = %ld\n", comparisons);
- }
- int main(int argc, char **argv)
- {
- MPI_Init(NULL, NULL);
- int world_rank;
- MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
- int world_size;
- MPI_Comm_size(MPI_COMM_WORLD, &world_size);
- const int PATTERN_COUNT = 8;
- int patternsComplete = 0;
- int indavidualCount = PATTERN_COUNT / world_size;
- int i = world_rank + 1;
- while (patternsComplete < indavidualCount)
- {
- readData(i);
- processData(i);
- i = i + world_size;
- patternsComplete++;
- }
- MPI_Finalize();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement