Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 09:09:53

0001 #ifndef ATOOLS__Org__My_MPI_H
0002 #define ATOOLS__Org__My_MPI_H
0003 
0004 #include "ATOOLS/Org/CXXFLAGS.H"
0005 
0006 #ifdef USING__MPI
0007 #include "mpi.h"
0008 #include <string>
0009 #endif
0010 
0011 #ifdef USING__Threading
0012 #include <pthread.h>
0013 inline int pthread_cond_signal
0014 (pthread_cond_t *c,pthread_mutex_t *m)
0015 {
0016   pthread_mutex_lock(m);
0017   int r(pthread_cond_signal(c));
0018   pthread_mutex_unlock(m);
0019   return r;
0020 }
0021 #endif
0022 
0023 #include <vector>
0024 
0025 namespace ATOOLS {
0026 
0027   class My_MPI {
0028 
0029   public:
0030 
0031     My_MPI();
0032 
0033     void PrintRankInfo();
0034     void PrintRank();
0035 
0036 #ifdef USING__MPI
0037 
0038     void Barrier() {
0039       MPI_Barrier(m_comm);
0040     }
0041 
0042     int Rank() {
0043       int rank;
0044       MPI_Comm_rank(m_comm, &rank);
0045       return rank;
0046     }
0047 
0048     int Size() {
0049       int size;
0050       MPI_Comm_size(m_comm, &size);
0051       return size;
0052     }
0053 
0054     void Bcast(void* buffer, int count, MPI_Datatype type) {
0055       MPI_Bcast(buffer, count, type, 0, m_comm);
0056     }
0057 
0058     void Reduce(void* buffer, int count, MPI_Datatype type, MPI_Op op) {
0059       if (Rank()==0)
0060         MPI_Reduce(MPI_IN_PLACE, buffer, count, type, op, 0, m_comm);
0061       else
0062         MPI_Reduce(buffer, buffer, count, type, op, 0, m_comm);
0063     }
0064 
0065     void Allreduce(void* buffer, int count, MPI_Datatype type, MPI_Op op) {
0066       MPI_Allreduce(MPI_IN_PLACE, buffer, count, type, op, m_comm);
0067     }
0068 
0069     void Allgather(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
0070                          void *recvbuf, int recvcount, MPI_Datatype recvtype) {
0071       MPI_Allgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype,
0072                     m_comm);
0073     }
0074 
0075     void Recv(void *buf, int count, MPI_Datatype datatype, int source, int tag)
0076     {
0077       MPI_Recv(buf, count, datatype, source, tag, m_comm, MPI_STATUS_IGNORE);
0078     }
0079 
0080     int Send(const void *buf, int count, MPI_Datatype datatype, int dest,
0081              int tag)
0082     {
0083       return MPI_Send(buf, count, datatype, dest, tag, m_comm);
0084     }
0085 
0086     void Gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
0087         void *recvbuf, int recvcount, MPI_Datatype recvtype,
0088         int root)
0089     {
0090       MPI_Gather(sendbuf, sendcount, sendtype,
0091          recvbuf, recvcount, recvtype, root, m_comm);
0092     }
0093 
0094     // Convenience method to get the maximum of some value from all ranks.
0095     int Allmax(int);
0096 
0097     // Convenience method to gather strings from all ranks into an array.
0098     // Allgather is used, such that the returned vector is filled for all
0099     // ranks. Note that this involves a non-trivial amount of communication
0100     // and should not be used too often.
0101     std::vector<std::string> AllgatherStrings(const std::string&);
0102 
0103 #endif
0104 
0105   private:
0106 
0107 #ifdef USING__MPI
0108     MPI_Comm m_comm;
0109 #endif
0110 
0111   };// end of class My_MPI
0112 
0113   extern My_MPI* mpi;
0114 
0115   void Abort(const int mode=0);
0116 
0117 }// end of namespace ATOOLS
0118 
0119 #endif