-
Notifications
You must be signed in to change notification settings - Fork 7
/
TaskCount.hpp
125 lines (99 loc) · 3.33 KB
/
TaskCount.hpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#pragma once
#define BOOST_TEST_DYN_LINK
#include <boost/serialization/export.hpp>
#include "task/Task.hpp"
using namespace combigrid;
/**
* functor for test function $f(x) = \sum_{i=0}^d x_i * (i+1)$
* which maps to points on a hyperplane
*/
template <typename FG_ELEMENT>
class TestFnCount {
public:
// function value
FG_ELEMENT operator()(const std::vector<double>& coords, size_t nrun = 1) {
FG_ELEMENT result = 0.;
for (DimType d = 0; d < coords.size(); ++d) {
result += coords[d] * std::pow(10,d);
}
result *= static_cast<double>(nrun);
return result;
}
};
/* simple task class to set all values on the grid to n_timestep * sum( (d_i + 1) * x_i)
*/
class TaskCount : public combigrid::Task {
public:
TaskCount(const LevelVector& l, const std::vector<BoundaryType>& boundary,
real coeff, LoadModel* loadModel)
: Task(l, boundary, coeff, loadModel), dfg_(nullptr), nrun_(0) {
BOOST_TEST_CHECKPOINT("TaskCount constructor");
}
void init(CommunicatorType lcomm, const std::vector<IndexVector>& decomposition) override {
auto nprocs = getCommSize(lcomm);
std::vector<int> p;
if (decomposition.size() == 0) {
p = {nprocs, 1};
} else {
for (const auto& d : decomposition) {
p.push_back(static_cast<int>(d.size()));
}
}
dfg_ = new OwningDistributedFullGrid<CombiDataType>(getDim(), getLevelVector(), lcomm,
getBoundary(), p, false, decomposition);
auto element = dfg_->getData();
for (size_t i = 0; i < dfg_->getNrLocalElements(); ++i) {
element[i] = -0.;
}
nrun_ = 0;
BOOST_TEST_CHECKPOINT("TaskCount init");
}
void run(CommunicatorType lcomm) override {
// std::cout << "run " << getCommRank(lcomm) << std::endl;
// increase each value by sum( (d_i+1) * x_i)
TestFnCount<CombiDataType> f;
for (IndexType li = 0; li < dfg_->getNrLocalElements(); ++li) {
std::vector<double> coords(getDim());
dfg_->getCoordsLocal(li, coords);
dfg_->getData()[li] += f(coords);
}
++nrun_;
BOOST_CHECK(dfg_);
time_ += 1.;
setFinished(true);
MPI_Barrier(lcomm);
BOOST_TEST_CHECKPOINT("TaskCount run");
}
void getFullGrid(FullGrid<CombiDataType>& fg, RankType r, CommunicatorType lcomm, int n = 0) override {
BOOST_TEST_CHECKPOINT("TaskCount getFullGrid");
dfg_->gatherFullGrid(fg, r);
}
DistributedFullGrid<CombiDataType>& getDistributedFullGrid(size_t n = 0) override { return *dfg_; }
void setZero() override {
BOOST_TEST_CHECKPOINT("TaskCount setZero");
}
~TaskCount() {
BOOST_TEST_CHECKPOINT("TaskCount destructor begin");
if (dfg_ != NULL) delete dfg_;
BOOST_TEST_CHECKPOINT("TaskCount destructor");
}
CombiDataType analyticalSolution(const std::vector<real>& coords, int n = 0) const override {
TestFnCount<CombiDataType> f;
return f(coords, nrun_);
}
real getCurrentTime() const override {
return time_;
}
protected:
TaskCount() : dfg_(NULL) {}
private:
friend class boost::serialization::access;
OwningDistributedFullGrid<CombiDataType>* dfg_;
size_t nrun_;
combigrid::real time_ = 0.;
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar& boost::serialization::base_object<Task>(*this);
// ar& nprocs_;
}
};