generated from learning-process/parallel_programming_course
-
Notifications
You must be signed in to change notification settings - Fork 18
Литвяков Даниил. Технология SEQ-MPI. Нахождение минимальных значений по столбцам матрицы. Вариант 18 #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
DanyaLitva
wants to merge
23
commits into
learning-process:master
from
DanyaLitva:litvyakov_d_min_val_by_col
Closed
Литвяков Даниил. Технология SEQ-MPI. Нахождение минимальных значений по столбцам матрицы. Вариант 18 #63
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
7ad02ef
without perf test task 1
DanyaLitva d05541e
with Perf
DanyaLitva e7a2e40
report upd
DanyaLitva 9407b8e
Update report.md
DanyaLitva b2e24dd
Update report.md
DanyaLitva ae1f3ab
clang-format
DanyaLitva 12ea5b7
clang fix
DanyaLitva 8200983
clang fix2
DanyaLitva 9ce1b9f
clang fix3
DanyaLitva 82680b5
Update main.cpp
DanyaLitva 44f9af7
Update main.cpp
DanyaLitva 1d90363
Update main.cpp
DanyaLitva 6ac282e
clang fix 4
DanyaLitva d5410fb
Update main.cpp
DanyaLitva a8aa1c9
review fix
DanyaLitva 2ec362d
review fix report
DanyaLitva a2a9b12
Update min_val_by_col.cpp
DanyaLitva d35ba53
Update min_val_by_col.cpp
DanyaLitva 5eb5e72
codecov fix
DanyaLitva 90c5a87
Update min_val_by_col.cpp
DanyaLitva f610083
Update min_val_by_col.cpp
DanyaLitva 4d3aaaf
Update min_val_by_col.cpp
DanyaLitva 89b52ca
Update min_val_by_col.cpp
DanyaLitva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
tasks/litvyakov_d_min_val_by_col/common/include/common.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #pragma once | ||
|
|
||
| #include <tuple> | ||
| #include <vector> | ||
|
|
||
| #include "task/include/task.hpp" | ||
|
|
||
| namespace litvyakov_d_min_val_by_col { | ||
|
|
||
| using InType = std::tuple<int, int, std::vector<double>>; | ||
| using OutType = std::vector<double>; | ||
| using TestType = std::tuple<int, int, std::vector<double>, std::vector<double>>; | ||
| using BaseTask = ppc::task::Task<InType, OutType>; | ||
|
|
||
| } // namespace litvyakov_d_min_val_by_col |
2,002 changes: 2,002 additions & 0 deletions
2,002
tasks/litvyakov_d_min_val_by_col/data/matrix_2000x2000
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "student": { | ||
| "first_name": "Даниил", | ||
| "last_name": "Литвяков", | ||
| "middle_name": "Дмитриевич", | ||
| "group_number": "3823Б1ПМоп3", | ||
| "task_number": "1" | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
tasks/litvyakov_d_min_val_by_col/mpi/include/min_val_by_col.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #pragma once | ||
|
|
||
| #include "litvyakov_d_min_val_by_col/common/include/common.hpp" | ||
| #include "task/include/task.hpp" | ||
|
|
||
| namespace litvyakov_d_min_val_by_col { | ||
|
|
||
| class LitvyakovDMinValByColMPI : public BaseTask { | ||
| public: | ||
| static constexpr ppc::task::TypeOfTask GetStaticTypeOfTask() { | ||
| return ppc::task::TypeOfTask::kMPI; | ||
| } | ||
| explicit LitvyakovDMinValByColMPI(const InType &in); | ||
|
|
||
| private: | ||
| bool ValidationImpl() override; | ||
| bool PreProcessingImpl() override; | ||
| bool RunImpl() override; | ||
| bool PostProcessingImpl() override; | ||
| }; | ||
|
|
||
| } // namespace litvyakov_d_min_val_by_col |
80 changes: 80 additions & 0 deletions
80
tasks/litvyakov_d_min_val_by_col/mpi/src/min_val_by_col.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| #include "litvyakov_d_min_val_by_col/mpi/include/min_val_by_col.hpp" | ||
|
|
||
| #include <mpi.h> | ||
|
|
||
| #include <algorithm> | ||
| #include <limits> | ||
| #include <type_traits> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include "litvyakov_d_min_val_by_col/common/include/common.hpp" | ||
|
|
||
| namespace litvyakov_d_min_val_by_col { | ||
|
|
||
| LitvyakovDMinValByColMPI::LitvyakovDMinValByColMPI(const InType &in) { | ||
| SetTypeOfTask(GetStaticTypeOfTask()); | ||
| GetInput() = in; | ||
| } | ||
|
|
||
| bool LitvyakovDMinValByColMPI::ValidationImpl() { | ||
| int world_rank = 0; | ||
| MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); | ||
| if (world_rank == 0) { | ||
| const auto &n = std::get<0>(GetInput()); | ||
| const auto &m = std::get<1>(GetInput()); | ||
| const auto &mat = std::get<2>(GetInput()); | ||
| return ((n > 0) && (m > 0) && std::cmp_equal(mat.size(), (n * m))); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool LitvyakovDMinValByColMPI::PreProcessingImpl() { | ||
| const auto &m = std::get<1>(GetInput()); | ||
| auto &mat = std::get<2>(GetInput()); | ||
| using Type = std::remove_reference_t<decltype(mat[0])>; | ||
| OutType &res = GetOutput(); | ||
| res.resize(m, std::numeric_limits<Type>::max()); | ||
| return std::cmp_equal(res.size(), m); | ||
| } | ||
|
|
||
| bool LitvyakovDMinValByColMPI::RunImpl() { | ||
| const auto &n = std::get<0>(GetInput()); | ||
| const auto &m = std::get<1>(GetInput()); | ||
| auto &mat = std::get<2>(GetInput()); | ||
|
|
||
| int world_size = 0; | ||
| int world_rank = 0; | ||
| MPI_Comm_size(MPI_COMM_WORLD, &world_size); | ||
| MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); | ||
|
|
||
| using Type = std::remove_reference_t<decltype(mat[0])>; | ||
| MPI_Datatype datatype = MPI_DOUBLE; | ||
|
|
||
| OutType &res = GetOutput(); | ||
| OutType proc_res(m, std::numeric_limits<Type>::max()); | ||
|
|
||
| for (int j = world_rank; j < m; j += world_size) { | ||
| Type min_val = std::numeric_limits<Type>::max(); | ||
| for (int i = 0; i < n; ++i) { | ||
| min_val = std::min(mat[(j * n) + i], min_val); | ||
| } | ||
| proc_res[j] = min_val; | ||
| } | ||
|
|
||
| MPI_Allreduce(proc_res.data(), res.data(), m, datatype, MPI_MIN, MPI_COMM_WORLD); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool LitvyakovDMinValByColMPI::PostProcessingImpl() { | ||
| int world_rank = 0; | ||
| MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); | ||
| if (world_rank == 0) { | ||
| const auto &m = std::get<1>(GetInput()); | ||
| return std::cmp_equal(GetOutput().size(), m); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| } // namespace litvyakov_d_min_val_by_col | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| ## Министерство науки и высшего образования Российской Федерации | ||
| ## Федеральное государственное автономное образовательное учреждение высшего образования | ||
| ## **«Национальный исследовательский Нижегородский государственный университет им. Н.И. Лобачевского»** | ||
| ### Институт информационных технологий, математики и механики | ||
|
|
||
| ### **Направление подготовки:** «Прикладная математика и информатика» | ||
|
|
||
| --- | ||
|
|
||
| # Отчёт | ||
| ### По задаче | ||
| **Нахождение минимальных значений по столбцам матрицы** | ||
| **Вариант №18** | ||
|
|
||
| **Выполнил:** | ||
| Студент группы 3823Б1ПМоп3 | ||
| **Литвяков Д.Д.** | ||
|
|
||
| --- | ||
|
|
||
| ## 1. Введение | ||
| Последовательные программы полностью используют аппаратные возможности. Параллельные версии алгоритмов могут ускорить работу программы, в том числе при работе с большими матрицами, за счёт одновременного выполнения нескольких задач на разных ядрах. | ||
|
|
||
| ## 2. Постановка задачи | ||
| Требуется найти минимальные значения по каждому столбцу матрицы размером n*m. | ||
| Вход: | ||
| n — число строк (int) | ||
| m — число столбцов (int) | ||
| std::vector<double> длины n*m | ||
| Выход: | ||
| std::vector<double> длины m — минимумы по каждому столбцу. | ||
|
|
||
| ## 3. Базовый Алгоритм (Последовательный) | ||
| ``` | ||
| for j in 0..m-1: | ||
| min_val = +inf | ||
| for i in 0..n-1: | ||
| v = mat[j*n + i] | ||
| if v < min_val: | ||
| min_val = v | ||
| res[j] = min_val | ||
| ``` | ||
|
|
||
| ## 4. Схема распараллеливания | ||
| В параллельной версии каждому процессу достаются столбцы следующим образом: 1 процесс - 1 столбец, 2 процесс - 2 столбец и так далее. После подсчёта локальных минимумов в столбцах результат объединяются с помощью MPI_Allreduce(proc_res.data(), res.data(), m, datatype, MPI_MIN, MPI_COMM_WORLD). | ||
|
|
||
| ## 5. Детали реализации | ||
| Каждый процесс хранит промежуточный результат в локальных векторах инициализированных максимальными значениями размера m. | ||
|
|
||
| ## 6. Характеристики локальной машины | ||
| - Аппаратное обеспечение/Операционная система: AMD Ryzen 5 5600, 32 GB DDR4-3200, Windows 10. | ||
| - Компилятор: MSVC, Тип сборки: Release. | ||
| - Данные: сгенерированная матрица матрица размерами 4000 на 4000 с элементами от -1e-6 до 1e-6. | ||
|
||
|
|
||
| ## 7. Результаты | ||
|
|
||
| ### 7.1 Корректность | ||
| Функциональные тесты: программа была проверена для типовых матриц. | ||
|
|
||
| ### 7.2 Производительность | ||
| Для теста производительности была использована сгенерированная матрица размерами 4000 на 4000 с элементами от -1e-6 до 1e-6. Результаты были записаны в таблицу: | ||
|
|
||
| | Mode | Count | Time, s | Speedup | Efficiency | | ||
| |-------------|-------|---------|---------|------------| | ||
| | seq | 1 | 0.0058 | 1.00 | N/A | | ||
| | mpi | 2 | 0.0044 | 1.31 | 31.0% | | ||
| | mpi | 4 | 0.0030 | 1.93 | 31.0% | | ||
|
|
||
| ## 8. Выводы | ||
| Реализована последовательная и параллельная версия алгоритма поиска минимума по столбцам. | ||
|
|
||
| ## 9. Список литературы | ||
| 1. https://learn.microsoft.com/ru-ru/message-passing-interface/microsoft-mpi | ||
22 changes: 22 additions & 0 deletions
22
tasks/litvyakov_d_min_val_by_col/seq/include/min_val_by_col.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #pragma once | ||
|
|
||
| #include "litvyakov_d_min_val_by_col/common/include/common.hpp" | ||
| #include "task/include/task.hpp" | ||
|
|
||
| namespace litvyakov_d_min_val_by_col { | ||
|
|
||
| class LitvyakovDMinValByColSEQ : public BaseTask { | ||
| public: | ||
| static constexpr ppc::task::TypeOfTask GetStaticTypeOfTask() { | ||
| return ppc::task::TypeOfTask::kSEQ; | ||
| } | ||
| explicit LitvyakovDMinValByColSEQ(const InType &in); | ||
|
|
||
| private: | ||
| bool ValidationImpl() override; | ||
| bool PreProcessingImpl() override; | ||
| bool RunImpl() override; | ||
| bool PostProcessingImpl() override; | ||
| }; | ||
|
|
||
| } // namespace litvyakov_d_min_val_by_col |
53 changes: 53 additions & 0 deletions
53
tasks/litvyakov_d_min_val_by_col/seq/src/min_val_by_col.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #include "litvyakov_d_min_val_by_col/seq/include/min_val_by_col.hpp" | ||
|
|
||
| #include <algorithm> | ||
| #include <limits> | ||
| #include <type_traits> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include "litvyakov_d_min_val_by_col/common/include/common.hpp" | ||
|
|
||
| namespace litvyakov_d_min_val_by_col { | ||
|
|
||
| LitvyakovDMinValByColSEQ::LitvyakovDMinValByColSEQ(const InType &in) { | ||
| SetTypeOfTask(GetStaticTypeOfTask()); | ||
| GetInput() = in; | ||
| } | ||
|
|
||
| bool LitvyakovDMinValByColSEQ::ValidationImpl() { | ||
| const auto &n = std::get<0>(GetInput()); | ||
| const auto &m = std::get<1>(GetInput()); | ||
| const auto &mat = std::get<2>(GetInput()); | ||
| return ((n > 0) && (m > 0) && std::cmp_equal(mat.size(), (n * m))); | ||
| } | ||
|
|
||
| bool LitvyakovDMinValByColSEQ::PreProcessingImpl() { | ||
| OutType &res = GetOutput(); | ||
| const auto &m = std::get<1>(GetInput()); | ||
| const auto &mat = std::get<2>(GetInput()); | ||
| using Type = std::remove_reference_t<decltype(mat[0])>; | ||
| res.resize(m, std::numeric_limits<Type>::max()); | ||
| return (std::cmp_equal(res.size(), m)); | ||
| } | ||
|
|
||
| bool LitvyakovDMinValByColSEQ::RunImpl() { | ||
| OutType &res = GetOutput(); | ||
| const auto &n = std::get<0>(GetInput()); | ||
| const auto &m = std::get<1>(GetInput()); | ||
| auto &mat = std::get<2>(GetInput()); | ||
|
|
||
| for (int j = 0; j < m; ++j) { | ||
| for (int i = 0; i < n; ++i) { | ||
| res[j] = std::min(res[j], mat[(j * n) + i]); | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| bool LitvyakovDMinValByColSEQ::PostProcessingImpl() { | ||
| return true; | ||
| } | ||
|
|
||
| } // namespace litvyakov_d_min_val_by_col |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "tasks_type": "processes", | ||
| "tasks": { | ||
| "mpi": "enabled", | ||
| "seq": "enabled" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| InheritParentConfig: true | ||
|
|
||
| Checks: > | ||
| -modernize-loop-convert, | ||
| -cppcoreguidelines-avoid-goto, | ||
| -cppcoreguidelines-avoid-non-const-global-variables, | ||
| -misc-use-anonymous-namespace, | ||
| -modernize-use-std-print, | ||
| -modernize-type-traits | ||
| CheckOptions: | ||
| - key: readability-function-cognitive-complexity.Threshold | ||
| value: 50 # Relaxed for tests |
69 changes: 69 additions & 0 deletions
69
tasks/litvyakov_d_min_val_by_col/tests/functional/main.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include <array> | ||
| #include <cfloat> | ||
| #include <cstddef> | ||
| #include <string> | ||
| #include <tuple> | ||
| #include <vector> | ||
|
|
||
| #include "litvyakov_d_min_val_by_col/common/include/common.hpp" | ||
| #include "litvyakov_d_min_val_by_col/mpi/include/min_val_by_col.hpp" | ||
| #include "litvyakov_d_min_val_by_col/seq/include/min_val_by_col.hpp" | ||
| #include "util/include/func_test_util.hpp" | ||
| #include "util/include/util.hpp" | ||
|
|
||
| namespace litvyakov_d_min_val_by_col { | ||
|
|
||
| class LitvyakovDRunFuncFindMinValByCol : public ppc::util::BaseRunFuncTests<InType, OutType, TestType> { | ||
| public: | ||
| static std::string PrintTestParam(const TestType &test_param) { | ||
| return "matrix_" + std::to_string(std::get<0>(test_param)) + "x" + std::to_string(std::get<1>(test_param)); | ||
| } | ||
|
|
||
| protected: | ||
| void SetUp() override { | ||
| params_ = std::get<static_cast<std::size_t>(ppc::util::GTestParamIndex::kTestParams)>(GetParam()); | ||
| input_data_ = std::make_tuple(std::get<0>(params_), std::get<1>(params_), std::get<2>(params_)); | ||
| output_vector_ = std::get<3>(params_); | ||
| } | ||
|
|
||
| bool CheckTestOutputData(OutType &output_data) final { | ||
| return output_data == output_vector_; | ||
| } | ||
|
|
||
| InType GetTestInputData() final { | ||
| return input_data_; | ||
| } | ||
|
|
||
| private: | ||
| TestType params_; | ||
| InType input_data_; | ||
| OutType output_vector_; | ||
| }; | ||
|
|
||
| const std::array<TestType, 8> kTestParam = { | ||
| TestType(3, 3, {-1, -2, -5, -4, -3, -8, -7, -6, -9}, {-5, -8, -9}), | ||
| TestType(2, 4, {0, 1, 5, 3, 2, 0, 8, 7}, {0, 3, 0, 7}), | ||
| TestType(1, 1, {-1.}, {-1.}), | ||
| TestType(4, 2, {8, 6, 4, 2, 7, 5, 3, 1}, {2, 1}), | ||
| TestType(1, 5, {9, 8, 7, 6, 5}, {9, 8, 7, 6, 5}), | ||
| TestType(5, 1, {3, -1, 7, 0, 2}, {-1}), | ||
| TestType(4, 3, {1.1, 4.0, 0.02, 7, 9, 2.0, 5, 1, 3, 8, 6, 4}, {0.02, 1, 3}), | ||
| TestType(2, 2, {1, 50, 100, -10.0001}, {1, -10.0001}), | ||
| }; | ||
|
|
||
| TEST_P(LitvyakovDRunFuncFindMinValByCol, FindMinInColumns) { | ||
| ExecuteTest(GetParam()); | ||
| } | ||
|
|
||
| const auto kTestTasksList = std::tuple_cat( | ||
| ppc::util::AddFuncTask<LitvyakovDMinValByColMPI, InType>(kTestParam, PPC_SETTINGS_litvyakov_d_min_val_by_col), | ||
| ppc::util::AddFuncTask<LitvyakovDMinValByColSEQ, InType>(kTestParam, PPC_SETTINGS_litvyakov_d_min_val_by_col)); | ||
|
|
||
| const auto kGtestValues = ppc::util::ExpandToValues(kTestTasksList); | ||
|
|
||
| const auto kPerfTestName = LitvyakovDRunFuncFindMinValByCol::PrintFuncTestName<LitvyakovDRunFuncFindMinValByCol>; | ||
|
|
||
| INSTANTIATE_TEST_SUITE_P(MinValByColumns, LitvyakovDRunFuncFindMinValByCol, kGtestValues, kPerfTestName); | ||
| } // namespace litvyakov_d_min_val_by_col |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see data scatter from rank 0 to other ranks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Исправлено
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aobolensk