Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <cstddef>
#include <string>
#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 -> size
using OutType = std::vector<double>;
using TestType = std::string;
using BaseTask = ppc::task::Task<InType, OutType>;

} // namespace zenin_a_sum_values_by_columns_matrix
4,002 changes: 4,002 additions & 0 deletions tasks/zenin_a_sum_values_by_columns_matrix/data/mat_perf.txt

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions tasks/zenin_a_sum_values_by_columns_matrix/data/matrix1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
10 10
1.0 2.0 5.0 6.0 18.0 32.0 5.0 8.0 7.0 14.0
4.0 5.0 6.5 41.0 36.0 5.0 23.0 76.0 7.0 4.0
7.5 8.5 9.0 5.0 3.0 14.0 76.0 12.0 29.0 12.0
3.7 4.8 7.8 9.6 2.4 6.0 12.0 13.0 12.5 10.0
1.0 2.0 7.0 13.0 9.5 3.8 7.9 45.7 12.0 19.0
4.0 5.3 6.5 61.0 36.0 7.0 17.0 76.0 7.0 2.0
3.7 2.8 7.8 6.6 2.4 6.0 12.0 23.0 12.5 13.0
12.8 45.6 10.0 34.8 12.0 7.5 6.9 3.0 6.0 8.7
4.5 8.7 10.0 4.0 5.0 34.0 11.0 12.0 29.0 52.0
32.0 75.8 12.7 56.8 3.0 9.0 15.0 4.6 3.8 9.0
3 changes: 3 additions & 0 deletions tasks/zenin_a_sum_values_by_columns_matrix/data/matrix2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2 4
1.5 2.5 3.5 4.5
5.5 6.5 7.5 8.5
5 changes: 5 additions & 0 deletions tasks/zenin_a_sum_values_by_columns_matrix/data/matrix3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
4 2
1.0 2.0
0.5 1.5
3.0 4.0
2.5 3.7
7 changes: 7 additions & 0 deletions tasks/zenin_a_sum_values_by_columns_matrix/data/matrix4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
6 6
-10.0 -3.6 -7.1 -3.3 -5.4 -9.0
-5.2 -9.3 -2.1 -7.6 -5.4 -8.5
-5.1 -4.4 -6.4 -7.7 -10.1 -7.3
-4.1 -6.9 -1.3 -6.3 -2.1 -5.8
-4.1 -7.9 -3.7 -5.8 -1.5 -5.1
-1.2 -10.2 -9.9 -6.6 -4.4 -10.8
13 changes: 13 additions & 0 deletions tasks/zenin_a_sum_values_by_columns_matrix/data/matrix5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
12 5
-3.6 5.1 5.7 -6.6 7.6
7.3 -5.3 3.1 -7.4 6.2
4.4 -6.8 10.9 3.8 2.8
-9.6 -10.3 -5.6 -4.6 4.9
8.3 -3.1 -3.2 7.1 2.2
-7.3 8.7 10.6 -8.6 6.1
8.7 10.5 4.9 5.4 3.7
7.2 -5.7 -7.1 -5.8 10.2
3.1 3.4 5.1 6.4 7.6
-10.9 1.6 7.4 5.6 4.8
-3.5 8.2 3.9 4.9 -10.4
6.7 -3.9 7.8 4.8 -7.6
4 changes: 4 additions & 0 deletions tasks/zenin_a_sum_values_by_columns_matrix/data/matrix6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
3 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
5 changes: 5 additions & 0 deletions tasks/zenin_a_sum_values_by_columns_matrix/data/matrix7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
4 9
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18
19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36
6 changes: 6 additions & 0 deletions tasks/zenin_a_sum_values_by_columns_matrix/data/matrix8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
5 13
1 2 3 4 5 6 7 8 9 10 11 12 13
14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49 50 51 52
53 54 55 56 57 58 59 60 61 62 63 64 65
14 changes: 14 additions & 0 deletions tasks/zenin_a_sum_values_by_columns_matrix/data/matrix9.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
13 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
31 32 33 34 35
36 37 38 39 40
41 42 43 44 45
46 47 48 49 50
51 52 53 54 55
56 57 58 59 60
61 62 63 64 65
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions tasks/zenin_a_sum_values_by_columns_matrix/info.json
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"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <cstddef>
#include <tuple>
#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 std::tuple<size_t, size_t> CalculateProcessColumns(int rank, int world_size, size_t columns);
static void CalculateLocalSums(const std::vector<double> &matrix_data, size_t columns, size_t total_rows,
size_t start_column, size_t cols_this_process, std::vector<double> &local_sums);
static void PrepareGathervParameters(int world_size, size_t base_cols_per_process, size_t remain,
std::vector<int> &recv_counts, std::vector<int> &displacements);
};

} // namespace zenin_a_sum_values_by_columns_matrix
143 changes: 143 additions & 0 deletions tasks/zenin_a_sum_values_by_columns_matrix/mpi/src/ops_mpi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#include "zenin_a_sum_values_by_columns_matrix/mpi/include/ops_mpi.hpp"

#include <mpi.h>

#include <cmath>
#include <cstddef>
#include <tuple>
#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;
}

std::tuple<size_t, size_t> ZeninASumValuesByColumnsMatrixMPI::CalculateProcessColumns(int rank, int world_size,
size_t columns) {
size_t base_cols_per_process = columns / world_size;
size_t remain = columns % world_size;

size_t start_column = rank * base_cols_per_process;
size_t cols_this_process = base_cols_per_process;

if (rank == world_size - 1) {
cols_this_process += remain;
}
return {start_column, cols_this_process};
}

void ZeninASumValuesByColumnsMatrixMPI::CalculateLocalSums(const std::vector<double> &matrix_data, size_t columns,
size_t total_rows, size_t start_column,
size_t cols_this_process, std::vector<double> &local_sums) {
for (size_t local_column = 0; local_column < cols_this_process; ++local_column) {
size_t global_col = start_column + local_column;
for (size_t row = 0; row < total_rows; ++row) {
local_sums[local_column] += matrix_data[(row * columns) + global_col];
}
}
}

void ZeninASumValuesByColumnsMatrixMPI::PrepareGathervParameters(int world_size, size_t base_cols_per_process,
size_t remain, std::vector<int> &recv_counts,
std::vector<int> &displacements) {
for (int i = 0; i < world_size; ++i) {
recv_counts[i] = static_cast<int>(base_cols_per_process);
if (i == world_size - 1) {
recv_counts[i] += static_cast<int>(remain);
}
if (i > 0) {
displacements[i] = displacements[i - 1] + recv_counts[i - 1];
}
}
}

bool ZeninASumValuesByColumnsMatrixMPI::RunImpl() {
auto &input = GetInput();
int world_size = 0;
int rank = 0;

MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &world_size);

size_t columns = 0;
std::vector<double> matrix_data;
size_t total_rows = 0;

if (rank == 0) {
columns = std::get<1>(input);
matrix_data = std::get<2>(input);
total_rows = std::get<0>(input);
if (matrix_data.size() % columns != 0) {
return false;
}
}

MPI_Bcast(&columns, 1, MPI_UNSIGNED_LONG, 0, MPI_COMM_WORLD);
MPI_Bcast(&total_rows, 1, MPI_UNSIGNED_LONG, 0, MPI_COMM_WORLD);

if (columns == 0) {
return false;
}

if (rank != 0) {
matrix_data.resize(total_rows * columns);
}
MPI_Bcast(matrix_data.data(), static_cast<int>(matrix_data.size()), MPI_DOUBLE, 0, MPI_COMM_WORLD);

auto [start_column, cols_this_process] = CalculateProcessColumns(rank, world_size, columns);
std::vector<double> local_sums(cols_this_process, 0.0);
CalculateLocalSums(matrix_data, columns, total_rows, start_column, cols_this_process, local_sums);

std::vector<double> global_sums;
if (rank == 0) {
global_sums.resize(columns, 0.0);
}

size_t base_cols_per_process = columns / world_size;
size_t remain = columns % world_size;

std::vector<int> recv_counts(world_size, 0);
std::vector<int> displacements(world_size, 0);

if (rank == 0) {
PrepareGathervParameters(world_size, base_cols_per_process, remain, recv_counts, displacements);
}

MPI_Gatherv(local_sums.data(), static_cast<int>(local_sums.size()), MPI_DOUBLE, global_sums.data(),
recv_counts.data(), displacements.data(), MPI_DOUBLE, 0, MPI_COMM_WORLD);

if (rank != 0) {
global_sums.resize(columns);
}

MPI_Bcast(global_sums.data(), static_cast<int>(columns), MPI_DOUBLE, 0, MPI_COMM_WORLD);
GetOutput() = global_sums;

return true;
}

bool ZeninASumValuesByColumnsMatrixMPI::PostProcessingImpl() {
return true;
}

} // namespace zenin_a_sum_values_by_columns_matrix
Loading
Loading