Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<thread>
- #include<cstdlib>
- #include<ctime>
- using namespace std;
- static const int MATRIX_SIZE = 100;
- static const int THREADS_NUMBER = 4;
- struct Matrix
- {
- int** elements;
- void obnulenie_mtr() {
- elements = new int*[MATRIX_SIZE];
- for (int i = 0; i < MATRIX_SIZE; ++i) {
- elements[i] = new int[MATRIX_SIZE];
- for (int j = 0; j < MATRIX_SIZE; ++j) {
- elements[i][j] = 0.;
- }
- }
- for (int i = 0; i < MATRIX_SIZE; i++)
- {
- delete[] elements[i];
- }
- delete[] elements;
- }
- void zapolnenie_mtr()
- {
- srand(static_cast<int>(time(0)));
- elements = new int*[MATRIX_SIZE];
- for (int i = 0; i < MATRIX_SIZE; ++i) {
- elements[i] = new int[MATRIX_SIZE];
- for (int j = 0; j < MATRIX_SIZE; ++j) {
- elements[i][j] = rand() % 10;
- }
- }
- for (int i = 0; i < MATRIX_SIZE; i++)
- {
- delete[] elements[i];
- }
- delete[] elements;
- }
- void show()
- {
- for (int i = 0; i < MATRIX_SIZE; i++)
- {
- for (int j = 0; j < MATRIX_SIZE; j++)
- {
- cout << elements[i][j] << " ";
- }
- cout << endl;
- }
- }
- };
- void mult(const Matrix& A, const Matrix& B, Matrix& C)
- {
- for (int i = 0; i < MATRIX_SIZE; ++i) {
- for (int j = 0; j < MATRIX_SIZE; ++j) {
- int result = 0;
- for (int k = 0; k < MATRIX_SIZE; ++k) {
- const int e1 = A.elements[i][k];
- const int e2 = B.elements[k][j];
- result += e1 * e2;
- }
- C.elements[i][j] = result;
- }
- }
- }
- void multiply_threading(const Matrix& A, const Matrix& B, Matrix& C, const int thread_number) {
- const int n_elements = (MATRIX_SIZE * MATRIX_SIZE);
- const int n_operations = n_elements / THREADS_NUMBER;
- const int rest_operations = n_elements % THREADS_NUMBER;
- int start_op, end_op;
- if (thread_number == 0) {
- start_op = n_operations * thread_number;
- end_op = (n_operations * (thread_number + 1)) + rest_operations;
- }
- else {
- start_op = n_operations * thread_number + rest_operations;
- end_op = (n_operations * (thread_number + 1)) + rest_operations;
- }
- for (int op = start_op; op < end_op; ++op) {
- const int row = op % MATRIX_SIZE;
- const int col = op / MATRIX_SIZE;
- int r = 0;
- for (int i = 0; i < MATRIX_SIZE; ++i) {
- const int e1 = A.elements[row][i];
- const int e2 = B.elements[i][col];
- r += e1 * e2;
- }
- C.elements[row][col] = r;
- }
- }
- void multithred_vipolnenie(const Matrix& A, const Matrix& B,Matrix& C) {
- std::thread threads[THREADS_NUMBER];
- for (int i = 0; i < THREADS_NUMBER; ++i) {
- threads[i] = std::thread(multiply_threading, std::ref(A),std::ref(B), std::ref(C),i);
- }
- for (int i = 0; i < THREADS_NUMBER; ++i) {
- threads[i].join();
- }
- }
- void obyavlenie_mtr(void(*execution_function)(const Matrix& A,const Matrix& B,Matrix& C)) {
- Matrix A, B, C;
- A.zapolnenie_mtr();
- B.zapolnenie_mtr();
- C.obnulenie_mtr();
- mult(A, B, C);
- }
- int main()
- {
- //obyavlenie_mtr(single_execution);
- obyavlenie_mtr(multithred_vipolnenie);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement