Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
458d4d5
add task and tests and report
DariyaSavva Nov 16, 2025
ae822a9
fix clang-format
DariyaSavva Nov 16, 2025
ab5c331
Fix code formatting and line endings
DariyaSavva Nov 16, 2025
b422fa7
fix include
DariyaSavva Nov 16, 2025
14a2964
modifix report
DariyaSavva Nov 25, 2025
3cd2ef7
fix mpi_version
DariyaSavva Nov 25, 2025
94b51a0
fix remember
DariyaSavva Dec 31, 2025
377460a
fix clangformat
DariyaSavva Dec 31, 2025
cf32e14
make task 2
DariyaSavva Jan 2, 2026
a281fdd
fix pre-commit
DariyaSavva Jan 2, 2026
1b9d9c4
fix inizialization x in mpi
DariyaSavva Jan 2, 2026
17742eb
fix inizialization struct
DariyaSavva Jan 3, 2026
95e5c55
fix vector in func test
DariyaSavva Jan 3, 2026
cbcb28a
add func cout
DariyaSavva Jan 3, 2026
5156fa5
add func cout 2
DariyaSavva Jan 3, 2026
5b81632
add func cout 3
DariyaSavva Jan 3, 2026
40fa313
fix report
DariyaSavva Jan 3, 2026
7749600
deleted empty
DariyaSavva Jan 22, 2026
a34b7d1
deleted empty fix
DariyaSavva Jan 22, 2026
4b9a7c4
tuple in struct
DariyaSavva Jan 29, 2026
627c8b5
tuple in struct inizializen
DariyaSavva Jan 29, 2026
5cbfea1
tuple in struct 3
DariyaSavva Jan 29, 2026
13b6359
moderniziren Report
DariyaSavva Jan 30, 2026
5e387b6
fix CI 0.0
DariyaSavva Jan 31, 2026
9029e65
Merge branch 'savva_d_zeidel_method' of https://github.com/DariyaSavv…
DariyaSavva Jan 31, 2026
87babbc
fix CI 0.1
DariyaSavva Jan 31, 2026
709ab5c
fix CI 0.2
DariyaSavva Jan 31, 2026
bfc69c7
fix CI 0.3
DariyaSavva Jan 31, 2026
028fa5e
fix CI 0.4
DariyaSavva Jan 31, 2026
cc548e6
fix CI 0.5
DariyaSavva Feb 1, 2026
ad2c843
fix CI 0.6
DariyaSavva Feb 1, 2026
b52c423
make seq version
DariyaSavva Feb 3, 2026
9cf225e
make mpi version and tests
DariyaSavva Feb 3, 2026
e767971
fix clang format mpi
DariyaSavva Feb 5, 2026
18d155f
Fix report 2
DariyaSavva Feb 11, 2026
dd682fe
fix clang-tidy
DariyaSavva Feb 14, 2026
34e3e30
Merge branch 'savva_d_conjugate_gradients' of https://github.com/Dari…
DariyaSavva Feb 14, 2026
43297f9
add include
DariyaSavva Feb 14, 2026
494d8fc
fix include 1
DariyaSavva Feb 15, 2026
f438852
fix clang-tidy 3
DariyaSavva Feb 16, 2026
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
28 changes: 28 additions & 0 deletions tasks/savva_d_conjugent_gradients/common/include/common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <string>
#include <vector>

#include "task/include/task.hpp"

namespace savva_d_conjugent_gradients {

struct InputSystem {
int n = 0;
std::vector<double> a;
std::vector<double> b;
};

using InType = InputSystem;
using OutType = std::vector<double>;

struct TestParams {
InputSystem in{};
OutType out;
std::string name;
};

using TestType = TestParams;
using BaseTask = ppc::task::Task<InType, OutType>;

} // namespace savva_d_conjugent_gradients
9 changes: 9 additions & 0 deletions tasks/savva_d_conjugent_gradients/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": "3"
}
}
31 changes: 31 additions & 0 deletions tasks/savva_d_conjugent_gradients/mpi/include/ops_mpi.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <vector>

#include "savva_d_conjugent_gradients/common/include/common.hpp"
#include "task/include/task.hpp"

namespace savva_d_conjugent_gradients {

class SavvaDConjugentGradientsMPI : public BaseTask {
public:
static constexpr ppc::task::TypeOfTask GetStaticTypeOfTask() {
return ppc::task::TypeOfTask::kMPI;
}
explicit SavvaDConjugentGradientsMPI(const InType &in);

private:
bool ValidationImpl() override;
bool PreProcessingImpl() override;
bool RunImpl() override;
bool PostProcessingImpl() override;
static void UpdateXR(std::vector<double> &x, std::vector<double> &r, const std::vector<double> &p,
const std::vector<double> &global_ap, double alpha, int n);
static std::vector<double> ComputeLocalAp(int n, int local_rows, const std::vector<double> &local_a,
const std::vector<double> &p);
static void RunCGIterations(int n, int local_rows, int local_offset, std::vector<double> &r,
const std::vector<double> &local_a, std::vector<double> &vector_x,
const std::vector<int> &counts, const std::vector<int> &displs);
};

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

#include <mpi.h>

#include <algorithm>
#include <cmath>
#include <cstddef>
#include <numeric>
// #include <ranges>
#include <vector>

#include "savva_d_conjugent_gradients/common/include/common.hpp"

namespace savva_d_conjugent_gradients {

SavvaDConjugentGradientsMPI::SavvaDConjugentGradientsMPI(const InType &in) {
SetTypeOfTask(GetStaticTypeOfTask());
GetInput() = in;
GetOutput() = std::vector<double>{};
}

bool SavvaDConjugentGradientsMPI::ValidationImpl() {
const auto &in = GetInput();

if (in.n < 0 || in.a.size() != static_cast<size_t>(in.n) * static_cast<size_t>(in.n) ||
in.b.size() != static_cast<size_t>(in.n)) {
return false;
}

for (int i = 0; i < in.n; ++i) {
for (int j = i + 1; j < in.n; ++j) {
if (std::abs(in.a[(i * in.n) + j] - in.a[(j * in.n) + i]) > 1e-9) {
return false;
}
}
}

return true;
}

bool SavvaDConjugentGradientsMPI::PreProcessingImpl() {
GetOutput().assign(GetInput().n, 0.0);
return true;
}

std::vector<double> SavvaDConjugentGradientsMPI::ComputeLocalAp(int n, int local_rows,
const std::vector<double> &local_a,
const std::vector<double> &p) {
std::vector<double> local_ap(local_rows, 0.0);
for (int i = 0; i < local_rows; ++i) {
double sum = 0.0;
for (int j = 0; j < n; ++j) {
sum += local_a[(i * n) + j] * p[j];
}
local_ap[i] = sum;
}
return local_ap;
}

void SavvaDConjugentGradientsMPI::UpdateXR(std::vector<double> &x, std::vector<double> &r, const std::vector<double> &p,
const std::vector<double> &global_ap, double alpha, int n) {
for (int i = 0; i < n; ++i) {
x[i] += alpha * p[i];
r[i] -= alpha * global_ap[i];
}
}

void SavvaDConjugentGradientsMPI::RunCGIterations(int n, int local_rows, int local_offset, std::vector<double> &r,
const std::vector<double> &local_a, std::vector<double> &vector_x,
const std::vector<int> &counts, const std::vector<int> &displs) {
// Константы
const int max_iter = 1000;
const double eps = 1e-9;

std::vector<double> p(n, 0.0);
p = r;
std::vector<double> global_ap(n, 0.0);
// std::vector<double> local_ap(local_rows, 0.0);

double rr_old = std::inner_product(r.begin(), r.end(), r.begin(), 0.0);

for (int iter = 0; iter < max_iter; ++iter) {
if (std::sqrt(rr_old) < eps) {
break;
}

// A_local * p

std::vector<double> local_ap = ComputeLocalAp(n, local_rows, local_a, p);

// собираем глобальный ap
std::ranges::fill(global_ap, 0.0);
for (int i = 0; i < local_rows; ++i) {
global_ap[local_offset + i] = local_ap[i];
}
MPI_Allgatherv(MPI_IN_PLACE, 0, MPI_DATATYPE_NULL, global_ap.data(), counts.data(), displs.data(), MPI_DOUBLE,
MPI_COMM_WORLD);

double p_ap = std::inner_product(p.begin(), p.end(), global_ap.begin(), 0.0);

if (std::abs(p_ap) < eps) {
break;
}

double alpha = rr_old / p_ap;

UpdateXR(vector_x, r, p, global_ap, alpha, n);

double rr_new = std::inner_product(r.begin(), r.end(), r.begin(), 0.0);

double beta = rr_new / rr_old;

rr_old = rr_new;

for (int i = 0; i < n; ++i) {
p[i] = r[i] + (beta * p[i]);
}
}
}

bool SavvaDConjugentGradientsMPI::RunImpl() {
int rank = 0;
int size = 0;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
const double *sendbuf_a = nullptr; // будут ненулевыми только на 0 процессе

int n = 0;
if (rank == 0) {
n = GetInput().n;
}
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
std::vector<double> r(n, 0.0);

if (rank == 0) {
sendbuf_a = GetInput().a.data();
const auto &full_b = GetInput().b;
std::ranges::copy(full_b, r.begin());
}

MPI_Bcast(r.data(), n, MPI_DOUBLE, 0, MPI_COMM_WORLD);

if (n == 0) {
return true;
}

std::vector<int> counts(size);
std::vector<int> displs(size);
std::vector<int> counts_a(size);
std::vector<int> displs_a(size);

int rows_per_proc = n / size;
int remainder = n % size;
int offset = 0;
int local_rows = 0;
int local_offset = 0;

for (int i = 0; i < size; ++i) {
counts[i] = rows_per_proc + (i < remainder ? 1 : 0);
displs[i] = offset;
counts_a[i] = counts[i] * n;
displs_a[i] = displs[i] * n;
offset += counts[i];

if (i == rank) {
local_offset = displs[rank];
local_rows = counts[rank];
}
}

// Выделение памяти под локальные данные

std::vector<double> local_a(static_cast<size_t>(local_rows) * static_cast<size_t>(n));

// Рассылка данных
MPI_Scatterv(sendbuf_a, counts_a.data(), displs_a.data(), MPI_DOUBLE, local_a.data(), local_rows * n, MPI_DOUBLE, 0,
MPI_COMM_WORLD);

auto &x = GetOutput();
x.assign(n, 0.0);
// Запуск алгоритма
RunCGIterations(n, local_rows, local_offset, r, local_a, x, counts, displs);

return true;
}

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

} // namespace savva_d_conjugent_gradients
Loading