-
Notifications
You must be signed in to change notification settings - Fork 80
Митяева Дарья. Технология SEQ-MPI. Нахождение минимальных значений по строкам матрицы. Вариант 17 #75
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 4 commits into
learning-process:master
from
daasmit07:mityaeva_d_min_v_rows_matrix_dev
Dec 13, 2025
Merged
Митяева Дарья. Технология SEQ-MPI. Нахождение минимальных значений по строкам матрицы. Вариант 17 #75
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
16 changes: 16 additions & 0 deletions
16
tasks/mityaeva_d_min_v_rows_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 <string> | ||
| #include <tuple> | ||
| #include <vector> | ||
|
|
||
| #include "task/include/task.hpp" | ||
|
|
||
| namespace mityaeva_d_min_v_rows_matrix { | ||
|
|
||
| using InType = std::vector<int>; | ||
| using OutType = std::vector<int>; | ||
| using TestType = std::tuple<int, std::string>; | ||
| using BaseTask = ppc::task::Task<InType, OutType>; | ||
|
|
||
| } // namespace mityaeva_d_min_v_rows_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ФИ2", | ||
| "task_number": "1" | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
tasks/mityaeva_d_min_v_rows_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,28 @@ | ||
| #pragma once | ||
|
|
||
| #include <vector> | ||
|
|
||
| #include "mityaeva_d_min_v_rows_matrix/common/include/common.hpp" | ||
| #include "task/include/task.hpp" | ||
|
|
||
| namespace mityaeva_d_min_v_rows_matrix { | ||
|
|
||
| class MinValuesInRowsMPI : public BaseTask { | ||
| public: | ||
| static constexpr ppc::task::TypeOfTask GetStaticTypeOfTask() { | ||
| return ppc::task::TypeOfTask::kMPI; | ||
| } | ||
| explicit MinValuesInRowsMPI(const InType &in); | ||
|
|
||
| private: | ||
| bool ValidationImpl() override; | ||
| bool PreProcessingImpl() override; | ||
| bool RunImpl() override; | ||
| bool PostProcessingImpl() override; | ||
| }; | ||
|
|
||
| std::vector<int> ProcessLocalRows(const std::vector<int> &input, int start_row, int my_rows, int cols); | ||
| void GatherResults(int rank, int size, int rows, int rows_per_process, int remainder, | ||
| const std::vector<int> &local_result, std::vector<int> &output); | ||
|
|
||
| } // namespace mityaeva_d_min_v_rows_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,159 @@ | ||
| #include "mityaeva_d_min_v_rows_matrix/mpi/include/ops_mpi.hpp" | ||
|
|
||
| #include <mpi.h> | ||
|
|
||
| #include <algorithm> | ||
| #include <cstddef> | ||
| #include <vector> | ||
|
|
||
| #include "mityaeva_d_min_v_rows_matrix/common/include/common.hpp" | ||
|
|
||
| namespace mityaeva_d_min_v_rows_matrix { | ||
|
|
||
| MinValuesInRowsMPI::MinValuesInRowsMPI(const InType &in) { | ||
| SetTypeOfTask(GetStaticTypeOfTask()); | ||
| GetInput() = in; | ||
| GetOutput() = std::vector<int>{0}; | ||
| } | ||
|
|
||
| bool MinValuesInRowsMPI::ValidationImpl() { | ||
| const auto &input = GetInput(); | ||
|
|
||
| if (input.empty() || input.size() < 2) { | ||
| return false; | ||
| } | ||
|
|
||
| int rows = input[0]; | ||
| int cols = input[1]; | ||
|
|
||
| if (rows <= 0 || cols <= 0) { | ||
| return false; | ||
| } | ||
|
|
||
| size_t expected_size = 2 + (static_cast<size_t>(rows) * static_cast<size_t>(cols)); | ||
| return input.size() == expected_size; | ||
| } | ||
|
|
||
| bool MinValuesInRowsMPI::PreProcessingImpl() { | ||
| return true; | ||
| } | ||
|
|
||
| std::vector<int> ProcessLocalRows(const std::vector<int> &input, int start_row, int my_rows, int cols) { | ||
| std::vector<int> local_result; | ||
| local_result.reserve(my_rows); | ||
|
|
||
| for (int i = 0; i < my_rows; ++i) { | ||
| int global_row = start_row + i; | ||
| int row_start_index = 2 + (global_row * cols); | ||
|
|
||
| if (cols == 0) { | ||
| local_result.push_back(0); | ||
| continue; | ||
| } | ||
|
|
||
| int min_val = input[row_start_index]; | ||
| for (int j = 1; j < cols; ++j) { | ||
| int current_val = input[row_start_index + j]; | ||
| min_val = std::min(current_val, min_val); | ||
| } | ||
| local_result.push_back(min_val); | ||
| } | ||
|
|
||
| return local_result; | ||
| } | ||
|
|
||
| void GatherResults(int rank, int size, int rows, int rows_per_process, int remainder, | ||
| const std::vector<int> &local_result, std::vector<int> &output) { | ||
| if (rank == 0) { | ||
| std::vector<int> global_result; | ||
| global_result.reserve(rows); | ||
|
|
||
| global_result.insert(global_result.end(), local_result.begin(), local_result.end()); | ||
|
|
||
| for (int src = 1; src < size; ++src) { | ||
| int src_rows = rows_per_process; | ||
| if (src < remainder) { | ||
| src_rows++; | ||
| } | ||
|
|
||
| if (src_rows > 0) { | ||
| std::vector<int> recv_buffer(src_rows); | ||
| MPI_Recv(recv_buffer.data(), src_rows, MPI_INT, src, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); | ||
| global_result.insert(global_result.end(), recv_buffer.begin(), recv_buffer.end()); | ||
| } | ||
| } | ||
|
|
||
| output.clear(); | ||
| output.reserve(rows + 1); | ||
| output.push_back(rows); | ||
| output.insert(output.end(), global_result.begin(), global_result.end()); | ||
|
|
||
| int output_size = static_cast<int>(output.size()); | ||
| for (int dst = 1; dst < size; ++dst) { | ||
| MPI_Send(output.data(), output_size, MPI_INT, dst, 0, MPI_COMM_WORLD); | ||
| } | ||
|
|
||
| } else { | ||
| if (!local_result.empty()) { | ||
| int local_size = static_cast<int>(local_result.size()); | ||
| MPI_Send(local_result.data(), local_size, MPI_INT, 0, 0, MPI_COMM_WORLD); | ||
| } | ||
|
|
||
| int result_size = 0; | ||
| MPI_Status status; | ||
| MPI_Probe(0, 0, MPI_COMM_WORLD, &status); | ||
| MPI_Get_count(&status, MPI_INT, &result_size); | ||
|
|
||
| std::vector<int> recv_buffer(result_size); | ||
| MPI_Recv(recv_buffer.data(), result_size, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); | ||
|
|
||
| output = recv_buffer; | ||
| } | ||
| } | ||
|
|
||
| bool MinValuesInRowsMPI::RunImpl() { | ||
| const auto &input = GetInput(); | ||
|
|
||
| try { | ||
| int rank = 0; | ||
| int size = 0; | ||
| MPI_Comm_rank(MPI_COMM_WORLD, &rank); | ||
| MPI_Comm_size(MPI_COMM_WORLD, &size); | ||
|
|
||
| int rows = input[0]; | ||
| int cols = input[1]; | ||
|
|
||
| int rows_per_process = rows / size; | ||
| int remainder = rows % size; | ||
|
|
||
| int my_rows = rows_per_process; | ||
| if (rank < remainder) { | ||
| my_rows++; | ||
| } | ||
|
|
||
| int start_row = 0; | ||
| for (int i = 0; i < rank; ++i) { | ||
| int previous_rows = rows_per_process; | ||
| if (i < remainder) { | ||
| previous_rows++; | ||
| } | ||
| start_row += previous_rows; | ||
| } | ||
|
|
||
| std::vector<int> local_result = ProcessLocalRows(input, start_row, my_rows, cols); | ||
| GatherResults(rank, size, rows, rows_per_process, remainder, local_result, GetOutput()); | ||
|
|
||
| MPI_Barrier(MPI_COMM_WORLD); | ||
| return true; | ||
|
|
||
| } catch (...) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| bool MinValuesInRowsMPI::PostProcessingImpl() { | ||
| const auto &output = GetOutput(); | ||
| return !output.empty() && output[0] > 0; | ||
| } | ||
|
|
||
| } // namespace mityaeva_d_min_v_rows_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.
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 this try-catch really needed?
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.
Exception handling with try-catch blocks is necessary in both implementations primarily for protection against standard library exceptions. The std::vector::operator[] can throw std::out_of_range if accessed with an invalid index, which could occur if there are calculation errors in determining row and column offsets. Additionally, memory allocation operations like reserve() and push_back() can throw std::bad_alloc when system memory is exhausted, particularly when processing large matrices.
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.
This is incorrect. You might be confusing it with std::vector::at.
See the details for the reference:
As for reserve and push_back, but these cases are unlikely in our scenarios. Anyways, I'm ok with the current approach