SHARE
TWEET

tasks

Sinux1 Nov 13th, 2019 110 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <omp.h>
  5.  
  6. int main()
  7. {
  8.     //starting time for calculating run time
  9.     double start = omp_get_wtime( );
  10.     //enum for purposes of declaring array size with variable
  11.     enum SIZE {SIZE = 26};
  12.  
  13.     //array to hold number of occurences of each letter in alphabet
  14.     //with index 0 representing A,..., index 25 = Z
  15.     int s_res[SIZE] = {};
  16.  
  17.     //number of threads requested
  18.     const int NUMB = 10;
  19.  
  20.     //char string for identifying chars from txt
  21.     char * alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  22.  
  23.     //requesting num threads is num of books
  24.     omp_set_num_threads(NUMB);
  25.  
  26.     //parallel block
  27.     #pragma omp parallel
  28.     {  
  29.         //task block
  30.         #pragma omp task
  31.         {   //for use in calculating file name
  32.             int ID = omp_get_thread_num();
  33.             //adding ID to char '0' will result in char '0' - '9' depending on ID
  34.             char filename[] = {'0' + ID,'.','t','x','t'};
  35.  
  36.             FILE *fptr;
  37.             fptr=fopen(filename,"r");
  38.             char ch;
  39.             //loop through txt file char at a time
  40.             while((ch=toupper((fgetc(fptr))))!=EOF)
  41.             {
  42.                 //pointer to memory address of ch in alpha
  43.                 const char *ptr = strchr(alpha, ch);
  44.                 if(ptr!=NULL)
  45.                 {  
  46.                     //calculate correct index
  47.                     int index = ptr - alpha;
  48.                     #pragma omp atomic
  49.                     //use index to increment array
  50.                     s_res[index]++;
  51.                 }
  52.  
  53.             }
  54.         }
  55.        
  56.     }
  57.    
  58.     //ending time for calculating run time
  59.     double end = omp_get_wtime( );
  60.  
  61.     printf("\nCompleted in %f seconds", end-start);
  62.     printf("\nThe letter frequencies are:\n");
  63.     //print results
  64.     for (int i =0; i < 26; i++)
  65.     {
  66.         printf("%c's: %d\n", alpha[i], s_res[i] );
  67.     }
  68.  
  69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top