Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.94 KB | None | 0 0
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <time.h>
  7. #include <mpi.h>
  8.  
  9.  
  10.  
  11. ////////////////////////////////////////////////////////////////////////////////
  12. // Program main
  13. ////////////////////////////////////////////////////////////////////////////////
  14.  
  15. char *textData;
  16. int textLength;
  17.  
  18. char *patternData[8];
  19. int patternLength;
  20. int patternCount;
  21.  
  22. clock_t c0, c1;
  23. time_t t0, t1;
  24.  
  25. void outOfMemory()
  26. {
  27.     fprintf (stderr, "Out of memory\n");
  28.     exit (0);
  29. }
  30.  
  31. void readFromFile (FILE *f, char **data, int *length)
  32. {
  33.     int ch;
  34.     int allocatedLength;
  35.     char *result;
  36.     int resultLength = 0;
  37.  
  38.     allocatedLength = 0;
  39.     result = NULL;
  40.  
  41.    
  42.  
  43.     ch = fgetc (f);
  44.     while (ch >= 0)
  45.     {
  46.         resultLength++;
  47.         if (resultLength > allocatedLength)
  48.         {
  49.             allocatedLength += 10000;
  50.             result = (char *) realloc (result, sizeof(char)*allocatedLength);
  51.             if (result == NULL)
  52.                 outOfMemory();
  53.         }
  54.         result[resultLength-1] = ch;
  55.         ch = fgetc(f);
  56.     }
  57.     *data = result;
  58.     *length = resultLength;
  59. }
  60.  
  61. int readData (i)
  62. {
  63.     FILE *f;
  64.     char fileName[1000];
  65. #ifdef DOS
  66.         sprintf (fileName, "inputs\\text.txt");
  67. #else
  68.     sprintf (fileName, "inputs/text.txt");
  69. #endif
  70.     f = fopen (fileName, "r");
  71.     if (f == NULL)
  72.         return 0;
  73.     readFromFile (f, &textData, &textLength);
  74.     fclose (f);
  75.  
  76.         printf("Read pattern number %d \n", i);
  77.         #ifdef DOS
  78.                 sprintf(fileName, "inputs\\pattern%d.txt", i);
  79.         #else
  80.                 sprintf(fileName, "inputs/pattern%d.txt", i);
  81.         #endif
  82.  
  83.         f = fopen(fileName, "r");
  84.         if (f == NULL)
  85.             return 0;
  86.         readFromFile(f, &patternData[i], &patternLength);
  87.         fclose(f);
  88.    
  89.  
  90.     return 1;
  91.  
  92. }
  93.  
  94.  
  95.  
  96. int hostMatch(long *comparisons, int index)
  97. {
  98.     int i,j,k, lastI;
  99.     char *pattern = patternData[index];
  100.     i=0;
  101.     j=0;
  102.     k=0;
  103.     lastI = textLength-patternLength;
  104.         *comparisons=0;
  105.  
  106.     while (i<=lastI && j<patternLength)
  107.     {
  108.                 (*comparisons)++;
  109.         if (textData[k] == pattern[j])
  110.         {
  111.             k++;
  112.             j++;
  113.         }
  114.         else
  115.         {
  116.             i++;
  117.             k=i;
  118.             j=0;
  119.         }
  120.     }
  121.     if (j == patternLength)
  122.         return i;
  123.     else
  124.         return -1;
  125. }
  126. void processData(int i)
  127. {
  128.     unsigned int result;
  129.     long comparisons;
  130.  
  131.     printf("Text length = %d\n", textLength);
  132.     printf("Pattern %d length = %d\n", i, patternLength);
  133.  
  134.     result = hostMatch(&comparisons, i);
  135.     if (result == -1)
  136.         printf("Pattern %d not found\n", i);
  137.     else
  138.         printf("Pattern %d found at position %d\n", i, result);
  139.     printf("# comparisons = %ld\n", comparisons);
  140.  
  141. }
  142.  
  143. int main(int argc, char **argv)
  144. {
  145.     MPI_Init(NULL, NULL);
  146.     int world_rank;
  147.     MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  148.     int world_size;
  149.     MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  150.  
  151.     const int PATTERN_COUNT = 8;
  152.  
  153.     int patternsComplete = 0;
  154.     int indavidualCount = PATTERN_COUNT / world_size;
  155.     int i = world_rank + 1;
  156.     while (patternsComplete < indavidualCount)
  157.     {
  158.         readData(i);
  159.         processData(i);
  160.         i = i + world_size;
  161.         patternsComplete++;
  162.     }
  163.  
  164.     MPI_Finalize();
  165.  
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement