From 2302085341e709fede9c08e9ed05880012f6fdc6 Mon Sep 17 00:00:00 2001 From: IconLivin Date: Sat, 5 Oct 2019 14:51:55 +0300 Subject: [PATCH 1/8] done --- 1706-4/kuzhelev_aa | 1 + 1 file changed, 1 insertion(+) create mode 160000 1706-4/kuzhelev_aa diff --git a/1706-4/kuzhelev_aa b/1706-4/kuzhelev_aa new file mode 160000 index 0000000..ab494b8 --- /dev/null +++ b/1706-4/kuzhelev_aa @@ -0,0 +1 @@ +Subproject commit ab494b8c04935def0d512084d188f487de110520 From 8874f12e57eef3f78ddb04492c9056592f885c0c Mon Sep 17 00:00:00 2001 From: IconLivin Date: Sat, 5 Oct 2019 15:25:05 +0300 Subject: [PATCH 2/8] Done --- 1706-4/kuzhelev_aa | 1 - 1706/kuzhelev_aa/Source.cpp | 184 ++++++++++++++++++++++++++++++++++++ 2 files changed, 184 insertions(+), 1 deletion(-) delete mode 160000 1706-4/kuzhelev_aa create mode 100644 1706/kuzhelev_aa/Source.cpp diff --git a/1706-4/kuzhelev_aa b/1706-4/kuzhelev_aa deleted file mode 160000 index ab494b8..0000000 --- a/1706-4/kuzhelev_aa +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ab494b8c04935def0d512084d188f487de110520 diff --git a/1706/kuzhelev_aa/Source.cpp b/1706/kuzhelev_aa/Source.cpp new file mode 100644 index 0000000..95dbbe2 --- /dev/null +++ b/1706/kuzhelev_aa/Source.cpp @@ -0,0 +1,184 @@ +#include +#include +#include + +using namespace::std; + +int curr_rank_proc; +int num_of_procs; +double time_seq_work_alg = 0; +double time_pp_work_alg = 0; + + +double* Create_vector_and_init(int size_row, int size_column) +{ + if (size_row < 1 || size_column < 1) + return NULL; + + double* vec; + vec = new double[size_row * size_column]; + srand((unsigned)time(NULL)); + if (size_row > 10 || size_column > 10) + { + for (int i = 0; i < size_row * size_column; i++)vec[i] = (double)(rand() % 100) - 50.0 + 0.5;; + } + else + { + for (int i = 0; i < size_row * size_column; i++) + { + cout << "Input" << i << " double element: " << endl; + cin >> vec[i]; + } + } + return vec; +} + +void Show_vec(double* vec, int size_matr_row, int size_matr_column) +{ + if (vec != NULL) + for (int i = 0; i < size_matr_row * size_matr_column; i++) + { + cout << vec[i] << " "; + cout << endl; + } +} +int main(int argc, char* argv[]) +{ + int size_row = 5, size_column = 5; + double* matrix_as_vector = NULL; + + double sum_el_seq = 0; + double sum_el_pp = 0; + + double end_time_of_seq_alg = 0; + double start_time_of_seq_alg = 0; + double end_time_of_pp_alg = 0; + double start_time_of_pp_alg = 0; + + + double partial_summ = 0; + int size_el = 1; + int size_work_of_proc = 0; + + MPI_Status stat; + + + /* Начало MPI кода */ + + MPI_Init(&argc, &argv); + + + MPI_Comm_size(MPI_COMM_WORLD, &num_of_procs); + + MPI_Comm_rank(MPI_COMM_WORLD, &curr_rank_proc); + + if (num_of_procs < 1) + { + cout << "Incorrect number of processes (at least 1 must be)" << endl; + return 0; + } + + if (curr_rank_proc == 0) + { + cout << "Input size of row: " << endl; + cin >> size_row; + cout << "Input size of column: " << endl; + cin >> size_column; + + size_el = size_row * size_column; + matrix_as_vector = Create_vector_and_init(size_row, size_column); + + if (matrix_as_vector == NULL) + { + cout << "Incorrect input data, try again" << endl; + return 0; + } + + if (size_row * size_column < 1000) + { + cout << "Current matrix:" << endl; + Show_vec(matrix_as_vector, size_row, size_column); + } + cout << endl; + + /* Подсчет суммы всех элементов матрицы (последовательная версия алгоритма): */ + + start_time_of_seq_alg = MPI_Wtime(); + + for (int i = 0; i < size_row * size_column; i++) + sum_el_seq += matrix_as_vector[i]; + + end_time_of_seq_alg = MPI_Wtime(); + time_seq_work_alg = end_time_of_seq_alg - start_time_of_seq_alg; + + cout << "Sum of all elements in matrix is " << sum_el_seq << " " << endl; + cout << "Spent time on the implementation of this algorithm (Sequence version)" << time_seq_work_alg << " ms " << endl; + cout << endl; + cout << "Num of procs: " << num_of_procs << endl; + + + + start_time_of_pp_alg = MPI_Wtime(); + + size_work_of_proc = size_el / num_of_procs; + + MPI_Bcast(&size_el, 1, MPI_INT, 0, MPI_COMM_WORLD); + for (int i = 1; i < num_of_procs; i++) + MPI_Send(matrix_as_vector + size_work_of_proc * (i - 1), size_work_of_proc, MPI_DOUBLE, i, 1, MPI_COMM_WORLD); + + + cout << "Process with rang " << curr_rank_proc << " start own job" << endl; + + for (int i = size_work_of_proc * (num_of_procs - 1); i < size_el; i++)//подсчитывает последнюю оставшуюся часть, если однопроцесорная система, считает все сама + partial_summ += matrix_as_vector[i]; + + sum_el_pp = partial_summ; + + for (int i = 1; i < num_of_procs; i++) + { + MPI_Recv(&partial_summ, 1, MPI_DOUBLE, i, 1, MPI_COMM_WORLD, &stat); + sum_el_pp += partial_summ; + } + + end_time_of_pp_alg = MPI_Wtime(); + + time_pp_work_alg = (end_time_of_pp_alg - start_time_of_pp_alg) * 1000; + cout << "Sum of all elements in matrix is (Parallel version): " << sum_el_pp << endl; + cout << "Spent time on the implementation of this algorithm (Parallel version)" << time_pp_work_alg << " ms" << endl; + + if (sum_el_pp == sum_el_seq) + cout << "Results of parallel and sequence versions are identical! " << endl; + else + cout << "Results of parallel and sequence versions are not identical! " << endl; + + if (time_pp_work_alg <= time_seq_work_alg) + cout << "Parallel version faster, then sequence" << endl; + else + cout << "Sequence version faster, then parallel" << endl; + + delete[] matrix_as_vector; + } + else + { + double* recv_matrix_as_vector; + + MPI_Recv(&size_el, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &stat); + + size_work_of_proc = size_el / num_of_procs; + recv_matrix_as_vector = new double[size_work_of_proc]; + MPI_Recv(recv_matrix_as_vector, size_work_of_proc, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD, &stat); + + cout << "Process with rang " << curr_rank_proc << " start own job" << endl; + + for (int i = 0; i < size_work_of_proc; i++) + partial_summ += recv_matrix_as_vector[i]; + + MPI_Send(&partial_summ, 1, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD); + delete[] recv_matrix_as_vector; + } + + MPI_Finalize(); + + /* Конец MPI кода */ + return 0; +} \ No newline at end of file From a3818a3f570caf8eef06fd94b6966a5a2077e359 Mon Sep 17 00:00:00 2001 From: IconLivin Date: Sun, 13 Oct 2019 20:53:35 +0300 Subject: [PATCH 3/8] NU PLZ --- 1706-4/Source.cpp | 185 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 1706-4/Source.cpp diff --git a/1706-4/Source.cpp b/1706-4/Source.cpp new file mode 100644 index 0000000..51ef9b8 --- /dev/null +++ b/1706-4/Source.cpp @@ -0,0 +1,185 @@ +#include +#include +#include + +using namespace std; + +int curr_rank_proc; +int num_of_procs; +double time_seq_work_alg = 0; +double time_pp_work_alg = 0; + + +double* Create_vector_and_init(int size_row, int size_column) +{ + if (size_row < 1 || size_column < 1) + return NULL; + + double* vec; + vec = new double[size_row * size_column]; + srand(time(NULL)); + if (size_row > 4 || size_column > 4) + { + for (int i = 0; i < size_row * size_column; i++)vec[i] = (double)(rand() % 100) - 50.0 + 0.5;; + } + else + { + for (int i = 0; i < size_row * size_column; i++) + { + cout << "Input" << i << " double element: " << endl; + cin >> vec[i]; + } + } + return vec; +} + +void Show_vec(double* vec, int size_matr_row, int size_matr_column) +{ + if (vec != NULL) + for (int i = 0; i < size_matr_row * size_matr_column; i++) + { + cout << vec[i] << " "; + cout << endl; + } +} +int main(int argc, char* argv[]) +{ + int size_row = 5, size_column = 5; + double* matrix_as_vector = NULL; + + double sum_el_seq = 0; + double sum_el_pp = 0; + + double end_time_of_seq_alg = 0; + double start_time_of_seq_alg = 0; + double end_time_of_pp_alg = 0; + double start_time_of_pp_alg = 0; + + + double partial_summ = 0; + int size_el = 1; + int size_work_of_proc = 0; + + MPI_Status stat; + + + /* Начало MPI кода */ + + MPI_Init(&argc, &argv); + + + MPI_Comm_size(MPI_COMM_WORLD, &num_of_procs); + + MPI_Comm_rank(MPI_COMM_WORLD, &curr_rank_proc); + + if (num_of_procs < 1) + { + cout << "Incorrect number of processes (at least 1 must be)" << endl; + return 0; + } + + if (curr_rank_proc == 0) + { + cout << "Input size of row: " << endl; + cin >> size_row; + cout << "Input size of column: " << endl; + cin >> size_column; + + size_el = size_row * size_column; + matrix_as_vector = Create_vector_and_init(size_row, size_column); + + if (matrix_as_vector == NULL) + { + cout << "Incorrect input data, try again" << endl; + return 0; + } + + if (size_row * size_column < 1000) + { + cout << "Current matrix:" << endl; + Show_vec(matrix_as_vector, size_row, size_column); + } + cout << endl; + + /* Подсчет суммы всех элементов матрицы (последовательная версия алгоритма): */ + + start_time_of_seq_alg = MPI_Wtime(); + + for (int i = 0; i < size_row * size_column; i++) + sum_el_seq += matrix_as_vector[i]; + + end_time_of_seq_alg = MPI_Wtime(); + time_seq_work_alg = end_time_of_seq_alg - start_time_of_seq_alg; + + cout << "Sum of all elements in matrix is " << sum_el_seq << " " << endl; + cout << "Spent time on the implementation of this algorithm (Sequence version)" << time_seq_work_alg << " ms " << endl; + cout << endl; + cout << "Num of procs: " << num_of_procs << endl; + + + + start_time_of_pp_alg = MPI_Wtime(); + + size_work_of_proc = size_el / num_of_procs; + + for (int i = 1; i < num_of_procs; i++) + MPI_Send(&size_el, 1, MPI_INT, i, 1, MPI_COMM_WORLD); + for (int i = 1; i < num_of_procs; i++) + MPI_Send(matrix_as_vector + size_work_of_proc * (i - 1), size_work_of_proc, MPI_DOUBLE, i, 1, MPI_COMM_WORLD); + + + cout << "Process with rang " << curr_rank_proc << " start own job" << endl; + + for (int i = size_work_of_proc * (num_of_procs - 1); i < size_el; i++)//подсчитывает последнюю оставшуюся часть, если однопроцесорная система, считает все сама + partial_summ += matrix_as_vector[i]; + + sum_el_pp = partial_summ; + + for (int i = 1; i < num_of_procs; i++) + { + MPI_Recv(&partial_summ, 1, MPI_DOUBLE, i, 1, MPI_COMM_WORLD, &stat); + sum_el_pp += partial_summ; + } + + end_time_of_pp_alg = MPI_Wtime(); + + time_pp_work_alg = end_time_of_pp_alg - start_time_of_pp_alg; + cout << "Sum of all elements in matrix is (Parallel version): " << sum_el_pp << endl; + cout << "Spent time on the implementation of this algorithm (Parallel version)" << time_pp_work_alg << " ms" << endl; + + if (sum_el_pp == sum_el_seq) + cout << "Results of parallel and sequence versions are identical! " << endl; + else + cout << "Results of parallel and sequence versions are not identical! " << endl; + + if (time_pp_work_alg <= time_seq_work_alg) + cout << "Parallel version faster, then sequence" << endl; + else + cout << "Sequence version faster, then parallel" << endl; + + delete[] matrix_as_vector; + } + else + { + double* recv_matrix_as_vector; + + MPI_Recv(&size_el, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &stat); + + size_work_of_proc = size_el / num_of_procs; + recv_matrix_as_vector = new double[size_work_of_proc]; + MPI_Recv(recv_matrix_as_vector, size_work_of_proc, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD, &stat); + + cout << "Process with rang " << curr_rank_proc << " start own job" << endl; + + for (int i = 0; i < size_work_of_proc; i++) + partial_summ += recv_matrix_as_vector[i]; + + MPI_Send(&partial_summ, 1, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD); + delete[] recv_matrix_as_vector; + } + + MPI_Finalize(); + + /* Конец MPI кода */ + return 0; +} \ No newline at end of file From cbb0ff5470fab5848a9feca1ee12dc40e0607add Mon Sep 17 00:00:00 2001 From: IconLivin Date: Sun, 13 Oct 2019 20:59:17 +0300 Subject: [PATCH 4/8] ch --- 1706-4/Source.cpp | 185 ------------------------------------ 1706/kuzhelev_aa/Source.cpp | 13 +-- 2 files changed, 7 insertions(+), 191 deletions(-) delete mode 100644 1706-4/Source.cpp diff --git a/1706-4/Source.cpp b/1706-4/Source.cpp deleted file mode 100644 index 51ef9b8..0000000 --- a/1706-4/Source.cpp +++ /dev/null @@ -1,185 +0,0 @@ -#include -#include -#include - -using namespace std; - -int curr_rank_proc; -int num_of_procs; -double time_seq_work_alg = 0; -double time_pp_work_alg = 0; - - -double* Create_vector_and_init(int size_row, int size_column) -{ - if (size_row < 1 || size_column < 1) - return NULL; - - double* vec; - vec = new double[size_row * size_column]; - srand(time(NULL)); - if (size_row > 4 || size_column > 4) - { - for (int i = 0; i < size_row * size_column; i++)vec[i] = (double)(rand() % 100) - 50.0 + 0.5;; - } - else - { - for (int i = 0; i < size_row * size_column; i++) - { - cout << "Input" << i << " double element: " << endl; - cin >> vec[i]; - } - } - return vec; -} - -void Show_vec(double* vec, int size_matr_row, int size_matr_column) -{ - if (vec != NULL) - for (int i = 0; i < size_matr_row * size_matr_column; i++) - { - cout << vec[i] << " "; - cout << endl; - } -} -int main(int argc, char* argv[]) -{ - int size_row = 5, size_column = 5; - double* matrix_as_vector = NULL; - - double sum_el_seq = 0; - double sum_el_pp = 0; - - double end_time_of_seq_alg = 0; - double start_time_of_seq_alg = 0; - double end_time_of_pp_alg = 0; - double start_time_of_pp_alg = 0; - - - double partial_summ = 0; - int size_el = 1; - int size_work_of_proc = 0; - - MPI_Status stat; - - - /* Начало MPI кода */ - - MPI_Init(&argc, &argv); - - - MPI_Comm_size(MPI_COMM_WORLD, &num_of_procs); - - MPI_Comm_rank(MPI_COMM_WORLD, &curr_rank_proc); - - if (num_of_procs < 1) - { - cout << "Incorrect number of processes (at least 1 must be)" << endl; - return 0; - } - - if (curr_rank_proc == 0) - { - cout << "Input size of row: " << endl; - cin >> size_row; - cout << "Input size of column: " << endl; - cin >> size_column; - - size_el = size_row * size_column; - matrix_as_vector = Create_vector_and_init(size_row, size_column); - - if (matrix_as_vector == NULL) - { - cout << "Incorrect input data, try again" << endl; - return 0; - } - - if (size_row * size_column < 1000) - { - cout << "Current matrix:" << endl; - Show_vec(matrix_as_vector, size_row, size_column); - } - cout << endl; - - /* Подсчет суммы всех элементов матрицы (последовательная версия алгоритма): */ - - start_time_of_seq_alg = MPI_Wtime(); - - for (int i = 0; i < size_row * size_column; i++) - sum_el_seq += matrix_as_vector[i]; - - end_time_of_seq_alg = MPI_Wtime(); - time_seq_work_alg = end_time_of_seq_alg - start_time_of_seq_alg; - - cout << "Sum of all elements in matrix is " << sum_el_seq << " " << endl; - cout << "Spent time on the implementation of this algorithm (Sequence version)" << time_seq_work_alg << " ms " << endl; - cout << endl; - cout << "Num of procs: " << num_of_procs << endl; - - - - start_time_of_pp_alg = MPI_Wtime(); - - size_work_of_proc = size_el / num_of_procs; - - for (int i = 1; i < num_of_procs; i++) - MPI_Send(&size_el, 1, MPI_INT, i, 1, MPI_COMM_WORLD); - for (int i = 1; i < num_of_procs; i++) - MPI_Send(matrix_as_vector + size_work_of_proc * (i - 1), size_work_of_proc, MPI_DOUBLE, i, 1, MPI_COMM_WORLD); - - - cout << "Process with rang " << curr_rank_proc << " start own job" << endl; - - for (int i = size_work_of_proc * (num_of_procs - 1); i < size_el; i++)//подсчитывает последнюю оставшуюся часть, если однопроцесорная система, считает все сама - partial_summ += matrix_as_vector[i]; - - sum_el_pp = partial_summ; - - for (int i = 1; i < num_of_procs; i++) - { - MPI_Recv(&partial_summ, 1, MPI_DOUBLE, i, 1, MPI_COMM_WORLD, &stat); - sum_el_pp += partial_summ; - } - - end_time_of_pp_alg = MPI_Wtime(); - - time_pp_work_alg = end_time_of_pp_alg - start_time_of_pp_alg; - cout << "Sum of all elements in matrix is (Parallel version): " << sum_el_pp << endl; - cout << "Spent time on the implementation of this algorithm (Parallel version)" << time_pp_work_alg << " ms" << endl; - - if (sum_el_pp == sum_el_seq) - cout << "Results of parallel and sequence versions are identical! " << endl; - else - cout << "Results of parallel and sequence versions are not identical! " << endl; - - if (time_pp_work_alg <= time_seq_work_alg) - cout << "Parallel version faster, then sequence" << endl; - else - cout << "Sequence version faster, then parallel" << endl; - - delete[] matrix_as_vector; - } - else - { - double* recv_matrix_as_vector; - - MPI_Recv(&size_el, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &stat); - - size_work_of_proc = size_el / num_of_procs; - recv_matrix_as_vector = new double[size_work_of_proc]; - MPI_Recv(recv_matrix_as_vector, size_work_of_proc, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD, &stat); - - cout << "Process with rang " << curr_rank_proc << " start own job" << endl; - - for (int i = 0; i < size_work_of_proc; i++) - partial_summ += recv_matrix_as_vector[i]; - - MPI_Send(&partial_summ, 1, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD); - delete[] recv_matrix_as_vector; - } - - MPI_Finalize(); - - /* Конец MPI кода */ - return 0; -} \ No newline at end of file diff --git a/1706/kuzhelev_aa/Source.cpp b/1706/kuzhelev_aa/Source.cpp index 95dbbe2..51ef9b8 100644 --- a/1706/kuzhelev_aa/Source.cpp +++ b/1706/kuzhelev_aa/Source.cpp @@ -2,7 +2,7 @@ #include #include -using namespace::std; +using namespace std; int curr_rank_proc; int num_of_procs; @@ -17,8 +17,8 @@ double* Create_vector_and_init(int size_row, int size_column) double* vec; vec = new double[size_row * size_column]; - srand((unsigned)time(NULL)); - if (size_row > 10 || size_column > 10) + srand(time(NULL)); + if (size_row > 4 || size_column > 4) { for (int i = 0; i < size_row * size_column; i++)vec[i] = (double)(rand() % 100) - 50.0 + 0.5;; } @@ -122,10 +122,11 @@ int main(int argc, char* argv[]) size_work_of_proc = size_el / num_of_procs; - MPI_Bcast(&size_el, 1, MPI_INT, 0, MPI_COMM_WORLD); + for (int i = 1; i < num_of_procs; i++) + MPI_Send(&size_el, 1, MPI_INT, i, 1, MPI_COMM_WORLD); for (int i = 1; i < num_of_procs; i++) MPI_Send(matrix_as_vector + size_work_of_proc * (i - 1), size_work_of_proc, MPI_DOUBLE, i, 1, MPI_COMM_WORLD); - + cout << "Process with rang " << curr_rank_proc << " start own job" << endl; @@ -142,7 +143,7 @@ int main(int argc, char* argv[]) end_time_of_pp_alg = MPI_Wtime(); - time_pp_work_alg = (end_time_of_pp_alg - start_time_of_pp_alg) * 1000; + time_pp_work_alg = end_time_of_pp_alg - start_time_of_pp_alg; cout << "Sum of all elements in matrix is (Parallel version): " << sum_el_pp << endl; cout << "Spent time on the implementation of this algorithm (Parallel version)" << time_pp_work_alg << " ms" << endl; From 9ab346f6efdace90b966f9faf68f681fa8f75832 Mon Sep 17 00:00:00 2001 From: valeria-vag Date: Mon, 14 Oct 2019 11:57:35 +0300 Subject: [PATCH 5/8] Finall changes --- 1706/kuzhelev_aa/Source.cpp | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/1706/kuzhelev_aa/Source.cpp b/1706/kuzhelev_aa/Source.cpp index 51ef9b8..e2eaefc 100644 --- a/1706/kuzhelev_aa/Source.cpp +++ b/1706/kuzhelev_aa/Source.cpp @@ -1,5 +1,5 @@ #include -#include +#include "mpi.h" #include using namespace std; @@ -56,7 +56,7 @@ int main(int argc, char* argv[]) double start_time_of_pp_alg = 0; - double partial_summ = 0; + double partial_summ = 0, temp_sum = 0; int size_el = 1; int size_work_of_proc = 0; @@ -122,8 +122,7 @@ int main(int argc, char* argv[]) size_work_of_proc = size_el / num_of_procs; - for (int i = 1; i < num_of_procs; i++) - MPI_Send(&size_el, 1, MPI_INT, i, 1, MPI_COMM_WORLD); + MPI_Bcast(&size_el, 1, MPI_INT, 0, MPI_COMM_WORLD); for (int i = 1; i < num_of_procs; i++) MPI_Send(matrix_as_vector + size_work_of_proc * (i - 1), size_work_of_proc, MPI_DOUBLE, i, 1, MPI_COMM_WORLD); @@ -131,15 +130,13 @@ int main(int argc, char* argv[]) cout << "Process with rang " << curr_rank_proc << " start own job" << endl; for (int i = size_work_of_proc * (num_of_procs - 1); i < size_el; i++)//подсчитывает последнюю оставшуюся часть, если однопроцесорная система, считает все сама - partial_summ += matrix_as_vector[i]; + temp_sum += matrix_as_vector[i]; - sum_el_pp = partial_summ; + sum_el_pp = temp_sum; - for (int i = 1; i < num_of_procs; i++) - { - MPI_Recv(&partial_summ, 1, MPI_DOUBLE, i, 1, MPI_COMM_WORLD, &stat); - sum_el_pp += partial_summ; - } + MPI_Reduce(&partial_summ, &temp_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); + sum_el_pp += temp_sum; + end_time_of_pp_alg = MPI_Wtime(); @@ -163,7 +160,7 @@ int main(int argc, char* argv[]) { double* recv_matrix_as_vector; - MPI_Recv(&size_el, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &stat); + MPI_Bcast(&size_el, 1, MPI_INT, 0, MPI_COMM_WORLD); size_work_of_proc = size_el / num_of_procs; recv_matrix_as_vector = new double[size_work_of_proc]; @@ -174,10 +171,9 @@ int main(int argc, char* argv[]) for (int i = 0; i < size_work_of_proc; i++) partial_summ += recv_matrix_as_vector[i]; - MPI_Send(&partial_summ, 1, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD); + MPI_Reduce(&partial_summ, &temp_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); delete[] recv_matrix_as_vector; } - MPI_Finalize(); /* Конец MPI кода */ From 83298ec4f942b3ff0b63007ffbfd10b12a7b25e7 Mon Sep 17 00:00:00 2001 From: "Kuzhelev, Anton" Date: Fri, 17 Apr 2020 19:20:48 +0300 Subject: [PATCH 6/8] Added OpenMP version of Dijsktra algorithm --- 1706/kuzhelev_aa/Source.cpp | 181 ------------------------------------ 1706/kuzhelev_aa/main.cpp | 126 +++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 181 deletions(-) delete mode 100644 1706/kuzhelev_aa/Source.cpp create mode 100644 1706/kuzhelev_aa/main.cpp diff --git a/1706/kuzhelev_aa/Source.cpp b/1706/kuzhelev_aa/Source.cpp deleted file mode 100644 index e2eaefc..0000000 --- a/1706/kuzhelev_aa/Source.cpp +++ /dev/null @@ -1,181 +0,0 @@ -#include -#include "mpi.h" -#include - -using namespace std; - -int curr_rank_proc; -int num_of_procs; -double time_seq_work_alg = 0; -double time_pp_work_alg = 0; - - -double* Create_vector_and_init(int size_row, int size_column) -{ - if (size_row < 1 || size_column < 1) - return NULL; - - double* vec; - vec = new double[size_row * size_column]; - srand(time(NULL)); - if (size_row > 4 || size_column > 4) - { - for (int i = 0; i < size_row * size_column; i++)vec[i] = (double)(rand() % 100) - 50.0 + 0.5;; - } - else - { - for (int i = 0; i < size_row * size_column; i++) - { - cout << "Input" << i << " double element: " << endl; - cin >> vec[i]; - } - } - return vec; -} - -void Show_vec(double* vec, int size_matr_row, int size_matr_column) -{ - if (vec != NULL) - for (int i = 0; i < size_matr_row * size_matr_column; i++) - { - cout << vec[i] << " "; - cout << endl; - } -} -int main(int argc, char* argv[]) -{ - int size_row = 5, size_column = 5; - double* matrix_as_vector = NULL; - - double sum_el_seq = 0; - double sum_el_pp = 0; - - double end_time_of_seq_alg = 0; - double start_time_of_seq_alg = 0; - double end_time_of_pp_alg = 0; - double start_time_of_pp_alg = 0; - - - double partial_summ = 0, temp_sum = 0; - int size_el = 1; - int size_work_of_proc = 0; - - MPI_Status stat; - - - /* Начало MPI кода */ - - MPI_Init(&argc, &argv); - - - MPI_Comm_size(MPI_COMM_WORLD, &num_of_procs); - - MPI_Comm_rank(MPI_COMM_WORLD, &curr_rank_proc); - - if (num_of_procs < 1) - { - cout << "Incorrect number of processes (at least 1 must be)" << endl; - return 0; - } - - if (curr_rank_proc == 0) - { - cout << "Input size of row: " << endl; - cin >> size_row; - cout << "Input size of column: " << endl; - cin >> size_column; - - size_el = size_row * size_column; - matrix_as_vector = Create_vector_and_init(size_row, size_column); - - if (matrix_as_vector == NULL) - { - cout << "Incorrect input data, try again" << endl; - return 0; - } - - if (size_row * size_column < 1000) - { - cout << "Current matrix:" << endl; - Show_vec(matrix_as_vector, size_row, size_column); - } - cout << endl; - - /* Подсчет суммы всех элементов матрицы (последовательная версия алгоритма): */ - - start_time_of_seq_alg = MPI_Wtime(); - - for (int i = 0; i < size_row * size_column; i++) - sum_el_seq += matrix_as_vector[i]; - - end_time_of_seq_alg = MPI_Wtime(); - time_seq_work_alg = end_time_of_seq_alg - start_time_of_seq_alg; - - cout << "Sum of all elements in matrix is " << sum_el_seq << " " << endl; - cout << "Spent time on the implementation of this algorithm (Sequence version)" << time_seq_work_alg << " ms " << endl; - cout << endl; - cout << "Num of procs: " << num_of_procs << endl; - - - - start_time_of_pp_alg = MPI_Wtime(); - - size_work_of_proc = size_el / num_of_procs; - - MPI_Bcast(&size_el, 1, MPI_INT, 0, MPI_COMM_WORLD); - for (int i = 1; i < num_of_procs; i++) - MPI_Send(matrix_as_vector + size_work_of_proc * (i - 1), size_work_of_proc, MPI_DOUBLE, i, 1, MPI_COMM_WORLD); - - - cout << "Process with rang " << curr_rank_proc << " start own job" << endl; - - for (int i = size_work_of_proc * (num_of_procs - 1); i < size_el; i++)//подсчитывает последнюю оставшуюся часть, если однопроцесорная система, считает все сама - temp_sum += matrix_as_vector[i]; - - sum_el_pp = temp_sum; - - MPI_Reduce(&partial_summ, &temp_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); - sum_el_pp += temp_sum; - - - end_time_of_pp_alg = MPI_Wtime(); - - time_pp_work_alg = end_time_of_pp_alg - start_time_of_pp_alg; - cout << "Sum of all elements in matrix is (Parallel version): " << sum_el_pp << endl; - cout << "Spent time on the implementation of this algorithm (Parallel version)" << time_pp_work_alg << " ms" << endl; - - if (sum_el_pp == sum_el_seq) - cout << "Results of parallel and sequence versions are identical! " << endl; - else - cout << "Results of parallel and sequence versions are not identical! " << endl; - - if (time_pp_work_alg <= time_seq_work_alg) - cout << "Parallel version faster, then sequence" << endl; - else - cout << "Sequence version faster, then parallel" << endl; - - delete[] matrix_as_vector; - } - else - { - double* recv_matrix_as_vector; - - MPI_Bcast(&size_el, 1, MPI_INT, 0, MPI_COMM_WORLD); - - size_work_of_proc = size_el / num_of_procs; - recv_matrix_as_vector = new double[size_work_of_proc]; - MPI_Recv(recv_matrix_as_vector, size_work_of_proc, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD, &stat); - - cout << "Process with rang " << curr_rank_proc << " start own job" << endl; - - for (int i = 0; i < size_work_of_proc; i++) - partial_summ += recv_matrix_as_vector[i]; - - MPI_Reduce(&partial_summ, &temp_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); - delete[] recv_matrix_as_vector; - } - MPI_Finalize(); - - /* Конец MPI кода */ - return 0; -} \ No newline at end of file diff --git a/1706/kuzhelev_aa/main.cpp b/1706/kuzhelev_aa/main.cpp new file mode 100644 index 0000000..8cf621f --- /dev/null +++ b/1706/kuzhelev_aa/main.cpp @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include +#include + +#define MAX_PATH 1000000000000000000 + +void Print_Matrix(const std::vector> &matrix) { + + if (matrix.size() <= 30) { + std::cout << std::endl; + for (const auto &row : matrix) { + for (const auto &num : row) { + std::cout << std::setw(5) << num << " "; + } + std::cout << std::endl; + } + std::cout << std::endl; + } + else { + std::ofstream ofs; + ofs.open("matrix.txt"); + for (const auto &row : matrix) { + for (const auto &num : row) { + ofs << num << " "; + } + ofs << std::endl; + } + } +} + +void Print_Vector(const std::vector& vec) { + std::cout << vec[0] << ": "; + for (size_t i = 1; i < vec[vec.size() - 1]; ++i) + std::cout << vec[i] << " "; + std::cout << std::endl; +} + +void Read_Matrix(std::vector> &matr,size_t size) { + matr.resize(size); + for (size_t i = 0; i < matr.size(); ++i) { + matr[i].resize(size, 0); + } + std::ifstream in; + in.open("../../graph.txt"); + int x, y, weight; + while (in >> x >> y >> weight) { + matr[x][y] = matr[y][x] = weight; + } + in.close(); +} + +int main() { + int t; + int begin_index; + std::ofstream out("log.txt"); + std::ifstream in("../../info.txt"); + in >> t >> begin_index; + if (t > 30)std::cout.rdbuf(out.rdbuf()); + std::cout << t << " " << begin_index; + std::vector> matr; + Read_Matrix(matr, t); + std::vector visited(t, 1); + std::vector min_path(t, MAX_PATH); + Print_Matrix(matr); + + min_path[begin_index] = 0; + uint64_t min_index, min, temp; + size_t i; + auto begin = std::chrono::steady_clock::now(); + do { + min_index = MAX_PATH; + min = MAX_PATH; + + #pragma omp parallel + { + int local_min_index = MAX_PATH; + int local_min = MAX_PATH; + + #pragma omp for + for (i = 0; i < visited.size(); ++i) { + if (visited[i] == 1 && (min_path[i] < local_min)) { + local_min = min_path[i]; + local_min_index = i; + } + } + #pragma omp critical + { + if (local_min < min) { + min = local_min; + min_index = local_min_index; + } + } + } + if (min_index != MAX_PATH) { + #pragma omp parallel for private(temp) + for (i = 0; i < visited.size(); ++i) { + if (matr[min_index][i] > 0) { + temp = min + matr[min_index][i]; + if (temp < min_path[i]) { + min_path[i] = temp; + } + } + + visited[min_index] = 0; + } + } + + } while (min_index < MAX_PATH); + + auto end = std::chrono::steady_clock::now(); + + auto elapsed_ms = std::chrono::duration_cast(end - begin); + + std::cout << std::endl; + + for (const auto row : min_path) { + std::cout << row << " "; + } + std::cerr << std::endl; + std::cerr << "Time spent to OpenMP algorithm:" << elapsed_ms.count() << " milliseconds" << std::endl; + return 0; + +} \ No newline at end of file From c97111e867efb14d98d510a21a0d9d60a803ca19 Mon Sep 17 00:00:00 2001 From: "Kuzhelev, Anton" Date: Fri, 15 May 2020 10:27:31 +0300 Subject: [PATCH 7/8] Added TBB version of Dijkstra algorithm --- .../{main.cpp => Dejkstra_TBB.cpp} | 78 ++++++++++--------- 1 file changed, 40 insertions(+), 38 deletions(-) rename 1706/kuzhelev_aa/{main.cpp => Dejkstra_TBB.cpp} (56%) diff --git a/1706/kuzhelev_aa/main.cpp b/1706/kuzhelev_aa/Dejkstra_TBB.cpp similarity index 56% rename from 1706/kuzhelev_aa/main.cpp rename to 1706/kuzhelev_aa/Dejkstra_TBB.cpp index 8cf621f..8137899 100644 --- a/1706/kuzhelev_aa/main.cpp +++ b/1706/kuzhelev_aa/Dejkstra_TBB.cpp @@ -1,11 +1,11 @@ -#include +#include #include #include #include #include #include -#define MAX_PATH 1000000000000000000 +#define MAX_PATH UINT64_MAX void Print_Matrix(const std::vector> &matrix) { @@ -38,7 +38,7 @@ void Print_Vector(const std::vector& vec) { std::cout << std::endl; } -void Read_Matrix(std::vector> &matr,size_t size) { +void Read_Matrix(std::vector> &matr, size_t size) { matr.resize(size); for (size_t i = 0; i < matr.size(); ++i) { matr[i].resize(size, 0); @@ -62,50 +62,52 @@ int main() { std::cout << t << " " << begin_index; std::vector> matr; Read_Matrix(matr, t); - std::vector visited(t, 1); + std::vector visited(t, 1); + std::vector min_vals(2, UINT64_MAX); std::vector min_path(t, MAX_PATH); Print_Matrix(matr); min_path[begin_index] = 0; uint64_t min_index, min, temp; - size_t i; + tbb::mutex mut; + int size = tbb::task_scheduler_init::default_num_threads(); + std::cerr << "Num of threads:" << size << std::endl; auto begin = std::chrono::steady_clock::now(); do { - min_index = MAX_PATH; - min = MAX_PATH; - - #pragma omp parallel - { - int local_min_index = MAX_PATH; - int local_min = MAX_PATH; - - #pragma omp for - for (i = 0; i < visited.size(); ++i) { - if (visited[i] == 1 && (min_path[i] < local_min)) { - local_min = min_path[i]; - local_min_index = i; + min_vals = tbb::parallel_reduce( + tbb::blocked_range(0, t), + std::vector(2) = {UINT64_MAX, UINT64_MAX}, + [&](const tbb::blocked_range& v, std::vector local_min_vals) { + for (int i = 0; i < v.end(); ++i) { + if (visited[i] == 1 && min_path[i] < local_min_vals[0]) { + local_min_vals[0] = min_path[i]; + local_min_vals[1] = i; + } } - } - #pragma omp critical - { - if (local_min < min) { - min = local_min; - min_index = local_min_index; + return local_min_vals; + }, + [&](std::vector x, std::vector y) { + if (x[0] < y[0]) { + return x; } - } - } + return y; + }); + min = min_vals[0]; + min_index = min_vals[1]; if (min_index != MAX_PATH) { - #pragma omp parallel for private(temp) - for (i = 0; i < visited.size(); ++i) { - if (matr[min_index][i] > 0) { - temp = min + matr[min_index][i]; - if (temp < min_path[i]) { - min_path[i] = temp; + tbb::parallel_for( + tbb::blocked_range(0,t), + [&](const tbb::blocked_range& v) { + for (int i = 0; i < v.end(); ++i) { + if (matr[min_index][i] > 0) { + mut.lock(); + temp = min + matr[min_index][i]; + min_path[i] = min_path[i] > temp ? temp : min_path[i]; + mut.unlock(); + } + visited[min_index] = 0; } - } - - visited[min_index] = 0; - } + }); } } while (min_index < MAX_PATH); @@ -120,7 +122,7 @@ int main() { std::cout << row << " "; } std::cerr << std::endl; - std::cerr << "Time spent to OpenMP algorithm:" << elapsed_ms.count() << " milliseconds" << std::endl; + std::cerr << "Time spent to TBB algorithm:" << elapsed_ms.count() << " milliseconds" << std::endl; return 0; - + } \ No newline at end of file From bf94d614d71d9d794868cc9e1d28741e4335d413 Mon Sep 17 00:00:00 2001 From: "Kuzhelev, Anton" Date: Tue, 19 May 2020 17:51:41 +0300 Subject: [PATCH 8/8] Changed algorithm for TBB version --- 1706/kuzhelev_aa/Dejkstra_TBB.cpp | 368 ++++++++++++++++++++++++------ 1 file changed, 292 insertions(+), 76 deletions(-) diff --git a/1706/kuzhelev_aa/Dejkstra_TBB.cpp b/1706/kuzhelev_aa/Dejkstra_TBB.cpp index 8137899..28a0dbe 100644 --- a/1706/kuzhelev_aa/Dejkstra_TBB.cpp +++ b/1706/kuzhelev_aa/Dejkstra_TBB.cpp @@ -1,21 +1,43 @@ -#include #include +#include #include #include #include -#include +#include +#include +#include +#include +#include +#include +#include +#include -#define MAX_PATH UINT64_MAX +#define MAX_PATH 1000000000000000000 -void Print_Matrix(const std::vector> &matrix) { +void Generate_matrix(std::vector &matr, int size) { + matr.resize(size*size); + int t = size; + std::mt19937 random(static_cast(time(0))); + for (size_t i = 0; i + 1 < matr.size(); ++i) { + if (i%size == 0 && !i) { + ++t; + } + if (i%t == 0 || matr[i]) { + continue; + } + matr[i] = random() % 1000; + } +} - if (matrix.size() <= 30) { - std::cout << std::endl; - for (const auto &row : matrix) { - for (const auto &num : row) { - std::cout << std::setw(5) << num << " "; +void Print_Matrix(const std::vector &matrix) { + + int size = sqrt(matrix.size()); + if (matrix.size() <= 900) { + for (int i = 0; i < matrix.size(); ++i) { + if (i%size == 0 && i) { + std::cout << std::endl; } - std::cout << std::endl; + std::cout << std::setw(5) << matrix[i] << " "; } std::cout << std::endl; } @@ -23,11 +45,28 @@ void Print_Matrix(const std::vector> &matrix) { std::ofstream ofs; ofs.open("matrix.txt"); for (const auto &row : matrix) { - for (const auto &num : row) { - ofs << num << " "; - } - ofs << std::endl; + ofs << row << " "; + + } + ofs << std::endl; + } +} + +void Input_Matrix(std::vector &matr, int size) { + int t = size; + matr.resize(size*size); + for (size_t i = 0; i < matr.size(); ++i) { + if (i%size == 0 && !i) { + ++t; + } + if (i%t == 0) { + continue; } + int num; + std::cerr << "Insert distance " << i + 1 << "-" << i * size + 1 << ":"; + std::cin >> num; + matr[i*size] = num; + } } @@ -38,91 +77,268 @@ void Print_Vector(const std::vector& vec) { std::cout << std::endl; } -void Read_Matrix(std::vector> &matr, size_t size) { - matr.resize(size); - for (size_t i = 0; i < matr.size(); ++i) { - matr[i].resize(size, 0); - } +void Read_Matrix(std::vector &matr, size_t size, const std::string &matr_info) { + matr.resize(size*size); + std::ifstream in; - in.open("../../graph.txt"); + in.open(matr_info); int x, y, weight; while (in >> x >> y >> weight) { - matr[x][y] = matr[y][x] = weight; + matr[x*size + y] = matr[y*size + x] = weight; } in.close(); } -int main() { - int t; - int begin_index; - std::ofstream out("log.txt"); - std::ifstream in("../../info.txt"); - in >> t >> begin_index; - if (t > 30)std::cout.rdbuf(out.rdbuf()); - std::cout << t << " " << begin_index; - std::vector> matr; - Read_Matrix(matr, t); - std::vector visited(t, 1); +void help(const char *name, const char *message) { + std::cout << "\n\n" << std::string(message) + + "\nThis is Dijkstra algorithm application.\n\n" + + "Please provide arguments in the following format:\n\n" + + " $ " + name + " \n\n" + + "Where are integer numbers, " + + "and and are strings.\n"; +} + +uint64_t minDistance(std::vector dist, std::vector visited) { + uint64_t min = UINT64_MAX, min_index; + + for (size_t i = 0; i < visited.size(); ++i) { + if (!visited[i] && dist[i] <= min) { + min = dist[i]; + min_index = i; + } + } + return min_index; +} + +void PrintPath(const std::vector &parent, int i) { + if (parent[i] == -1) { + return; + } + PrintPath(parent, parent[i]); + std::cout << " " << i; +} + +void PrintSolution(const std::vector &min_dist, const std::vector &parent, const int &begin) { + std::cout << "Vertex" << std::setw(10) << "Distance" << std::setw(10) << "Path\n"; + for (int i = 0; i < parent.size(); ++i) { + if (i == begin)continue; + std::cout << begin << " -> " << i << "\t" << min_dist[i] << "\t\t" << begin; + PrintPath(parent, i); + std::cout << std::endl; + } +} + +void Sequence_Alg(const std::vector &matr, std::vector &min_path, std::vector &parent) { + const int t = sqrt(matr.size()); + std::vector visited(t, false); + + for (int count = 0; count < t - 1; ++count) { + int u = minDistance(min_path, visited); + + visited[u] = true; + + for (int i = 0; i < t; ++i) { + if (!visited[i] && matr[u*t + i] && min_path[u] + matr[u*t + i] < min_path[i]) { + parent[i] = u; + min_path[i] = min_path[u] + matr[u*t + i]; + } + } + } +} + +int minDistanceOMP(std::vector dist, std::vector visited) { + uint64_t min = UINT64_MAX, min_index; + +#pragma omp parallel + { + int local_min = UINT64_MAX, local_min_index; + +#pragma omp for + for (int i = 0; i < visited.size(); ++i) { + if (!visited[i] && dist[i] < local_min) { + local_min = dist[i]; + local_min_index = i; + } + } +#pragma omp critical + if (local_min < min) { + min = local_min; + min_index = local_min_index; + } + } + + return min_index; +} + +void OpenMP_Alg(const std::vector &matr, std::vector &min_path, std::vector &parent) { + const int t = sqrt(matr.size()); + + std::vector visited(t, false); + + for (int count = 0; count < t - 1; ++count) { + int u = minDistanceOMP(min_path, visited); + visited[u] = true; +#pragma omp parallel for + for (int i = 0; i < t; ++i) { + if (!visited[i] && matr[u*t + i] && min_path[u] + matr[u*t + i] < min_path[i]) { + min_path[i] = min_path[u] + matr[u*t + i]; + parent[i] = u; + } + } + } +} + +int minDistanceTBB(std::vector dist, std::vector visited) { std::vector min_vals(2, UINT64_MAX); - std::vector min_path(t, MAX_PATH); - Print_Matrix(matr); - min_path[begin_index] = 0; - uint64_t min_index, min, temp; - tbb::mutex mut; + min_vals = tbb::parallel_reduce( + tbb::blocked_range(0, visited.size()), + std::vector(2) = {UINT64_MAX, UINT64_MAX}, + [&](const tbb::blocked_range& v, std::vector local_min_vals) { + for (int i = 0; i < v.end(); ++i) { + if (!visited[i] && dist[i] < local_min_vals[0]) { + local_min_vals[0] = dist[i]; + local_min_vals[1] = i; + } + + } + return local_min_vals; + }, + [&](std::vector x, std::vector y) { + return x[0] < y[0] ? x : y; + } + ); + return min_vals[1]; +} + +void TBB_Alg(const std::vector &matr, std::vector &min_path, std::vector &parent) { + const int t = sqrt(matr.size()); + int size = tbb::task_scheduler_init::default_num_threads(); std::cerr << "Num of threads:" << size << std::endl; - auto begin = std::chrono::steady_clock::now(); - do { - min_vals = tbb::parallel_reduce( - tbb::blocked_range(0, t), - std::vector(2) = {UINT64_MAX, UINT64_MAX}, - [&](const tbb::blocked_range& v, std::vector local_min_vals) { + + std::vector visited(t, false); + tbb::mutex mutex; + + for (int count = 0; count < t - 1; ++count) { + int u = minDistanceTBB(min_path, visited); + visited[u] = true; + tbb::parallel_for( + tbb::blocked_range(0,t), + [&](const tbb::blocked_range& v) { for (int i = 0; i < v.end(); ++i) { - if (visited[i] == 1 && min_path[i] < local_min_vals[0]) { - local_min_vals[0] = min_path[i]; - local_min_vals[1] = i; + if (!visited[i] && matr[u*t + i] && min_path[u] + matr[u*t + i] < min_path[i]) { + mutex.lock(); + min_path[i] = min_path[u] + matr[u*t + i]; + parent[i] = u; + mutex.unlock(); } } - return local_min_vals; - }, - [&](std::vector x, std::vector y) { - if (x[0] < y[0]) { - return x; - } - return y; - }); - min = min_vals[0]; - min_index = min_vals[1]; - if (min_index != MAX_PATH) { - tbb::parallel_for( - tbb::blocked_range(0,t), - [&](const tbb::blocked_range& v) { - for (int i = 0; i < v.end(); ++i) { - if (matr[min_index][i] > 0) { - mut.lock(); - temp = min + matr[min_index][i]; - min_path[i] = min_path[i] > temp ? temp : min_path[i]; - mut.unlock(); - } - visited[min_index] = 0; - } - }); - } + } + ); + } + +} - } while (min_index < MAX_PATH); +int main(int argc, char *argv[]) { + if (argc != 5) { + help(argv[0], ""); + return 1; + } + int begin_index; + int t; + try { + t = std::stoi(argv[1]); + begin_index = std::stoi(argv[2]); + } + catch (std::invalid_argument) { + help(argv[0], "ERROR: Wrong matrix size value or begin index value.\n\n"); + return 1; + } + if (begin_index >= t) { + help(argv[0], "ERROR: Wrong begin index value.\n\n"); + return 1; + } + const std::string log = std::string(argv[3]); + const std::string info = std::string(argv[4]); + std::ofstream out(log); + std::vector matr; + + if (t < 1) { + help(argv[0], "ERROR: Size of matrix can't be less than 1.\\n"); + return 1; + } + int key = 1; + if (t > 30)std::cout.rdbuf(out.rdbuf()); + std::cerr << "Input matrix by yourself [1] [default]:\nGenerate matrix(Full graph) [2] :\n\nChoose one option:"; + std::cin >> key; + switch (key) { + case 2: + Generate_matrix(matr, t); + break; + default: + Input_Matrix(matr, t); + } + + std::vector min_path(t, MAX_PATH); + std::vector parent(t, -1); + Print_Matrix(matr); + + + + min_path[begin_index] = 0; + + auto begin = std::chrono::steady_clock::now(); + Sequence_Alg(matr, min_path, parent); auto end = std::chrono::steady_clock::now(); auto elapsed_ms = std::chrono::duration_cast(end - begin); - std::cout << std::endl; + std::vector omp_min_path(t, MAX_PATH); + std::vector omp_parent(t, -1); + + omp_min_path[begin_index] = 0; + begin = std::chrono::steady_clock::now(); + OpenMP_Alg(matr, omp_min_path, omp_parent); + end = std::chrono::steady_clock::now(); + + auto omp_elapsed_ms = std::chrono::duration_cast(end - begin); + + std::vector tbb_min_path(t, MAX_PATH); + std::vector tbb_parent(t, -1); + + tbb_min_path[begin_index] = 0; + + begin = std::chrono::steady_clock::now(); + TBB_Alg(matr, tbb_min_path, tbb_parent); + end = std::chrono::steady_clock::now(); + + for (const auto row : tbb_parent) { + std::cout << row << " "; + } + + auto tbb_elapsed_ms = std::chrono::duration_cast(end - begin); + + std::cout << "\n------------------------Sequence algorithm:------------------------\n"; + PrintSolution(min_path, parent, begin_index); + + std::cout << "\n------------------------OpenMP algorithm:------------------------\n"; + PrintSolution(omp_min_path, omp_parent, begin_index); + + std::cout << "\n------------------------TBB algorithm:------------------------\n"; + PrintSolution(tbb_min_path, tbb_parent, begin_index); + + std::cout << std::endl; for (const auto row : min_path) { std::cout << row << " "; } - std::cerr << std::endl; - std::cerr << "Time spent to TBB algorithm:" << elapsed_ms.count() << " milliseconds" << std::endl; + std::cout << std::endl; + std::ofstream out1(info); + out1 << matr.size() << " " << begin_index; + std::cerr << "Time spent to sequence algorithm:" << elapsed_ms.count() << " milliseconds" << std::endl; + std::cerr << "Time spent to OpenMP algorithm:" << omp_elapsed_ms.count() << " milliseconds" << std::endl; + std::cerr << "Time spent to TBB algorithm:" << tbb_elapsed_ms.count() << " milliseconds" << std::endl; return 0; - } \ No newline at end of file