forked from learning-process/parallel_programming_course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathops_mpi.cpp
More file actions
70 lines (54 loc) · 1.55 KB
/
ops_mpi.cpp
File metadata and controls
70 lines (54 loc) · 1.55 KB
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
#include "example_processes/mpi/include/ops_mpi.hpp"
#include <mpi.h>
#include <numeric>
#include <vector>
#include "core/util/include/util.hpp"
#include "example_processes/common/include/common.hpp"
namespace nesterov_a_test_task_processes {
NesterovATestTaskMPI::NesterovATestTaskMPI(const InType &in) {
SetTypeOfTask(GetStaticTypeOfTask());
GetInput() = in;
GetOutput() = 0;
}
bool NesterovATestTaskMPI::ValidationImpl() { return (GetInput() > 0) && (GetOutput() == 0); }
bool NesterovATestTaskMPI::PreProcessingImpl() {
GetOutput() = 2 * GetInput();
return GetOutput() > 0;
}
bool NesterovATestTaskMPI::RunImpl() {
auto input = GetInput();
if (input == 0) {
return false;
}
for (InType i = 0; i < GetInput(); i++) {
for (InType j = 0; j < GetInput(); j++) {
for (InType k = 0; k < GetInput(); k++) {
std::vector<InType> tmp(i + j + k, 1);
GetOutput() += std::accumulate(tmp.begin(), tmp.end(), 0);
GetOutput() -= i + j + k;
}
}
}
const int num_threads = ppc::util::GetNumThreads();
GetOutput() *= num_threads;
int rank = 0;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
GetOutput() /= num_threads;
} else {
int counter = 0;
for (int i = 0; i < num_threads; i++) {
counter++;
}
if (counter != 0) {
GetOutput() /= counter;
}
}
MPI_Barrier(MPI_COMM_WORLD);
return GetOutput() > 0;
}
bool NesterovATestTaskMPI::PostProcessingImpl() {
GetOutput() -= GetInput();
return GetOutput() > 0;
}
} // namespace nesterov_a_test_task_processes