Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<thread>
- #include<cstdlib>
- #include<ctime>
- #include <chrono>
- using namespace std;
- static const int MATRIX_SIZE = 1000;
- typedef chrono::time_point<chrono::steady_clock> my_clock;
- my_clock startExecution(string title) {
- my_clock start = chrono::high_resolution_clock::now();
- printf("Start: %s\n", title.c_str());
- return start;
- }
- void stopExecution(string title, my_clock start) {
- my_clock end = chrono::high_resolution_clock::now();
- chrono::duration<double, std::milli> diff = end - start;
- printf("Stop: %s. Total: %f ms\n", title.c_str(), diff.count());
- }
- double MatrixMult(double**A, double**B, double**C)
- {
- double checkSum = 0;
- for (int i = 0; i < MATRIX_SIZE; i++)
- {
- for (int j = 0; j < MATRIX_SIZE; j++)
- {
- for (int k = 0; k < MATRIX_SIZE; k++)
- {
- C[i][j] += A[i][k] * B[k][j];
- }
- }
- }
- cout << "kontrolnaya summa" << endl;
- for (int i = 0; i < MATRIX_SIZE; i++)
- {
- checkSum += C[i][i];
- }
- cout << checkSum << endl;
- return checkSum;
- }
- void threadMain(double** A, double**B, double** C, int firstIndex, int lastIndex)
- {
- double checkSum = 0;
- for (int index = firstIndex; index < lastIndex; ++index) {
- double sum = 0.0;
- for (int i = 0; i < MATRIX_SIZE; ++i) {
- int fRow = index / MATRIX_SIZE;
- int sCol = index % MATRIX_SIZE;
- sum += A[fRow][i] * B[i][sCol];
- }
- int rRow = index / MATRIX_SIZE;
- int rCol = index % MATRIX_SIZE;
- C[rRow][rCol] = sum;
- }
- }
- void MatrixMultMT(double**A, double**B, double**C, int threadCount)
- {
- int cellsForThread = (MATRIX_SIZE * MATRIX_SIZE) / threadCount;
- int firstIndex = 0;
- double checkSum = 0;
- thread* threadList = new thread[threadCount];
- for (int threadIndex = threadCount - 1; threadIndex >= 0; --threadIndex) {
- int lastIndex = firstIndex + cellsForThread;
- if (threadIndex == 0) {
- lastIndex = MATRIX_SIZE * MATRIX_SIZE;
- }
- threadList[threadIndex] = thread(threadMain, ref(A), ref(B), ref(C), firstIndex, lastIndex);
- firstIndex = lastIndex;
- }
- for (int i = 0; i < threadCount; i++) {
- threadList[i].join();
- }
- cout << "kontrolnaya summa" << endl;
- for (int i = 0; i < MATRIX_SIZE; i++)
- {
- checkSum += C[i][i];
- }
- cout << checkSum << endl;
- }
- int main()
- {
- srand(time(NULL));
- int threadsCount;
- cout << "vvedite kol-vo potokov" << endl;
- cin >> threadsCount;
- double**A, **B, **C;
- A = new double*[MATRIX_SIZE];
- for (int i = 0; i < MATRIX_SIZE; ++i) {
- A[i] = new double[MATRIX_SIZE];
- for (int j = 0; j < MATRIX_SIZE; ++j) {
- A[i][j] = (rand() % 901 + 100) / 100.0;
- }
- }
- B = new double*[MATRIX_SIZE];
- for (int i = 0; i < MATRIX_SIZE; ++i) {
- B[i] = new double[MATRIX_SIZE];
- for (int j = 0; j < MATRIX_SIZE; ++j) {
- B[i][j] = (rand() % 901 + 100) / 100.0;
- }
- }
- C = new double*[MATRIX_SIZE];
- for (int i = 0; i < MATRIX_SIZE; ++i) {
- C[i] = new double[MATRIX_SIZE];
- for (int j = 0; j < MATRIX_SIZE; ++j) {
- C[i][j] = 0.0;
- }
- }
- /*cout << "first matrix"<<endl;
- for (int i = 0; i < MATRIX_SIZE; i++)
- {
- for (int j = 0; j < MATRIX_SIZE; j++)
- {
- cout << A[i][j] << " ";
- }
- cout << endl;
- }
- cout <<"second matrix"<< endl;
- for (int i = 0; i < MATRIX_SIZE; i++)
- {
- for (int j = 0; j < MATRIX_SIZE; j++)
- {
- cout << B[i][j] << " ";
- }
- cout << endl;
- }*/
- my_clock timer;
- timer = startExecution("single-thread");
- stopExecution("single-thread", timer);
- MatrixMult(A, B, C);
- timer = startExecution("multi-threads");
- MatrixMultMT(A, B, C, threadsCount);
- stopExecution("multi-thread", timer);
- for (int i = 0; i < MATRIX_SIZE; i++) {
- delete[] A[i];
- }
- delete[] A;
- for (int i = 0; i < MATRIX_SIZE; i++) {
- delete[] B[i];
- }
- delete[] B;
- for (int i = 0; i < MATRIX_SIZE; i++) {
- delete[] C[i];
- }
- delete[] C;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement