generated from learning-process/parallel_programming_course
-
Notifications
You must be signed in to change notification settings - Fork 80
Агафонов Илья. Технология SEQ-MPI. Решетка-тор. Вариант 9 #289
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
Open
ilya-agafonov
wants to merge
4
commits into
learning-process:master
Choose a base branch
from
ilya-agafonov:agafonov_i_torus_grid
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,21 @@ | ||
| #pragma once | ||
|
|
||
| #include <string> | ||
| #include <tuple> | ||
|
|
||
| #include "task/include/task.hpp" | ||
|
|
||
| namespace agafonov_i_torus_grid { | ||
|
|
||
| struct TorusTaskData { | ||
| int value; | ||
| int source_rank; | ||
| int dest_rank; | ||
| }; | ||
|
|
||
| using InType = TorusTaskData; | ||
| using OutType = int; | ||
| using TestType = std::tuple<int, std::string>; | ||
| using BaseTask = ppc::task::Task<InType, OutType>; | ||
|
|
||
| } // namespace agafonov_i_torus_grid |
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ФИ1", | ||
| "task_number": "2" | ||
| } | ||
| } |
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,24 @@ | ||
| #pragma once | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "agafonov_i_torus_grid/common/include/common.hpp" | ||
| #include "task/include/task.hpp" | ||
|
|
||
| namespace agafonov_i_torus_grid { | ||
|
|
||
| class TorusGridTaskMPI : public BaseTask { | ||
| public: | ||
| static constexpr ppc::task::TypeOfTask GetStaticTypeOfTask() { | ||
| return ppc::task::TypeOfTask::kMPI; | ||
| } | ||
| explicit TorusGridTaskMPI(const InType &in); | ||
|
|
||
| private: | ||
| bool ValidationImpl() override; | ||
| bool PreProcessingImpl() override; | ||
| bool RunImpl() override; | ||
| bool PostProcessingImpl() override; | ||
| }; | ||
|
|
||
| } // namespace agafonov_i_torus_grid |
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,82 @@ | ||
| #include "agafonov_i_torus_grid/mpi/include/ops_mpi.hpp" | ||
|
|
||
| #include <mpi.h> | ||
|
|
||
| #include <array> | ||
| #include <cmath> | ||
|
|
||
| #include "agafonov_i_torus_grid/common/include/common.hpp" | ||
|
|
||
| namespace agafonov_i_torus_grid { | ||
|
|
||
| TorusGridTaskMPI::TorusGridTaskMPI(const InType &in) { | ||
| SetTypeOfTask(GetStaticTypeOfTask()); | ||
| GetInput() = in; | ||
| } | ||
|
|
||
| bool TorusGridTaskMPI::ValidationImpl() { | ||
| int world_size = 0; | ||
| MPI_Comm_size(MPI_COMM_WORLD, &world_size); | ||
| auto data = GetInput(); | ||
|
|
||
| return (world_size >= 1 && data.source_rank >= 0 && data.source_rank < world_size && data.dest_rank >= 0 && | ||
| data.dest_rank < world_size); | ||
| } | ||
|
|
||
| bool TorusGridTaskMPI::PreProcessingImpl() { | ||
| GetOutput() = 0; | ||
| return true; | ||
| } | ||
|
|
||
| bool TorusGridTaskMPI::RunImpl() { | ||
| int world_size = 0; | ||
| int world_rank = 0; | ||
| MPI_Comm_size(MPI_COMM_WORLD, &world_size); | ||
| MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); | ||
|
|
||
| auto data = GetInput(); | ||
| int res = 0; | ||
|
|
||
| std::array<int, 2> dims = {0, 0}; | ||
| MPI_Dims_create(world_size, 2, dims.data()); | ||
| std::array<int, 2> periods = {1, 1}; | ||
| MPI_Comm torus_comm = MPI_COMM_NULL; | ||
| MPI_Cart_create(MPI_COMM_WORLD, 2, dims.data(), periods.data(), 0, &torus_comm); | ||
|
|
||
| if (data.source_rank == data.dest_rank) { | ||
| if (world_rank == data.source_rank) { | ||
| res = data.value; | ||
| } | ||
| } else { | ||
| std::array<int, 2> src_coords = {0, 0}; | ||
| std::array<int, 2> dest_coords = {0, 0}; | ||
| MPI_Cart_coords(torus_comm, data.source_rank, 2, src_coords.data()); | ||
| MPI_Cart_coords(torus_comm, data.dest_rank, 2, dest_coords.data()); | ||
|
|
||
| int src_cart_rank = 0; | ||
| int dest_cart_rank = 0; | ||
| MPI_Cart_rank(torus_comm, src_coords.data(), &src_cart_rank); | ||
| MPI_Cart_rank(torus_comm, dest_coords.data(), &dest_cart_rank); | ||
|
|
||
| if (world_rank == src_cart_rank) { | ||
| res = data.value; | ||
| MPI_Send(&res, 1, MPI_INT, dest_cart_rank, 0, torus_comm); | ||
| } else if (world_rank == dest_cart_rank) { | ||
| MPI_Recv(&res, 1, MPI_INT, src_cart_rank, 0, torus_comm, MPI_STATUS_IGNORE); | ||
| } | ||
| } | ||
|
|
||
| MPI_Bcast(&res, 1, MPI_INT, data.dest_rank, MPI_COMM_WORLD); | ||
| GetOutput() = res; | ||
|
|
||
| if (torus_comm != MPI_COMM_NULL) { | ||
| MPI_Comm_free(&torus_comm); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool TorusGridTaskMPI::PostProcessingImpl() { | ||
| return true; | ||
| } | ||
|
|
||
| } // namespace agafonov_i_torus_grid | ||
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,62 @@ | ||
| # Решетка-тор | ||
|
|
||
| - Student: Агафонов Илья Дмитриевич, group 3823Б1ФИ1 | ||
| - Technology: SEQ | MPI | ||
| - Variant: 9 | ||
|
|
||
| ## 1. Introduction | ||
| Целью данной работы является реализация алгоритма обмена данными в вычислительной сети с топологией «двумерный тор» (Torus Grid). Топология тора обеспечивает высокую степень связности и отказоустойчивость, что делает её актуальной для распределенных вычислений. Ожидаемый результат — корректная передача сообщения от заданного узла-источника к узлу-приемнику с использованием интерфейса MPI. | ||
|
|
||
| ## 2. Problem Statement | ||
| Необходимо реализовать передачу целочисленного значения (`int`) между двумя произвольными узлами (процессами) в логической сетке размера $N \times N$. | ||
| - **Входные данные:** Значение для передачи, ранг отправителя (`source_rank`) и ранг получателя (`dest_rank`). | ||
| - **Выходные данные:** После завершения работы значение должно быть доступно на всех процессах. | ||
| - **Ограничения:** Количество запущенных процессов $P$ должно быть полным квадратом ($P = N^2$). | ||
|
|
||
| ## 3. Baseline Algorithm (Sequential) | ||
| Последовательный алгоритм имитирует передачу данных путем прямого копирования значения из входной структуры в выходную переменную. Логика маршрутизации отсутствует, так как в последовательном режиме существует только один процесс, который одновременно является и источником, и приемником. | ||
|
|
||
| ## 4. Parallelization Scheme | ||
| Для параллельной реализации используется библиотека MPI и следующая схема взаимодействия: | ||
| 1. **Топология:** Процессы интерпретируются как узлы сетки. Проверка `ValidationImpl` гарантирует, что количество процессов позволяет сформировать квадратную структуру. | ||
| 2. **Точечная передача (Point-to-Point):** Процесс-источник (`source_rank`) использует `MPI_Send` для отправки данных. Процесс-приемник (`dest_rank`) использует `MPI_Recv` для их получения. | ||
| 3. **Широковещательная рассылка:** Чтобы результат стал доступен всем узлам (согласно требованиям), используется `MPI_Bcast` от ранга-получателя. | ||
|
|
||
| ## 5. Implementation Details | ||
| - **Основные функции:** | ||
| - `ValidationImpl`: Проверяет границы рангов и квадратность сетки. | ||
| - `RunImpl`: Реализует логику `Send` -> `Recv` -> `Bcast`. | ||
| - **Линтер:** Код прошел проверку `clang-tidy-21`, исправлены замечания по упрощению логических выражений (законы Де Моргана) и чистоте заголовочных файлов. | ||
|
|
||
| ## 6. Experimental Setup | ||
| **Hardware/OS:** | ||
| - **Процессор:** Процессор AMD Ryzen 5 5500U, ядер: 6, логических процессоров: 12 | ||
| - **Оперативная память:** 16 ГБ DDR4 | ||
| - **Операционная система:** Windows 10 Pro 22H2 | ||
| - **Toolchain:** | ||
| - **Компилятор:** g++ 13.3.0 | ||
| - **Тип сборки:** Release (-O3 ) | ||
| - **MPI:** Open MPI 4.1.6 | ||
|
|
||
| ## 7. Results and Discussion | ||
|
|
||
| ### 7.1 Correctness | ||
| Функциональные тесты успешно пройдены для различных сценариев передачи (между узлами, самопередача). Проверка проводилась на 4 процессах с использованием `mpirun`. | ||
|
|
||
| ### 7.2 Performance | ||
| Результаты замера времени выполнения (`task_run`) на 4 процессах: | ||
|
|
||
| | Mode | Count | Time, s | Speedup | Efficiency | | ||
| |-------------|-------|--------------|---------|------------| | ||
| | seq | 1 | 0.00000014 | 1.00 | N/A | | ||
| | mpi | 4 | 0.00014809 | 0.00032 | 0.45% | | ||
| | mpi | 9 | 0.00040852 | 0.00023 | 0.002% | | ||
|
|
||
| Низкие показатели ускорения и эффективности обусловлены небольшим объемом передаваемых данных, при котором время самой коммуникации оказывается на несколько порядков меньше, чем накладные расходы MPI. Рост времени выполнения при переходе от 4 к 9 процессам подтверждает усложнение логики маршрутизации в сетке 3x3 по сравнению с 2x2. | ||
|
|
||
| ## 8. Conclusions | ||
| Алгоритм обмена в топологии «Тор» реализован и верифицирован. Программа демонстрирует стабильную работу и проходит статический анализ кода. Опытным путем подтверждено, что для микро-задач коммуникационные затраты MPI значительно превышают время полезных вычислений. | ||
|
|
||
| ## 9. Список литературы | ||
| 1. Материлы и документация по курсу | ||
| 2. Документация стандарта MPI: https://www.mpi-forum.org/ |
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 "agafonov_i_torus_grid/common/include/common.hpp" | ||
| #include "task/include/task.hpp" | ||
|
|
||
| namespace agafonov_i_torus_grid { | ||
|
|
||
| class TorusGridTaskSEQ : public BaseTask { | ||
| public: | ||
| static constexpr ppc::task::TypeOfTask GetStaticTypeOfTask() { | ||
| return ppc::task::TypeOfTask::kSEQ; | ||
| } | ||
| explicit TorusGridTaskSEQ(const InType &in); | ||
|
|
||
| private: | ||
| bool ValidationImpl() override; | ||
| bool PreProcessingImpl() override; | ||
| bool RunImpl() override; | ||
| bool PostProcessingImpl() override; | ||
| }; | ||
|
|
||
| } // namespace agafonov_i_torus_grid |
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,30 @@ | ||
| #include "agafonov_i_torus_grid/seq/include/ops_seq.hpp" | ||
|
|
||
| #include "agafonov_i_torus_grid/common/include/common.hpp" | ||
|
|
||
| namespace agafonov_i_torus_grid { | ||
|
|
||
| TorusGridTaskSEQ::TorusGridTaskSEQ(const InType &in) { | ||
| SetTypeOfTask(GetStaticTypeOfTask()); | ||
| GetInput() = in; | ||
| } | ||
|
|
||
| bool TorusGridTaskSEQ::ValidationImpl() { | ||
| return true; | ||
| } | ||
|
|
||
| bool TorusGridTaskSEQ::PreProcessingImpl() { | ||
| GetOutput() = 0; | ||
| return true; | ||
| } | ||
|
|
||
| bool TorusGridTaskSEQ::RunImpl() { | ||
| GetOutput() = GetInput().value; | ||
| return true; | ||
| } | ||
|
|
||
| bool TorusGridTaskSEQ::PostProcessingImpl() { | ||
| return true; | ||
| } | ||
|
|
||
| } // namespace agafonov_i_torus_grid |
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 |
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 <gtest/gtest.h> | ||
|
|
||
| #include <array> | ||
| #include <cstddef> | ||
| #include <string> | ||
| #include <tuple> | ||
|
|
||
| #include "agafonov_i_torus_grid/common/include/common.hpp" | ||
| #include "agafonov_i_torus_grid/mpi/include/ops_mpi.hpp" | ||
| #include "agafonov_i_torus_grid/seq/include/ops_seq.hpp" | ||
| #include "util/include/func_test_util.hpp" | ||
| #include "util/include/util.hpp" | ||
|
|
||
| namespace agafonov_i_torus_grid { | ||
|
|
||
| class TorusGridFuncTests : public ppc::util::BaseRunFuncTests<InType, OutType, TestType> { | ||
| public: | ||
| TorusGridFuncTests() = default; | ||
|
|
||
| static std::string PrintTestParam( | ||
| const testing::TestParamInfo<ppc::util::FuncTestParam<InType, OutType, TestType>> &info) { | ||
| auto params = std::get<static_cast<std::size_t>(ppc::util::GTestParamIndex::kTestParams)>(info.param); | ||
| return std::to_string(std::get<0>(params)) + "_" + std::get<1>(params); | ||
| } | ||
|
|
||
| protected: | ||
| void SetUp() override { | ||
| auto params = std::get<static_cast<std::size_t>(ppc::util::GTestParamIndex::kTestParams)>(GetParam()); | ||
| int test_case = std::get<0>(params); | ||
|
|
||
| if (test_case == 1) { | ||
| input_data_ = {.value = 123, .source_rank = 0, .dest_rank = 3}; | ||
| expected_output_ = 123; | ||
| } else if (test_case == 2) { | ||
| input_data_ = {.value = 555, .source_rank = 1, .dest_rank = 2}; | ||
| expected_output_ = 555; | ||
| } else { | ||
| input_data_ = {.value = 99, .source_rank = 0, .dest_rank = 0}; | ||
| expected_output_ = 99; | ||
| } | ||
| } | ||
|
|
||
| bool CheckTestOutputData(OutType &output_data) final { | ||
| return output_data == expected_output_; | ||
| } | ||
| InType GetTestInputData() final { | ||
| return input_data_; | ||
| } | ||
|
|
||
| private: | ||
| InType input_data_{.value = 0, .source_rank = 0, .dest_rank = 0}; | ||
| OutType expected_output_{0}; | ||
| }; | ||
|
|
||
| TEST_P(TorusGridFuncTests, RunTests) { | ||
| ExecuteTest(GetParam()); | ||
| } | ||
|
|
||
| namespace { | ||
|
|
||
| const std::array<TestType, 3> kTestParams = { | ||
| std::make_tuple(1, "transfer_0_to_3"), std::make_tuple(2, "transfer_1_to_2"), std::make_tuple(3, "self_transfer")}; | ||
|
|
||
| auto GetMpiValues() { | ||
| auto tasks = ppc::util::AddFuncTask<TorusGridTaskMPI, InType>(kTestParams, PPC_SETTINGS_agafonov_i_torus_grid); | ||
| return ppc::util::ExpandToValues(tasks); | ||
| } | ||
|
|
||
| auto GetSeqValues() { | ||
| auto tasks = ppc::util::AddFuncTask<TorusGridTaskSEQ, InType>(kTestParams, PPC_SETTINGS_agafonov_i_torus_grid); | ||
| return ppc::util::ExpandToValues(tasks); | ||
| } | ||
|
|
||
| INSTANTIATE_TEST_SUITE_P(MPI, TorusGridFuncTests, GetMpiValues(), TorusGridFuncTests::PrintTestParam); | ||
|
|
||
| INSTANTIATE_TEST_SUITE_P(SEQ, TorusGridFuncTests, GetSeqValues(), TorusGridFuncTests::PrintTestParam); | ||
|
|
||
| } // namespace | ||
|
|
||
| } // namespace agafonov_i_torus_grid |
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.
Is it torus topology? Looks like, no