XdmfDSMCommMPI.cpp 9.06 KB
Newer Older
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
/*****************************************************************************/
/*                                    XDMF                                   */
/*                       eXtensible Data Model and Format                    */
/*                                                                           */
/*  Id : XdmfDSMCommMPI.cpp                                                  */
/*                                                                           */
/*  Author:                                                                  */
/*     Andrew Burns                                                          */
/*     andrew.j.burns2@us.army.mil                                           */
/*     US Army Research Laboratory                                           */
/*     Aberdeen Proving Ground, MD                                           */
/*                                                                           */
/*     Copyright @ 2013 US Army Research Laboratory                          */
/*     All Rights Reserved                                                   */
/*     See Copyright.txt for details                                         */
/*                                                                           */
/*     This software is distributed WITHOUT ANY WARRANTY; without            */
/*     even the implied warranty of MERCHANTABILITY or FITNESS               */
/*     FOR A PARTICULAR PURPOSE.  See the above copyright notice             */
/*     for more information.                                                 */
/*                                                                           */
/*****************************************************************************/

/*=========================================================================
  This code is derived from an earlier work and is distributed
  with permission from, and thanks to ...
=========================================================================*/

/*============================================================================

  Project                 : H5FDdsm
  Module                  : H5FDdsmCommMpi.cxx

  Authors:
     John Biddiscombe     Jerome Soumagne
     biddisco@cscs.ch     soumagne@cscs.ch

  Copyright (C) CSCS - Swiss National Supercomputing Centre.
  You may use modify and and distribute this code freely providing
  1) This copyright notice appears on all copies of source code
  2) An acknowledgment appears with any substantial usage of the code
  3) If this code is contributed to any other open source project, it
  must not be reformatted such that the indentation, bracketing or
  overall style is modified significantly.

  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  This work has received funding from the European Community's Seventh
  Framework Programme (FP7/2007-2013) under grant agreement 225967 âxtMuSEâOC

============================================================================*/

#include <XdmfDSMCommMPI.hpp>
#include <XdmfError.hpp>
#include <mpi.h>
#include <string.h>

XdmfDSMCommMPI::XdmfDSMCommMPI()
{
61 62 63 64 65 66 67
  IntraComm = MPI_COMM_NULL;
  Id = -1;
  IntraSize = -1;
  InterComm = MPI_COMM_NULL;
  InterSize = -1;
  SetDsmMasterHostName("");
  InterCommType = XDMF_DSM_COMM_MPI;
68 69 70 71
}

XdmfDSMCommMPI::~XdmfDSMCommMPI()
{
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  if (InterComm != MPI_COMM_NULL) {
    int status = MPI_Comm_free(&InterComm);
    if (status != MPI_SUCCESS) {
      try {
                          XdmfError::message(XdmfError::FATAL, "Failed to free intercomm Comm");
      }
      catch (XdmfError e) {
        throw e;
      }
    }
  }
  if (IntraComm != MPI_COMM_NULL) {
    int status = MPI_Comm_free(&IntraComm);
    if (status != MPI_SUCCESS) {
      try {
        XdmfError::message(XdmfError::FATAL, "Failed to free intercomm Comm");
      }
      catch (XdmfError e) {
        throw e;
      }
    }
  }
94 95 96 97 98
}

void
XdmfDSMCommMPI::SetDsmMasterHostName(const char *hostName)
{
99
  strcpy(DsmMasterHostName, hostName);
100 101 102 103 104
}

char *
XdmfDSMCommMPI::GetDsmMasterHostName()
{
105
  return DsmMasterHostName;
106 107 108 109 110
}

int
XdmfDSMCommMPI::GetId()
{
111
  return this->Id;
112 113 114 115 116
}

int
XdmfDSMCommMPI::GetIntraSize()
{
117
  return this->IntraSize;
118 119 120 121 122
}

int
XdmfDSMCommMPI::GetInterSize()
{
123
  return this->InterSize;
124 125 126 127 128
}

int
XdmfDSMCommMPI::GetInterCommType()
{
129
  return this->InterCommType;
130 131 132 133 134
}

void
XdmfDSMCommMPI::Init()
{
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  int size, rank;
  if (MPI_Comm_size(this->IntraComm, &size) != MPI_SUCCESS) {
    try {
      XdmfError::message(XdmfError::FATAL, "Failed to initialize size");
    }
    catch (XdmfError e) {
      throw e;
    }
  }
  if (MPI_Comm_rank(this->IntraComm, &rank) != MPI_SUCCESS) {
    try {
      XdmfError::message(XdmfError::FATAL, "Failed to initialize rank");
    }
    catch (XdmfError e) {
      throw e;
    }
  }
152

153 154
  this->Id = rank;
  this->IntraSize = size;
155 156 157 158 159
}

void
XdmfDSMCommMPI::OpenPort()
{
160 161 162 163 164 165 166 167 168 169 170 171 172
  if (Id == 0) {
    int status = MPI_Open_port(MPI_INFO_NULL, DsmMasterHostName);
    if (status != MPI_SUCCESS) {
      try {
        std::string message = "Failed to open port ";
        message = message + DsmMasterHostName;
        XdmfError::message(XdmfError::FATAL, message);
      }
      catch (XdmfError e) {
        throw e;
      }
    }
  }
173 174 175 176 177
}

void
XdmfDSMCommMPI::ClosePort()
{
178 179 180 181 182 183 184 185 186 187 188 189 190 191
  if (Id == 0) {
    int status;
    status = MPI_Open_port(MPI_INFO_NULL, DsmMasterHostName);
    if (status != MPI_SUCCESS) {
      try {
        std::string message = "Failed to close port ";
        message = message + DsmMasterHostName;
        XdmfError::message(XdmfError::FATAL, message);
      }
      catch (XdmfError e) {
        throw e;
      }
    }
  }
192 193 194 195 196
}

void
XdmfDSMCommMPI::Accept()
{
197 198 199 200 201 202 203 204 205 206 207 208 209 210
  int status = MPI_Comm_accept(DsmMasterHostName, MPI_INFO_NULL, 0, IntraComm, &InterComm);
  if (status != MPI_SUCCESS) {
    try {
      std::string message = "Failed to accept port ";
      message = message + DsmMasterHostName;
      XdmfError::message(XdmfError::FATAL, message);
    }
    catch (XdmfError e) {
      throw e;
    }
  }
  else {
    MPI_Comm_remote_size(InterComm, &InterSize);
  }
211 212 213 214 215
}

int
XdmfDSMCommMPI::Connect()
{
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
  if (InterComm != MPI_COMM_NULL) {
    // If the intercomm already exists, no need to connect
    // If you want to reset the intercomm, set it to MPI_COMM_NULL before calling this
    // using either SetInterComm or Disconnect
    return MPI_SUCCESS;
  }
  else {
    MPI_Errhandler_set(IntraComm, MPI_ERRORS_RETURN);
    int status = MPI_Comm_connect(DsmMasterHostName, MPI_INFO_NULL, 0, IntraComm, &InterComm);
    MPI_Errhandler_set(IntraComm, MPI_ERRORS_ARE_FATAL);
    if (status != MPI_SUCCESS) {
      try {
        std::string message = "Failed to connect to port ";
        message = message + DsmMasterHostName;
        XdmfError::message(XdmfError::FATAL, message);
      }
      catch (XdmfError e) {
        throw e;
      }
    }
    else {
      status = MPI_Comm_remote_size(InterComm, &InterSize);
      return MPI_SUCCESS;
    }
  }
  return MPI_SUCCESS;
242 243 244 245 246
}

void
XdmfDSMCommMPI::Disconnect()
{
247 248 249 250 251 252 253 254 255 256 257 258
  if (InterComm != MPI_COMM_NULL) {
    int status = MPI_Comm_free(&InterComm);
    if (status != MPI_SUCCESS) {
      try {
        XdmfError::message(XdmfError::FATAL, "Failed to disconnect Comm");
      }
      catch (XdmfError e) {
        throw e;
      }
    }
  }
  InterComm = MPI_COMM_NULL;
259 260 261 262 263
}

void
XdmfDSMCommMPI::DupComm(MPI_Comm comm)
{
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
  if (IntraComm != comm) {
    int status;
    if (IntraComm != MPI_COMM_NULL) {
      status = MPI_Comm_free(&IntraComm);
      if (status != MPI_SUCCESS) {
        try {
                            XdmfError::message(XdmfError::FATAL, "Failed to disconnect Comm");
        }
        catch (XdmfError e) {
          throw e;
        }
      }
    }
    if (comm != MPI_COMM_NULL) {
      status = MPI_Comm_dup(comm, &IntraComm);
      if (status != MPI_SUCCESS) {
        try {
          XdmfError::message(XdmfError::FATAL, "Failed to duplicate Comm");
        }
        catch (XdmfError e) {
          throw e;
        }
      }
      else {
        status = MPI_Comm_size(IntraComm, &IntraSize);
        status = MPI_Comm_rank(IntraComm, &Id);
      }
    }
  }
293 294 295 296 297
}

void
XdmfDSMCommMPI::DupInterComm(MPI_Comm comm)
{
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
  if (InterComm != comm) {
    int status;
    if (InterComm != MPI_COMM_NULL) {
      status = MPI_Comm_free(&InterComm);
      if (status != MPI_SUCCESS) {
        try {
          XdmfError::message(XdmfError::FATAL, "Failed to disconnect Comm");
        }
        catch (XdmfError e) {
          throw e;
        }
      }
    }
    if (comm != MPI_COMM_NULL) {
      status = MPI_Comm_dup(comm, &InterComm);
      if (status != MPI_SUCCESS) {
        try {
          XdmfError::message(XdmfError::FATAL, "Failed to duplicate Comm");
        }
        catch (XdmfError e) {
          throw e;
        }
      }
      else {
        status = MPI_Comm_size(InterComm, &InterSize);
        if (status != MPI_SUCCESS) {
          MPI_Comm_remote_size(InterComm, &InterSize);
        }
      }
    }
    else {
      InterSize = -1;
    }
  }
332 333 334 335 336
}

MPI_Comm
XdmfDSMCommMPI::GetInterComm()
{
337
  return InterComm;
338 339 340 341 342
}

MPI_Comm
XdmfDSMCommMPI::GetIntraComm()
{
343
  return IntraComm;
344
}