Comm.h 18.2 KB
Newer Older
1 2
///////////////////////////////////////////////////////////////////////////////
//
3
// File Comm.h
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
//
// For more information, please see: http://www.nektar.info
//
// The MIT License
//
// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
// Department of Aeronautics, Imperial College London (UK), and Scientific
// Computing and Imaging Institute, University of Utah (USA).
//
// License for the specific language governing rights and limitations under
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
32
// Description: Base communication class
33 34 35 36 37
//
///////////////////////////////////////////////////////////////////////////////
#ifndef NEKTAR_LIB_UTILITIES_COMM_H
#define NEKTAR_LIB_UTILITIES_COMM_H

38 39
#include <vector>

40
#include <LibUtilities/Communication/GsLib.hpp>
41
#include <LibUtilities/BasicUtils/NekFactory.hpp>
42
#include <LibUtilities/LibUtilitiesDeclspec.h>
David Moxey's avatar
David Moxey committed
43 44
#include <boost/enable_shared_from_this.hpp>
#include <boost/static_assert.hpp>
45

46
#include <LibUtilities/BasicConst/NektarUnivTypeDefs.hpp>
David Moxey's avatar
David Moxey committed
47
// namespace Nektar { template <typename Dim, typename DataType> class Array; }
48
#include <LibUtilities/BasicUtils/SharedArray.hpp>
49
#include <LibUtilities/Communication/CommDataType.h>
50

51 52
namespace Nektar
{
David Moxey's avatar
David Moxey committed
53 54 55 56
namespace LibUtilities
{
// Forward declarations
class Comm;
57

David Moxey's avatar
David Moxey committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
/// Pointer to a Communicator object.
typedef boost::shared_ptr<Comm> CommSharedPtr;

/// Datatype of the NekFactory used to instantiate classes derived from
/// the EquationSystem class.
typedef LibUtilities::NekFactory<std::string, Comm, int, char **> CommFactory;

LIB_UTILITIES_EXPORT CommFactory &GetCommFactory();

/// Type of operation to perform in AllReduce.
enum ReduceOperator
{
    ReduceSum,
    ReduceMax,
    ReduceMin
};

75 76 77
typedef int GsHandleId;
struct GsHandle;

David Moxey's avatar
David Moxey committed
78 79 80 81 82 83 84 85 86
/// Base communications class
class Comm : public boost::enable_shared_from_this<Comm>
{
public:
    LIB_UTILITIES_EXPORT Comm(int narg, char *arg[]);
    LIB_UTILITIES_EXPORT virtual ~Comm();

    LIB_UTILITIES_EXPORT inline void Finalise();

87 88
    LIB_UTILITIES_EXPORT inline void* GetComm();

David Moxey's avatar
David Moxey committed
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
    /// Returns number of processes
    LIB_UTILITIES_EXPORT inline int GetSize();
    LIB_UTILITIES_EXPORT inline int GetRank();
    LIB_UTILITIES_EXPORT inline const std::string &GetType() const;

    /// Block execution until all processes reach this point
    LIB_UTILITIES_EXPORT inline void Block();

    /// Return the time in seconds
    LIB_UTILITIES_EXPORT inline NekDouble Wtime();

    template <class T> void Send(int pProc, T &pData);
    template <class T> void Recv(int pProc, T &pData);
    template <class T>
    void SendRecv(int pSendProc, T &pSendData, int pRecvProc, T &pRecvData);
    template <class T>
    void SendRecvReplace(int pSendProc, int pRecvProc, T &pData);

    template <class T> void AllReduce(T &pData, enum ReduceOperator pOp);

    template <class T> void AlltoAll(T &pSendData, T &pRecvData);
    template <class T>
    void AlltoAllv(Array<OneD, T> &pSendData,
                   Array<OneD, int> &pSendDataSizeMap,
                   Array<OneD, int> &pSendDataOffsetMap,
                   Array<OneD, T> &pRecvData,
                   Array<OneD, int> &pRecvDataSizeMap,
                   Array<OneD, int> &pRecvDataOffsetMap);

    template <class T> void Bcast(T &data, int rootProc);

    template <class T>
    void Exscan(T &pData, const enum ReduceOperator pOp, T &ans);

    template <class T> T Gather(const int rootProc, T &val);
    template <class T> T Scatter(const int rootProc, T &pData);

126
    LIB_UTILITIES_EXPORT inline CommSharedPtr CommCreateIf(int colour);
David Moxey's avatar
David Moxey committed
127 128 129 130 131 132 133 134

    LIB_UTILITIES_EXPORT inline void SplitComm(int pRows, int pColumns);
    LIB_UTILITIES_EXPORT inline CommSharedPtr GetRowComm();
    LIB_UTILITIES_EXPORT inline CommSharedPtr GetColumnComm();

    LIB_UTILITIES_EXPORT inline bool TreatAsRankZero(void);
    LIB_UTILITIES_EXPORT inline bool RemoveExistingFiles(void);

135 136 137 138 139 140 141 142 143 144 145
    LIB_UTILITIES_EXPORT inline GsHandle GsInit(
            const Nektar::Array<OneD, long> pId,
            bool verbose = true);
    LIB_UTILITIES_EXPORT inline void GsUnique(
            const Nektar::Array<OneD, long> pId);
    LIB_UTILITIES_EXPORT inline void GsGather(
            Nektar::Array<OneD, NekDouble> pU,
            Gs::gs_op pOp,
            GsHandleId pGsh,
            Nektar::Array<OneD, NekDouble> pBuffer = NullNekDouble1DArray);

146
    LIB_UTILITIES_EXPORT inline int EnrolSpare();
147 148
    LIB_UTILITIES_EXPORT inline void BeginTransactionLog();
    LIB_UTILITIES_EXPORT inline void EndTransactionLog();
149 150 151
    LIB_UTILITIES_EXPORT inline bool IsRecovering();

    LIB_UTILITIES_EXPORT inline void StateAdd(const std::string& name, const int& data);
152
    LIB_UTILITIES_EXPORT inline void StateAdd(const std::string& name, const NekDouble& data);
153 154
    LIB_UTILITIES_EXPORT inline void StateAdd(const std::string& name, const NekDouble* data, const int n);
    LIB_UTILITIES_EXPORT inline void StateGet(const std::string& name, int& data);
155
    LIB_UTILITIES_EXPORT inline void StateGet(const std::string& name, NekDouble& data);
156 157 158
    LIB_UTILITIES_EXPORT inline void StateGet(const std::string& name, NekDouble* data, const int n);
    LIB_UTILITIES_EXPORT inline void StateCommit();
    LIB_UTILITIES_EXPORT inline void StateRestore();
159

David Moxey's avatar
David Moxey committed
160 161 162 163 164 165 166 167 168
protected:
    int m_size;                 ///< Number of processes
    std::string m_type;         ///< Type of communication
    CommSharedPtr m_commRow;    ///< Row communicator
    CommSharedPtr m_commColumn; ///< Column communicator

    Comm();

    virtual void v_Finalise()   = 0;
169
    virtual void* v_GetComm()     = 0;
David Moxey's avatar
David Moxey committed
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
    virtual int v_GetRank()     = 0;
    virtual void v_Block()      = 0;
    virtual NekDouble v_Wtime() = 0;
    virtual void v_Send(void *buf, int count, CommDataType dt, int dest)   = 0;
    virtual void v_Recv(void *buf, int count, CommDataType dt, int source) = 0;
    virtual void v_SendRecv(void *sendbuf, int sendcount, CommDataType sendtype,
                            int dest, void *recvbuf, int recvcount,
                            CommDataType recvtype, int source) = 0;
    virtual void v_SendRecvReplace(void *buf, int count, CommDataType dt,
                                   int pSendProc, int pRecvProc) = 0;
    virtual void v_AllReduce(void *buf, int count, CommDataType dt,
                             enum ReduceOperator pOp) = 0;
    virtual void v_AlltoAll(void *sendbuf, int sendcount, CommDataType sendtype,
                            void *recvbuf, int recvcount,
                            CommDataType recvtype) = 0;
    virtual void v_AlltoAllv(void *sendbuf, int sendcounts[], int sensdispls[],
                             CommDataType sendtype, void *recvbuf,
                             int recvcounts[], int rdispls[],
                             CommDataType recvtype) = 0;
    virtual void v_Bcast(void *buffer, int count, CommDataType dt,
                         int root) = 0;

    virtual void v_Exscan(Array<OneD, unsigned long long> &pData,
                          const enum ReduceOperator pOp,
                          Array<OneD, unsigned long long> &ans) = 0;

    virtual void v_Gather(void *sendbuf, int sendcount, CommDataType sendtype,
                          void *recvbuf, int recvcount, CommDataType recvtype,
                          int root) = 0;
    virtual void v_Scatter(void *sendbuf, int sendcount, CommDataType sendtype,
                           void *recvbuf, int recvcount, CommDataType recvtype,
                           int root) = 0;

203 204 205 206 207 208 209 210 211 212
    virtual GsHandle v_GsInit(const Nektar::Array<OneD, long> pId,
                            bool verbose) = 0;
    virtual void v_GsUnique(
            const Nektar::Array<OneD, long> pId) = 0;
    virtual void v_GsGather(
            Nektar::Array<OneD, NekDouble> pU,
            Gs::gs_op pOp,
            GsHandleId pGsh,
            Nektar::Array<OneD, NekDouble> pBuffer) = 0;

213
    virtual CommSharedPtr v_CommCreateIf(int colour) = 0;
David Moxey's avatar
David Moxey committed
214 215 216
    virtual void v_SplitComm(int pRows, int pColumns) = 0;
    virtual bool v_TreatAsRankZero(void) = 0;
    LIB_UTILITIES_EXPORT virtual bool v_RemoveExistingFiles(void);
217 218

    virtual int v_EnrolSpare() = 0;
219 220
    virtual void v_BeginTransactionLog() {}
    virtual void v_EndTransactionLog() {}
221 222 223
    virtual bool v_IsRecovering() { return false; }

    virtual void v_StateAdd(const std::string& name, const int& data) {}
224
    virtual void v_StateAdd(const std::string& name, const NekDouble& data) {}
225 226
    virtual void v_StateAdd(const std::string& name, const NekDouble* data, const int n) {}
    virtual void v_StateGet(const std::string& name, int& data) {}
227
    virtual void v_StateGet(const std::string& name, NekDouble& data) {}
228 229 230
    virtual void v_StateGet(const std::string& name, NekDouble* data, const int n) {}
    virtual void v_StateCommit() {}
    virtual void v_StateRestore() {}
231

David Moxey's avatar
David Moxey committed
232 233
};

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
struct GsHandle {

    GsHandle() : comm(CommSharedPtr()), idx(0) {}
    GsHandle(const GsHandle& pSrc) : comm(pSrc.comm), idx(pSrc.idx) {}
    GsHandle(CommSharedPtr pComm, GsHandleId pId)
        : comm(pComm), idx(pId) {}

    void Gather( Nektar::Array<OneD, NekDouble> pU,
            Gs::gs_op pOp,
            Nektar::Array<OneD, NekDouble> pBuffer = NullNekDouble1DArray) const
    {
        comm->GsGather(pU, pOp, idx, pBuffer);
    }

    CommSharedPtr comm;
    GsHandleId idx;

};


David Moxey's avatar
David Moxey committed
254 255 256 257 258 259 260 261
/**
 *
 */
inline void Comm::Finalise()
{
    v_Finalise();
}

262 263 264 265 266 267 268 269
/**
 *
 */
inline void* Comm::GetComm()
{
    return v_GetComm();
}

David Moxey's avatar
David Moxey committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
/**
 *
 */
inline int Comm::GetSize()
{
    return m_size;
}

/**
 *
 */
inline int Comm::GetRank()
{
    return v_GetRank();
}

/**
 *
 */
inline const std::string &Comm::GetType() const
{
    return m_type;
}
293

David Moxey's avatar
David Moxey committed
294 295 296 297 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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
/**
 *
 */
inline void Comm::Block()
{
    v_Block();
}

/**
 *
 */
inline double Comm::Wtime()
{
    return v_Wtime();
}

template <class T> void Comm::Send(int pProc, T &pData)
{
    v_Send(CommDataTypeTraits<T>::GetPointer(pData),
           CommDataTypeTraits<T>::GetCount(pData),
           CommDataTypeTraits<T>::GetDataType(), pProc);
}

template <class T> void Comm::Recv(int pProc, T &pData)
{
    v_Recv(CommDataTypeTraits<T>::GetPointer(pData),
           CommDataTypeTraits<T>::GetCount(pData),
           CommDataTypeTraits<T>::GetDataType(), pProc);
}

/**
 *
 */
template <class T>
void Comm::SendRecv(int pSendProc, T &pSendData, int pRecvProc, T &pRecvData)
{
    v_SendRecv(CommDataTypeTraits<T>::GetPointer(pSendData),
               CommDataTypeTraits<T>::GetCount(pSendData),
               CommDataTypeTraits<T>::GetDataType(), pSendProc,
               CommDataTypeTraits<T>::GetPointer(pRecvData),
               CommDataTypeTraits<T>::GetCount(pRecvData),
               CommDataTypeTraits<T>::GetDataType(), pRecvProc);
}

/**
 *
 */
template <class T>
void Comm::SendRecvReplace(int pSendProc, int pRecvProc, T &pData)
{
    v_SendRecvReplace(CommDataTypeTraits<T>::GetPointer(pData),
                      CommDataTypeTraits<T>::GetCount(pData),
                      CommDataTypeTraits<T>::GetDataType(), pSendProc,
                      pRecvProc);
}

/**
 *
 */
template <class T> void Comm::AllReduce(T &pData, enum ReduceOperator pOp)
{
    v_AllReduce(CommDataTypeTraits<T>::GetPointer(pData),
                CommDataTypeTraits<T>::GetCount(pData),
                CommDataTypeTraits<T>::GetDataType(), pOp);
}

template <class T> void Comm::AlltoAll(T &pSendData, T &pRecvData)
{
    BOOST_STATIC_ASSERT_MSG(
        CommDataTypeTraits<T>::IsVector,
        "AlltoAll only valid with Array or vector arguments.");
    int sendSize = CommDataTypeTraits<T>::GetCount(pSendData);
    int recvSize = CommDataTypeTraits<T>::GetCount(pRecvData);
    ASSERTL0(sendSize == recvSize,
             "Send and Recv arrays have incompatible sizes in AlltoAll");

    int count = sendSize / GetSize();
    ASSERTL0(count * GetSize() == sendSize,
             "Array size incompatible with size of communicator");

    v_AlltoAll(CommDataTypeTraits<T>::GetPointer(pSendData), count,
               CommDataTypeTraits<T>::GetDataType(),
               CommDataTypeTraits<T>::GetPointer(pRecvData), count,
               CommDataTypeTraits<T>::GetDataType());
}

/**
 *
 */
template <class T>
void Comm::AlltoAllv(Array<OneD, T> &pSendData,
                     Array<OneD, int> &pSendDataSizeMap,
                     Array<OneD, int> &pSendDataOffsetMap,
                     Array<OneD, T> &pRecvData,
                     Array<OneD, int> &pRecvDataSizeMap,
                     Array<OneD, int> &pRecvDataOffsetMap)
{
    v_AlltoAllv(pSendData.get(), pSendDataSizeMap.get(),
                pSendDataOffsetMap.get(), CommDataTypeTraits<T>::GetDataType(),
                pRecvData.get(), pRecvDataSizeMap.get(),
                pRecvDataOffsetMap.get(), CommDataTypeTraits<T>::GetDataType());
}

/**
 *
 */
template <class T> void Comm::Bcast(T &pData, int pRoot)
{
    v_Bcast(CommDataTypeTraits<T>::GetPointer(pData),
            CommDataTypeTraits<T>::GetCount(pData),
            CommDataTypeTraits<T>::GetDataType(), pRoot);
}

template <class T>
void Comm::Exscan(T &pData, const enum ReduceOperator pOp, T &ans)
{
    ASSERTL0(CommDataTypeTraits<T>::GetCount(pData) ==
                 CommDataTypeTraits<T>::GetCount(ans),
             "Input and output array sizes don't match");
    v_Exscan(CommDataTypeTraits<T>::GetPointer(pData),
             CommDataTypeTraits<T>::GetPointer(ans),
             CommDataTypeTraits<T>::GetCount(pData),
             CommDataTypeTraits<T>::GetDataType(), pOp);
}

/**
 * Concatenate all the input arrays, in rank order, onto the process with rank
 * == rootProc
 */
template <class T> T Comm::Gather(const int rootProc, T &val)
{
    BOOST_STATIC_ASSERT_MSG(
        CommDataTypeTraits<T>::IsVector,
        "Gather only valid with Array or vector arguments.");
    bool amRoot  = (GetRank() == rootProc);
    unsigned nEl = CommDataTypeTraits<T>::GetCount(val);

    unsigned nOut = amRoot ? GetSize() * nEl : 0;
    T ans(nOut);
    void *recvbuf = amRoot ? CommDataTypeTraits<T>::GetPointer(ans) : NULL;

    v_Gather(CommDataTypeTraits<T>::GetPointer(val), nEl,
             CommDataTypeTraits<T>::GetDataType(), recvbuf, nEl,
             CommDataTypeTraits<T>::GetDataType(), rootProc);
    return ans;
}
/**
 * Scatter pData across ranks in chunks of len(pData)/num_ranks
 */
template <class T> T Comm::Scatter(const int rootProc, T &pData)
{
    BOOST_STATIC_ASSERT_MSG(
        CommDataTypeTraits<T>::IsVector,
        "Scatter only valid with Array or vector arguments.");

    bool amRoot  = (GetRank() == rootProc);
    unsigned nEl = CommDataTypeTraits<T>::GetCount(pData) / GetSize();

    void *sendbuf = amRoot ? CommDataTypeTraits<T>::GetPointer(pData) : NULL;
    T ans(nEl);

    v_Scatter(sendbuf, nEl, CommDataTypeTraits<T>::GetDataType(),
              CommDataTypeTraits<T>::GetPointer(ans), nEl,
              CommDataTypeTraits<T>::GetDataType(), rootProc);
    return ans;
}

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484

inline GsHandle Comm::GsInit(
        const Nektar::Array<OneD, long> pId,
        bool verbose)
{
    return v_GsInit(pId, verbose);
}

inline void Comm::GsUnique(
        const Nektar::Array<OneD, long> pId)
{
    v_GsUnique(pId);
}

inline void Comm::GsGather(
        Nektar::Array<OneD, NekDouble> pU,
        Gs::gs_op pOp,
        GsHandleId pGsh,
        Nektar::Array<OneD, NekDouble> pBuffer)
{
    v_GsGather(pU, pOp, pGsh, pBuffer);
}


David Moxey's avatar
David Moxey committed
485 486 487
/**
 * @brief If the flag is non-zero create a new communicator.
 */
488
inline CommSharedPtr Comm::CommCreateIf(int colour)
David Moxey's avatar
David Moxey committed
489
{
490
    return v_CommCreateIf(colour);
David Moxey's avatar
David Moxey committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
}

/**
 * @brief Splits this communicator into a grid of size pRows*pColumns
 * and creates row and column communicators. By default the communicator
 * is a single row.
 */
inline void Comm::SplitComm(int pRows, int pColumns)
{
    v_SplitComm(pRows, pColumns);
}

/**
 * @brief Retrieve the row communicator to which this process belongs.
 */
inline CommSharedPtr Comm::GetRowComm()
{
    if (!m_commRow.get())
    {
        return shared_from_this();
    }
    else
    {
        return m_commRow;
    }
}

/**
 * @brief Retrieve the column communicator to which this process
 * belongs.
 */
inline CommSharedPtr Comm::GetColumnComm()
{
    if (!m_commColumn.get())
    {
        return shared_from_this();
    }
    else
    {
        return m_commColumn;
531 532 533
    }
}

David Moxey's avatar
David Moxey committed
534 535 536 537 538 539 540 541 542
inline bool Comm::TreatAsRankZero(void)
{
    return v_TreatAsRankZero();
}

inline bool Comm::RemoveExistingFiles(void)
{
    return v_RemoveExistingFiles();
}
543 544 545 546 547 548

inline int Comm::EnrolSpare()
{
    return v_EnrolSpare();
}

549
inline void Comm::BeginTransactionLog()
550
{
551
    v_BeginTransactionLog();
552 553
}

554
inline void Comm::EndTransactionLog()
555
{
556
    v_EndTransactionLog();
557
}
558

559 560 561 562 563 564 565 566 567 568
inline bool Comm::IsRecovering()
{
    return v_IsRecovering();
}

inline void Comm::StateAdd(const std::string& name, const int& data)
{
    v_StateAdd(name, data);
}

569 570 571 572 573
inline void Comm::StateAdd(const std::string& name, const NekDouble& data)
{
    v_StateAdd(name, data);
}

574 575 576 577 578 579 580 581 582 583
inline void Comm::StateAdd(const std::string& name, const NekDouble* data, const int n)
{
    v_StateAdd(name, data, n);
}

inline void Comm::StateGet(const std::string& name, int& data)
{
    v_StateGet(name, data);
}

584 585 586 587 588
inline void Comm::StateGet(const std::string& name, NekDouble& data)
{
    v_StateGet(name, data);
}

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
inline void Comm::StateGet(const std::string& name, NekDouble* data, const int n)
{
    v_StateGet(name, data, n);
}

inline void Comm::StateCommit()
{
    v_StateCommit();
}

inline void Comm::StateRestore()
{
    v_StateRestore();
}

David Moxey's avatar
David Moxey committed
604 605 606
}
}

607
#endif