Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name: Wilcox, Matthew
- * Email: mwilco4@lsu.edu
- * Project: PA-1 (Multithreading)
- * Instructor: Feng Chen
- * Class: cs4103-sp20
- * Login ID: cs4103xx
- */
- #include <iostream>
- #include <string>
- #include <vector>
- #include <pthread.h>
- #include <numeric>
- #include <set>
- #include <tgmath.h>
- #include <fstream>
- using namespace std;
- const int AREA_SIZE = 9, SUBGRID_LEN = sqrt(AREA_SIZE), NUM_THREADS = AREA_SIZE * 3, MIN_VALUE = 0, MAX_VALUE = 10;
- pthread_spinlock_t lock;
- pthread_barrier_t barrier;
- struct thread_data
- {
- long id;
- string type;
- vector<int> area;
- };
- int get_cell(vector<int> grid, int x, int y)
- {
- return grid[x * AREA_SIZE + y];
- }
- void* validate(void* thread_args)
- {
- set<int> hash;
- auto valid = true;
- struct thread_data* data = (struct thread_data*) thread_args;
- for (auto i = AREA_SIZE - 1; i >= 0; i--)
- {
- auto cell = data->area[i];
- if (!(cell > MIN_VALUE && cell < MAX_VALUE) || hash.find(cell) != hash.end())
- {
- valid = false;
- break;
- }
- hash.insert(cell);
- }
- pthread_spin_lock(&lock);
- cout << "Thread " << data->id + 1 << ", " << data->type << (valid ? ", Valid" : ", Invalid") << endl;
- pthread_spin_unlock(&lock);
- pthread_exit(0);
- return 0;
- }
- int main()
- {
- pthread_t threads[NUM_THREADS];
- struct thread_data thread_data[NUM_THREADS];
- long thread_id = 0;
- vector<int> grid;
- pthread_spin_init(&lock, PTHREAD_PROCESS_SHARED);
- //pthread_barrier_init(&barrier, NULL, NUM_THREADS + 1);
- ifstream input("sudoku.txt");
- if (input)
- {
- int cell;
- while (input >> cell)
- {
- grid.push_back(cell);
- }
- }
- for (auto i = 0; i < AREA_SIZE; i++, thread_id++)
- {
- vector<int> row(AREA_SIZE);
- for(auto j = 0; j < AREA_SIZE; j++)
- {
- row[j] = get_cell(grid, i, j);
- }
- string row_type = "Row ";
- row_type.insert(4, to_string(i + 1));
- thread_data[thread_id] = { thread_id, row_type, row };
- if (pthread_create(&threads[thread_id], NULL, validate, (void*) &thread_data[thread_id]))
- cout << "error creating thread: " << thread_id << endl;
- }
- for (auto i = 0; i < AREA_SIZE; i++, thread_id++)
- {
- vector<int> column(AREA_SIZE);
- for(auto j = 0; j < AREA_SIZE; j++)
- {
- column[j] = get_cell(grid, j, i);
- }
- string col_type = "Column ";
- col_type.insert(7, to_string(i + 1));
- thread_data[thread_id] = { thread_id, col_type, column };
- if (pthread_create(&threads[thread_id], NULL, validate, (void*) &thread_data[thread_id]))
- cout << "error creating thread: " << thread_id + 1 << endl;
- }
- for (auto i = 0; i < AREA_SIZE; i += SUBGRID_LEN)
- {
- for (auto j = 0; j < AREA_SIZE; j += SUBGRID_LEN, thread_id++)
- {
- vector<int> subgrid;
- for (auto k = i; k < i + SUBGRID_LEN; k++)
- {
- for (auto m = j; m < j + SUBGRID_LEN; m++)
- subgrid.push_back(get_cell(grid, k, m));
- }
- string subgrid_type = "Subgrid RxC";
- for (auto s = 1; s <= SUBGRID_LEN; s++)
- {
- subgrid_type.insert(8 + s, to_string(i + s));
- subgrid_type.append(to_string(j + s));
- }
- thread_data[thread_id] = { thread_id, subgrid_type, subgrid };
- if (pthread_create(&threads[thread_id], NULL, validate, (void*) &thread_data[thread_id]))
- cout << "error creating thread: " << thread_id + 1 << endl;
- }
- }
- for (auto i = 0; i < NUM_THREADS; i++)
- {
- if (pthread_join(threads[i], NULL))
- cout << "error joining thread: " << i + 1 << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement