Skip to content
Merged
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
22 changes: 22 additions & 0 deletions tasks/afanasyev_a_integ_rect_method/omp/include/ops_omp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

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

namespace afanasyev_a_integ_rect_method {

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

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

} // namespace afanasyev_a_integ_rect_method
68 changes: 68 additions & 0 deletions tasks/afanasyev_a_integ_rect_method/omp/src/ops_omp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "afanasyev_a_integ_rect_method/omp/include/ops_omp.hpp"

#include <cmath>
#include <vector>

#include "afanasyev_a_integ_rect_method/common/include/common.hpp"

namespace afanasyev_a_integ_rect_method {
namespace {

double ExampleIntegrand(const std::vector<double> &x) {
double s = 0.0;
for (double xi : x) {
s += xi * xi;
}
return std::exp(-s);
}

} // namespace

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

bool AfanasyevAIntegRectMethodOMP::ValidationImpl() {
return (GetInput() > 0);
}

bool AfanasyevAIntegRectMethodOMP::PreProcessingImpl() {
return true;
}

bool AfanasyevAIntegRectMethodOMP::RunImpl() {
const int n = GetInput();
if (n <= 0) {
return false;
}

const int k_dim = 3;

const double h = 1.0 / static_cast<double>(n);

double sum = 0.0;

#pragma omp parallel for collapse(3) reduction(+ : sum) default(none) shared(n, h, k_dim)
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < n; ++k) {
std::vector<double> x = {(static_cast<double>(i) + 0.5) * h, (static_cast<double>(j) + 0.5) * h,
(static_cast<double>(k) + 0.5) * h};
sum += ExampleIntegrand(x);
}
}
}

const double volume = std::pow(h, k_dim);
GetOutput() = sum * volume;

return true;
}

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

} // namespace afanasyev_a_integ_rect_method
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <tuple>

#include "afanasyev_a_integ_rect_method/common/include/common.hpp"
#include "afanasyev_a_integ_rect_method/omp/include/ops_omp.hpp"
#include "afanasyev_a_integ_rect_method/seq/include/ops_seq.hpp"
#include "util/include/func_test_util.hpp"
#include "util/include/util.hpp"
Expand Down Expand Up @@ -60,7 +61,9 @@ const std::array<TestType, 3> kTestParam = {std::make_tuple(10, "n10"), std::mak
std::make_tuple(80, "n80")};

const auto kTestTasksList = std::tuple_cat(ppc::util::AddFuncTask<AfanasyevAIntegRectMethodSEQ, InType>(
kTestParam, PPC_SETTINGS_afanasyev_a_integ_rect_method));
kTestParam, PPC_SETTINGS_afanasyev_a_integ_rect_method),
ppc::util::AddFuncTask<AfanasyevAIntegRectMethodOMP, InType>(
kTestParam, PPC_SETTINGS_afanasyev_a_integ_rect_method));

const auto kGtestValues = ppc::util::ExpandToValues(kTestTasksList);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cmath>

#include "afanasyev_a_integ_rect_method/common/include/common.hpp"
#include "afanasyev_a_integ_rect_method/omp/include/ops_omp.hpp"
#include "afanasyev_a_integ_rect_method/seq/include/ops_seq.hpp"
#include "util/include/perf_test_util.hpp"

Expand Down Expand Up @@ -40,7 +41,8 @@ TEST_P(AfanasyevAIntegRectMethodPerfTests, RunPerfModes) {
namespace {

const auto kAllPerfTasks =
ppc::util::MakeAllPerfTasks<InType, AfanasyevAIntegRectMethodSEQ>(PPC_SETTINGS_afanasyev_a_integ_rect_method);
ppc::util::MakeAllPerfTasks<InType, AfanasyevAIntegRectMethodSEQ, AfanasyevAIntegRectMethodOMP>(
PPC_SETTINGS_afanasyev_a_integ_rect_method);

const auto kGtestValues = ppc::util::TupleToGTestValues(kAllPerfTasks);

Expand Down
Loading