Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.73 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.  const int MASTER_SOURCE = 0;
  16.  
  17. char *textData;
  18. int textLength;
  19.  
  20. char *patternData;
  21. int patternLength;
  22.  
  23. clock_t c0, c1;
  24. time_t t0, t1;
  25.  
  26. void outOfMemory()
  27. {
  28.     fprintf (stderr, "Out of memory\n");
  29.     exit (0);
  30. }
  31.  
  32. void readFromFile (FILE *f, char **data, int *length)
  33. {
  34.     int ch;
  35.     int allocatedLength;
  36.     char *result;
  37.     int resultLength = 0;
  38.  
  39.     allocatedLength = 0;
  40.     result = NULL;
  41.  
  42.    
  43.  
  44.     ch = fgetc (f);
  45.     while (ch >= 0)
  46.     {
  47.         resultLength++;
  48.         if (resultLength > allocatedLength)
  49.         {
  50.             allocatedLength += 10000;
  51.             result = (char *) realloc (result, sizeof(char)*allocatedLength);
  52.             if (result == NULL)
  53.                 outOfMemory();
  54.         }
  55.         result[resultLength-1] = ch;
  56.         ch = fgetc(f);
  57.     }
  58.     *data = result;
  59.     *length = resultLength;
  60. }
  61.  
  62.  /*Moved reading of text data to seperate function that is only called once for the sake of efficiency,
  63. rather than reassigning the same text data for each pattern.
  64. */
  65. int readTextData()
  66. {
  67.     FILE *f;
  68.     char fileName[1000];
  69.     #ifdef DOS
  70.         sprintf (fileName, "inputs\\text.txt");
  71. #else
  72.     sprintf (fileName, "inputs/text.txt");
  73. #endif
  74.     f = fopen (fileName, "r");
  75.     if (f == NULL)
  76.         return 0;
  77.     readFromFile (f, &textData, &textLength);
  78.     fclose (f);
  79.    
  80.     return 1;
  81. }
  82. /*Modified readData function now only reads the pattern that is passed to it.
  83. */
  84. int readPatternData (int patternNumber)
  85. {
  86.     FILE *f;
  87.     char fileName[1000];
  88. #ifdef DOS
  89.         sprintf (fileName, "inputs\\pattern%d.txt", patternNumber);
  90. #else
  91.     sprintf (fileName, "inputs/pattern%d.txt", patternNumber);
  92. #endif
  93.     f = fopen (fileName, "r");
  94.     if (f == NULL)
  95.         return 0;
  96.     readFromFile (f, &patternData, &patternLength);
  97.     fclose (f);
  98.  
  99.  
  100.     return 1;
  101.  
  102. }
  103.  
  104. int hostMatch(long *comparisons)
  105. {
  106.     int i,j,k, lastI;
  107.    
  108.     i=0;
  109.     j=0;
  110.     k=0;
  111.     lastI = textLength-patternLength;
  112.         *comparisons=0;
  113.  
  114.     while (i<=lastI && j<patternLength)
  115.     {
  116.                 (*comparisons)++;
  117.         if (textData[k] == patternData[j])
  118.         {
  119.             k++;
  120.             j++;
  121.         }
  122.         else
  123.         {
  124.             i++;
  125.             k=i;
  126.             j=0;
  127.         }
  128.     }
  129.     if (j == patternLength)
  130.         return i;
  131.     else
  132.         return -1;
  133. }
  134.  
  135. void processData()
  136. {
  137.     unsigned int result;
  138.         long comparisons;
  139.  
  140.     printf ("Text length = %d\n", textLength);
  141.     printf ("Pattern length = %d\n", patternLength);
  142.  
  143.     result = hostMatch(&comparisons);
  144.     if (result == -1)
  145.         printf ("Pattern not found\n");
  146.     else
  147.         printf ("Patternfound at position %d\n", result);
  148.         printf ("# comparisons = %ld\n", comparisons);
  149.  
  150. }
  151.  
  152. int main(int argc, char **argv)
  153. {
  154.   // Initialize the MPI environment
  155.   MPI_Init(NULL, NULL);
  156.   // Find out rank, size
  157.   int world_rank;
  158.   MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  159.   int world_size;
  160.   MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  161.  
  162.   // We are assuming at least 2 processes for this task
  163.   if (world_size < 2) {
  164.     fprintf(stderr, "World size must be two for %s\n", argv[0]);
  165.     MPI_Abort(MPI_COMM_WORLD, 1);
  166.   }
  167.    
  168.     if (world_rank == 0){
  169.         printf("\nNumber of Processes: %d\n", world_size); 
  170.         readTextData();
  171.         int patternNumber = 1;
  172.         readPatternData(patternNumber);
  173.        
  174.         int num_of_chars_per_process = textLength/world_size;
  175.        
  176.         int i;
  177.         for (i == 1; i < world_size; i++){
  178.             char *textToSend[num_of_chars_per_process];
  179.             int j;
  180.             for (j == 0; j < num_of_chars_per_process; j++){
  181.                 textToSend[j] = textData[(num_of_chars_per_process*i)+j];
  182.             }
  183.             MPI_Send(&patternLength, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
  184.             MPI_Send(&patternData, &patternLength, MPI_CHAR, i, 0, MPI_COMM_WORLD);
  185.             MPI_Send(&num_of_chars_per_process, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
  186.             MPI_Send(&textToSend, &num_of_chars_per_process, MPI_CHAR, i, 0, MPI_COMM_WORLD);
  187.         }
  188.        
  189.         patternNumber++;
  190.     } else {
  191.         MPI_Recv(&patternLength, 1, MPI_INT, MASTER_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  192.         MPI_Recv(&patternData, &patternLength, MPI_CHAR, MASTER_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  193.         MPI_Recv(&textLength, 1, MPI_INT, MASTER_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  194.         MPI_Recv(&textData, &textLength, MPI_CHAR, MASTER_SOURCE, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  195.         processData();
  196.  
  197.     }
  198.  
  199.     MPI_Finalize();
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement