Skip to content
Open
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,66 @@
#pragma once

#include <cmath>
#include <cstdint>
#include <tuple>

#include "task/include/task.hpp"

namespace dergynov_s_trapezoid_integration {

struct Input {
double a;
double b;
int n;
int func_id;
};

using InType = Input;
using OutType = double;
using TestType = std::tuple<int, const char *>;
using BaseTask = ppc::task::Task<InType, OutType>;

enum class FunctionId : std::uint8_t {
kLinear = 0,
kQuadratic = 1,
kSin = 2
};

inline bool IsValidFunctionId(int id) {
return id >= 0 && id <= 2;
}

inline double Function(double x, int id) {
if (!IsValidFunctionId(id)) return 0.0;

switch (static_cast<FunctionId>(id)) {
case FunctionId::kLinear:
return x;
case FunctionId::kQuadratic:
return x * x;
case FunctionId::kSin:
return std::sin(x);
default:
return 0.0;
}
}

inline double GetExactIntegral(const InType& in) {
if (!IsValidFunctionId(in.func_id)) return 0.0;

const double a = in.a;
const double b = in.b;

switch (static_cast<FunctionId>(in.func_id)) {
case FunctionId::kLinear:
return (b * b - a * a) / 2.0;
case FunctionId::kQuadratic:
return (b * b * b - a * a * a) / 3.0;
case FunctionId::kSin:
return std::cos(a) - std::cos(b);
default:
return 0.0;
}
}

} // namespace dergynov_s_trapezoid_integration
9 changes: 9 additions & 0 deletions tasks/dergynov_s_trapezoid_integration/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"student": {
"first_name": "first_name_p",
"last_name": "last_name_p",
"middle_name": "middle_name_p",
"group_number": "2222222_p",
"task_number": "1"
}
}
22 changes: 22 additions & 0 deletions tasks/dergynov_s_trapezoid_integration/mpi/include/ops_mpi.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "dergynov_s_trapezoid_integration/common/include/common.hpp"

namespace dergynov_s_trapezoid_integration {

class DergynovSTrapezoidIntegrationMPI : public BaseTask {
public:
static constexpr ppc::task::TypeOfTask GetStaticTypeOfTask() {
return ppc::task::TypeOfTask::kMPI;
}

explicit DergynovSTrapezoidIntegrationMPI(const InType& in);

private:
bool ValidationImpl() override;
bool PreProcessingImpl() override;
bool RunImpl() override;
bool PostProcessingImpl() override;
};

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

#include <mpi.h>
#include <algorithm>

namespace dergynov_s_trapezoid_integration {

DergynovSTrapezoidIntegrationMPI::DergynovSTrapezoidIntegrationMPI(const InType& in) {
SetTypeOfTask(GetStaticTypeOfTask());
GetInput() = in;
GetOutput() = 0.0;
}

bool DergynovSTrapezoidIntegrationMPI::ValidationImpl() {
const auto& in = GetInput();
return (in.n > 0) && (in.a < in.b);
}

bool DergynovSTrapezoidIntegrationMPI::PreProcessingImpl() {
GetOutput() = 0.0;
return true;
}

bool DergynovSTrapezoidIntegrationMPI::RunImpl() {
int rank = 0, size = 0;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

InType in = (rank == 0) ? GetInput() : InType{};
MPI_Bcast(&in, sizeof(InType), MPI_BYTE, 0, MPI_COMM_WORLD);
GetInput() = in;

const double a = in.a;
const double b = in.b;
const int n = in.n;

if (n <= 0 || !(a < b)) {
if (rank == 0) GetOutput() = 0.0;
return true;
}

int base = n / size;
int rem = n % size;
int local_n = base + (rank < rem ? 1 : 0);
int start = rank * base + std::min(rank, rem);
int end = start + local_n;

const double h = (b - a) / static_cast<double>(n);

double local_sum = 0.0;
for (int i = start; i < end; ++i) {
double x1 = a + h * i;
double x2 = a + h * (i + 1);
local_sum += 0.5 * (Function(x1, in.func_id) + Function(x2, in.func_id)) * h;
}

double global_sum = 0.0;
MPI_Reduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
MPI_Bcast(&global_sum, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);

GetOutput() = global_sum;
return true;
}

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

} // namespace dergynov_s_trapezoid_integration
Loading
Loading