#include #include #include #include int ** A, ** B, ** BT, ** C; const int size = 5000; void transpose() { for (int i=0; i < size; i++) { for (int j=0; j < size; j++) { BT[j][i] = B[i][j]; } } } void multiply(int start, int stop) { for (int i = start; i < stop; i++) { for (int j = start; j < stop; j++) { int tmp = 0; for (int k = start; k < stop; k++) { tmp += A[i][k] * B[k][j]; } C[i][j]=tmp; } } } void multiply_transposed(int start, int stop) { for (int i = start; i < stop; i++) { for (int j = start; j < stop; j++) { int tmp = 0; for (int k = start; k < stop; k++) { tmp += A[i][k] * BT[j][k]; } C[i][j]=tmp; } } } void execute(int size_th = 4, int method = 0) { if (size_th > 1) { std::vector < std::thread > threads; int stop = 0, start = 0; auto start_time = std::chrono::steady_clock::now(); while (stop < size) { start = stop; stop += (int) size / size_th; std::cout << stop << std::endl; if (method == 0) threads.push_back(std::thread(multiply, start, stop - 1)); else threads.push_back(std::thread(multiply_transposed, start, stop - 1)); } for(int i=0; i < size_th; i++) { threads[i].join(); } auto end_time = std::chrono::steady_clock::now(); std::chrono::duration elapsed_seconds = end_time - start_time; std::cout << "Elapsed time: " << elapsed_seconds.count() << std::endl; } else { auto start_time = std::chrono::steady_clock::now(); if (method == 0) multiply(0, size - 1); else multiply_transposed(0, size - 1); auto end_time = std::chrono::steady_clock::now(); std::chrono::duration elapsed_seconds = end_time - start_time; std::cout << "Elapsed time: " << elapsed_seconds.count() << std::endl; } } int main() { A = new int *[size]; B = new int *[size]; BT = new int *[size]; C = new int *[size]; for(int i=0; i < size; i++) { A[i] = new int[size]; B[i] = new int[size]; BT[i] = new int[size]; C[i] = new int[size]; } for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { A[i][j] = rand() % 100 + 1; B[i][j] = rand() % 100 + 1; C[i][j] = 0; } } transpose(); // For normal multiply // execute(1, 0); // execute(2, 0); // execute(4, 0); // For transposed multiply // execute(1, 1); // execute(2, 1); // execute(4, 1); return 0; }