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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
/***************************************************************************
* Copyright (C) 2015 by *
* Lam-Tung Nguyen <nltung@gmail.com> *
* *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef MPIHELPER_H
#define MPIHELPER_H
#include <string>
#include <vector>
#include "tools.h"
#include "TreeCollection.h"
#include "ObjectStream.h"
#ifdef _IQTREE_MPI
#include <mpi.h>
#endif
#define PROC_MASTER 0
#define TREE_TAG 1 // Message contain trees
#define STOP_TAG 2 // Stop message
#define BOOT_TAG 3 // Message to please send bootstrap trees
#define BOOT_TREE_TAG 4 // bootstrap tree tag
#define LOGL_CUTOFF_TAG 5 // send logl_cutoff for ultrafast bootstrap
using namespace std;
class MPIHelper {
public:
/**
* Singleton method: get one and only one getInstance of the class
*/
static MPIHelper &getInstance();
/**
destructor
*/
~MPIHelper();
int getNumProcesses() const {
return numProcesses;
}
void setNumProcesses(int numProcesses) {
MPIHelper::numProcesses = numProcesses;
}
int getProcessID() const {
return processID;
}
bool isMaster() const {
return processID == PROC_MASTER;
}
bool isWorker() const {
return processID != PROC_MASTER;
}
void setProcessID(int processID) {
MPIHelper::processID = processID;
}
/** @return true if got any message from another process */
bool gotMessage();
/**
* Receive trees that sent to the current process
*
* @param fromAll
* wait until at least one tree from each remaining process has been received
* @param maxNumTrees
* Only received up to maxNumTrees to prevent the function to block because it can constantly receive
* new trees
* @param trees[OUT]
* Trees received from other processes
* @param tag MPI tag
*/
void receiveTrees(bool fromAll, int maxNumTrees, TreeCollection &trees, int tag);
/**
* Receive trees that sent to the current process
*
* @param trees[OUT]
* Trees received from other processes
* @param tag MPI tag
* @return source process ID
*/
int receiveTrees(TreeCollection &trees, int tag);
/**
* Send trees to all other processes
* @param treeStrings vector of trees
* @param scores vector containing scores of the trees with same order as in treeStrings
* @param tag used to classified the message
*/
void distributeTrees(vector<string> &treeStrings, vector<double> &scores, int tag = TREE_TAG);
/**
* Similar to distributeTrees but only 1 tree is sent
* @param treeString
* @param score
* @param tag
*/
void distributeTree(string treeString, double score, int tag);
/**
* Send trees to a dest process
* @param dest MPI rank of destination process
* @param treeStrings vector of trees
* @param scores vector containing scores of the trees with same order as in treeStrings
* @param tag used to classified the message
*/
void sendTrees(int dest, vector<string> &treeStrings, vector<double> &scores, int tag);
/**
* Send one tree to a dest process
* @param dest MPI rank of destination process
* @param treeString NEWICK tree string
* @param score its score
* @param tag used to classified the message
*/
void sendTree(int dest, string treeString, double score, int tag);
/**
* Blocking Send and then receive trees with a dest process
* @param dest MPI rank of destination process
* @param[in,out] treeString NEWICK tree string
* @param[in,out] score its score
* @param tag used to classified the message
* return the message tag
*/
int sendRecvTrees(int dest, vector<string> &treeStrings, vector<double> &scores, int tag);
/**
* Blocking receive and then send trees with a dest process
* @param dest MPI rank of destination process
* @param[in,out] treeString NEWICK tree string
* @param[in,out] score its score
* @param tag used to classified the message
* return the message tag
*/
int recvSendTrees(vector<string> &treeStrings, vector<double> &scores, vector<bool> &should_send, int tag);
/**
gather trees from workers to master
*/
void gatherTrees(TreeCollection &trees);
/**
broadcase trees from master to works
*/
void broadcastTrees(TreeCollection &trees);
/**
* Send a message to other process, e.g. STOP_TAG
*/
void sendMsg(int tag, string msg);
/**
* Check if a message is received, e.g. STOP_TAG
*/
bool checkMsg(int tag);
/**
* Check if a message is received, e.g. STOP_TAG
*/
bool checkMsg(int tag, string &msg);
/** wrapper for MPI_Send a string
@param str string to send
@param dest destination process
@param tag message tag
*/
#ifdef _IQTREE_MPI
void sendString(string &str, int dest, int tag);
/** wrapper for MPI_Recv a string
@param[out] str string received
@param src source process
@param tag message tag
@return the source process that sent the message
*/
int recvString(string &str, int src = MPI_ANY_SOURCE, int tag = MPI_ANY_TAG);
/** wrapper for MPI_Send an entire Checkpoint object
@param ckp Checkpoint object to send
@param dest destination process
*/
void sendCheckpoint(Checkpoint *ckp, int dest);
/** wrapper for MPI_Recv an entire Checkpoint object
@param[out] ckp Checkpoint object received
@param src source process
@param tag message tag
@return the source process that sent the message
*/
int recvCheckpoint(Checkpoint *ckp, int src = MPI_ANY_SOURCE);
/**
wrapper for MPI_Bcast to broadcast checkpoint from Master to all Workers
@param ckp Checkpoint object
*/
void broadcastCheckpoint(Checkpoint *ckp);
/**
wrapper for MPI_Gather to gather all checkpoints into Master
@param ckp Checkpoint object
*/
void gatherCheckpoint(Checkpoint *ckp);
#endif
void increaseTreeSent(int inc = 1) {
numTreeSent += inc;
}
void increaseTreeReceived(int inc = 1) {
numTreeReceived += inc;
}
private:
/**
* Remove the buffers for finished messages
*/
int cleanUpMessages();
private:
MPIHelper() { }; // Disable constructor
MPIHelper(MPIHelper const &) { }; // Disable copy constructor
void operator=(MPIHelper const &) { }; // Disable assignment
int processID;
int numProcesses;
public:
int getNumTreeReceived() const {
return numTreeReceived;
}
void setNumTreeReceived(int numTreeReceived) {
MPIHelper::numTreeReceived = numTreeReceived;
}
int getNumTreeSent() const {
return numTreeSent;
}
void setNumTreeSent(int numTreeSent) {
MPIHelper::numTreeSent = numTreeSent;
}
void resetNumbers() {
numTreeSent = 0;
numTreeReceived = 0;
numNNISearch = 0;
}
private:
int numTreeSent;
int numTreeReceived;
public:
int getNumNNISearch() const {
return numNNISearch;
}
void setNumNNISearch(int numNNISearch) {
MPIHelper::numNNISearch = numNNISearch;
}
private:
int numNNISearch;
#ifdef _IQTREE_MPI
// A list storing messages and the corresponding requests that have been sent from the current process.
// When a message has been successfully received, it will be deleted from the list
vector< pair<MPI_Request *, ObjectStream *> > sentMessages;
#endif
};
#endif
|