-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathops_mpi.cpp
More file actions
93 lines (81 loc) · 2.51 KB
/
ops_mpi.cpp
File metadata and controls
93 lines (81 loc) · 2.51 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "bruskova_v_global_optimization/mpi/include/ops_mpi.hpp"
#include <mpi.h>
#include <algorithm>
#include <limits>
#include <vector>
namespace bruskova_v_global_optimization {
bool BruskovaVGlobalOptimizationMPI::ValidationImpl() {
return taskData->inputs_count[0] == 5;
}
bool BruskovaVGlobalOptimizationMPI::PreProcessingImpl() {
int rank = 0;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
auto *in = reinterpret_cast<double *>(taskData->inputs[0]);
x_min_ = in[0];
x_max_ = in[1];
y_min_ = in[2];
y_max_ = in[3];
step_ = in[4];
}
result_ = {std::numeric_limits<double>::max(), 0.0, 0.0};
return true;
}
bool BruskovaVGlobalOptimizationMPI::RunImpl() {
int size = 0, rank = 0;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
double params[5] = {x_min_, x_max_, y_min_, y_max_, step_};
MPI_Bcast(params, 5, MPI_DOUBLE, 0, MPI_COMM_WORLD);
if (rank != 0) {
x_min_ = params[0];
x_max_ = params[1];
y_min_ = params[2];
y_max_ = params[3];
step_ = params[4];
}
int total_steps = static_cast<int>((x_max_ - x_min_) / step_) + 1;
int local_steps = total_steps / size;
int remainder = total_steps % size;
int start_step = (rank * local_steps) + (rank < remainder ? rank : remainder);
int my_steps = local_steps + (rank < remainder ? 1 : 0);
double local_min[3] = {std::numeric_limits<double>::max(), 0.0, 0.0};
for (int i = 0; i < my_steps; ++i) {
double x = x_min_ + (start_step + i) * step_;
for (double y = y_min_; y <= y_max_; y += step_) {
double val = (x * x) + (y * y);
if (val < local_min[0]) {
local_min[0] = val;
local_min[1] = x;
local_min[2] = y;
}
}
}
if (rank == 0) {
result_ = {local_min[0], local_min[1], local_min[2]};
for (int p = 1; p < size; ++p) {
double recv_min[3];
MPI_Recv(recv_min, 3, MPI_DOUBLE, p, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if (recv_min[0] < result_[0]) {
result_[0] = recv_min[0];
result_[1] = recv_min[1];
result_[2] = recv_min[2];
}
}
} else {
MPI_Send(local_min, 3, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
}
return true;
}
bool BruskovaVGlobalOptimizationMPI::PostProcessingImpl() {
int rank = 0;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
auto *out = reinterpret_cast<double *>(taskData->outputs[0]);
out[0] = result_[0];
out[1] = result_[1];
out[2] = result_[2];
}
return true;
}
} // namespace bruskova_v_global_optimization