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

Last change on this file since 2576 was 2576, checked in by ymipsl, 10 months ago

Implement tracking of unfree communicators.

YM

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