Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.93 KB | None | 0 0
  1. /*
  2. * Name: Wilcox, Matthew
  3. * Email: mwilco4@lsu.edu
  4. * Project: PA-1 (Multithreading)
  5. * Instructor: Feng Chen
  6. * Class: cs4103-sp20
  7. * Login ID: cs4103xx
  8. */
  9.  
  10. #include <iostream>
  11. #include <string>
  12. #include <vector>
  13. #include <pthread.h>
  14. #include <numeric>
  15. #include <set>
  16. #include <tgmath.h>
  17. #include <fstream>
  18.  
  19. using namespace std;
  20.  
  21. const int AREA_SIZE = 9, SUBGRID_LEN = sqrt(AREA_SIZE), NUM_THREADS = AREA_SIZE * 3, MIN_VALUE = 0, MAX_VALUE = 10;
  22. pthread_spinlock_t lock;
  23. pthread_barrier_t barrier;
  24.  
  25. struct thread_data
  26. {
  27.     long id;
  28.     string type;
  29.     vector<int> area;
  30. };
  31.  
  32. int get_cell(vector<int> grid, int x, int y)
  33. {
  34.     return grid[x * AREA_SIZE + y];
  35. }
  36.  
  37. void* validate(void* thread_args)
  38. {
  39.     set<int> hash;
  40.     auto valid = true;
  41.     struct thread_data* data = (struct thread_data*) thread_args;
  42.     for (auto i = AREA_SIZE - 1; i >= 0; i--)
  43.     {
  44.         auto cell = data->area[i];
  45.         if (!(cell > MIN_VALUE && cell < MAX_VALUE) || hash.find(cell) != hash.end())
  46.         {
  47.             valid = false;
  48.             break;
  49.         }
  50.            
  51.         hash.insert(cell);
  52.     }
  53.  
  54.     pthread_spin_lock(&lock);
  55.     cout << "Thread " << data->id + 1 << ", " << data->type << (valid ? ", Valid" : ", Invalid") << endl;
  56.     pthread_spin_unlock(&lock);
  57.     pthread_exit(0);
  58.     return 0;
  59. }
  60.  
  61. int main()
  62. {
  63.     pthread_t threads[NUM_THREADS];
  64.     struct thread_data thread_data[NUM_THREADS];
  65.    
  66.     long thread_id = 0;
  67.     vector<int> grid;
  68.  
  69.     pthread_spin_init(&lock, PTHREAD_PROCESS_SHARED);
  70.     //pthread_barrier_init(&barrier, NULL, NUM_THREADS + 1);
  71.    
  72.     ifstream input("sudoku.txt");
  73.     if (input)
  74.     {
  75.         int cell;
  76.         while (input >> cell)
  77.         {            
  78.             grid.push_back(cell);
  79.         }
  80.     }
  81.  
  82.     for (auto i = 0; i < AREA_SIZE; i++, thread_id++)
  83.     {
  84.         vector<int> row(AREA_SIZE);
  85.         for(auto j = 0; j < AREA_SIZE; j++)
  86.         {
  87.             row[j] = get_cell(grid, i, j);
  88.         }
  89.  
  90.         string row_type = "Row ";
  91.         row_type.insert(4, to_string(i + 1));
  92.  
  93.         thread_data[thread_id] = { thread_id, row_type, row };
  94.         if (pthread_create(&threads[thread_id], NULL, validate, (void*) &thread_data[thread_id]))
  95.             cout << "error creating thread: " << thread_id << endl;
  96.     }
  97.  
  98.     for (auto i = 0; i < AREA_SIZE; i++, thread_id++)
  99.     {
  100.         vector<int> column(AREA_SIZE);
  101.         for(auto j = 0; j < AREA_SIZE; j++)
  102.         {
  103.             column[j] = get_cell(grid, j, i);
  104.         }
  105.  
  106.         string col_type = "Column ";
  107.         col_type.insert(7, to_string(i + 1));
  108.  
  109.         thread_data[thread_id] = { thread_id, col_type, column };
  110.         if (pthread_create(&threads[thread_id], NULL, validate, (void*) &thread_data[thread_id]))
  111.             cout << "error creating thread: " << thread_id + 1 << endl;
  112.     }
  113.  
  114.     for (auto i = 0; i < AREA_SIZE; i += SUBGRID_LEN)
  115.     {
  116.         for (auto j = 0; j < AREA_SIZE; j += SUBGRID_LEN, thread_id++)
  117.         {
  118.             vector<int> subgrid;
  119.             for (auto k = i; k < i + SUBGRID_LEN; k++)
  120.             {
  121.                 for (auto m = j; m < j + SUBGRID_LEN; m++)
  122.                     subgrid.push_back(get_cell(grid, k, m));
  123.             }
  124.  
  125.             string subgrid_type = "Subgrid RxC";
  126.             for (auto s = 1; s <= SUBGRID_LEN; s++)
  127.             {
  128.                 subgrid_type.insert(8 + s, to_string(i + s));
  129.                 subgrid_type.append(to_string(j + s));
  130.             }
  131.  
  132.             thread_data[thread_id] = { thread_id, subgrid_type, subgrid };
  133.             if (pthread_create(&threads[thread_id], NULL, validate, (void*) &thread_data[thread_id]))
  134.                 cout << "error creating thread: " << thread_id + 1 << endl;
  135.         }
  136.     }
  137.  
  138.     for (auto i = 0; i < NUM_THREADS; i++)
  139.     {
  140.         if (pthread_join(threads[i], NULL))
  141.             cout << "error joining thread: " << i + 1 << endl;
  142.     }
  143.  
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement