MAiNGO
mpiUtilities.h
Go to the documentation of this file.
1 /**********************************************************************************
2  * Copyright (c) 2019 Process Systems Engineering (AVT.SVT), RWTH Aachen University
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License 2.0 which is available at
6  * http://www.eclipse.org/legal/epl-2.0.
7  *
8  * SPDX-License-Identifier: EPL-2.0
9  *
10  **********************************************************************************/
11 
12 #pragma once
13 
14 #ifdef HAVE_MAiNGO_MPI
15 #include "babNode.h"
16 #include "mpi.h"
17 
18 #define MAiNGO_IF_BAB_MANAGER if (_rank == 0) {
19 #define MAiNGO_IF_BAB_WORKER if (_rank != 0) {
20 #define MAiNGO_ELSE \
21  } \
22  else \
23  {
24 #define MAiNGO_END_IF }
25 
26 #define MAiNGO_MPI_BARRIER MPI_Barrier(MPI_COMM_WORLD);
27 #define MAiNGO_MPI_FINALIZE MPI_Finalize();
28 #else
29 #define MAiNGO_IF_BAB_MANAGER
30 #define MAiNGO_IF_BAB_WORKER
31 #define MAiNGO_ELSE
32 #define MAiNGO_END_IF
33 
34 #define MAiNGO_MPI_BARRIER
35 #define MAiNGO_MPI_FINALIZE
36 
37 #endif
38 
39 #ifdef HAVE_MAiNGO_MPI
40 
41 namespace maingo {
42 
43 
48 enum BCAST_TAG {
49  BCAST_NOTHING_PENDING = 0,
50  BCAST_EXCEPTION,
51  BCAST_EVERYTHING_FINE,
52  BCAST_TIGHTENING_INFEASIBLE,
53  BCAST_CONSTR_PROP_INFEASIBLE,
54  BCAST_INFEASIBLE,
55  BCAST_FEASIBLE,
56  BCAST_TERMINATE,
57  BCAST_FOUND_FEAS,
58  BCAST_SCALING_NEEDED
59 };
60 
65 enum COMMUNICATION_TAG {
66  TAG_EXCEPTION = 0,
67  TAG_FOUND_INCUMBENT,
68  TAG_NEW_INCUMBENT,
69  TAG_NEW_INCUMBENT_ID,
70  TAG_NODE_REQUEST,
71  TAG_NEW_NODE_NO_INCUMBENT,
72  TAG_NEW_NODE_NEW_INCUMBENT,
73  TAG_NEW_NODE_UBD,
74  TAG_SOLVED_NODE_STATUS_NORMAL,
75  TAG_SOLVED_NODE_STATUS_CONVERGED,
76  TAG_SOLVED_NODE_STATUS_INFEAS,
77  TAG_SOLVED_NODE_LBD,
78  TAG_SOLVED_NODE_SOLUTION_POINT,
79  TAG_SOLVED_NODE_STATISTICS,
80  TAG_NODE_ID,
81  TAG_NODE_PRUNING_SCORE,
82  TAG_NODE_LOWER_BOUNDS,
83  TAG_NODE_UPPER_BOUNDS,
84  TAG_NODE_HAS_INCUMBENT,
85  TAG_NODE_DEPTH,
86  TAG_WORKER_FINISHED,
87  TAG_MS_STOP_SOLVING,
88  TAG_MS_NEW_POINT,
89  TAG_MS_SOLUTION,
90  TAG_MS_FEAS,
91  TAG_MS_INFEAS,
92 };
93 
100 inline void
101 send_babnode(const babBase::BabNode &node, const int dest)
102 {
103 
104  int id = node.get_ID();
105  std::vector<double> lb = node.get_lower_bounds();
106  std::vector<double> ub = node.get_upper_bounds();
107  int hI = node.holds_incumbent() ? 1 : 0;
108  double pruningScore = node.get_pruning_score();
109  int depth = node.get_depth();
110  // Send all information
111  MPI_Ssend(&id, 1, MPI_INT, dest, TAG_NODE_ID, MPI_COMM_WORLD);
112  MPI_Ssend(&pruningScore, 1, MPI_DOUBLE, dest, TAG_NODE_PRUNING_SCORE, MPI_COMM_WORLD);
113  MPI_Ssend(lb.data(), lb.size(), MPI_DOUBLE, dest, TAG_NODE_LOWER_BOUNDS, MPI_COMM_WORLD);
114  MPI_Ssend(ub.data(), lb.size(), MPI_DOUBLE, dest, TAG_NODE_UPPER_BOUNDS, MPI_COMM_WORLD);
115  MPI_Ssend(&hI, 1, MPI_INT, dest, TAG_NODE_HAS_INCUMBENT, MPI_COMM_WORLD);
116  MPI_Ssend(&depth, 1, MPI_INT, dest, TAG_NODE_DEPTH, MPI_COMM_WORLD);
117 }
118 
126 inline void
127 recv_babnode(babBase::BabNode &node, const int source, const unsigned nvar)
128 {
129 
130  int id;
131  std::vector<double> lb(nvar, 0);
132  std::vector<double> ub(nvar, 0);
133  int hI;
134  double pruningScore;
135  int depth;
136 
137 
138  MPI_Recv(&id, 1, MPI_INT, source, TAG_NODE_ID, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
139  MPI_Recv(&pruningScore, 1, MPI_DOUBLE, source, TAG_NODE_PRUNING_SCORE, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
140  MPI_Recv(lb.data(), nvar, MPI_DOUBLE, source, TAG_NODE_LOWER_BOUNDS, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
141  MPI_Recv(ub.data(), nvar, MPI_DOUBLE, source, TAG_NODE_UPPER_BOUNDS, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
142  MPI_Recv(&hI, 1, MPI_INT, source, TAG_NODE_HAS_INCUMBENT, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
143  MPI_Recv(&depth, 1, MPI_INT, source, TAG_NODE_DEPTH, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
144 
145  node = babBase::BabNode(pruningScore, lb, ub, id, depth, hI == 1);
146 }
147 
157 inline void
158 recv_vector_double(std::vector<double> &vec, int source, int tag, MPI_Comm comm, MPI_Status *status)
159 {
160 
161  MPI_Status probeStatus;
162  int messageSize;
163 
164  MPI_Probe(source, tag, MPI_COMM_WORLD, &probeStatus);
165  MPI_Get_count(&probeStatus, MPI_DOUBLE, &messageSize);
166  vec.resize(messageSize);
167 
168  MPI_Recv(vec.data(), messageSize, MPI_DOUBLE, source, tag, comm, status);
169 }
170 
180 inline void
181 recv_vector_int(std::vector<int> &vec, int source, int tag, MPI_Comm comm, MPI_Status *status)
182 {
183 
184  MPI_Status probeStatus;
185  int messageSize;
186 
187  MPI_Probe(source, tag, MPI_COMM_WORLD, &probeStatus);
188  MPI_Get_count(&probeStatus, MPI_INT, &messageSize);
189  vec.resize(messageSize);
190 
191  MPI_Recv(vec.data(), messageSize, MPI_INT, source, tag, comm, status);
192 }
193 
201 struct WorkerNodeComparator {
209  bool operator()(const std::pair<bool, double> &a, const std::pair<bool, double> &b) const
210  {
211  if (a.first) {
212  if (b.first) {
213  return a.second < b.second;
214  }
215  else {
216  return true;
217  }
218  }
219  else {
220  return false;
221  }
222  };
223 };
224 
225 
226 } // end namespace maingo
227 
228 #endif
int get_ID() const
Function for querying the node ID.
Definition: babNode.h:100
Class representing a node in the Branch-and-Bound tree.
Definition: babNode.h:35
std::vector< double > get_lower_bounds() const
Function for querying the lower bounds on the optimization variables within this node.
Definition: babNode.h:90
int get_depth() const
Function for querying the node depth.
Definition: babNode.h:105
namespace holding all essentials of MAiNGO
Definition: aleModel.h:25
bool holds_incumbent() const
Function obtaining information whether the node holds the incumbent.
Definition: babNode.h:110
std::vector< double > get_upper_bounds() const
Function for querying the upper bounds on the optimization variables within this node.
Definition: babNode.h:95
double get_pruning_score() const
Function for querying the pruning score within this node.
Definition: babNode.h:80