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
- ////////////////////////////////////////////////////////////////////////////////
- const int MASTER_SOURCE = 0;
- char *textData;
- int textLength;
- char *patternData;
- int patternLength;
- 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;
- }
- /*Moved reading of text data to seperate function that is only called once for the sake of efficiency,
- rather than reassigning the same text data for each pattern.
- */
- int readTextData()
- {
- 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);
- return 1;
- }
- /*Modified readData function now only reads the pattern that is passed to it.
- */
- int readPatternData (int patternNumber)
- {
- FILE *f;
- char fileName[1000];
- #ifdef DOS
- sprintf (fileName, "inputs\\pattern%d.txt", patternNumber);
- #else
- sprintf (fileName, "inputs/pattern%d.txt", patternNumber);
- #endif
- f = fopen (fileName, "r");
- if (f == NULL)
- return 0;
- readFromFile (f, &patternData, &patternLength);
- fclose (f);
- return 1;
- }
- int hostMatch(long *comparisons)
- {
- int i,j,k, lastI;
- i=0;
- j=0;
- k=0;
- lastI = textLength-patternLength;
- *comparisons=0;
- while (i<=lastI && j<patternLength)
- {
- (*comparisons)++;
- if (textData[k] == patternData[j])
- {
- k++;
- j++;
- }
- else
- {
- i++;
- k=i;
- j=0;
- }
- }
- if (j == patternLength)
- return i;
- else
- return -1;
- }
- void processData()
- {
- unsigned int result;
- long comparisons;
- printf ("Text length = %d\n", textLength);
- printf ("Pattern length = %d\n", patternLength);
- result = hostMatch(&comparisons);
- if (result == -1)
- printf ("Pattern not found\n");
- else
- printf ("Patternfound at position %d\n", result);
- printf ("# comparisons = %ld\n", comparisons);
- }
- int main(int argc, char **argv)
- {
- // Initialize the MPI environment
- MPI_Init(NULL, NULL);
- // Find out rank, size
- int world_rank;
- MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
- int world_size;
- MPI_Comm_size(MPI_COMM_WORLD, &world_size);
- // We are assuming at least 2 processes for this task
- if (world_size < 2) {
- fprintf(stderr, "World size must be two for %s\n", argv[0]);
- MPI_Abort(MPI_COMM_WORLD, 1);
- }
- if (world_rank == 0){
- printf("\nNumber of Processes: %d\n", world_size);
- readTextData();
- int patternNumber = 1;
- readPatternData(patternNumber);
- int num_of_chars_per_process = textLength/world_size;
- int i;
- for (i == 1; i < world_size; i++){
- char *textToSend[num_of_chars_per_process];
- int j;
- for (j == 0; j < num_of_chars_per_process; j++){
- textToSend[j] = textData[(num_of_chars_per_process*i)+j];
- }
- MPI_Send(&patternLength, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
- MPI_Send(&patternData, &patternLength, MPI_CHAR, i, 0, MPI_COMM_WORLD);
- MPI_Send(&num_of_chars_per_process, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
- MPI_Send(&textToSend, &num_of_chars_per_process, MPI_CHAR, i, 0, MPI_COMM_WORLD);
- }
- patternNumber++;
- } else {
- MPI_Recv(&patternLength, 1, MPI_INT, MASTER_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- MPI_Recv(&patternData, &patternLength, MPI_CHAR, MASTER_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- MPI_Recv(&textLength, 1, MPI_INT, MASTER_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- MPI_Recv(&textData, &textLength, MPI_CHAR, MASTER_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
- processData();
- }
- MPI_Finalize();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement