From 7889c931c2be9131e3854f2f038275bc7312ead5 Mon Sep 17 00:00:00 2001 From: Extra5enS Date: Sat, 3 Oct 2020 12:05:44 +0300 Subject: [PATCH 01/16] Add impl with all done tests --- OpenMP2/src/main.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/OpenMP2/src/main.cpp b/OpenMP2/src/main.cpp index 8dab215..4a69b5b 100644 --- a/OpenMP2/src/main.cpp +++ b/OpenMP2/src/main.cpp @@ -3,9 +3,21 @@ #include #include -double calc(uint32_t x_last, uint32_t num_threads) -{ - return 0; +double calc(uint32_t x_last, uint32_t num_threads) { + double res = 0; + double* res_buf = (double*)calloc(x_last, sizeof(double)); + #pragma omp parallel num_threads(num_threads) + { + #pragma omp for + for(int i = x_last; i > 0; --i) { + double buf = 1.0 / i; + res_buf[i - 1] = buf; + } + } + for(int i = x_last; i > 0; --i) { + res += res_buf[i - 1]; + } + return res; } int main(int argc, char** argv) From 805bc45db6f19b7f92febe2ce67f2412cdf0c627 Mon Sep 17 00:00:00 2001 From: Extra5enS Date: Wed, 7 Oct 2020 12:45:30 +0300 Subject: [PATCH 02/16] for rebase --- OpenMP2/src/main.cpp | 5 ++--- OpenMP3/src/main.cpp | 20 +++++++++++++++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/OpenMP2/src/main.cpp b/OpenMP2/src/main.cpp index 4a69b5b..cfe430c 100644 --- a/OpenMP2/src/main.cpp +++ b/OpenMP2/src/main.cpp @@ -4,14 +4,13 @@ #include double calc(uint32_t x_last, uint32_t num_threads) { - double res = 0; + double res = 0; double* res_buf = (double*)calloc(x_last, sizeof(double)); #pragma omp parallel num_threads(num_threads) { #pragma omp for for(int i = x_last; i > 0; --i) { - double buf = 1.0 / i; - res_buf[i - 1] = buf; + res_buf[i - 1] = 1.0 / i; } } for(int i = x_last; i > 0; --i) { diff --git a/OpenMP3/src/main.cpp b/OpenMP3/src/main.cpp index aa92f69..bb6f657 100644 --- a/OpenMP3/src/main.cpp +++ b/OpenMP3/src/main.cpp @@ -3,9 +3,23 @@ #include #include -double calc() -{ - return 0; +double calc() { + size_t n_thread = 0; + double x1, x2, dx, res = 0.0; + printf("Enter first and last x, and dx\n"); + scanf("%lf %lf %lf", &x1, &x2, &dx); + printf("Enter num of thread\n"); + scanf("%ld", &n_thread); + int n = ceil((x2 - x1) / dx); // #pragma omp for don't work with double type + + #pragma omp parallel num_threads(n_thread) + { + #pragma omp for reduction(+:res) + for(int _x = 0; _x < n; ++_x) { + res += (f(x1 + _x * dx) + f(x1 + (_x + 1)*dx)) / 2 * dx; + } + } + return res; } int main(int argc, char** argv) From 60e614990061529057cd011ed12c6d965c0d0fcf Mon Sep 17 00:00:00 2001 From: Extra5enS Date: Fri, 9 Oct 2020 01:24:03 +0300 Subject: [PATCH 03/16] OpenMP3 --- OpenMP3/src/main.cpp | 52 ++++++++++++++++++++++++++----------- OpenMP3/tests/00/input.txt | 1 + OpenMP3/tests/00/output.txt | 1 + OpenMP3/tests/01/input.txt | 1 + OpenMP3/tests/01/output.txt | 1 + OpenMP3/tests/02/input.txt | 1 + OpenMP3/tests/02/output.txt | 1 + OpenMP3/tests/03/input.txt | 1 + OpenMP3/tests/03/output.txt | 1 + OpenMP3/tests/04/input.txt | 1 + OpenMP3/tests/04/output.txt | 1 + OpenMP3/tests/05/input.txt | 1 + OpenMP3/tests/05/output.txt | 1 + OpenMP3/tests/06/input.txt | 1 + OpenMP3/tests/06/output.txt | 1 + OpenMP3/tests/07/input.txt | 1 + OpenMP3/tests/07/output.txt | 1 + 17 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 OpenMP3/tests/00/input.txt create mode 100644 OpenMP3/tests/00/output.txt create mode 100644 OpenMP3/tests/01/input.txt create mode 100644 OpenMP3/tests/01/output.txt create mode 100644 OpenMP3/tests/02/input.txt create mode 100644 OpenMP3/tests/02/output.txt create mode 100644 OpenMP3/tests/03/input.txt create mode 100644 OpenMP3/tests/03/output.txt create mode 100644 OpenMP3/tests/04/input.txt create mode 100644 OpenMP3/tests/04/output.txt create mode 100644 OpenMP3/tests/05/input.txt create mode 100644 OpenMP3/tests/05/output.txt create mode 100644 OpenMP3/tests/06/input.txt create mode 100644 OpenMP3/tests/06/output.txt create mode 100644 OpenMP3/tests/07/input.txt create mode 100644 OpenMP3/tests/07/output.txt diff --git a/OpenMP3/src/main.cpp b/OpenMP3/src/main.cpp index bb6f657..e17b342 100644 --- a/OpenMP3/src/main.cpp +++ b/OpenMP3/src/main.cpp @@ -2,23 +2,42 @@ #include #include #include +#include -double calc() { - size_t n_thread = 0; - double x1, x2, dx, res = 0.0; - printf("Enter first and last x, and dx\n"); - scanf("%lf %lf %lf", &x1, &x2, &dx); - printf("Enter num of thread\n"); - scanf("%ld", &n_thread); - int n = ceil((x2 - x1) / dx); // #pragma omp for don't work with double type - - #pragma omp parallel num_threads(n_thread) +#define PI (2 * asin(1)) +#define DELTA(dx) (PI / dx) + +double (*f)(double) = sin; + +double calc(double x0, double x1, double dx, uint32_t num_threads) +{ + if(x1 - x0 == 0) { + return 0.0; + } + double res = 0.0; + size_t n = size_t((x1 - x0) / dx) + 1; // #pragma omp for don't work with double type + double* resbuf = (double*)calloc(n, sizeof(double)); + #pragma omp parallel num_threads(num_threads) { - #pragma omp for reduction(+:res) - for(int _x = 0; _x < n; ++_x) { - res += (f(x1 + _x * dx) + f(x1 + (_x + 1)*dx)) / 2 * dx; + #pragma omp for + for(size_t _x = 0; _x < n; ++_x) { + if(_x == 0 || _x == n - 1) { + resbuf[_x] = f(x1 + _x * dx) / 2 * dx; + } else { + resbuf[_x ]= f(x1 + _x * dx) * dx; + } } } + size_t j = 0; + size_t s = 0; + for(size_t i = 0; i < n; ++i) { + res += resbuf[j]; + j += DELTA(dx); + if(j * dx > x1 - x0) { + s++; + j = s; + } + } return res; } @@ -49,12 +68,15 @@ int main(int argc, char** argv) } // Read arguments from input + double x0 = 0.0, x1 =0.0, dx = 0.0; + uint32_t num_threads = 0; + input >> x0 >> x1 >> dx >> num_threads; // Calculation - double res = calc(); + double res = calc(x0, x1, dx, num_threads); // Write result - output << std::setprecision(15) << res; + output << std::setprecision(15) << res << std::endl; // Prepare to exit output.close(); input.close(); diff --git a/OpenMP3/tests/00/input.txt b/OpenMP3/tests/00/input.txt new file mode 100644 index 0000000..3ebc099 --- /dev/null +++ b/OpenMP3/tests/00/input.txt @@ -0,0 +1 @@ +0 0 0.00001 1 diff --git a/OpenMP3/tests/00/output.txt b/OpenMP3/tests/00/output.txt new file mode 100644 index 0000000..573541a --- /dev/null +++ b/OpenMP3/tests/00/output.txt @@ -0,0 +1 @@ +0 diff --git a/OpenMP3/tests/01/input.txt b/OpenMP3/tests/01/input.txt new file mode 100644 index 0000000..365fe5f --- /dev/null +++ b/OpenMP3/tests/01/input.txt @@ -0,0 +1 @@ +0 0 0.00001 8 diff --git a/OpenMP3/tests/01/output.txt b/OpenMP3/tests/01/output.txt new file mode 100644 index 0000000..573541a --- /dev/null +++ b/OpenMP3/tests/01/output.txt @@ -0,0 +1 @@ +0 diff --git a/OpenMP3/tests/02/input.txt b/OpenMP3/tests/02/input.txt new file mode 100644 index 0000000..6a7872d --- /dev/null +++ b/OpenMP3/tests/02/input.txt @@ -0,0 +1 @@ +0 3.14159265358979324 0.0000001 1 diff --git a/OpenMP3/tests/02/output.txt b/OpenMP3/tests/02/output.txt new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/OpenMP3/tests/02/output.txt @@ -0,0 +1 @@ +2 diff --git a/OpenMP3/tests/03/input.txt b/OpenMP3/tests/03/input.txt new file mode 100644 index 0000000..b38c3e6 --- /dev/null +++ b/OpenMP3/tests/03/input.txt @@ -0,0 +1 @@ +0 3.14159265358979324 0.0000001 8 diff --git a/OpenMP3/tests/03/output.txt b/OpenMP3/tests/03/output.txt new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/OpenMP3/tests/03/output.txt @@ -0,0 +1 @@ +2 diff --git a/OpenMP3/tests/04/input.txt b/OpenMP3/tests/04/input.txt new file mode 100644 index 0000000..b22e601 --- /dev/null +++ b/OpenMP3/tests/04/input.txt @@ -0,0 +1 @@ +0 34.5575191894877 0.000001 1 diff --git a/OpenMP3/tests/04/output.txt b/OpenMP3/tests/04/output.txt new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/OpenMP3/tests/04/output.txt @@ -0,0 +1 @@ +2 diff --git a/OpenMP3/tests/05/input.txt b/OpenMP3/tests/05/input.txt new file mode 100644 index 0000000..ed6b489 --- /dev/null +++ b/OpenMP3/tests/05/input.txt @@ -0,0 +1 @@ +0 34.5575191894877 0.000001 8 diff --git a/OpenMP3/tests/05/output.txt b/OpenMP3/tests/05/output.txt new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/OpenMP3/tests/05/output.txt @@ -0,0 +1 @@ +2 diff --git a/OpenMP3/tests/06/input.txt b/OpenMP3/tests/06/input.txt new file mode 100644 index 0000000..f05342e --- /dev/null +++ b/OpenMP3/tests/06/input.txt @@ -0,0 +1 @@ +0 317.300858012569117 0.000001 1 diff --git a/OpenMP3/tests/06/output.txt b/OpenMP3/tests/06/output.txt new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/OpenMP3/tests/06/output.txt @@ -0,0 +1 @@ +2 diff --git a/OpenMP3/tests/07/input.txt b/OpenMP3/tests/07/input.txt new file mode 100644 index 0000000..adb550c --- /dev/null +++ b/OpenMP3/tests/07/input.txt @@ -0,0 +1 @@ +0 317.300858012569117 0.000001 8 diff --git a/OpenMP3/tests/07/output.txt b/OpenMP3/tests/07/output.txt new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/OpenMP3/tests/07/output.txt @@ -0,0 +1 @@ +2 From 8ac5f256597edd2f59bd65ec80323943ff2e40ef Mon Sep 17 00:00:00 2001 From: Extra5enS Date: Fri, 9 Oct 2020 01:33:09 +0300 Subject: [PATCH 04/16] I don't know --- OpenMP3/src/main.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/OpenMP3/src/main.cpp b/OpenMP3/src/main.cpp index d554444..2cea3f1 100644 --- a/OpenMP3/src/main.cpp +++ b/OpenMP3/src/main.cpp @@ -76,11 +76,7 @@ int main(int argc, char** argv) double res = calc(x0, x1, dx, num_threads); // Write result -<<<<<<< HEAD - output << std::setprecision(15) << res << std::endl; -======= output << std::setprecision(13) << res << std::endl; ->>>>>>> 0f2d8930dcb55854e681a8927a619bc0a698c550 // Prepare to exit output.close(); input.close(); From 4f4e9e710a544d8bfdd798f63cc7385b6e17f208 Mon Sep 17 00:00:00 2001 From: Extra5enS Date: Fri, 9 Oct 2020 01:43:40 +0300 Subject: [PATCH 05/16] OpenMP3 all test done! --- OpenMP3/src/main.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/OpenMP3/src/main.cpp b/OpenMP3/src/main.cpp index 2cea3f1..4c20380 100644 --- a/OpenMP3/src/main.cpp +++ b/OpenMP3/src/main.cpp @@ -22,18 +22,18 @@ double calc(double x0, double x1, double dx, uint32_t num_threads) #pragma omp for for(size_t _x = 0; _x < n; ++_x) { if(_x == 0 || _x == n - 1) { - resbuf[_x] = f(x1 + _x * dx) / 2 * dx; + resbuf[_x] = f(x0 + _x * dx) / 2 * dx; } else { - resbuf[_x ]= f(x1 + _x * dx) * dx; + resbuf[_x ]= f(x0 + _x * dx) * dx; } } } size_t j = 0; size_t s = 0; - for(size_t i = 0; i < n; ++i) { + for(size_t i = 0; i <= n; ++i) { res += resbuf[j]; j += DELTA(dx); - if(j * dx > x1 - x0) { + if(j >= n) { s++; j = s; } From 061f16a145116835ad1424ad4603dbc2aa5c63f8 Mon Sep 17 00:00:00 2001 From: Extra5enS Date: Wed, 14 Oct 2020 11:25:47 +0300 Subject: [PATCH 06/16] . --- OpenMP4/run.sh | 2 +- OpenMP4/src/main.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 2 deletions(-) diff --git a/OpenMP4/run.sh b/OpenMP4/run.sh index 892ddd6..d1ef341 100755 --- a/OpenMP4/run.sh +++ b/OpenMP4/run.sh @@ -1,7 +1,7 @@ #! /bin/bash # run.sh compiler="g++" -flags="-fopenmp -std=c++11" +flags="-fopenmp -lm -std=c++11" src="./src/main.cpp" build="./build" exe="$build/task" diff --git a/OpenMP4/src/main.cpp b/OpenMP4/src/main.cpp index 036d8e3..09fd262 100644 --- a/OpenMP4/src/main.cpp +++ b/OpenMP4/src/main.cpp @@ -2,10 +2,96 @@ #include #include #include +#include +/* +typedef struct { + double value; + size_t gr; +} limd_t; + +limd_t limd_add(limd_t lv, limd_t rv) { + size_t new_gr = 0; + if(lv.gr > rv.gr) { + new_gr = lv.gr = rv.gr; + lv.value /= pow(10, lv.gr - rv.gr); + } else { + new_gr = rv.gr = lv.gr; + lv.value /= pow(10, rv.gr - lv.gr); + } + limd_t res; + res.value = lv.value + rv.value; + res.gr = new_gr; + return res; +} + +limd_t limd_mul(limd_t lv, limd_t rv) { + limd_t res; + res.value = lv.value * rv.value; + res.gr = lv.gr + rv.gr; + return res; +} + +limd_t limd_sub(limd_t lv, limd_t rv) { + rv.value *= -1; + return limd_add(lv, rv); +} + +limd_t limd_dev(limd_t lv, limd_t rv) { + rv.value = 1 / rv.value; + rv.gr *= -1; + return limd_mul(lv, rv); +} + +limd_t limd_init(double value) { + if(value == 0) { + return {0, (size_t(-1))}; + } + limd_t res = {value, 0}; + while(res.value > 10 && res.value < 1) { + if(res.value > 1.0) { + res.value /= 10; + res.gr--; + } else { + res.value *= 10; + res.gr++; + } + } + return res; +}*/ double calc(uint32_t x_last, uint32_t num_threads) { - return 0; + double* buf = (double*)calloc(x_last, sizeof(double)); + int* fbuf = (int*)calloc(x_last, sizeof(int)); + buf[0] = 1; + fbuf[0] = 1; + #pragma omp parallel num_threads(num_threads) + { + #pragma omp for + for(size_t i = 0; i < x_last; ++i) { + size_t my_f = 1; + for(size_t n = i; n > 0; --n) { + if(buf[n - 1] < 0) { + fbuf[i] = -1; + buf[i] = 0; + break; + } + my_f *= n; + if(buf[n - 1] > 0) { + my_f *= fbuf[n - 1]; + fbuf[i] = my_f; + buf[i] = 1.0 / fbuf[i]; + break; + } + } + } + } + + double res = 0; + for(size_t i = x_last; i > 0; --i) { + res += buf[i - 1]; + } + return res; } int main(int argc, char** argv) From 2c233074f5117ec3fc8eb0bc4ab391b614f881a0 Mon Sep 17 00:00:00 2001 From: Extra5enS Date: Fri, 16 Oct 2020 17:07:15 +0300 Subject: [PATCH 07/16] Omp4 --- OpenMP4/src/main.cpp | 52 ++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/OpenMP4/src/main.cpp b/OpenMP4/src/main.cpp index 09fd262..bc38cd3 100644 --- a/OpenMP4/src/main.cpp +++ b/OpenMP4/src/main.cpp @@ -3,6 +3,8 @@ #include #include #include + +#include /* typedef struct { double value; @@ -61,37 +63,45 @@ limd_t limd_init(double value) { double calc(uint32_t x_last, uint32_t num_threads) { - double* buf = (double*)calloc(x_last, sizeof(double)); - int* fbuf = (int*)calloc(x_last, sizeof(int)); - buf[0] = 1; - fbuf[0] = 1; + double exp = 0.0; + std::vector fact(x_last); + for(auto i = next(fact.begin()); i != fact.end(); i = next(i)) { + *i = -1; + } + fact[0] = 1; + double c = 0.0; #pragma omp parallel num_threads(num_threads) { #pragma omp for - for(size_t i = 0; i < x_last; ++i) { - size_t my_f = 1; - for(size_t n = i; n > 0; --n) { - if(buf[n - 1] < 0) { - fbuf[i] = -1; - buf[i] = 0; + for(size_t n = 1; n <= x_last; ++n) { + double my_fact = ( 1.0 / n ); + for(size_t i = n - 1; i >= 0; --i) { + if(fact[i] == -1) { + my_fact *= ( 1.0 / i ); + } else if(fact[i] > 0) { + my_fact *= fact[i]; + fact[n] = my_fact; break; - } - my_f *= n; - if(buf[n - 1] > 0) { - my_f *= fbuf[n - 1]; - fbuf[i] = my_f; - buf[i] = 1.0 / fbuf[i]; + } else { // fact[i] < 0 break; } } + //std::cout << "I HAVE " << my_fact << std::endl; + /* + #pragma omp critical + { + double y = my_fact - c; + double t = exp + y; + c = (t - exp) - y; + exp = t; + }*/ } } - - double res = 0; - for(size_t i = x_last; i > 0; --i) { - res += buf[i - 1]; + + for(auto i = fact.rbegin(); i != fact.rend(); i = next(i)) { + exp += *i; } - return res; + return exp; } int main(int argc, char** argv) From f715c72ffae35b148746516ef1078a8a9b14376a Mon Sep 17 00:00:00 2001 From: Extra5enS Date: Fri, 30 Oct 2020 14:49:34 +0300 Subject: [PATCH 08/16] static impl --- Loop1/src/main.cpp | 150 +++++++++++++++++++-------------------------- 1 file changed, 63 insertions(+), 87 deletions(-) diff --git a/Loop1/src/main.cpp b/Loop1/src/main.cpp index 443ded1..af0498d 100644 --- a/Loop1/src/main.cpp +++ b/Loop1/src/main.cpp @@ -4,104 +4,80 @@ #include #include #include +#include -#define ROOT 0 - +#include +#include +#define ROOT 0 -#define MPI_recv_start_data(worker, have_task) { \ - MPI_Recv(worker, 1, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, NULL); \ - MPI_Recv(have_task, 1, MPI_INT, *worker, 1, MPI_COMM_WORLD, NULL); \ -} - -#define MPI_send_start_data(rank, have_task) { \ - MPI_Send(rank, 1, MPI_INT, ROOT, 1, MPI_COMM_WORLD); \ - MPI_Send(have_task, 1, MPI_INT, ROOT, 1, MPI_COMM_WORLD); \ -} +#define S_TAG 1 + +#define coord(x, y, xSize) ((y)*(xSize) + x) + +// arr[y*xSize + x] = sin(0.00001*arr[y*xSize + x]); +void calc(double* arr, uint32_t ySize, uint32_t xSize, int rank, int size) { + int32_t step = 0; + int32_t* range = NULL; + int32_t* displace = NULL; + + MPI_Bcast(&xSize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); + MPI_Bcast(&ySize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); + + if(rank == ROOT) { + uint32_t y_step = ceil(ySize / (1.0 * size)); + uint32_t prev_range = 0; + + range = (int32_t*)calloc(size, sizeof(int32_t)); + displace = (int32_t*)calloc(size, sizeof(int32_t)); + + for(int send_rank = 0; send_rank < size; ++send_rank) { + // We will send start_y, step, xSize and part of arr + uint32_t real_step = std::min(y_step, ySize - prev_range); + range[send_rank] = real_step * xSize; + displace[send_rank] = prev_range * xSize; + prev_range += y_step; + } + } + MPI_Scatter(range, 1, MPI_INT, + &step, 1, MPI_INT, + ROOT, MPI_COMM_WORLD); -#define MPI_recv_res(worker, arr, xSize) { \ - uint32_t ux, uy; \ - MPI_Recv(&ux, 1, MPI_UNSIGNED, worker, 2, MPI_COMM_WORLD, NULL); \ - MPI_Recv(&uy, 1, MPI_UNSIGNED, worker, 2, MPI_COMM_WORLD, NULL); \ - MPI_Recv(&(arr[uy*xSize + ux]), 1, MPI_DOUBLE, worker, 2, MPI_COMM_WORLD, NULL); \ -} - -#define MPI_send_res(x, y, arr) { \ - MPI_Send(x, 1, MPI_UNSIGNED, ROOT, 2, MPI_COMM_WORLD); \ - MPI_Send(y, 1, MPI_UNSIGNED, ROOT, 2, MPI_COMM_WORLD); \ - MPI_Send(arr, 1, MPI_DOUBLE, ROOT, 2, MPI_COMM_WORLD); \ -} -#define MPI_recv_data(x, y, arr) { \ - MPI_Recv(x, 1, MPI_UNSIGNED, ROOT, 1, MPI_COMM_WORLD, NULL); \ - MPI_Recv(y, 1, MPI_UNSIGNED, ROOT, 1, MPI_COMM_WORLD, NULL); \ - if(*x == uint32_t(-1) && *y == uint32_t(-1)) { \ - break; \ - } \ - MPI_Recv(arr, 1, MPI_DOUBLE, ROOT, 1, MPI_COMM_WORLD, NULL); \ -} + double* my_copy = (double*) calloc(step, sizeof(double)); + MPI_Scatterv(arr, range, displace, MPI_DOUBLE, + my_copy, step, MPI_DOUBLE, + ROOT, MPI_COMM_WORLD); -#define MPI_send_data(worker, x, y, arr, xSize, need_to_send_arr) { \ - MPI_Send(x, 1, MPI_UNSIGNED, worker, 1, MPI_COMM_WORLD); \ - MPI_Send(y, 1, MPI_UNSIGNED, worker, 1, MPI_COMM_WORLD); \ - if(need_to_send_arr) { \ - MPI_Send(&arr[*y * xSize + *x], 1, MPI_DOUBLE, worker, 1, MPI_COMM_WORLD); \ - } \ -} -void calc(double* arr, uint32_t ySize, uint32_t xSize, int rank, int size) -{ - MPI_Comm_rank(MPI_COMM_WORLD, &rank); - MPI_Comm_size(MPI_COMM_WORLD, &size); - - if(size == 1) { - for(uint32_t y = 0; y < ySize; ++y) { - for(uint32_t x = 0; x < xSize; ++x) { - arr[y*xSize + x] = sin(0.00001*arr[y*xSize + x]); - } + for(int32_t x = 0; x < step; ++x) { + my_copy[x] = sin(0.00001*my_copy[x]); + } + /* + if(size > 1 && rank == 1) { + for(int i = 0; i < step; ++i) { + printf("%lf \n", my_copy[i]); } - return; + }*/ + /* + if(rank == ROOT) { + for(int i = 0; i < size; ++i) { + printf("rang[i] = %d, dit[i] = %d\n", range[i], displace[i]); } - - //we will + }*/ + + //printf("[%d] xSize = %u, step = %u, my_copy = %p\n", rank, xSize, step, my_copy); + + MPI_Gatherv(my_copy, step, MPI_DOUBLE, + arr, range, displace, MPI_DOUBLE, + ROOT, MPI_COMM_WORLD); + if(rank == ROOT) { - int worker = 0; - int have_task = 0; - for(uint32_t y = 0; y < ySize; ++y) { - for(uint32_t x = 0; x < xSize; ++x) { - MPI_recv_start_data(&worker, &have_task); - - if(have_task != 0) { // Recv task - MPI_recv_res(worker, arr, xSize); - } - - MPI_send_data(worker, &x, &y, arr, xSize, 1); - } - } - for(int i = 1; i < size; ++i) { - MPI_recv_start_data(&worker, &have_task); - - if(have_task != 0) { // Recv task - MPI_recv_res(worker, arr, xSize); - } - - uint32_t x = uint32_t(-1), y = x; - MPI_send_data(worker, &x, &y, arr, xSize, 0); - } - } else { - int have_task = 0; - while(1) { - MPI_send_start_data(&rank, &have_task); - have_task = 1; - - uint32_t x = 0, y = 0; - double arr = 0; - MPI_recv_data(&x, &y, &arr); - - arr = sin(0.00001*arr); - MPI_send_res(&x, &y, &arr); - } + + free(range); + free(displace); } + free(my_copy); } int main(int argc, char** argv) From 41e932bc51b571573afb01868869695a055d501d Mon Sep 17 00:00:00 2001 From: Extra5enS Date: Fri, 30 Oct 2020 21:00:37 +0300 Subject: [PATCH 09/16] all tests done --- Loop2/src/main.cpp | 75 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 64 insertions(+), 11 deletions(-) diff --git a/Loop2/src/main.cpp b/Loop2/src/main.cpp index 51ea220..361e996 100644 --- a/Loop2/src/main.cpp +++ b/Loop2/src/main.cpp @@ -5,18 +5,71 @@ #include #include -void calc(double* arr, uint32_t ySize, uint32_t xSize, int rank, int size) -{ - if (rank == 0 && size > 0) - { - for (uint32_t y = 0; y < ySize - 1; y++) - { - for (uint32_t x = 3; x < xSize; x++) - { - arr[y*xSize + x] = sin(0.00001*arr[(y + 1)*xSize + x - 3]); - } +#include + +#define ROOT 0 +// auntidep +// y = 1; y < ySize; +// x = 0; y < xSize - 3 +//arr[(y - 1)*xSize + (x + 3)] = sin(0.00001*arr[y*xSize + x]); + +void calc(double* arr, uint32_t ySize, uint32_t xSize, int rank, int size) { + int32_t step = 0; + int32_t* range = NULL; + int32_t* displace = NULL; + + MPI_Bcast(&xSize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); + MPI_Bcast(&ySize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); + + if(rank == ROOT) { + uint32_t y_step = ceil(ySize / (1.0 * size)) + 1; + uint32_t prev_range = 0; + + range = (int32_t*)calloc(size, sizeof(int32_t)); + displace = (int32_t*)calloc(size, sizeof(int32_t)); + // реализуем перекрытие + for(int send_rank = 0; send_rank < size; ++send_rank) { + // We will send start_y, step, xSize and part of arr + uint32_t real_step = std::min(y_step, ySize - prev_range); + range[send_rank] = real_step * xSize; + displace[send_rank] = prev_range * xSize; + prev_range += y_step - 1; + } } - } + MPI_Scatter(range, 1, MPI_INT, + &step, 1, MPI_INT, + ROOT, MPI_COMM_WORLD); + + + double* my_copy = (double*) calloc(step, sizeof(double)); + MPI_Scatterv(arr, range, displace, MPI_DOUBLE, + my_copy, step, MPI_DOUBLE, + ROOT, MPI_COMM_WORLD); + + if(rank == ROOT) { + for(int i = 0; i < size; ++i) { + range[i] -= (range[i] == 0) ? 0 : xSize; + } + } + + if(step != 0) { + for(uint32_t y = 0; y < step / xSize - 1; ++y) { + for(uint32_t x = 3; x < xSize; ++x) { + my_copy[y*xSize + x] = sin(0.00001*my_copy[(y + 1) * xSize + (x - 3)]); + } + } + } + + MPI_Gatherv(my_copy, (step == 0)?0:step - xSize, MPI_DOUBLE, + arr, range, displace, MPI_DOUBLE, + ROOT, MPI_COMM_WORLD); + + if(rank == ROOT) { + + free(range); + free(displace); + } + free(my_copy); } int main(int argc, char** argv) From b6d0d382fbbbc7675adc5308267b17e4c388dae5 Mon Sep 17 00:00:00 2001 From: Extra5enS Date: Sat, 31 Oct 2020 11:40:22 +0300 Subject: [PATCH 10/16] Loop3 with 4 proc --- Loop3/src/main.cpp | 96 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 87 insertions(+), 9 deletions(-) diff --git a/Loop3/src/main.cpp b/Loop3/src/main.cpp index 43cce1f..25b08ff 100644 --- a/Loop3/src/main.cpp +++ b/Loop3/src/main.cpp @@ -5,18 +5,96 @@ #include #include +#define ROOT 0 + +void form_arr(double* arr, double* copy_arr, uint32_t ySize, uint32_t xSize, int* ran) { + int pos = 0; + for(int i = 0; i < 4; ++i) { + for(uint32_t y = i; y < ySize; y += 4) { + memcpy(©_arr[(pos++) * xSize], &arr[y * xSize], xSize * sizeof(double)); + ran[i] += xSize; + } + } +} + +void unform_arr(double* arr, double* copy_arr, uint32_t ySize, uint32_t xSize) { + int pos = 0; + for(int i = 0; i < 4; ++i) { + for(uint32_t y = i; y < ySize; y += 4) { + memcpy(&arr[y * xSize], ©_arr[(pos++) * xSize], xSize * sizeof(double)); + } + } +} + void calc(double* arr, uint32_t ySize, uint32_t xSize, int rank, int size) { - if (rank == 0 && size > 0) - { - for (uint32_t y = 4; y < ySize; y++) - { - for (uint32_t x = 0; x < xSize; x++) - { - arr[y*xSize + x] = sin(arr[(y - 4)*xSize + x]); - } + MPI_Comm comm; + if(size < 4) { + if(rank == ROOT) { + for (uint32_t y = 4; y < ySize; y++) { + for (uint32_t x = 0; x < xSize; x++) { + arr[y*xSize + x] = sin(arr[(y - 4)*xSize + x]); + } + } + } + return; } - } + + MPI_Comm_split(MPI_COMM_WORLD, (rank < 4) ? 1 : MPI_UNDEFINED, rank, &comm); + if(rank >= 4) { + return; + } + + MPI_Comm_rank(comm, &rank); + MPI_Comm_size(comm, &size); + + int* ran = NULL; + int* dis = NULL; + double* new_array = NULL; + int step = 0; + + MPI_Bcast(&xSize, 1, MPI_UNSIGNED, ROOT, comm); + MPI_Bcast(&ySize, 1, MPI_UNSIGNED, ROOT, comm); + /* + if(rank == ROOT) { + printf("%d\n", size); + }*/ + + if(rank == ROOT) { + ran = (int*)calloc(4, sizeof(int)); + dis = (int*)calloc(4, sizeof(int)); + new_array = (double*)calloc(xSize*ySize, sizeof(double)); + form_arr(arr, new_array, ySize, xSize, ran); + dis[1] = ran[0]; + dis[2] = ran[1] + dis[1]; + dis[3] = ran[2] + dis[2]; + } + + MPI_Scatter(ran, 1, MPI_INT, + &step, 1, MPI_INT, + ROOT, comm); + //printf("[%d] task = %d, step = %d\n", rank, ySize * xSize, step); + + double* my_copy = (double*)calloc(step, sizeof(double)); + MPI_Scatterv(new_array, ran, dis, MPI_DOUBLE, + my_copy, step, MPI_DOUBLE, + ROOT, comm); + + for(int x = xSize; x < step; ++x) { + my_copy[x] = sin(my_copy[x - xSize]); + } + + MPI_Gatherv(my_copy, step, MPI_DOUBLE, + new_array, ran, dis, MPI_DOUBLE, + ROOT, comm); + + if(rank == ROOT) { + unform_arr(arr, new_array, ySize, xSize); + free(new_array); + free(ran); + free(dis); + } + free(my_copy); } int main(int argc, char** argv) From 69a32e4fd24711e6d4e205913f11484a30f80d6d Mon Sep 17 00:00:00 2001 From: Extra5enS Date: Sun, 1 Nov 2020 10:09:25 +0300 Subject: [PATCH 11/16] We have some problem --- Loop4/src/main.cpp | 189 +++++++++++++++++++++++++++++---------------- 1 file changed, 121 insertions(+), 68 deletions(-) diff --git a/Loop4/src/main.cpp b/Loop4/src/main.cpp index d958d54..daf28cb 100644 --- a/Loop4/src/main.cpp +++ b/Loop4/src/main.cpp @@ -5,93 +5,146 @@ #include #include +#define ROOT 0 + +// arr[z*ySize*xSize + y*xSize + x] = sin(arr[(z - 1)*ySize*xSize + (y + 1)*xSize + x + 1]); 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]); + int32_t step = 0; + int* range = NULL; + int* displace = NULL; + int* recv_range = NULL; + + MPI_Bcast(&xSize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); + MPI_Bcast(&ySize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); + MPI_Bcast(&zSize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); + + if(rank == ROOT) { + uint32_t y_step = ceil(ySize / (1.0 * size)) + 1; + uint32_t prev_range = 0; + range = (int*)calloc(size, sizeof(int)); + displace = (int*)calloc(size, sizeof(int)); + + for(int send_rank = 0; send_rank < size; ++send_rank) { + uint32_t real_step = std::min(y_step, ySize - prev_range); + range[send_rank] = real_step * xSize; + displace[send_rank] = prev_range * xSize; + prev_range += y_step; + } + + recv_range = (int*)calloc(size, sizeof(int)); + for(int i = 0; i < size; ++i) { + recv_range[i] = range[i] - (range[i] == 0) ? 0 : xSize; } - } + + } + + MPI_Scatter(range, 1, MPI_INT, + &step, 1, MPI_INT, + ROOT, MPI_COMM_WORLD); + + double* my_copy = (double*)calloc(step, sizeof(double)); + + + for(size_t z = 1; z < zSize; ++z) { + MPI_Scatterv(arr + (z - 1)*ySize*xSize, range, displace, MPI_DOUBLE, + my_copy, step, MPI_DOUBLE, + ROOT, MPI_COMM_WORLD); + + if(step != 0) { + for(uint32_t y = 0; y < step / xSize - 1; ++y) { + for(uint32_t x = 0; x < xSize - 1; ++x) { + my_copy[y*xSize + x] = sin(my_copy[(y + 1) * xSize + (x + 1)]); + } + } + } + + MPI_Gatherv(my_copy, (step == 0) ? 0 : step - xSize, MPI_DOUBLE, + arr + z*ySize*xSize, recv_range, displace, MPI_DOUBLE, + ROOT, MPI_COMM_WORLD); + } + + if(rank == ROOT) { + free(range); + free(displace); + free(recv_range); } - } } int main(int argc, char** argv) { - int rank = 0, size = 0, buf = 0; - uint32_t zSize = 0, ySize = 0, xSize = 0; - double* arr = 0; - - MPI_Init(&argc, &argv); - MPI_Comm_rank(MPI_COMM_WORLD, &rank); - MPI_Comm_size(MPI_COMM_WORLD, &size); - - if (rank == 0) - { - // Check arguments - if (argc != 3) - { - std::cout << "[Error] Usage \n"; - buf = 1; - MPI_Bcast(&buf, 1, MPI_INT, 0, MPI_COMM_WORLD); - return 1; - } + int rank = 0, size = 0, buf = 0; + uint32_t zSize = 0, ySize = 0, xSize = 0; + double* arr = 0; - // Prepare input file - std::ifstream input(argv[1]); - if (!input.is_open()) + MPI_Init(&argc, &argv); + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + MPI_Comm_size(MPI_COMM_WORLD, &size); + + if (rank == 0) { - std::cout << "[Error] Can't open " << argv[1] << " for write\n"; - buf = 1; - MPI_Bcast(&buf, 1, MPI_INT, 0, MPI_COMM_WORLD); - return 1; - } + // Check arguments + if (argc != 3) + { + std::cout << "[Error] Usage \n"; + buf = 1; + MPI_Bcast(&buf, 1, MPI_INT, 0, MPI_COMM_WORLD); + return 1; + } + + // Prepare input file + std::ifstream input(argv[1]); + if (!input.is_open()) + { + std::cout << "[Error] Can't open " << argv[1] << " for write\n"; + buf = 1; + MPI_Bcast(&buf, 1, MPI_INT, 0, MPI_COMM_WORLD); + return 1; + } - // Read arguments from input - input >> zSize >> ySize >> xSize; - MPI_Bcast(&buf, 1, MPI_INT, 0, MPI_COMM_WORLD); + // Read arguments from input + input >> zSize >> ySize >> xSize; + MPI_Bcast(&buf, 1, MPI_INT, 0, MPI_COMM_WORLD); - arr = new double[zSize * ySize * xSize]; - for (uint32_t z = 0; z < zSize; z++) { - for (uint32_t y = 0; y < ySize; y++) { - for (uint32_t x = 0; x < xSize; x++) { - input >> arr[z*ySize*xSize + y*xSize + x]; + arr = new double[zSize * ySize * xSize]; + for (uint32_t z = 0; z < zSize; z++) { + for (uint32_t y = 0; y < ySize; y++) { + for (uint32_t x = 0; x < xSize; x++) { + input >> arr[z*ySize*xSize + y*xSize + x]; + } + } + } + input.close(); + } else { + MPI_Bcast(&buf, 1, MPI_INT, 0, MPI_COMM_WORLD); + if (buf != 0) + { + return 1; } - } - } - input.close(); - } else { - MPI_Bcast(&buf, 1, MPI_INT, 0, MPI_COMM_WORLD); - if (buf != 0) - { - return 1; } - } - calc(arr, zSize, ySize, xSize, rank, size); + calc(arr, zSize, ySize, xSize, rank, size); - if (rank == 0) - { - // Prepare output file - std::ofstream output(argv[2]); - if (!output.is_open()) + if (rank == 0) { - std::cout << "[Error] Can't open " << argv[2] << " for read\n"; - delete arr; - return 1; - } + // Prepare output file + std::ofstream output(argv[2]); + if (!output.is_open()) + { + std::cout << "[Error] Can't open " << argv[2] << " for read\n"; + delete arr; + return 1; + } - for (uint32_t z = 0; z < zSize; z++) { - for (uint32_t y = 0; y < ySize; y++) { - for (uint32_t x = 0; x < xSize; x++) { - output << " " << arr[z*ySize*xSize + y*xSize + x]; + for (uint32_t z = 0; z < zSize; z++) { + for (uint32_t y = 0; y < ySize; y++) { + for (uint32_t x = 0; x < xSize; x++) { + output << " " << arr[z*ySize*xSize + y*xSize + x]; + } + output << std::endl; + } + output << std::endl; } - output << std::endl; - } - output << std::endl; - } output.close(); delete arr; } From b5af3da0fb96c59a2901d1fc92946cb7ca189954 Mon Sep 17 00:00:00 2001 From: Extra5enS Date: Thu, 12 Nov 2020 22:57:54 +0300 Subject: [PATCH 12/16] a --- Loop4/src/main.cpp | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/Loop4/src/main.cpp b/Loop4/src/main.cpp index daf28cb..dc3fd36 100644 --- a/Loop4/src/main.cpp +++ b/Loop4/src/main.cpp @@ -10,16 +10,30 @@ // arr[z*ySize*xSize + y*xSize + x] = sin(arr[(z - 1)*ySize*xSize + (y + 1)*xSize + x + 1]); void calc(double* arr, uint32_t zSize, uint32_t ySize, uint32_t xSize, int rank, int size) { + if(size == 1) { + for(int64_t z = 1; z < zSize; ++z) { + for(int64_t y = 0; y < ySize - 1; ++y) { + for(int64_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]); + } + } + } + return; + } + int32_t step = 0; int* range = NULL; int* displace = NULL; int* recv_range = NULL; - + + double* buf = NULL; + MPI_Bcast(&xSize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); MPI_Bcast(&ySize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); MPI_Bcast(&zSize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); if(rank == ROOT) { + buf = (double*)calloc(xSize*ySize, sizeof(double)); uint32_t y_step = ceil(ySize / (1.0 * size)) + 1; uint32_t prev_range = 0; range = (int*)calloc(size, sizeof(int)); @@ -28,17 +42,16 @@ void calc(double* arr, uint32_t zSize, uint32_t ySize, uint32_t xSize, int rank, for(int send_rank = 0; send_rank < size; ++send_rank) { uint32_t real_step = std::min(y_step, ySize - prev_range); range[send_rank] = real_step * xSize; - displace[send_rank] = prev_range * xSize; + displace[send_rank] = prev_range * xSize + xSize; prev_range += y_step; } recv_range = (int*)calloc(size, sizeof(int)); for(int i = 0; i < size; ++i) { + recv_range[i] = range[i] - (range[i] == 0) ? 0 : xSize; } - } - MPI_Scatter(range, 1, MPI_INT, &step, 1, MPI_INT, ROOT, MPI_COMM_WORLD); @@ -58,12 +71,16 @@ void calc(double* arr, uint32_t zSize, uint32_t ySize, uint32_t xSize, int rank, } } } - + printf("[%d] my_copy[0] = %lf, step = %d\n", rank, my_copy[0], step); MPI_Gatherv(my_copy, (step == 0) ? 0 : step - xSize, MPI_DOUBLE, - arr + z*ySize*xSize, recv_range, displace, MPI_DOUBLE, - ROOT, MPI_COMM_WORLD); + buf, recv_range, displace, MPI_DOUBLE, + ROOT, MPI_COMM_WORLD); + if(rank == ROOT) { + memcpy(arr + z*ySize*xSize, buf, xSize*ySize*sizeof(double)); + } } + free(my_copy); if(rank == ROOT) { free(range); free(displace); From 729f68ca392785f40b6bd2a3872f2cb2cf266ac8 Mon Sep 17 00:00:00 2001 From: Extra5enS Date: Fri, 13 Nov 2020 19:18:21 +0300 Subject: [PATCH 13/16] visual update Loop2, main ideas and main part of code saved --- Loop2/src/main.cpp | 104 +++++++++++++++++++++++++++------------------ 1 file changed, 62 insertions(+), 42 deletions(-) diff --git a/Loop2/src/main.cpp b/Loop2/src/main.cpp index 361e996..bf99524 100644 --- a/Loop2/src/main.cpp +++ b/Loop2/src/main.cpp @@ -14,62 +14,82 @@ //arr[(y - 1)*xSize + (x + 3)] = sin(0.00001*arr[y*xSize + x]); void calc(double* arr, uint32_t ySize, uint32_t xSize, int rank, int size) { - int32_t step = 0; - int32_t* range = NULL; - int32_t* displace = NULL; + int* send_dist = NULL; + int* send_range = NULL; - MPI_Bcast(&xSize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); + int* recv_dist = NULL; + int* recv_range = NULL; + MPI_Bcast(&ySize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); + MPI_Bcast(&xSize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); + int real_rank_size = 0; + + if(rank == ROOT) { + size_t my_calc_size = ceil((ySize) * 1.0 / size); + // if proc_rank > ceil(ySize / my_calc_size) it will do nothing + real_rank_size = ceil((ySize) * 1.0 / my_calc_size); + size_t now_size = 0; - if(rank == ROOT) { - uint32_t y_step = ceil(ySize / (1.0 * size)) + 1; - uint32_t prev_range = 0; + recv_dist = (int*)calloc(size, sizeof(int)); + recv_range = (int*)calloc(size, sizeof(int)); + + send_dist = (int*)calloc(size, sizeof(int)); + send_range = (int*)calloc(size, sizeof(int)); + + for(int i = 0; i < real_rank_size; ++i) { + recv_dist[i] = i * xSize * my_calc_size; + recv_range[i] = xSize * std::min(my_calc_size, ySize - now_size); + now_size += std::min(my_calc_size, ySize - now_size); + } - range = (int32_t*)calloc(size, sizeof(int32_t)); - displace = (int32_t*)calloc(size, sizeof(int32_t)); - // реализуем перекрытие - for(int send_rank = 0; send_rank < size; ++send_rank) { - // We will send start_y, step, xSize and part of arr - uint32_t real_step = std::min(y_step, ySize - prev_range); - range[send_rank] = real_step * xSize; - displace[send_rank] = prev_range * xSize; - prev_range += y_step - 1; + for(int i = 0; i < real_rank_size; ++i) { + send_range[i] = (i == real_rank_size - 1)? recv_range[i] : recv_range[i] + xSize; + send_dist[i] = recv_dist[i]; } - } - MPI_Scatter(range, 1, MPI_INT, - &step, 1, MPI_INT, + } + int send_step = 0; + int recv_step = 0; + + + // Bug with MPI <- i hate it! really! + MPI_Scatter( + send_range, 1, MPI_INT, + &send_step, size, MPI_INT, + ROOT, MPI_COMM_WORLD); + MPI_Scatter( + recv_range, 1, MPI_INT, + &recv_step, size, MPI_INT, ROOT, MPI_COMM_WORLD); - - double* my_copy = (double*) calloc(step, sizeof(double)); - MPI_Scatterv(arr, range, displace, MPI_DOUBLE, - my_copy, step, MPI_DOUBLE, - ROOT, MPI_COMM_WORLD); - - if(rank == ROOT) { - for(int i = 0; i < size; ++i) { - range[i] -= (range[i] == 0) ? 0 : xSize; - } - } + double* my_calc = NULL; + my_calc = (double*)calloc(send_step, sizeof(double)); - if(step != 0) { - for(uint32_t y = 0; y < step / xSize - 1; ++y) { - for(uint32_t x = 3; x < xSize; ++x) { - my_copy[y*xSize + x] = sin(0.00001*my_copy[(y + 1) * xSize + (x - 3)]); + MPI_Scatterv( + arr, send_range, send_dist, MPI_DOUBLE, + my_calc, send_step, MPI_DOUBLE, + ROOT, MPI_COMM_WORLD); + + if((uint32_t)send_step > xSize) { + for(size_t y = 0; y < send_step / xSize - 1; ++y) { + for(size_t x = 3; x < xSize; ++x) { + my_calc[y*xSize + x] = sin(0.00001*my_calc[(y + 1) * xSize + (x - 3)]); } } } - - MPI_Gatherv(my_copy, (step == 0)?0:step - xSize, MPI_DOUBLE, - arr, range, displace, MPI_DOUBLE, - ROOT, MPI_COMM_WORLD); - if(rank == ROOT) { + MPI_Gatherv( + my_calc, recv_step, MPI_DOUBLE, + arr, recv_range, recv_dist, MPI_DOUBLE, + ROOT, MPI_COMM_WORLD); + - free(range); - free(displace); + free(my_calc); + if(rank == ROOT) { + free(recv_dist); + free(recv_range); + free(send_dist); + free(send_range); } - free(my_copy); } int main(int argc, char** argv) From fd63ee6a22413e8bfea9d1b2b9fa84b3b298a2b7 Mon Sep 17 00:00:00 2001 From: Extra5enS Date: Fri, 13 Nov 2020 19:48:33 +0300 Subject: [PATCH 14/16] Look4 is done --- Loop4/src/main.cpp | 122 +++++++++++++++++++++++++-------------------- 1 file changed, 68 insertions(+), 54 deletions(-) diff --git a/Loop4/src/main.cpp b/Loop4/src/main.cpp index dc3fd36..71e7607 100644 --- a/Loop4/src/main.cpp +++ b/Loop4/src/main.cpp @@ -10,81 +10,95 @@ // arr[z*ySize*xSize + y*xSize + x] = sin(arr[(z - 1)*ySize*xSize + (y + 1)*xSize + x + 1]); void calc(double* arr, uint32_t zSize, uint32_t ySize, uint32_t xSize, int rank, int size) { - if(size == 1) { - for(int64_t z = 1; z < zSize; ++z) { - for(int64_t y = 0; y < ySize - 1; ++y) { - for(int64_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]); - } - } - } - return; - } - - int32_t step = 0; - int* range = NULL; - int* displace = NULL; + int* send_dist = NULL; + int* send_range = NULL; + + int* recv_dist = NULL; int* recv_range = NULL; - - double* buf = NULL; - MPI_Bcast(&xSize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); - MPI_Bcast(&ySize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); + double* new_arr = NULL; + MPI_Bcast(&zSize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); - + MPI_Bcast(&ySize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); + MPI_Bcast(&xSize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); + int real_rank_size = 0; + if(rank == ROOT) { - buf = (double*)calloc(xSize*ySize, sizeof(double)); - uint32_t y_step = ceil(ySize / (1.0 * size)) + 1; - uint32_t prev_range = 0; - range = (int*)calloc(size, sizeof(int)); - displace = (int*)calloc(size, sizeof(int)); - - for(int send_rank = 0; send_rank < size; ++send_rank) { - uint32_t real_step = std::min(y_step, ySize - prev_range); - range[send_rank] = real_step * xSize; - displace[send_rank] = prev_range * xSize + xSize; - prev_range += y_step; - } + size_t my_calc_size = ceil((ySize) * 1.0 / size); + // if proc_rank > ceil(ySize / my_calc_size) it will do nothing + real_rank_size = ceil((ySize) * 1.0 / my_calc_size); + size_t now_size = 0; + + new_arr = (double*)calloc(xSize*ySize, sizeof(double)); + recv_dist = (int*)calloc(size, sizeof(int)); recv_range = (int*)calloc(size, sizeof(int)); - for(int i = 0; i < size; ++i) { - - recv_range[i] = range[i] - (range[i] == 0) ? 0 : xSize; + + send_dist = (int*)calloc(size, sizeof(int)); + send_range = (int*)calloc(size, sizeof(int)); + + for(int i = 0; i < real_rank_size; ++i) { + recv_dist[i] = i * xSize * my_calc_size; + recv_range[i] = xSize * std::min(my_calc_size, ySize - now_size); + now_size += std::min(my_calc_size, ySize - now_size); } - } - MPI_Scatter(range, 1, MPI_INT, - &step, 1, MPI_INT, + + for(int i = 0; i < real_rank_size; ++i) { + send_range[i] = (i == real_rank_size - 1)? recv_range[i] : recv_range[i] + xSize; + send_dist[i] = recv_dist[i]; + } + } + int send_step = 0; + int recv_step = 0; + + + // Bug with MPI <- i hate it! really! + MPI_Scatter( + send_range, 1, MPI_INT, + &send_step, size, MPI_INT, + ROOT, MPI_COMM_WORLD); + MPI_Scatter( + recv_range, 1, MPI_INT, + &recv_step, size, MPI_INT, ROOT, MPI_COMM_WORLD); - double* my_copy = (double*)calloc(step, sizeof(double)); + double* my_calc = NULL; + my_calc = (double*)calloc(send_step, sizeof(double)); + for(size_t z = 0; z < zSize - 1; ++z) { + MPI_Scatterv( + arr + z*xSize*ySize, send_range, send_dist, MPI_DOUBLE, + my_calc, send_step, MPI_DOUBLE, + ROOT, MPI_COMM_WORLD); - for(size_t z = 1; z < zSize; ++z) { - MPI_Scatterv(arr + (z - 1)*ySize*xSize, range, displace, MPI_DOUBLE, - my_copy, step, MPI_DOUBLE, - ROOT, MPI_COMM_WORLD); - - if(step != 0) { - for(uint32_t y = 0; y < step / xSize - 1; ++y) { - for(uint32_t x = 0; x < xSize - 1; ++x) { - my_copy[y*xSize + x] = sin(my_copy[(y + 1) * xSize + (x + 1)]); + if((uint32_t)send_step > xSize) { + for(size_t y = 0; y < send_step / xSize - 1; ++y) { + for(size_t x = 0; x < xSize - 1; ++x) { + my_calc[y*xSize + x] = sin(my_calc[(y + 1) * xSize + (x + 1)]); } } } - printf("[%d] my_copy[0] = %lf, step = %d\n", rank, my_copy[0], step); - MPI_Gatherv(my_copy, (step == 0) ? 0 : step - xSize, MPI_DOUBLE, - buf, recv_range, displace, MPI_DOUBLE, + + MPI_Gatherv( + my_calc, recv_step, MPI_DOUBLE, + new_arr, recv_range, recv_dist, MPI_DOUBLE, ROOT, MPI_COMM_WORLD); if(rank == ROOT) { - memcpy(arr + z*ySize*xSize, buf, xSize*ySize*sizeof(double)); + for(size_t y = 0; y < xSize - 1; ++y) { + for(size_t x = 0; x < xSize - 1; ++x) { + (arr + (z + 1)*xSize*ySize)[y * xSize + x] = new_arr[y * xSize + x]; + } + } } } - free(my_copy); + free(my_calc); if(rank == ROOT) { - free(range); - free(displace); + free(new_arr); + free(recv_dist); free(recv_range); + free(send_dist); + free(send_range); } } From 00858574136be2c2267768fad18a95068e4d27fa Mon Sep 17 00:00:00 2001 From: Extra5enS Date: Mon, 16 Nov 2020 19:24:00 +0300 Subject: [PATCH 15/16] add calc with transp --- Loop3/src/main.cpp | 121 +++++++++++++++++++-------------------------- 1 file changed, 51 insertions(+), 70 deletions(-) diff --git a/Loop3/src/main.cpp b/Loop3/src/main.cpp index 25b08ff..05fa0ad 100644 --- a/Loop3/src/main.cpp +++ b/Loop3/src/main.cpp @@ -7,94 +7,75 @@ #define ROOT 0 -void form_arr(double* arr, double* copy_arr, uint32_t ySize, uint32_t xSize, int* ran) { - int pos = 0; - for(int i = 0; i < 4; ++i) { - for(uint32_t y = i; y < ySize; y += 4) { - memcpy(©_arr[(pos++) * xSize], &arr[y * xSize], xSize * sizeof(double)); - ran[i] += xSize; - } - } -} - -void unform_arr(double* arr, double* copy_arr, uint32_t ySize, uint32_t xSize) { - int pos = 0; - for(int i = 0; i < 4; ++i) { - for(uint32_t y = i; y < ySize; y += 4) { - memcpy(&arr[y * xSize], ©_arr[(pos++) * xSize], xSize * sizeof(double)); - } - } -} - void calc(double* arr, uint32_t ySize, uint32_t xSize, int rank, int size) { - MPI_Comm comm; - if(size < 4) { - if(rank == ROOT) { - for (uint32_t y = 4; y < ySize; y++) { - for (uint32_t x = 0; x < xSize; x++) { - arr[y*xSize + x] = sin(arr[(y - 4)*xSize + x]); - } + double* new_arr = NULL; + if(rank == ROOT) { + new_arr = (double*)calloc(xSize * ySize, sizeof(double)); + for(size_t y = 0; y < ySize; ++y) { + for(size_t x = 0; x < xSize; ++x) { + new_arr[x*ySize + y] = arr[y*xSize + x]; } } - return; } - MPI_Comm_split(MPI_COMM_WORLD, (rank < 4) ? 1 : MPI_UNDEFINED, rank, &comm); - if(rank >= 4) { - return; - } - - MPI_Comm_rank(comm, &rank); - MPI_Comm_size(comm, &size); + int32_t step = 0; + int32_t* range = NULL; + int32_t* displace = NULL; - int* ran = NULL; - int* dis = NULL; - double* new_array = NULL; - int step = 0; - - MPI_Bcast(&xSize, 1, MPI_UNSIGNED, ROOT, comm); - MPI_Bcast(&ySize, 1, MPI_UNSIGNED, ROOT, comm); - /* - if(rank == ROOT) { - printf("%d\n", size); - }*/ + MPI_Bcast(&xSize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); + MPI_Bcast(&ySize, 1, MPI_UNSIGNED, ROOT, MPI_COMM_WORLD); - if(rank == ROOT) { - ran = (int*)calloc(4, sizeof(int)); - dis = (int*)calloc(4, sizeof(int)); - new_array = (double*)calloc(xSize*ySize, sizeof(double)); - form_arr(arr, new_array, ySize, xSize, ran); - dis[1] = ran[0]; - dis[2] = ran[1] + dis[1]; - dis[3] = ran[2] + dis[2]; + if(rank == ROOT) { + uint32_t x_step = ceil(xSize / (1.0 * size)); + uint32_t prev_range = 0; + + range = (int32_t*)calloc(size, sizeof(int32_t)); + displace = (int32_t*)calloc(size, sizeof(int32_t)); + + for(int send_rank = 0; send_rank < size; ++send_rank) { + // We will send start_y, step, xSize and part of arr + uint32_t real_step = std::min(x_step, xSize - prev_range); + range[send_rank] = real_step * ySize; + displace[send_rank] = prev_range * ySize; + prev_range += x_step; + } } - - MPI_Scatter(ran, 1, MPI_INT, + MPI_Scatter(range, 1, MPI_INT, &step, 1, MPI_INT, - ROOT, comm); - //printf("[%d] task = %d, step = %d\n", rank, ySize * xSize, step); + ROOT, MPI_COMM_WORLD); + - double* my_copy = (double*)calloc(step, sizeof(double)); - MPI_Scatterv(new_array, ran, dis, MPI_DOUBLE, + double* my_copy = (double*) calloc(step, sizeof(double)); + MPI_Scatterv(new_arr, range, displace, MPI_DOUBLE, my_copy, step, MPI_DOUBLE, - ROOT, comm); + ROOT, MPI_COMM_WORLD); - for(int x = xSize; x < step; ++x) { - my_copy[x] = sin(my_copy[x - xSize]); + for(size_t x = 0; x < step / ySize; ++x) { + for(size_t y = 4; y < ySize; ++y) { + my_copy[x*ySize + y] = sin(my_copy[x*ySize + y - 4]); + } } - + MPI_Gatherv(my_copy, step, MPI_DOUBLE, - new_array, ran, dis, MPI_DOUBLE, - ROOT, comm); + new_arr, range, displace, MPI_DOUBLE, + ROOT, MPI_COMM_WORLD); if(rank == ROOT) { - unform_arr(arr, new_array, ySize, xSize); - free(new_array); - free(ran); - free(dis); + for(size_t y = 0; y < ySize; ++y) { + for(size_t x = 0; x < xSize; ++x) { + arr[y*xSize + x] = new_arr[x*ySize + y]; + } + } + } + + + if(rank == ROOT) { + free(new_arr); + free(range); + free(displace); } - free(my_copy); + free(my_copy); } int main(int argc, char** argv) From 4812af719e299d75c42a250f6a470e9dbaecdced Mon Sep 17 00:00:00 2001 From: Extra5enS Date: Mon, 16 Nov 2020 19:41:05 +0300 Subject: [PATCH 16/16] [fix] fix bug with noncorect stepsize --- Loop3/src/main.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Loop3/src/main.cpp b/Loop3/src/main.cpp index 05fa0ad..0d9c88f 100644 --- a/Loop3/src/main.cpp +++ b/Loop3/src/main.cpp @@ -38,19 +38,18 @@ void calc(double* arr, uint32_t ySize, uint32_t xSize, int rank, int size) uint32_t real_step = std::min(x_step, xSize - prev_range); range[send_rank] = real_step * ySize; displace[send_rank] = prev_range * ySize; - prev_range += x_step; + prev_range += real_step; } } MPI_Scatter(range, 1, MPI_INT, &step, 1, MPI_INT, ROOT, MPI_COMM_WORLD); - - double* my_copy = (double*) calloc(step, sizeof(double)); + double* my_copy = NULL; + my_copy = (double*) calloc(step, sizeof(double)); MPI_Scatterv(new_arr, range, displace, MPI_DOUBLE, my_copy, step, MPI_DOUBLE, ROOT, MPI_COMM_WORLD); - for(size_t x = 0; x < step / ySize; ++x) { for(size_t y = 4; y < ySize; ++y) { my_copy[x*ySize + y] = sin(my_copy[x*ySize + y - 4]);