From 2cee218cbdd1831b71cff9732a696a64779f843e Mon Sep 17 00:00:00 2001 From: 11asa11 Date: Sun, 20 Oct 2019 21:41:53 +0300 Subject: [PATCH 1/4] git status! --- 1706-4/Andreev_SA/Source1.cpp | 75 +++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 1706-4/Andreev_SA/Source1.cpp diff --git a/1706-4/Andreev_SA/Source1.cpp b/1706-4/Andreev_SA/Source1.cpp new file mode 100644 index 0000000..5900948 --- /dev/null +++ b/1706-4/Andreev_SA/Source1.cpp @@ -0,0 +1,75 @@ +#include +#include "mpi.h" +using namespace std; +int countOfPrintSymbol(const char *str, int size) +{ + int count = 0; + for (int i = 0; i < size; ++i) { + if ((' ' != str[i]) && ('\0' != str[i]) && ('\n' != str[i]) && ('\t' != str[i]) && ((int)str[i] >= 33) && ((int)str[i] <= 255)) + count++; + } + return count; +} +int main(int argc, char **argv) +{ + setlocale(LC_ALL, "rus"); + int ProcRank, ProcNum; + MPI_Init(NULL, NULL); + MPI_Comm_size(MPI_COMM_WORLD, &ProcNum); + MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank); + MPI_Status status; + char* str = NULL; int count = 0, buffer; + int part; + int len; + double start = 0; + double finish,dTime; + int sizeStr, length; + if (ProcRank == 0) + { + cout << "Size str: "; + cin >> sizeStr; + str = new char[sizeStr]; + str[sizeStr - 1] = '\0'; + length = strlen(str); + for (int i = 0; i < sizeStr - 1; i++) + { + str[i] = (char)(rand() % (35 - 32) + 32); + //str[i] = (char)(rand() % (100 - 32) + 32); + //cout<< "Symbol:" << str[i] << endl; + } + cout << endl; + part = length / ProcNum; + len = length % ProcNum; + start = MPI_Wtime(); + for (int i = 1; i < ProcNum; i++) + { + MPI_Send(str + len + part * i, part, MPI_CHAR, i, 0, MPI_COMM_WORLD); + } + } + MPI_Bcast(&length, 1, MPI_INT, 0, MPI_COMM_WORLD); + MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD); + MPI_Bcast(&part, 1, MPI_INT, 0, MPI_COMM_WORLD); + if (ProcRank>0) + { + char* recvStr = new char[length]; + MPI_Recv(recvStr, length, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status); + count = countOfPrintSymbol(recvStr, length); + MPI_Send(&count, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); + } + if (ProcRank == 0) + { + //count = countOfPrintSymbol(str, len + part); + for (int i = 1; i < ProcNum; i++) + { + MPI_Recv(&buffer, 1, MPI_INT, i, MPI_ANY_TAG, MPI_COMM_WORLD, &status); + count += buffer; + } + count += countOfPrintSymbol(str, part+len); + finish = MPI_Wtime(); + dTime = finish - start; + } + if(ProcRank!=0) { cout << ProcRank << "process count:" << count << endl; } + else {cout<< "Result:" << count; } + MPI_Finalize(); + return 0; +} From 5f109ec10dac19ff0bd7af7116c7861f388232fe Mon Sep 17 00:00:00 2001 From: 11asa11 Date: Sat, 18 Apr 2020 23:31:52 +0300 Subject: [PATCH 2/4] Added lab2(OpenMP) --- 1706-4/Andreev_SA/Source1.cpp | 75 ----- 1706-4/Andreev_SA/StrassenOpenMP.cpp | 443 +++++++++++++++++++++++++++ 2 files changed, 443 insertions(+), 75 deletions(-) delete mode 100644 1706-4/Andreev_SA/Source1.cpp create mode 100644 1706-4/Andreev_SA/StrassenOpenMP.cpp diff --git a/1706-4/Andreev_SA/Source1.cpp b/1706-4/Andreev_SA/Source1.cpp deleted file mode 100644 index 5900948..0000000 --- a/1706-4/Andreev_SA/Source1.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include -#include "mpi.h" -using namespace std; -int countOfPrintSymbol(const char *str, int size) -{ - int count = 0; - for (int i = 0; i < size; ++i) { - if ((' ' != str[i]) && ('\0' != str[i]) && ('\n' != str[i]) && ('\t' != str[i]) && ((int)str[i] >= 33) && ((int)str[i] <= 255)) - count++; - } - return count; -} -int main(int argc, char **argv) -{ - setlocale(LC_ALL, "rus"); - int ProcRank, ProcNum; - MPI_Init(NULL, NULL); - MPI_Comm_size(MPI_COMM_WORLD, &ProcNum); - MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank); - MPI_Status status; - char* str = NULL; int count = 0, buffer; - int part; - int len; - double start = 0; - double finish,dTime; - int sizeStr, length; - if (ProcRank == 0) - { - cout << "Size str: "; - cin >> sizeStr; - str = new char[sizeStr]; - str[sizeStr - 1] = '\0'; - length = strlen(str); - for (int i = 0; i < sizeStr - 1; i++) - { - str[i] = (char)(rand() % (35 - 32) + 32); - //str[i] = (char)(rand() % (100 - 32) + 32); - //cout<< "Symbol:" << str[i] << endl; - } - cout << endl; - part = length / ProcNum; - len = length % ProcNum; - start = MPI_Wtime(); - for (int i = 1; i < ProcNum; i++) - { - MPI_Send(str + len + part * i, part, MPI_CHAR, i, 0, MPI_COMM_WORLD); - } - } - MPI_Bcast(&length, 1, MPI_INT, 0, MPI_COMM_WORLD); - MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD); - MPI_Bcast(&part, 1, MPI_INT, 0, MPI_COMM_WORLD); - if (ProcRank>0) - { - char* recvStr = new char[length]; - MPI_Recv(recvStr, length, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status); - count = countOfPrintSymbol(recvStr, length); - MPI_Send(&count, 1, MPI_INT, 0, 0, MPI_COMM_WORLD); - } - if (ProcRank == 0) - { - //count = countOfPrintSymbol(str, len + part); - for (int i = 1; i < ProcNum; i++) - { - MPI_Recv(&buffer, 1, MPI_INT, i, MPI_ANY_TAG, MPI_COMM_WORLD, &status); - count += buffer; - } - count += countOfPrintSymbol(str, part+len); - finish = MPI_Wtime(); - dTime = finish - start; - } - if(ProcRank!=0) { cout << ProcRank << "process count:" << count << endl; } - else {cout<< "Result:" << count; } - MPI_Finalize(); - return 0; -} diff --git a/1706-4/Andreev_SA/StrassenOpenMP.cpp b/1706-4/Andreev_SA/StrassenOpenMP.cpp new file mode 100644 index 0000000..f669bad --- /dev/null +++ b/1706-4/Andreev_SA/StrassenOpenMP.cpp @@ -0,0 +1,443 @@ +#include +#include +#include +#include + +using namespace std; + +void PrintMatrix(double* matrix, int N) +{ + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + std::cout.width(6); + //cout << matrix[i*N + j] << " "; + std::cout << matrix[i * N + j] << " " << std::setw(6); + } + std::cout << std::endl; + } + std::cout << std::endl; +} + +void GenerateRandomMatrix(double* matrix1, double* matrix2, int N) +{ + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + matrix1[i * N + j] = ((rand() % 101 - 50) / 10.0); + matrix2[i * N + j] = ((rand() % 101 - 50) / 10.0); + /*matrix1[i*N + j] = (double)rand() / (double)RAND_MAX* 5; + matrix2[i*N + j] = (double)rand() / (double)RAND_MAX* 5 ;*/ + } + } +} + +double* CreateMatrix(int N) +{ + double* matrix = new double[N * N]; + return matrix; +} + +double* defaultMult(double* matrix1, double* matrix2, int N) +{ + double* tmp = CreateMatrix(N); + double sum; + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) { + sum = 0; + for (int k = 0; k < N; k++) + { + sum += matrix1[i * N + k] * matrix2[k * N + j]; + } + tmp[i * N + j] = sum; + } + } + return tmp; +} + +double* Add(double* matrix1, double* matrix2, int N) +{ + double* tmp = CreateMatrix(N); + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + tmp[i * N + j] = matrix1[i * N + j] + matrix2[i * N + j]; + } + } + return tmp; +} + + +double* Add(double* matrix1, double* matrix2, double* matrix3, double* matrix4, int N) +{ + double* tmp = CreateMatrix(N); + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + tmp[i * N + j] = matrix1[i * N + j] + matrix2[i * N + j] + matrix3[i * N + j] + matrix4[i * N + j]; + } + } + return tmp; +} + + +double* Sub(double* matrix1, double* matrix2, int N) +{ + double* tmp = CreateMatrix(N); + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + tmp[i * N + j] = matrix1[i * N + j] - matrix2[i * N + j]; + } + } + return tmp; +} + + +double* Sub(double* matrix1, double* matrix2, double* matrix3, double* matrix4, int N) +{ + double* tmp = CreateMatrix(N); + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + tmp[i * N + j] = matrix1[i * N + j] + matrix2[i * N + j] + matrix3[i * N + j] - matrix4[i * N + j]; + } + } + return tmp; +} + + +bool matComparison(double* matrix1, double* matrix2, int N) +{ + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + { + if (matrix1[i * N + j] != matrix2[i * N + j]) + { + return false; + //std::cout << "Not Equal" << std::endl; + } + } + } + return true; +} + +double* Str_alg(double* matrix1, double* matrix2, int N, int threshold); + +double* StrassenParallel(double* matrix1, double* matrix2, int N, int threshold, int ThreadsNum) +{ + double* Rez; + + if (N <= threshold) + Rez = defaultMult(matrix1, matrix2, N); + else + { + Rez = CreateMatrix(N); + N = N / 2; + + double* A[4]; double* B[4]; double* C[4]; double* P[7]; + + double* TMP1; double* TMP2; double* TMP3; double* TMP4; double* TMP5; + double* TMP6; double* TMP7; double* TMP8; double* TMP9; double* TMP10; + + for (int i = 0; i < 4; i++) + { + A[i] = CreateMatrix(N); + B[i] = CreateMatrix(N); + } + + int i, j; +#pragma omp parallel //shared(ThreadsNum) + { + //std::cout << "Num: " << omp_get_num_threads() << std::endl; +#pragma omp for private(i,j) schedule(static) +// schedule(static) — статическое планирование. При использовании такой опции итерации цикла будут поровну (приблизительно) поделены между потоками +// schedule(static, 10) — блочно-циклическое распределение итераций. Каждый поток получает заданное число итераций в начале цикла + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + { + A[0][i * N + j] = matrix1[2 * i * N + j]; + A[1][i * N + j] = matrix1[2 * i * N + j + N]; + A[2][i * N + j] = matrix1[2 * i * N + j + 2 * N * N]; + A[3][i * N + j] = matrix1[2 * i * N + j + 2 * N * N + N]; + + B[0][i * N + j] = matrix2[2 * i * N + j]; + B[1][i * N + j] = matrix2[2 * i * N + j + N]; + B[2][i * N + j] = matrix2[2 * i * N + j + 2 * N * N]; + B[3][i * N + j] = matrix2[2 * i * N + j + 2 * N * N + N]; + } + } +#pragma omp sections + { +#pragma omp section + { + TMP1 = Add(A[0], A[3], N); // (A11 + A22) + } +#pragma omp section + { + TMP2 = Add(B[0], B[3], N); // (B11 + B22) + } +#pragma omp section + { + TMP3 = Add(A[2], A[3], N); // (A21 + A22) + } +#pragma omp section + { + TMP4 = Sub(B[1], B[3], N); // (B12 - B22) + } +#pragma omp section + { + TMP5 = Sub(B[2], B[0], N); // (B21 - B11) + } +#pragma omp section + { + TMP6 = Add(A[0], A[1], N); // (A11 + A12) + } +#pragma omp section + { + TMP7 = Sub(A[2], A[0], N); // (A21 - A11) + } +#pragma omp section + { + TMP8 = Add(B[0], B[1], N); // (B11 + B12) + } +#pragma omp section + { + TMP9 = Sub(A[1], A[3], N); // (A12 - A22) + } +#pragma omp section + { + TMP10 = Add(B[2], B[3], N); // (B21 + B22) + } + } + +#pragma omp sections + { +#pragma omp section + { + P[0] = StrassenParallel(TMP1, TMP2, N, threshold, ThreadsNum); // (A11 + A22)*(B11 + B22) + } +#pragma omp section + { + P[1] = StrassenParallel(TMP3, B[0], N, threshold, ThreadsNum); // (A21 + A22)*B11 + } +#pragma omp section + P[2] = StrassenParallel(A[0], TMP4, N, threshold, ThreadsNum); // A11*(B12 - B22) +#pragma omp section + P[3] = StrassenParallel(A[3], TMP5, N, threshold, ThreadsNum); // A22*(B21 - B11) +#pragma omp section + P[4] = StrassenParallel(TMP6, B[3], N, threshold, ThreadsNum); // (A11 + A12)*B22 +#pragma omp section + P[5] = StrassenParallel(TMP7, TMP8, N, threshold, ThreadsNum); // (A21 - A11)*(B11 + B12) +#pragma omp section + P[6] = StrassenParallel(TMP9, TMP10, N, threshold, ThreadsNum); // (A12 - A22)*(B21 + B22) + } + + +#pragma omp sections + { +#pragma omp section + C[0] = Sub(P[0], P[3], P[6], P[4], N); // P1 + P4 - P5 + P7 +#pragma omp section + C[1] = Add(P[2], P[4], N); // P3 + P5 +#pragma omp section + C[2] = Add(P[1], P[3], N); // P2 + P4 +#pragma omp section + C[3] = Sub(P[0], P[2], P[5], P[1], N); // P1 - P2 + P3 + P6 + } + +#pragma omp for private(i,j) schedule(static) + for (i = 0; i < N; i++) + for (j = 0; j < N; j++) { + Rez[i * 2 * N + j] = C[0][i * N + j]; + Rez[i * 2 * N + j + N] = C[1][i * N + j]; + Rez[i * 2 * N + j + 2 * N * N] = C[2][i * N + j]; + Rez[i * 2 * N + j + 2 * N * N + N] = C[3][i * N + j]; + } + } + + for (int i = 0; i < 4; i++) { + delete[] A[i]; + delete[] B[i]; + delete[] C[i]; + } + + for (int i = 0; i < 7; i++) { + delete[] P[i]; + } + + delete[]TMP1; delete[]TMP2; delete[]TMP3; delete[]TMP4; delete[]TMP5; + delete[]TMP6; delete[]TMP7; delete[]TMP8; delete[]TMP9; delete[]TMP10; + } + + //std::cout << "Threads: "<> degree; + Size = (int)pow(2, degree); + std::cout << "Size of matrix: " << Size << std::endl << std::endl; + + std::cout << "Number of threads: "; + std::cin >> ThreadsNum; + std::cout << std::endl; + omp_set_num_threads(ThreadsNum); + + double* matA = nullptr; + double* matB = nullptr; + double* matResDefaultMult = nullptr; + double* matRes = nullptr; + double* matResParallel = nullptr; + + matA = CreateMatrix(Size); + matB = CreateMatrix(Size); + matRes = CreateMatrix(Size); + + GenerateRandomMatrix(matA, matB, Size); + + //std::cout << "Matrix A:" << std::endl; + //PrintMatrix(matA, Size); + + //std::cout << "Matrix B:" << std::endl; + //PrintMatrix(matB, Size); + + //startDefaultMult = omp_get_wtime(); + // matResDefaultMult = defaultMult(matA, matB, Size); + //endDefaultMult = omp_get_wtime(); + + //std::cout << "Threads: "< Date: Mon, 20 Apr 2020 19:15:58 +0300 Subject: [PATCH 3/4] some fixes --- 1706-4/Andreev_SA/StrassenOpenMP.cpp | 40 ++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/1706-4/Andreev_SA/StrassenOpenMP.cpp b/1706-4/Andreev_SA/StrassenOpenMP.cpp index f669bad..f2d5b93 100644 --- a/1706-4/Andreev_SA/StrassenOpenMP.cpp +++ b/1706-4/Andreev_SA/StrassenOpenMP.cpp @@ -130,9 +130,8 @@ bool matComparison(double* matrix1, double* matrix2, int N) return true; } -double* Str_alg(double* matrix1, double* matrix2, int N, int threshold); -double* StrassenParallel(double* matrix1, double* matrix2, int N, int threshold, int ThreadsNum) +double* StrassenParallel(double* matrix1, double* matrix2, int N, int threshold) { double* Rez; @@ -224,45 +223,64 @@ double* StrassenParallel(double* matrix1, double* matrix2, int N, int threshold, { #pragma omp section { - P[0] = StrassenParallel(TMP1, TMP2, N, threshold, ThreadsNum); // (A11 + A22)*(B11 + B22) + P[0] = StrassenParallel(TMP1, TMP2, N, threshold); // (A11 + A22)*(B11 + B22) } #pragma omp section { - P[1] = StrassenParallel(TMP3, B[0], N, threshold, ThreadsNum); // (A21 + A22)*B11 + P[1] = StrassenParallel(TMP3, B[0], N, threshold); // (A21 + A22)*B11 } #pragma omp section - P[2] = StrassenParallel(A[0], TMP4, N, threshold, ThreadsNum); // A11*(B12 - B22) + { + P[2] = StrassenParallel(A[0], TMP4, N, threshold); // A11*(B12 - B22) + } #pragma omp section - P[3] = StrassenParallel(A[3], TMP5, N, threshold, ThreadsNum); // A22*(B21 - B11) + { + P[3] = StrassenParallel(A[3], TMP5, N, threshold); // A22*(B21 - B11) + } #pragma omp section - P[4] = StrassenParallel(TMP6, B[3], N, threshold, ThreadsNum); // (A11 + A12)*B22 + { + P[4] = StrassenParallel(TMP6, B[3], N, threshold); // (A11 + A12)*B22 + } #pragma omp section - P[5] = StrassenParallel(TMP7, TMP8, N, threshold, ThreadsNum); // (A21 - A11)*(B11 + B12) + { + P[5] = StrassenParallel(TMP7, TMP8, N, threshold); // (A21 - A11)*(B11 + B12) + } #pragma omp section - P[6] = StrassenParallel(TMP9, TMP10, N, threshold, ThreadsNum); // (A12 - A22)*(B21 + B22) + { + P[6] = StrassenParallel(TMP9, TMP10, N, threshold); // (A12 - A22)*(B21 + B22) + } } #pragma omp sections { #pragma omp section + { C[0] = Sub(P[0], P[3], P[6], P[4], N); // P1 + P4 - P5 + P7 + } #pragma omp section + { C[1] = Add(P[2], P[4], N); // P3 + P5 + } #pragma omp section + { C[2] = Add(P[1], P[3], N); // P2 + P4 + } #pragma omp section + { C[3] = Sub(P[0], P[2], P[5], P[1], N); // P1 - P2 + P3 + P6 + } } #pragma omp for private(i,j) schedule(static) - for (i = 0; i < N; i++) + for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { Rez[i * 2 * N + j] = C[0][i * N + j]; Rez[i * 2 * N + j + N] = C[1][i * N + j]; Rez[i * 2 * N + j + 2 * N * N] = C[2][i * N + j]; Rez[i * 2 * N + j + 2 * N * N + N] = C[3][i * N + j]; } + } } for (int i = 0; i < 4; i++) { @@ -337,7 +355,7 @@ int main() endStras = omp_get_wtime(); startStrasParallel = omp_get_wtime(); - matResParallel = StrassenParallel(matA, matB, Size, 64, ThreadsNum); + matResParallel = StrassenParallel(matA, matB, Size, 64); endStrasParallel = omp_get_wtime(); if (matComparison(matRes, matResParallel, Size) != true) { std::cout << "Mats are not equal" << std::endl< Date: Wed, 13 May 2020 18:11:31 +0300 Subject: [PATCH 4/4] design fixes --- 1706-4/Andreev_SA/StrassenOpenMP.cpp | 34 +++++++++------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/1706-4/Andreev_SA/StrassenOpenMP.cpp b/1706-4/Andreev_SA/StrassenOpenMP.cpp index f2d5b93..aaa0e63 100644 --- a/1706-4/Andreev_SA/StrassenOpenMP.cpp +++ b/1706-4/Andreev_SA/StrassenOpenMP.cpp @@ -131,6 +131,8 @@ bool matComparison(double* matrix1, double* matrix2, int N) } +double* Str_alg(double* matrix1, double* matrix2, int N, int threshold); + double* StrassenParallel(double* matrix1, double* matrix2, int N, int threshold) { double* Rez; @@ -223,31 +225,31 @@ double* StrassenParallel(double* matrix1, double* matrix2, int N, int threshold) { #pragma omp section { - P[0] = StrassenParallel(TMP1, TMP2, N, threshold); // (A11 + A22)*(B11 + B22) + P[0] = Str_alg(TMP1, TMP2, N, threshold); // (A11 + A22)*(B11 + B22) } #pragma omp section { - P[1] = StrassenParallel(TMP3, B[0], N, threshold); // (A21 + A22)*B11 + P[1] = Str_alg(TMP3, B[0], N, threshold); // (A21 + A22)*B11 } #pragma omp section { - P[2] = StrassenParallel(A[0], TMP4, N, threshold); // A11*(B12 - B22) + P[2] = Str_alg(A[0], TMP4, N, threshold); // A11*(B12 - B22) } #pragma omp section { - P[3] = StrassenParallel(A[3], TMP5, N, threshold); // A22*(B21 - B11) + P[3] = Str_alg(A[3], TMP5, N, threshold); // A22*(B21 - B11) } #pragma omp section { - P[4] = StrassenParallel(TMP6, B[3], N, threshold); // (A11 + A12)*B22 + P[4] = Str_alg(TMP6, B[3], N, threshold); // (A11 + A12)*B22 } #pragma omp section { - P[5] = StrassenParallel(TMP7, TMP8, N, threshold); // (A21 - A11)*(B11 + B12) + P[5] = Str_alg(TMP7, TMP8, N, threshold); // (A21 - A11)*(B11 + B12) } #pragma omp section { - P[6] = StrassenParallel(TMP9, TMP10, N, threshold); // (A12 - A22)*(B21 + B22) + P[6] = Str_alg(TMP9, TMP10, N, threshold); // (A12 - A22)*(B21 + B22) } } @@ -338,18 +340,6 @@ int main() GenerateRandomMatrix(matA, matB, Size); - //std::cout << "Matrix A:" << std::endl; - //PrintMatrix(matA, Size); - - //std::cout << "Matrix B:" << std::endl; - //PrintMatrix(matB, Size); - - //startDefaultMult = omp_get_wtime(); - // matResDefaultMult = defaultMult(matA, matB, Size); - //endDefaultMult = omp_get_wtime(); - - //std::cout << "Threads: "<