Skip to content

Commit c6b2f2d

Browse files
committed
feat: add vector average task (MPI+SEQ) - variant 4
1 parent a89df69 commit c6b2f2d

12 files changed

Lines changed: 324 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <tuple>
5+
#include <vector>
6+
7+
#include "task/include/task.hpp"
8+
9+
namespace nazyrov_a_a_vector_avg {
10+
11+
using InType = std::vector<int>;
12+
using OutType = double;
13+
using TestType = std::tuple<int, std::string>;
14+
using BaseTask = ppc::task::Task<InType, OutType>;
15+
16+
} // namespace nazyrov_a_a_vector_avg
23 Bytes
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"student": {
3+
"full_name": "full_name_p",
4+
"group_number": "2222222_p",
5+
"task_number": "1"
6+
}
7+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include "../../../nazyrov_a_a_vector_avg/common/include/common.hpp"
4+
5+
namespace nazyrov_a_a_vector_avg {
6+
7+
class VectorAvgMPI : public BaseTask {
8+
public:
9+
explicit VectorAvgMPI(const InType &in);
10+
~VectorAvgMPI() override = default;
11+
12+
static constexpr ppc::task::TypeOfTask GetStaticTypeOfTask() {
13+
return ppc::task::TypeOfTask::kMPI;
14+
}
15+
16+
protected:
17+
bool ValidationImpl() override;
18+
bool PreProcessingImpl() override;
19+
bool RunImpl() override;
20+
bool PostProcessingImpl() override;
21+
22+
private:
23+
int world_rank_{0};
24+
int world_size_{1};
25+
};
26+
27+
} // namespace nazyrov_a_a_vector_avg
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "../../../nazyrov_a_a_vector_avg/mpi/include/ops_mpi.hpp"
2+
3+
#include <mpi.h>
4+
5+
#include <numeric>
6+
7+
namespace nazyrov_a_a_vector_avg {
8+
9+
VectorAvgMPI::VectorAvgMPI(const InType &in) : BaseTask() {
10+
GetInput() = in;
11+
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank_);
12+
MPI_Comm_size(MPI_COMM_WORLD, &world_size_);
13+
}
14+
15+
bool VectorAvgMPI::ValidationImpl() {
16+
return true;
17+
}
18+
19+
bool VectorAvgMPI::PreProcessingImpl() {
20+
return true;
21+
}
22+
23+
bool VectorAvgMPI::RunImpl() {
24+
const auto &input = GetInput();
25+
int n = static_cast<int>(input.size());
26+
int local_n = n / world_size_;
27+
int start = world_rank_ * local_n;
28+
int end = (world_rank_ == world_size_ - 1) ? n : start + local_n;
29+
30+
double local_sum = std::accumulate(input.begin() + start, input.begin() + end, 0.0);
31+
int local_count = end - start;
32+
33+
double global_sum = 0.0;
34+
int global_count = 0;
35+
36+
MPI_Reduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
37+
MPI_Reduce(&local_count, &global_count, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
38+
39+
if (world_rank_ == 0) {
40+
GetOutput() = global_sum / static_cast<double>(global_count);
41+
}
42+
43+
return true;
44+
}
45+
46+
bool VectorAvgMPI::PostProcessingImpl() {
47+
return true;
48+
}
49+
50+
} // namespace nazyrov_a_a_vector_avg

tasks/nazyrov_a_a_vector_avg/report.md

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
3+
#include "../../../nazyrov_a_a_vector_avg/common/include/common.hpp"
4+
5+
namespace nazyrov_a_a_vector_avg {
6+
7+
class VectorAvgSEQ : public BaseTask {
8+
public:
9+
explicit VectorAvgSEQ(const InType &in);
10+
~VectorAvgSEQ() override = default;
11+
12+
static constexpr ppc::task::TypeOfTask GetStaticTypeOfTask() {
13+
return ppc::task::TypeOfTask::kSEQ;
14+
}
15+
16+
protected:
17+
bool ValidationImpl() override;
18+
bool PreProcessingImpl() override;
19+
bool RunImpl() override;
20+
bool PostProcessingImpl() override;
21+
};
22+
23+
} // namespace nazyrov_a_a_vector_avg
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "../../../nazyrov_a_a_vector_avg/seq/include/ops_seq.hpp"
2+
3+
#include <numeric>
4+
5+
namespace nazyrov_a_a_vector_avg {
6+
7+
VectorAvgSEQ::VectorAvgSEQ(const InType &in) : BaseTask() {
8+
GetInput() = in;
9+
}
10+
11+
bool VectorAvgSEQ::ValidationImpl() {
12+
return true;
13+
}
14+
15+
bool VectorAvgSEQ::PreProcessingImpl() {
16+
return true;
17+
}
18+
19+
bool VectorAvgSEQ::RunImpl() {
20+
const auto &input = GetInput();
21+
double sum = std::accumulate(input.begin(), input.end(), 0.0);
22+
GetOutput() = sum / static_cast<double>(input.size());
23+
return true;
24+
}
25+
26+
bool VectorAvgSEQ::PostProcessingImpl() {
27+
return true;
28+
}
29+
30+
} // namespace nazyrov_a_a_vector_avg
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"tasks": {
3+
"mpi": "enabled",
4+
"seq": "enabled"
5+
},
6+
"tasks_type": "processes"
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
InheritParentConfig: true
2+
3+
Checks: >
4+
-modernize-loop-convert,
5+
-cppcoreguidelines-avoid-goto,
6+
-cppcoreguidelines-avoid-non-const-global-variables,
7+
-misc-override-with-different-visibility,
8+
-misc-use-anonymous-namespace,
9+
-modernize-use-std-print,
10+
-modernize-type-traits
11+
12+
CheckOptions:
13+
- key: readability-function-cognitive-complexity.Threshold
14+
value: 50 # Relaxed for tests

0 commit comments

Comments
 (0)