Advertisement
Sinux1

locks

Nov 13th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include "omp.h"
  5. int main()
  6. {
  7.     //starting time for calculating run time
  8.     double start = omp_get_wtime( );
  9.     //enum for purposes of declaring array size with variable
  10.     enum SIZE {SIZE = 26};
  11.  
  12.     //array to hold number of occurences of each letter in alphabet
  13.     //with index 0 representing A,..., index 25 = Z
  14.     int s_res[SIZE] = {};
  15.  
  16.     //number of threads requested
  17.     const int NUMB = 10;
  18.  
  19.     //char string for identifying chars from txt
  20.     char * alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  21.  
  22.     //requessting that num threads is num of books
  23.     omp_set_num_threads(NUMB);
  24.  
  25.     //array of locks for each char of alphabet
  26.     omp_lock_t res_lock[SIZE] = {};
  27.  
  28.     //parallel block
  29.     #pragma omp parallel
  30.     {
  31.         //omp for loop
  32.         #pragma omp for schedule(auto)
  33.  
  34.         //initialize every single lock
  35.         for(int i = 0; i < SIZE; i++) { omp_init_lock(&res_lock[i]); }
  36.        
  37.         //omp for loop for main work
  38.         #pragma omp for schedule(static, 1)
  39.             for(int i = 0; i < NUMB; i++)
  40.             {
  41.  
  42.                 //adding ID to char '0' will result in char '0' - '9' depending on ID
  43.                 char filename[] = {'0' + i,'.','t','x','t'};
  44.                
  45.                 FILE *fptr;
  46.                 fptr=fopen(filename,"r");
  47.                 char ch;
  48.                 //loop through txt file char at a time
  49.                 while((ch=toupper((fgetc(fptr))))!=EOF)
  50.                 {
  51.                     //pointer to memory address of ch in alpha
  52.                     const char *ptr = strchr(alpha, ch);
  53.                     if(ptr!=NULL)
  54.                     {  
  55.                         //calculate correct index
  56.                         int index = ptr - alpha;
  57.                         //setting lock
  58.                         omp_set_lock(&res_lock[index]);
  59.                         //use index to increment correct array
  60.                         s_res[index]++;
  61.                         //unset lock
  62.                         omp_unset_lock(&res_lock[index]);
  63.                     }
  64.  
  65.                 }
  66.             }
  67.         //omp for
  68.         #pragma omp for schedule(auto)
  69.         //destroy all locks
  70.         for(int i = 0; i < SIZE; i++) { omp_destroy_lock(&res_lock[i]); }
  71.     }
  72.  
  73.     //ending time for calculating run time
  74.     double end = omp_get_wtime( );
  75.  
  76.     printf("\nCompleted in %f seconds", end-start);
  77.     printf("\nThe letter frequencies are:\n");
  78.     //print results
  79.     for (int i =0; i < 26; i++)
  80.     {
  81.         printf("%c's: %d\n", alpha[i], s_res[i] );
  82.     }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement