artdaq_mpich_plugin  v1_00_03
MPISentry.cc
1 #define TRACE_NAME "MPISentry"
2 #include "artdaq/DAQdata/Globals.hh"
3 #include "artdaq-mpich-plugin/Application/MPISentry.hh"
5 #include "cetlib_except/exception.h"
6 
7 #include <sstream>
8 
10 MPISentry(int* argc_ptr, char*** argv_ptr)
11  :
12  threading_level_(0)
13  , rank_(-1)
14  , procs_(0)
15 {
16  MPI_Init(argc_ptr, argv_ptr);
17  initialize_();
18 }
19 
21 MPISentry(int* argc_ptr,
22  char*** argv_ptr,
23  int threading_level)
24  :
25  threading_level_(0)
26  , rank_(-1)
27  , procs_(0)
28 {
29  MPI_Init_thread(argc_ptr, argv_ptr, threading_level, &threading_level_);
30  initialize_();
31 
32  std::ostringstream threadresult;
33  threadresult << "MPI initialized with requested thread support level of "
34  << threading_level << ", actual support level = "
35  << threading_level_ << ".";
36 
37  TLOG(TLVL_DEBUG) << threadresult.str() ;
38 
39  if (threading_level != threading_level_) throw cet::exception("MPISentry") << threadresult.str();
40 
41  TLOG(TLVL_DEBUG)
42  << "size = "
43  << procs_
44  << ", rank = "
45  << rank_ ;
46 }
47 
49 MPISentry(int* argc_ptr,
50  char*** argv_ptr,
51  int threading_level, artdaq::TaskType type, MPI_Comm& local_group_comm)
52  :
53  threading_level_(0)
54  , rank_(-1)
55  , procs_(0)
56 {
57  MPI_Init_thread(argc_ptr, argv_ptr, threading_level, &threading_level_);
58  initialize_();
59 
60  std::ostringstream threadresult;
61  threadresult << "MPI initialized with requested thread support level of "
62  << threading_level << ", actual support level = "
63  << threading_level_ << ".";
64 
65  TLOG(TLVL_DEBUG) << threadresult.str() ;
66 
67  if (threading_level != threading_level_) throw cet::exception("MPISentry") << threadresult.str();
68 
69  TLOG(TLVL_DEBUG)
70  << "size = "
71  << procs_
72  << ", rank = "
73  << rank_ ;
74 
75 
76  int status = MPI_Comm_split(MPI_COMM_WORLD, type, 0, &local_group_comm);
77 
78  if (status == MPI_SUCCESS)
79  {
80  int temp_rank;
81  MPI_Comm_rank(local_group_comm, &temp_rank);
82 
83  TLOG(TLVL_DEBUG) << "Successfully created local communicator for type "
84  << type << ", identifier = 0x"
85  << std::hex << local_group_comm << std::dec
86  << ", rank = " << temp_rank << ".";
87  }
88  else
89  {
90  std::ostringstream groupcommresult;
91  groupcommresult << "Failed to create the local MPI communicator group for "
92  << "task type #" << type << ", status code = " << status << ".";
93  TLOG(TLVL_ERROR) << groupcommresult.str();
94  throw cet::exception("MPISentry") << groupcommresult.str();
95  }
96 }
97 
100 {
101  MPI_Finalize();
102 }
103 
104 int
107 {
108  return threading_level_;
109 }
110 
111 int
113 rank() const
114 {
115  return rank_;
116 }
117 
118 int
120 procs() const
121 {
122  return procs_;
123 }
124 
125 void
126 artdaq::MPISentry::
127 initialize_()
128 {
129  MPI_Comm_size(MPI_COMM_WORLD, &procs_);
130  MPI_Comm_rank(MPI_COMM_WORLD, &rank_);
131  my_rank = rank_;
132 }
MPISentry(int *argc_ptr, char ***argv_ptr)
MPISentry Constructor.
Definition: MPISentry.cc:10
int rank() const
Get the MPI rank of the application.
Definition: MPISentry.cc:113
int threading_level() const
Get the actual threading level.
Definition: MPISentry.cc:106
int procs() const
The number of processes in the MPI context.
Definition: MPISentry.cc:120
~MPISentry()
MPISentry Destructor. Calls MPI_Finalize.
Definition: MPISentry.cc:99