From 4fb065ee72a88daeb2d4331b83a3162faa05bd24 Mon Sep 17 00:00:00 2001 From: EgoRga717 Date: Sun, 8 Nov 2020 21:49:27 +0300 Subject: [PATCH 01/13] Added OpenMP2, but failed TEXT04 --- OpenMP2/src/main.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/OpenMP2/src/main.cpp b/OpenMP2/src/main.cpp index 8dab215..9779957 100644 --- a/OpenMP2/src/main.cpp +++ b/OpenMP2/src/main.cpp @@ -3,9 +3,15 @@ #include #include -double calc(uint32_t x_last, uint32_t num_threads) -{ - return 0; +double calc(uint32_t x_last, uint32_t num_threads) //Fails at TEST04 in last number(sometimes pre-last number). +{ //The strange thing is that my sum little bigger than test sum. + double sum = 0.0; //May be parallel calc is more accuracy? + #pragma omp parallel for num_threads(num_threads) reduction(+:sum) + for(uint32_t i = x_last; i > 0; i--) + { + sum += 1.0 / i; + } + return sum; } int main(int argc, char** argv) From 7d7e1ce01e93cd344173888273b466cb1c337d97 Mon Sep 17 00:00:00 2001 From: EgoRga717 Date: Mon, 9 Nov 2020 01:48:20 +0300 Subject: [PATCH 02/13] Added OpenMP3 --- OpenMP3/src/main.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/OpenMP3/src/main.cpp b/OpenMP3/src/main.cpp index 416ee06..9fa854b 100644 --- a/OpenMP3/src/main.cpp +++ b/OpenMP3/src/main.cpp @@ -11,7 +11,13 @@ double func(double x) double calc(double x0, double x1, double dx, uint32_t num_threads) { - return 0; + int steps_num = round((x1 - x0) / dx); + double sum = (- func(x0) / 2.0 + func(x0 + steps_num * dx) / 2.0) * dx; //<-*dx + #pragma omp parallel for num_threads(num_threads) reduction(+:sum) + for(int i = 0; i < steps_num; ++i) + sum += func(x0 + i * dx) * dx; //<--*dx + //sum *= dx; //At first, i used this, and without "* dx" in marked "<--*dx" strings. But tests 06 and 07 were failed (1.99999999 != 2) + return sum; } int main(int argc, char** argv) From e1dc71c5b3beb9f4317d6e32e2cb0fd0e43c9e3b Mon Sep 17 00:00:00 2001 From: EgoRga717 Date: Tue, 10 Nov 2020 20:35:49 +0300 Subject: [PATCH 03/13] Added OpenMP4, failed in last number in TEST05 (build:...5; test:...6) --- OpenMP4/src/main.cpp | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/OpenMP4/src/main.cpp b/OpenMP4/src/main.cpp index 036d8e3..2428abb 100644 --- a/OpenMP4/src/main.cpp +++ b/OpenMP4/src/main.cpp @@ -3,9 +3,32 @@ #include #include -double calc(uint32_t x_last, uint32_t num_threads) +double calc(uint32_t x_last, uint32_t num_threads) //Failed at Test05, the last number is 5, but in test it is 6 { - return 0; + double sum = 0.0; + double pre_sum = 0.0; + double arr_sum[num_threads]; + double k = 1.0; + double arr_k[num_threads]; + #pragma omp parallel num_threads(num_threads) firstprivate(k, pre_sum) shared(arr_sum, arr_k) + { + #pragma omp for + for(uint32_t i = 0; i < x_last; ++i) + { + pre_sum += k; + k /= i + 1.0; + } + int thread_id = omp_get_thread_num(); + arr_k[thread_id] = k; + arr_sum[thread_id] = pre_sum; + } + for(uint32_t i = 1; i < num_threads - 1; ++i) //the last of arr_k isn't necessary + arr_k[i] *= arr_k[i - 1]; + #pragma omp parallel for num_threads(num_threads) reduction(+:sum) + for(uint32_t i = num_threads - 1; i > 0; --i) + sum += arr_sum[i] * arr_k[i - 1]; + sum += arr_sum[0]; + return sum; } int main(int argc, char** argv) From 2fd0912da839c8420aef59e7d9ad162966b9479b Mon Sep 17 00:00:00 2001 From: EgoRga717 Date: Wed, 11 Nov 2020 19:02:11 +0300 Subject: [PATCH 04/13] Added OpenMP5. Anomaly: when table is small, and iterations are many, 8 threads takes more time than 1 thread (TEST 05 and TEST 04) --- OpenMP5/src/main.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/OpenMP5/src/main.cpp b/OpenMP5/src/main.cpp index bf042c5..20980ea 100644 --- a/OpenMP5/src/main.cpp +++ b/OpenMP5/src/main.cpp @@ -1,3 +1,5 @@ +//:set tabstop=2 + #include #include #include @@ -5,7 +7,56 @@ void calc(uint32_t xSize, uint32_t ySize, uint32_t iterations, uint32_t num_threads, uint8_t* inputFrame, uint8_t* outputFrame) { + uint8_t* neighbours_map[xSize + 2][ySize + 2]; //to easy understand who is neighbour + #pragma omp parallel for num_threads(num_threads) shared(neighbours_map, inputFrame) + for(uint64_t i = 0; i < xSize * ySize; ++i) + { + uint32_t tmp_x = i % xSize; + uint32_t tmp_y = i / xSize; + neighbours_map[tmp_x + 1][tmp_y + 1] = &inputFrame[i]; + } + #pragma omp parallel for num_threads(num_threads) shared(neighbours_map, inputFrame) + for(uint32_t i = 0; i < xSize; ++i) + { + neighbours_map[i + 1][0] = &inputFrame[i + (ySize-1)*xSize]; + neighbours_map[i + 1][ySize + 1] = &inputFrame[i + 0*xSize]; + } + #pragma omp parallel for num_threads(num_threads) shared(neighbours_map, inputFrame) + for(uint32_t i = 0; i < ySize; ++i) + { + neighbours_map[0][i + 1] = &inputFrame[(xSize-1) + i*xSize]; + neighbours_map[xSize + 1][i + 1] = &inputFrame[0 + i*xSize]; + } + neighbours_map[0][0] = &inputFrame[(xSize-1)+(ySize-1)*xSize]; + neighbours_map[0][ySize + 1] = &inputFrame[(xSize-1) + 0*xSize]; + neighbours_map[xSize + 1][0] = &inputFrame[0 + (ySize-1)*xSize]; + neighbours_map[xSize + 1][ySize + 1] = &inputFrame[0 + 0*xSize]; + + if(iterations == 0) // little kostil + #pragma omp parallel for num_threads(num_threads) shared(inputFrame, outputFrame) + for(uint64_t i = 0; i < xSize*ySize; ++i) + outputFrame[i] = inputFrame[i]; + for(uint32_t iter = 0; iter < iterations; ++iter) + { + #pragma omp parallel for num_threads(num_threads) shared(neighbours_map, inputFrame, outputFrame) + for(uint64_t i = 0; i < xSize*ySize; ++i) + { + uint32_t x = i % xSize; + uint32_t y = i / xSize; + uint8_t neighbours_sum = *neighbours_map[x][y] + *neighbours_map[x+1][y] + //123 (1,2) + *neighbours_map[x+2][y] + *neighbours_map[x][y+1] + *neighbours_map[x+2][y+1] + //4X5 (3,4,5) + *neighbours_map[x][y+2] + *neighbours_map[x+1][y+2] + *neighbours_map[x+2][y+2]; //678 (6,7,8) + if((inputFrame[i] == 0 && neighbours_sum == 3) || \ + (inputFrame[i] == 1 && (neighbours_sum == 2 || neighbours_sum == 3))) + outputFrame[i] = 1; + else + outputFrame[i] = 0; + } + #pragma omp parallel for num_threads(num_threads) shared(inputFrame, outputFrame) + for(uint64_t i = 0; i < xSize*ySize; ++i) + inputFrame[i] = outputFrame[i]; + } } int main(int argc, char** argv) From cfb2c856aad5d888ca3b6bdb1d100440744f9e4b Mon Sep 17 00:00:00 2001 From: EgoRga717 Date: Fri, 13 Nov 2020 16:02:34 +0300 Subject: [PATCH 05/13] Added Loop1, but increasing number of threads increases time, in my laptop --- Loop1/src/main.cpp | 54 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/Loop1/src/main.cpp b/Loop1/src/main.cpp index 318f940..af47ecf 100644 --- a/Loop1/src/main.cpp +++ b/Loop1/src/main.cpp @@ -7,16 +7,58 @@ void calc(double* arr, uint32_t ySize, uint32_t xSize, int rank, int size) { + int count; + uint64_t xySize; + int mod; + double* local_array; if (rank == 0 && size > 0) { - for (uint32_t y = 0; y < ySize; y++) + xySize = xSize * ySize; + int div = xySize / size; + mod = xySize % size; + + count = div + 1; + for(int i = 0; i < mod; ++i) { - for (uint32_t x = 0; x < xSize; x++) - { - arr[y*xSize + x] = sin(0.00001*arr[y*xSize + x]); - } + MPI_Send(&count, 1 , MPI_INT, i + 1, 0, MPI_COMM_WORLD); + MPI_Send(&arr[(count - 1) + i * count], count , MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD); } + + --count; + for(int i = mod; i < size - 1; ++i) + { + MPI_Send(&count, 1 , MPI_INT, i + 1, 0, MPI_COMM_WORLD); + MPI_Send(&arr[count + i * count + mod], count, MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD); + } + + local_array = arr; + } + if (rank != 0 && size > 0) + { + MPI_Recv(&count, 1 , MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + local_array = new double[count]; + MPI_Recv(local_array, count, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); } + + for(int i = 0; i < count; ++i) + local_array[i] = sin(0.00001*(local_array[i])); + + if (rank != 0 && size > 0) + { + MPI_Send(local_array, count, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD); + } + + if (rank == 0 && size > 0) + { + ++count; + for(int i = 0; i < mod; ++i) + MPI_Recv(&arr[(count - 1) + i * count], count, MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + --count; + for(int i = mod; i < size - 1; ++i) + MPI_Recv(&arr[count + i * count + mod], count, MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + } + if (rank != 0 && size > 0) + delete local_array; } int main(int argc, char** argv) @@ -73,7 +115,7 @@ int main(int argc, char** argv) } calc(arr, ySize, xSize, rank, size); - + if (rank == 0) { // Prepare output file From 20695de91f6df5e4d6b45c058b7cf88af3c4c6fb Mon Sep 17 00:00:00 2001 From: EgoRga717 Date: Tue, 17 Nov 2020 17:53:11 +0300 Subject: [PATCH 06/13] Added Loop2 --- Loop2/src/main.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/Loop2/src/main.cpp b/Loop2/src/main.cpp index 51ea220..e63218b 100644 --- a/Loop2/src/main.cpp +++ b/Loop2/src/main.cpp @@ -5,20 +5,75 @@ #include #include +//arr[y*xSize + x] = sin(0.00001*arr[(y + 1)*xSize + x - 3]); void calc(double* arr, uint32_t ySize, uint32_t xSize, int rank, int size) { + int count; + int mod; + double* local_array; + double* send_array; if (rank == 0 && size > 0) { - for (uint32_t y = 0; y < ySize - 1; y++) + uint64_t xySize; + xSize -= 3; + ySize -= 1; + xySize = xSize * ySize; + int div = xySize / size; + mod = xySize % size; + send_array = new double[xySize]; + + for(uint32_t i = 0; i < ySize; ++i) + for(uint32_t j = 0; j < xSize; ++j) + send_array[i * xSize + j] = arr[(i + 1) * (xSize + 3) + j]; + + count = div + 1; + for(int i = 0; i < mod; ++i) { - for (uint32_t x = 3; x < xSize; x++) - { - arr[y*xSize + x] = sin(0.00001*arr[(y + 1)*xSize + x - 3]); - } + MPI_Send(&count, 1 , MPI_INT, i + 1, 0, MPI_COMM_WORLD); + MPI_Send(&send_array[(count - 1) + i * count], count , MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD); + } + + --count; + for(int i = mod; i < size - 1; ++i) + { + MPI_Send(&count, 1 , MPI_INT, i + 1, 0, MPI_COMM_WORLD); + MPI_Send(&send_array[count + i * count + mod], count, MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD); } + + local_array = send_array; + } + if (rank != 0 && size > 0) + { + MPI_Recv(&count, 1 , MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + local_array = new double[count]; + MPI_Recv(local_array, count, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + } + + for(int i = 0; i < count; ++i) + local_array[i] = sin(0.00001*(local_array[i])); + + if (rank != 0 && size > 0) + { + MPI_Send(local_array, count, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD); + delete local_array; + } + + if (rank == 0 && size > 0) + { + ++count; + for(int i = 0; i < mod; ++i) + MPI_Recv(&send_array[(count - 1) + i * count], count, MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + --count; + for(int i = mod; i < size - 1; ++i) + MPI_Recv(&send_array[count + i * count + mod], count, MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + for(uint32_t i = 0; i < ySize; ++i) + for(uint32_t j = 0; j < xSize; ++j) + arr[i * (xSize + 3) + (j + 3)] = send_array[i * xSize + j]; + delete send_array; } } + int main(int argc, char** argv) { int rank = 0, size = 0, buf = 0; From 43c4e0b813e3d704697854fd9a4b4e1180dabf65 Mon Sep 17 00:00:00 2001 From: EgoRga717 Date: Wed, 18 Nov 2020 02:15:11 +0300 Subject: [PATCH 07/13] Added Loop3 --- Loop3/src/main.cpp | 97 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 92 insertions(+), 5 deletions(-) diff --git a/Loop3/src/main.cpp b/Loop3/src/main.cpp index 43cce1f..b6515c2 100644 --- a/Loop3/src/main.cpp +++ b/Loop3/src/main.cpp @@ -7,18 +7,105 @@ void calc(double* arr, uint32_t ySize, uint32_t xSize, int rank, int size) { + int count; + int chain_size; //Some threads might needs chain_size-2 calculations. But for easier coding, we will always calculate chain_size-1 calculations + int mod; + double* local_array; + double* send_array; + + if(rank == 0 && size > 0) + { + chain_size = ySize / 4 + 1; + if (ySize % 4 == 0) + --chain_size; + } + MPI_Bcast(&chain_size, 1, MPI_INT, 0, MPI_COMM_WORLD); + if (rank == 0 && size > 0) { - for (uint32_t y = 4; y < ySize; y++) + uint64_t startSize; + chain_size = ySize / 4 + 1; + if (ySize % 4 == 0) + --chain_size; + startSize = xSize * 4; + int div = startSize / size; + mod = startSize % size; + send_array = new double[(div + 1) * chain_size]; + count = div + 1; + for(int i = 0; i < mod; ++i) { - for (uint32_t x = 0; x < xSize; x++) - { - arr[y*xSize + x] = sin(arr[(y - 4)*xSize + x]); - } + MPI_Send(&count, 1 , MPI_INT, i + 1, 0, MPI_COMM_WORLD); + for(int j = 0; j < chain_size; ++j) + for(int k = 0; k < count; ++k) + send_array[j * count + k] = arr[(count - 1) + i * count + j * 4 * xSize + k]; + MPI_Send(send_array, count * chain_size, MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD); + } + + --count; + for(int i = mod; i < size - 1; ++i) + { + MPI_Send(&count, 1 , MPI_INT, i + 1, 0, MPI_COMM_WORLD); + for(int j = 0; j < chain_size; ++j) + for(int k = 0; k < count; ++k) + send_array[j * count + k] = arr[count + mod + i * count + j * 4 * xSize + k]; + MPI_Send(send_array, count * chain_size, MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD); } + + local_array = new double[count * chain_size]; + for(int j = 0; j < chain_size; ++j) + for(int k = 0; k < count; ++k) + local_array[j * count + k] = arr[j * 4 * xSize + k]; + } + + if (rank != 0 && size > 0) + { + MPI_Recv(&count, 1 , MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + local_array = new double[count * chain_size]; + MPI_Recv(local_array, count * chain_size, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); } + + for(int i = 0; i < count; ++i) + for(int j = 0; j < chain_size - 1; ++j) + local_array[i + (j + 1) * count] = sin(local_array[i + j * count]); + + if (rank != 0 && size > 0) + { + MPI_Send(local_array, count * chain_size, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD); + } + + if (rank == 0 && size > 0) + { + int xySize = xSize * ySize; + int ind; + ++count; + for(int i = 0; i < mod; ++i) + { + MPI_Recv(send_array, count * chain_size, MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + for(int j = 0; j < chain_size; ++j) + for(int k = 0; k < count; ++k) + if ((ind = (count - 1) + i * count + j * 4 * xSize + k) < xySize) + arr[ind] = send_array[j * count + k]; + } + --count; + for(int i = mod; i < size - 1; ++i) + { + MPI_Recv(send_array, count * chain_size, MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + for(int j = 0; j < chain_size; ++j) + for(int k = 0; k < count; ++k) + if ((ind = count + mod + i * count + j * 4 * xSize + k) < xySize) + arr[ind] = send_array[j * count + k]; + } + for(int j = 0; j < chain_size; ++j) + for(int k = 0; k < count; ++k) + if((ind = j * 4 * xSize + k) < xySize) + arr[j * 4 * xSize + k] = local_array[j * count + k]; + delete send_array; + } + + delete local_array; } + int main(int argc, char** argv) { int rank = 0, size = 0, buf = 0; From 0f099d044a4251c269ee1108838fbd32a752687a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=B5=D0=BE=D1=80=D0=B3=D0=B8=D0=B9=20=D0=A0=D0=B0?= =?UTF-8?q?=D0=B9=D1=81=D0=B8=D1=85?= <43410520+EgoRga717@users.noreply.github.com> Date: Wed, 18 Nov 2020 14:46:03 +0300 Subject: [PATCH 08/13] Added Loop3. Deleted 3 repeated strings --- Loop3/src/main.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/Loop3/src/main.cpp b/Loop3/src/main.cpp index b6515c2..d6a97aa 100644 --- a/Loop3/src/main.cpp +++ b/Loop3/src/main.cpp @@ -24,9 +24,6 @@ void calc(double* arr, uint32_t ySize, uint32_t xSize, int rank, int size) if (rank == 0 && size > 0) { uint64_t startSize; - chain_size = ySize / 4 + 1; - if (ySize % 4 == 0) - --chain_size; startSize = xSize * 4; int div = startSize / size; mod = startSize % size; From 393040e9919437f7d6dee2dce809717326253cba Mon Sep 17 00:00:00 2001 From: EgoRga717 Date: Wed, 18 Nov 2020 20:52:35 +0300 Subject: [PATCH 09/13] Added Loop4. May be slow, the slowest test (01_p8) lasts 11 sec --- Loop4/src/main.cpp | 170 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 164 insertions(+), 6 deletions(-) diff --git a/Loop4/src/main.cpp b/Loop4/src/main.cpp index d958d54..c8916f5 100644 --- a/Loop4/src/main.cpp +++ b/Loop4/src/main.cpp @@ -4,18 +4,176 @@ #include #include #include +#include //Add this for NaN in border_checker + +double border_checker(double* array, int xSize, int ySize, int zSize, int x, int y, int z) +{ + if((x < xSize) && (x >= 0) && (y < ySize) && (y >= 0) && (z < zSize) && (z >= 0)) + return array[z * xSize * ySize + y * xSize + x]; + else + return std::numeric_limits::quiet_NaN(); +} void calc(double* arr, uint32_t zSize, uint32_t ySize, uint32_t xSize, int rank, int size) { - if (rank == 0 && size > 0) { - for (uint32_t z = 1; z < zSize; z++) { - for (uint32_t y = 0; y < ySize - 1; y++) { - for (uint32_t x = 0; x < xSize - 1; x++) { - arr[z*ySize*xSize + y*xSize + x] = sin(arr[(z - 1)*ySize*xSize + (y + 1)*xSize + x + 1]); - } + int count; + int chain_size; //Some threads might needs chain_size-2 calculations. But for easier coding, we will always calculate chain_size-1 calculations + int mod; + double* local_array; + double* send_array; + int* root_x; + int* root_y; + int* root_z; + if(rank == 0 && size > 0) + { + chain_size = std::min(std::min(xSize, ySize), zSize); //it's max chain size + } + MPI_Bcast(&chain_size, 1, MPI_INT, 0, MPI_COMM_WORLD); + + if (rank == 0 && size > 0) + { //roots: z = 0; x = xSize; y = ySize; 3 sides, sideSize - 1 + uint64_t startSize = (xSize - 1) * (ySize - 1) + (xSize - 1) * (zSize - 2) + (ySize - 2) * (zSize - 2); + root_x = new int[startSize]; + root_y = new int[startSize]; + root_z = new int[startSize]; + int index = 0; + for(uint32_t i = 1; i < xSize; ++i) + for(uint32_t j = 1; j < ySize; ++j) + { + root_x[index] = i; + root_y[index] = j; + root_z[index] = 0; + ++index; + } + for(uint32_t i = 1; i < xSize; ++i) + for(uint32_t j = 1; j < zSize - 1; ++j) + { + root_x[index] = i; + root_y[index] = ySize - 1; + root_z[index] = j; + ++index; } + for(uint32_t i = 1; i < ySize - 1; ++i) + for(uint32_t j = 1; j < zSize - 1; ++j) + { + root_x[index] = xSize - 1; + root_y[index] = i; + root_z[index] = j; + ++index; + } + int div = startSize / size; + mod = startSize % size; + send_array = new double[(div + 1) * chain_size]; + count = div + 1; + for(int i = 0; i < mod; ++i) + { + MPI_Send(&count, 1 , MPI_INT, i + 1, 0, MPI_COMM_WORLD); + for(int j = 0; j < chain_size; ++j) + for(int k = 0; k < count; ++k) + send_array[j * count + k] = border_checker(arr, xSize, ySize, zSize, + root_x[(count - 1) + i * count + k] - j * 1, + root_y[(count - 1) + i * count + k] - j * 1, + root_z[(count - 1) + i * count + k] + j * 1); + MPI_Send(send_array, count * chain_size, MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD); + } + + --count; + for(int i = mod; i < size - 1; ++i) + { + MPI_Send(&count, 1 , MPI_INT, i + 1, 0, MPI_COMM_WORLD); + for(int j = 0; j < chain_size; ++j) + for(int k = 0; k < count; ++k) + send_array[j * count + k] = border_checker(arr, xSize, ySize, zSize, + root_x[count + mod + i * count + k] - j * 1, + root_y[count + mod + i * count + k] - j * 1, + root_z[count + mod + i * count + k] + j * 1); + + MPI_Send(send_array, count * chain_size, MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD); + } + + local_array = new double[count * chain_size]; + for(int j = 0; j < chain_size; ++j) + for(int k = 0; k < count; ++k) + local_array[j * count + k] = border_checker(arr, xSize, ySize, zSize, + root_x[k] - j * 1, + root_y[k] - j * 1, + root_z[k] + j * 1); + + } + + if (rank != 0 && size > 0) + { + MPI_Recv(&count, 1 , MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + local_array = new double[count * chain_size]; + MPI_Recv(local_array, count * chain_size, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + } + + for(int i = 0; i < count; ++i) + for(int j = 0; j < chain_size - 1; ++j) + { + if(local_array[i + (j + 1) * count] == local_array[i + (j + 1) * count]) //Check if not NaN + local_array[i + (j + 1) * count] = sin(local_array[i + j * count]); + else + j = chain_size; //End calculating for this chain + } + + if (rank != 0 && size > 0) + { + MPI_Send(local_array, count * chain_size, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD); + } + + if (rank == 0 && size > 0) + { + double tmp; + ++count; + for(int i = 0; i < mod; ++i) + { + MPI_Recv(send_array, count * chain_size, MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + for(int k = 0; k < count; ++k) + for(int j = 0; j < chain_size; ++j) + { + if((tmp = send_array[j * count + k]) == (send_array[j * count + k])) //Check if not NaN + arr[(root_x[(count - 1) + i * count + k] - j * 1) + + (root_y[(count - 1) + i * count + k] - j * 1) * xSize + + (root_z[(count - 1) + i * count + k] + j * 1) * xSize * ySize] = tmp; + else + j = chain_size; + } } + --count; + for(int i = mod; i < size - 1; ++i) + { + MPI_Recv(send_array, count * chain_size, MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + for(int k = 0; k < count; ++k) + for(int j = 0; j < chain_size; ++j) + { + if((tmp = send_array[j * count + k]) == (send_array[j * count + k])) //Check if not NaN + arr[(root_x[count + mod + i * count + k] - j * 1) + + (root_y[count + mod + i * count + k] - j * 1) * xSize + + (root_z[count + mod + i * count + k] + j * 1) * xSize * ySize] = tmp; + else + j = chain_size; + } + + } + for(int k = 0; k < count; ++k) + for(int j = 0; j < chain_size; ++j) + { + if((tmp = local_array[j * count + k]) == (local_array[j * count + k])) //Check if not NaN + arr[(root_x[k] - j * 1) + + (root_y[k] - j * 1) * xSize + + (root_z[k] + j * 1) * xSize * ySize] = tmp; + else + j = chain_size; + } + + delete send_array; + delete root_x; + delete root_y; + delete root_z; } + + delete local_array; } int main(int argc, char** argv) From 59a5009d55e4e6658a1189cc19751b1d2f5c02a4 Mon Sep 17 00:00:00 2001 From: EgoRga717 Date: Thu, 19 Nov 2020 19:08:34 +0300 Subject: [PATCH 10/13] Edited OpenMP2 --- OpenMP2/src/main.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/OpenMP2/src/main.cpp b/OpenMP2/src/main.cpp index 9779957..ebe18f7 100644 --- a/OpenMP2/src/main.cpp +++ b/OpenMP2/src/main.cpp @@ -6,11 +6,12 @@ double calc(uint32_t x_last, uint32_t num_threads) //Fails at TEST04 in last number(sometimes pre-last number). { //The strange thing is that my sum little bigger than test sum. double sum = 0.0; //May be parallel calc is more accuracy? - #pragma omp parallel for num_threads(num_threads) reduction(+:sum) + double* arr_sum = new double[x_last]; + #pragma omp parallel for num_threads(num_threads) //reduction(+:sum) for(uint32_t i = x_last; i > 0; i--) - { - sum += 1.0 / i; - } + arr_sum[i - 1] += 1.0 / i; + for(uint32_t i = x_last; i > 0; --i) + sum += arr_sum[i - 1]; return sum; } From cc82163c83cad995494675e4c0a100914146355c Mon Sep 17 00:00:00 2001 From: EgoRga717 Date: Fri, 20 Nov 2020 03:07:30 +0300 Subject: [PATCH 11/13] Added heat map --- HeatMap/HeatMap.png | Bin 0 -> 13232 bytes HeatMap/heatTable.txt | 21 ++++++++++++++++++++ HeatMap/main.cpp | 44 +++++++++++++++++++++++++++++++++++++++++ HeatMap/map_builder.py | 8 ++++++++ 4 files changed, 73 insertions(+) create mode 100644 HeatMap/HeatMap.png create mode 100644 HeatMap/heatTable.txt create mode 100644 HeatMap/main.cpp create mode 100644 HeatMap/map_builder.py diff --git a/HeatMap/HeatMap.png b/HeatMap/HeatMap.png new file mode 100644 index 0000000000000000000000000000000000000000..bcbb2b00871a8b5b064c3340a693f72725e01009 GIT binary patch literal 13232 zcmeHtc~p~Gwr`MfC_%Zl10o7-gMiAQpip5{DWxJZ2qGeriV#sC8U$nvmIaojf(8T` zBc+JU3J5ZUKsf*b5|Md~$_#-BA%qYT?mkrY>$>Z8-}~Nr-PQj**K&n~kbLKSXaDwZ z*te`L&DN~mvKoWItigTvtqlevS%tw!d|b5xUb#5<)erE1y?Xf6Ra=6`)qt}^cZ|i^ ztCzh9SG_#X{n_80=<7-F(b3e=+^hcQ3s{D$!c!VGE?e! zv-yVEUrr=kn`x1ItN$p6rPzc!;;~{C>+C&VkP$OXf!}wDD#+QNbf52XDZkg+&Tb=I zUPs*;Ikp7+a3VxeYs9A@F;cEdRkFQNop{*i-jwl z*e&4u#2><@Tb-QUyXnj$!M){h&|W^mNWKl_Cb1fx;9|6i1GUGJbTtB4p#ijdE$6a* z%}*`rYws%+GL3};^xgSAmNCmP)~c)bShw8O>wDQP2gN(&f z(ZlO0bKmw&MFk9eFxP6Fo9gH05<;`v2lFeY>Bk=K*&go_+uA(vj z)UBBCl$2%C8w$fj^9v(pCiI2`onS*_-=T({U;?N8AeB=o)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t start_time, end_time; + clock_t arr_time[TABLE_SIZE][TABLE_SIZE]; + + std::ofstream fout("heatTable.txt"); + fout << TABLE_SIZE << '\n'; + + for(int i = 1; i <= TABLE_SIZE; ++i) + for(int j = 1; j <= TABLE_SIZE; ++j) + { + start_time = clock(); + #pragma omp parallel num_threads(i) + { + #pragma omp parallel num_threads(j) + { + usleep(10); + } + } + end_time = clock(); + arr_time[i - 1][j - 1] = end_time - start_time; + } + + for(int i = 0; i < TABLE_SIZE; ++i) + { + for(int j = 0; j < TABLE_SIZE; ++j) + fout << arr_time[i][j] << ' '; + fout << '\n'; + } + fout.close(); + return 0; +} diff --git a/HeatMap/map_builder.py b/HeatMap/map_builder.py new file mode 100644 index 0000000..62943bb --- /dev/null +++ b/HeatMap/map_builder.py @@ -0,0 +1,8 @@ +import matplotlib.pyplot as plt +import numpy as np + +with open('heatTable.txt', 'r') as f: + table_size = f.readline() + data = np.loadtxt('heatTable.txt', skiprows=1) + plt.imshow(data, cmap='hot', interpolation='nearest') + plt.show() From 8e050f14694a61d1b34444e1124c49bb706ac52b Mon Sep 17 00:00:00 2001 From: EgoRga717 Date: Mon, 23 Nov 2020 18:12:28 +0300 Subject: [PATCH 12/13] Added CM1. Have small inaccuracy in multithreading tests --- CompMath1/src/main.cpp | 119 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 108 insertions(+), 11 deletions(-) diff --git a/CompMath1/src/main.cpp b/CompMath1/src/main.cpp index 7e114bd..78ba2f6 100644 --- a/CompMath1/src/main.cpp +++ b/CompMath1/src/main.cpp @@ -12,30 +12,127 @@ double acceleration(double t) void calc(double* trace, uint32_t traceSize, double t0, double dt, double y0, double y1, int rank, int size) { + int count, pre_count; + int mod; + double v, u; + double start_t; + // Sighting shot - double v0 = 0; + MPI_Bcast(&dt, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); if (rank == 0 && size > 0) { - trace[0] = y0; - trace[1] = y0 + dt*v0; - for (uint32_t i = 2; i < traceSize; i++) + count = traceSize / size; + mod = traceSize % size; + + ++count; + for(int i = 0; i < mod; ++i) + { + start_t = t0 + dt * ((count - 1) + i * count); + MPI_Send(&count, 1 , MPI_INT, i + 1, 0, MPI_COMM_WORLD); + MPI_Send(&start_t, 1 , MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD); + } + + --count; + for(int i = mod; i < size - 1; ++i) { - trace[i] = dt*dt*acceleration(t0 + (i - 1)*dt) + 2*trace[i - 1] - trace[i - 2]; + start_t = t0 + dt * (count + mod + i * count); + MPI_Send(&count, 1 , MPI_INT, i + 1, 0, MPI_COMM_WORLD); + MPI_Send(&start_t, 1 , MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD); } + start_t = t0; + + } + if (rank != 0 && size > 0) + { + MPI_Recv(&count, 1 , MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + MPI_Recv(&start_t, 1 , MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); } - // The final shot + v = 0; + double* local_array = new double[count + 1]; + if (rank != 0 && size > 0) + local_array[0] = 0; if (rank == 0 && size > 0) + local_array[0] = y0; + local_array[1] = local_array[0] + dt * v; + for (int i = 2; i < (count + 1); i++) + local_array[i] = dt*dt*acceleration(start_t + (i - 1)*dt) + 2*local_array[i - 1] - local_array[i - 2]; + u = (local_array[count] - local_array[count - 1]) / dt; // b = local_array[count] + + double tmp_a, tmp_b, tmp_u, tmp_v; + + if (rank != 0 && size > 0) + { + MPI_Recv(&pre_count, 1 , MPI_INT, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + MPI_Recv(&tmp_a, 1 , MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + MPI_Recv(&tmp_b, 1 , MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + MPI_Recv(&tmp_u, 1 , MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + MPI_Recv(&tmp_v, 1 , MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + local_array[0] = tmp_a + tmp_b + dt * pre_count * tmp_v; + v = tmp_u + tmp_v; + } + if ((rank != size - 1) && size > 1) + { + MPI_Send(&count, 1 , MPI_INT, rank + 1, 0, MPI_COMM_WORLD); + MPI_Send(&local_array[0], 1 , MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD); + MPI_Send(&local_array[count], 1 , MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD); + MPI_Send(&u, 1 , MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD); + MPI_Send(&v, 1 , MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD); + } + if (rank == size - 1) { - v0 = (y1 - trace[traceSize - 1])/(dt*traceSize); - trace[0] = y0; - trace[1] = y0 + dt*v0; - for (uint32_t i = 2; i < traceSize; i++) + if (size > 1) { - trace[i] = dt*dt*acceleration(t0 + (i - 1)*dt) + 2*trace[i - 1] - trace[i - 2]; + double tmp_y1_miss = local_array[0] + local_array[count - 1] + dt * (count - 1) * v; //because we dont need local_array[count] + MPI_Send(&tmp_y1_miss, 1 , MPI_DOUBLE, 0, 0, MPI_COMM_WORLD); } } + + // The final shot + if (rank == 0 && size > 0) + { + double y1_miss; + if (size > 1) + MPI_Recv(&y1_miss, 1 , MPI_DOUBLE, size - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + else + y1_miss = local_array[0] + local_array[count - 1] + dt * (count - 1) * v; + printf("y1_miss: %f\n", y1_miss); + v = (y1 - y1_miss) / (dt * traceSize); + local_array[0] = y0; + } + if (rank != 0 && size > 0) + { + double tmp_a_2, tmp_v_2; + MPI_Recv(&tmp_a_2, 1 , MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + MPI_Recv(&tmp_v_2, 1 , MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + local_array[0] = tmp_b + tmp_a_2 + dt * pre_count * tmp_v_2; + v = tmp_u + tmp_v_2; + } + if ((rank != size - 1) && size > 0) + { + MPI_Send(&local_array[0], 1 , MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD); + MPI_Send(&v, 1 , MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD); + } + local_array[1] = local_array[0] + dt * v; + for(int i = 2; i < count; ++i) + local_array[i] = dt*dt*acceleration(start_t + (i - 1)*dt) + 2*local_array[i - 1] - local_array[i - 2]; + + if (rank != 0 && size > 0) + MPI_Send(local_array, count , MPI_DOUBLE, 0, 0, MPI_COMM_WORLD); + if (rank == 0 && size > 0) + { + for(int i = 0; i < count; ++i) + trace[i] = local_array[i]; + ++count; + for(int i = 0; i < mod; ++i) + MPI_Recv(&trace[(count - 1) + i * count], count , MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + --count; + for(int i = mod; i < size - 1; ++i) + MPI_Recv(&trace[count + mod + i * count], count , MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + + } + delete local_array; } int main(int argc, char** argv) From dd5a0e4651a99a9a414719281d7b735cf3d548dd Mon Sep 17 00:00:00 2001 From: EgoRga717 Date: Tue, 24 Nov 2020 17:04:42 +0300 Subject: [PATCH 13/13] Added CompMath2 --- CompMath2/src/main.cpp | 128 ++++++++++++++++++++++++++++------------- 1 file changed, 87 insertions(+), 41 deletions(-) diff --git a/CompMath2/src/main.cpp b/CompMath2/src/main.cpp index 5c4527a..227549a 100644 --- a/CompMath2/src/main.cpp +++ b/CompMath2/src/main.cpp @@ -7,62 +7,108 @@ void calc(double* frame, uint32_t ySize, uint32_t xSize, double delta, int rank, int size) { + int count, mod; + double* local_array; + MPI_Bcast(&xSize, 1, MPI_INT, 0, MPI_COMM_WORLD); + if (rank == 0 && size > 0) { - double diff = 0; - double* tmpFrame = new double[ySize * xSize]; - // Prepare tmpFrame - for (uint32_t y = 0; y < ySize; y++) + count = (ySize - 2) / size; + mod = (ySize - 2) % size; + + ++count; + for(int i = 0; i < mod; ++i) { - tmpFrame[y*xSize] = frame[y*xSize]; - tmpFrame[y*xSize + xSize - 1] = frame[y*xSize + xSize - 1]; + MPI_Send(&count, 1 , MPI_INT, i + 1, 0, MPI_COMM_WORLD); + MPI_Send(&frame[(1 + (count - 1) + i * count - 1) * xSize], (count + 2) * xSize , MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD); } - for (uint32_t x = 1; x < xSize - 1; x++) + + --count; + for(int i = mod; i < size - 1; ++i) { - tmpFrame[x] = frame[x]; - tmpFrame[(ySize - 1)*xSize + x] = frame[(ySize - 1)*xSize + x]; - } - // Calculate first iteration - for (uint32_t y = 1; y < ySize - 1; y++) - { - for (uint32_t x = 1; x < xSize - 1; x++) - { - tmpFrame[y*xSize + x] = (frame[(y + 1)*xSize + x] + frame[(y - 1)*xSize + x] +\ - frame[y*xSize + x + 1] + frame[y*xSize + x - 1])/4.0; - diff += std::abs(tmpFrame[y*xSize + x] - frame[y*xSize + x]); - } + MPI_Send(&count, 1 , MPI_INT, i + 1, 0, MPI_COMM_WORLD); + MPI_Send(&frame[(1 + count + mod + i * count - 1) * xSize], (count + 2) * xSize , MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD); } - double* currFrame = tmpFrame; - double* nextFrame = frame; - uint32_t iteration = 1; - // Calculate frames - while (diff > delta) + local_array = frame; + } + + if (rank != 0 && size > 0) + { + MPI_Recv(&count, 1 , MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + local_array = new double[(count + 2) * xSize]; + MPI_Recv(local_array, (count + 2) * xSize , MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + } + + double* up_string = new double[xSize - 2]; //up_string is what was upper + double* tmp_string = new double[xSize - 1]; //tmp_string[0] is what was lefter + int is_calculate = 1; + double diff; + double diff_sum; + double* diff_recv; + if (rank == 0 && size > 0) + diff_recv = new double[size]; + while(is_calculate) + { + diff = 0; + for(uint32_t x = 1; x < xSize - 1; ++x) + up_string[x - 1] = local_array[x]; + for(int y = 1; y < count + 1; ++y) { - diff = 0; - for (uint32_t y = 1; y < ySize - 1; y++) + tmp_string[0] = local_array[y * xSize]; + for(uint32_t x = 1; x < xSize - 1; ++x) { - for (uint32_t x = 1; x < xSize - 1; x++) - { - nextFrame[y*xSize + x] = (currFrame[(y + 1)*xSize + x] + currFrame[(y - 1)*xSize + x] +\ - currFrame[y*xSize + x + 1] + currFrame[y*xSize + x - 1])/4.0; - diff += std::abs(nextFrame[y*xSize + x] - currFrame[y*xSize + x]); - } + tmp_string[x] = local_array[y*xSize + x]; + local_array[y*xSize + x] = (up_string[x - 1] + local_array[(y + 1)*xSize + x] +\ + local_array[y*xSize + x + 1] + tmp_string[x - 1])/4.0; + up_string[x - 1] = tmp_string[x]; + diff += std::abs(local_array[y*xSize + x] - tmp_string[x]); } - std::swap(currFrame, nextFrame); - iteration++; } - // Copy result from tmp - if (iteration % 2 == 1) + //0--->ySize + if (rank != 0 && size > 0) + MPI_Recv(&local_array[1], xSize - 2 , MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + if ((rank != size - 1) && size > 0) + MPI_Send(&local_array[count * xSize + 1], xSize - 2 , MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD); + //0<---ySize + if ((rank != size - 1) && size > 0) + MPI_Recv(&local_array[(count + 1) * xSize + 1], xSize - 2 , MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + if (rank != 0 && size > 0) + MPI_Send(&local_array[1 * xSize + 1], xSize - 2 , MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD); + + MPI_Gather(&diff, 1, MPI_DOUBLE, diff_recv, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); + if (rank == 0 && size > 0) { - for (uint32_t i = 0; i < xSize*ySize; i++) - { - frame[i] = tmpFrame[i]; - } + diff_sum = 0; + for(int i = 0; i < size; ++i) + diff_sum += diff_recv[i]; + if (diff_sum <= delta) + is_calculate = 0; } - delete tmpFrame; + MPI_Bcast(&is_calculate, 1, MPI_INT, 0, MPI_COMM_WORLD); + + } + if (rank != 0 && size > 0) + { + MPI_Send(&local_array[1 * xSize], xSize * count , MPI_DOUBLE, 0, 0, MPI_COMM_WORLD); } + if (rank == 0 && size > 0) + { + ++count; + for(int i = 0; i < mod; ++i) + MPI_Recv(&frame[(1 + (count - 1) + i * count) * xSize], count * xSize , MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + --count; + for(int i = mod; i < size - 1; ++i) + MPI_Recv(&frame[(1 + count + mod + i * count) * xSize], count * xSize, MPI_DOUBLE, i + 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); + } + + if (rank == 0 && size > 0) + delete diff_recv; + if (rank != 0 && size > 0) + delete local_array; + delete up_string; + delete tmp_string; } int main(int argc, char** argv)