-
Notifications
You must be signed in to change notification settings - Fork 80
Зенин Антон. Технология SEQ-MPI. Сумма значений по столбцам матрицы. Вариант 12. #60
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
Merged
allnes
merged 9 commits into
learning-process:master
from
AntonZenin:ZeninA_sum_values_by_columns_matrix
Dec 10, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
31c14f2
added task from my another branch
AntonZenin b08981c
clang-format
AntonZenin 7d9e26e
report updated
AntonZenin 67bf396
fix
AntonZenin 435b48a
trial fix
AntonZenin 0d19694
fixed
AntonZenin a17d667
trial commit
AntonZenin bf694f6
small report fix
AntonZenin 87f6cdf
mpi optimized, tests and report changed
AntonZenin 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
16 changes: 16 additions & 0 deletions
16
tasks/zenin_a_sum_values_by_columns_matrix/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,16 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
| #include <tuple> | ||
| #include <vector> | ||
|
|
||
| #include "task/include/task.hpp" | ||
|
|
||
| namespace zenin_a_sum_values_by_columns_matrix { | ||
|
|
||
| using InType = std::tuple<size_t, size_t, std::vector<double>>; // rows -> columns -> matrix | ||
| using OutType = std::vector<double>; | ||
| using TestType = std::tuple<size_t, size_t>; | ||
| using BaseTask = ppc::task::Task<InType, OutType>; | ||
|
|
||
| } // namespace zenin_a_sum_values_by_columns_matrix |
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": "1" | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
tasks/zenin_a_sum_values_by_columns_matrix/mpi/include/ops_mpi.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,27 @@ | ||
| #pragma once | ||
| #include <cstddef> | ||
| #include <vector> | ||
|
|
||
| #include "task/include/task.hpp" | ||
| #include "zenin_a_sum_values_by_columns_matrix/common/include/common.hpp" | ||
|
|
||
| namespace zenin_a_sum_values_by_columns_matrix { | ||
|
|
||
| class ZeninASumValuesByColumnsMatrixMPI : public BaseTask { | ||
| public: | ||
| static constexpr ppc::task::TypeOfTask GetStaticTypeOfTask() { | ||
| return ppc::task::TypeOfTask::kMPI; | ||
| } | ||
| explicit ZeninASumValuesByColumnsMatrixMPI(const InType &in); | ||
|
|
||
| private: | ||
| bool ValidationImpl() override; | ||
| bool PreProcessingImpl() override; | ||
| bool RunImpl() override; | ||
| bool PostProcessingImpl() override; | ||
|
|
||
| static void FillSendBuffer(const std::vector<double> &mat, std::vector<double> &sendbuf, size_t rows, size_t cols, | ||
| size_t base, size_t rest, int world_size); | ||
| }; | ||
|
|
||
| } // namespace zenin_a_sum_values_by_columns_matrix |
126 changes: 126 additions & 0 deletions
126
tasks/zenin_a_sum_values_by_columns_matrix/mpi/src/ops_mpi.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,126 @@ | ||
| #include "zenin_a_sum_values_by_columns_matrix/mpi/include/ops_mpi.hpp" | ||
|
|
||
| #include <mpi.h> | ||
|
|
||
| #include <cmath> | ||
| #include <cstddef> | ||
| #include <stdexcept> | ||
| #include <tuple> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include "zenin_a_sum_values_by_columns_matrix/common/include/common.hpp" | ||
|
|
||
| namespace zenin_a_sum_values_by_columns_matrix { | ||
|
|
||
| ZeninASumValuesByColumnsMatrixMPI::ZeninASumValuesByColumnsMatrixMPI(const InType &in) { | ||
| SetTypeOfTask(GetStaticTypeOfTask()); | ||
| GetInput() = in; | ||
| GetOutput() = OutType{}; | ||
| } | ||
|
|
||
| bool ZeninASumValuesByColumnsMatrixMPI::ValidationImpl() { | ||
| auto &input = GetInput(); | ||
| return ((std::get<0>(input)) * std::get<1>(input) == std::get<2>(input).size() && (GetOutput().empty())); | ||
| } | ||
|
|
||
| bool ZeninASumValuesByColumnsMatrixMPI::PreProcessingImpl() { | ||
| int rank = 0; | ||
| MPI_Comm_rank(MPI_COMM_WORLD, &rank); | ||
| if (rank != 0) { | ||
| return true; | ||
| } | ||
| GetOutput().clear(); | ||
| return true; | ||
| } | ||
|
|
||
| void ZeninASumValuesByColumnsMatrixMPI::FillSendBuffer(const std::vector<double> &mat, std::vector<double> &sendbuf, | ||
| size_t rows, size_t cols, size_t base, size_t rest, | ||
| int world_size) { | ||
| size_t pos = 0; | ||
|
allnes marked this conversation as resolved.
|
||
| for (int proc = 0; proc < world_size; proc++) { | ||
| auto proc_size = static_cast<size_t>(proc); | ||
| size_t pc_begin = (proc_size * base) + (std::cmp_less(proc_size, rest) ? proc_size : rest); | ||
| size_t pc_end = pc_begin + (base + (std::cmp_less(proc_size, rest) ? 1 : 0)); | ||
| for (size_t col = pc_begin; col < pc_end; col++) { | ||
| for (size_t row = 0; row < rows; row++) { | ||
| sendbuf[pos++] = mat[(row * cols) + col]; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| bool ZeninASumValuesByColumnsMatrixMPI::RunImpl() { | ||
| auto rows = static_cast<size_t>(std::get<0>(GetInput())); | ||
| auto cols = static_cast<size_t>(std::get<1>(GetInput())); | ||
|
allnes marked this conversation as resolved.
|
||
| const std::vector<double> &mat = std::get<2>(GetInput()); | ||
|
|
||
| std::vector<double> &global_sum = GetOutput(); | ||
|
|
||
| int rank = 0; | ||
| int world_size = 0; | ||
|
|
||
| MPI_Comm_rank(MPI_COMM_WORLD, &rank); | ||
| MPI_Comm_size(MPI_COMM_WORLD, &world_size); | ||
|
|
||
| const size_t base = cols / static_cast<size_t>(world_size); | ||
| const size_t rest = cols % static_cast<size_t>(world_size); | ||
|
|
||
| const size_t my_cols = base + (std::cmp_less(static_cast<size_t>(rank), rest) ? 1 : 0); | ||
|
|
||
| std::vector<int> sendcounts(static_cast<size_t>(world_size)); | ||
| std::vector<int> displs(static_cast<size_t>(world_size)); | ||
| if (rank == 0) { | ||
| int offset = 0; | ||
| for (int proc = 0; proc < world_size; proc++) { | ||
| size_t pc = base + (std::cmp_less(static_cast<size_t>(proc), rest) ? 1 : 0); | ||
| sendcounts[proc] = static_cast<int>(pc * rows); | ||
| displs[proc] = offset; | ||
| offset += sendcounts[proc]; | ||
| } | ||
| } | ||
|
|
||
| std::vector<double> sendbuf; | ||
| if (rank == 0) { | ||
| sendbuf.resize(rows * cols); | ||
| FillSendBuffer(mat, sendbuf, rows, cols, base, rest, world_size); | ||
| } | ||
| std::vector<double> local_block(rows * my_cols); | ||
| MPI_Scatterv(sendbuf.data(), sendcounts.data(), displs.data(), MPI_DOUBLE, local_block.data(), | ||
| static_cast<int>(local_block.size()), MPI_DOUBLE, 0, MPI_COMM_WORLD); | ||
|
|
||
| std::vector<double> local_sum(my_cols, 0.0); | ||
| for (size_t col_id = 0; col_id < my_cols; col_id++) { | ||
| for (size_t row_id = 0; row_id < rows; row_id++) { | ||
| local_sum[col_id] += local_block[(col_id * rows) + row_id]; | ||
| } | ||
| } | ||
| std::vector<int> recvcounts(static_cast<size_t>(world_size)); | ||
| std::vector<int> recvdispls(static_cast<size_t>(world_size)); | ||
|
|
||
| if (rows == 0) { | ||
| throw std::runtime_error("Matrix has zero rows"); | ||
| } | ||
|
|
||
| if (rank == 0) { | ||
| size_t offset = 0; | ||
| for (int proc = 0; proc < world_size; proc++) { | ||
| recvcounts[proc] = sendcounts[proc] / static_cast<int>(rows); | ||
| recvdispls[proc] = static_cast<int>(offset); | ||
| offset += static_cast<size_t>(recvcounts[proc]); | ||
| } | ||
| global_sum.assign(cols, 0.0); | ||
| } | ||
|
|
||
| MPI_Gatherv(local_sum.data(), static_cast<int>(my_cols), MPI_DOUBLE, global_sum.data(), recvcounts.data(), | ||
| recvdispls.data(), MPI_DOUBLE, 0, MPI_COMM_WORLD); | ||
| global_sum.resize(cols); | ||
| MPI_Bcast(global_sum.data(), static_cast<int>(cols), MPI_DOUBLE, 0, MPI_COMM_WORLD); | ||
| return true; | ||
| } | ||
|
|
||
| bool ZeninASumValuesByColumnsMatrixMPI::PostProcessingImpl() { | ||
| return true; | ||
| } | ||
|
|
||
| } // namespace zenin_a_sum_values_by_columns_matrix | ||
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.
Uh oh!
There was an error while loading. Please reload this page.