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
|
/***************************************************************************
* 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 "utils/tools.h"
#include "utils/checkpoint.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();
/** initialize MPI */
void init(int argc, char *argv[]);
/** finalize MPI */
void finalize();
/**
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;
}
/** synchronize random seed from master to all workers */
void syncRandomSeed();
/** count the number of host with the same name as the current host */
int countSameHost();
/** @return true if got any message from another process */
bool gotMessage();
/** 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;
};
#endif
|