source: XIOS3/trunk/src/mpi_tools.cpp @ 2584

Last change on this file since 2584 was 2584, checked in by ymipsl, 9 months ago

Set overload of mpi functions in xios namespace, so only XIOS MPI calls will be intercepted.
YM

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 2.4 KB
Line 
1#include "mpi.hpp"
2#include "backtrace.hpp"
3#include "exception.hpp"
4#include "log.hpp"
5#include <string>
6#include <sstream>
7
8namespace xios 
9{
10  std::map<MPI_Comm,string> CCommTrack::commTrack_ ;
11  void CCommTrack::registerComm(const MPI_Comm& comm)
12  {
13    if (comm!=MPI_COMM_NULL)
14    {
15      auto it = commTrack_.find(comm) ;
16      if (it == commTrack_.end())  commTrack_[comm] = MemTrack::backTrace(3);
17      else ERROR("CCommtrack::registerComm", << "Communicator already allocated : " << endl<<it->second)
18    }
19  }
20
21  void CCommTrack::releaseComm(const MPI_Comm& comm)
22  {
23    auto it = commTrack_.find(comm) ;
24    //if (it == commTrack_.end())  info(100)<<"WARNING : CCommtrack::releaseComm => Communicator not allocated !" << endl ;
25    if (it == commTrack_.end())  { ERROR("void CCommTrack::releaseComm(const MPI_Comm& comm)",<<"Communicator not allocated !" << endl) ; }
26    else commTrack_.erase(it) ;
27  }
28
29  void CCommTrack::dumpComm(void)
30  {
31    ostringstream ostr ;
32    ostr<<" LIST of UNFREED MPI COMMUNICATORS"<<endl ;
33    for(auto& it : commTrack_ )
34    {
35      ostr<<"---------------------------"<<endl ;
36      ostr<<" -- unfreed communicator --"<<endl ;
37      ostr<<" --       backtrace      --"<<endl ;
38      ostr<<it.second<<endl ;
39    }
40    info(100)<<ostr.str() ;
41  }
42
43  int MPI_Bcast_string(std::string& str, int root, MPI_Comm comm) 
44  {
45    int commRank ;
46    int ret ;
47    MPI_Comm_rank(comm,&commRank) ;
48    int strSize ;
49    if (commRank==root) strSize=str.size() ;
50    MPI_Bcast(&strSize,1,MPI_INT,root,comm) ;
51 
52    if (commRank==root) ret=MPI_Bcast((char*)str.data(), strSize, MPI_CHAR, root, comm) ;
53    else
54    {
55      char* tmp=new char[strSize] ;
56      ret=MPI_Bcast(tmp, strSize, MPI_CHAR, root, comm) ;
57      str=std::string(tmp,strSize) ;
58      delete [] tmp ;
59    }
60    return ret ;
61  }
62
63  template<>
64  MPI_Datatype MPI_GetType<bool>(void) { return MPI_CXX_BOOL ;}
65
66  template<>
67  MPI_Datatype MPI_GetType<char>(void) { return MPI_CHAR ;}
68
69  template<>
70  MPI_Datatype MPI_GetType<short int>(void) { return MPI_SHORT ;}
71
72  template<>
73  MPI_Datatype MPI_GetType<int>(void) { return MPI_INT ;}
74
75  template<>
76  MPI_Datatype MPI_GetType<size_t>(void) { return MPI_SIZE_T ;}
77
78  template<>
79  MPI_Datatype MPI_GetType<float>(void) { return MPI_FLOAT ;}
80
81  template<>
82  MPI_Datatype MPI_GetType<double>(void) { return MPI_DOUBLE ;}
83
84  template<>
85  MPI_Datatype MPI_GetType<long double>(void) { return MPI_LONG_DOUBLE ;}
86
87}
Note: See TracBrowser for help on using the repository browser.