artdaq  v2_02_03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
MPISentry.cc
1 #include "artdaq/DAQdata/Globals.hh"
2 #include "artdaq/Application/MPI2/MPISentry.hh"
4 #include "cetlib/exception.h"
5 
6 #include <sstream>
7 
9 MPISentry(int* argc_ptr, char*** argv_ptr)
10  :
11  threading_level_(0)
12  , rank_(-1)
13  , procs_(0)
14 {
15  MPI_Init(argc_ptr, argv_ptr);
16  initialize_();
17 }
18 
20 MPISentry(int* argc_ptr,
21  char*** argv_ptr,
22  int threading_level)
23  :
24  threading_level_(0)
25  , rank_(-1)
26  , procs_(0)
27 {
28  MPI_Init_thread(argc_ptr, argv_ptr, threading_level, &threading_level_);
29  initialize_();
30 
31  std::ostringstream threadresult;
32  threadresult << "MPI initialized with requested thread support level of "
33  << threading_level << ", actual support level = "
34  << threading_level_ << ".";
35 
36  TLOG_DEBUG("MPISentry") << threadresult.str() << TLOG_ENDL;
37 
38  if (threading_level != threading_level_) throw cet::exception("MPISentry") << threadresult.str();
39 
40  TLOG_DEBUG("MPISentry")
41  << "size = "
42  << procs_
43  << ", rank = "
44  << rank_ << TLOG_ENDL;
45 }
46 
48 MPISentry(int* argc_ptr,
49  char*** argv_ptr,
50  int threading_level, artdaq::TaskType type, MPI_Comm& local_group_comm)
51  :
52  threading_level_(0)
53  , rank_(-1)
54  , procs_(0)
55 {
56  MPI_Init_thread(argc_ptr, argv_ptr, threading_level, &threading_level_);
57  initialize_();
58 
59  std::ostringstream threadresult;
60  threadresult << "MPI initialized with requested thread support level of "
61  << threading_level << ", actual support level = "
62  << threading_level_ << ".";
63 
64  TLOG_DEBUG("MPISentry") << threadresult.str() << TLOG_ENDL;
65 
66  if (threading_level != threading_level_) throw cet::exception("MPISentry") << threadresult.str();
67 
68  TLOG_DEBUG("MPISentry")
69  << "size = "
70  << procs_
71  << ", rank = "
72  << rank_ << TLOG_ENDL;
73 
74  std::ostringstream groupcommresult;
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  groupcommresult << "Successfully created local communicator for type "
84  << type << ", identifier = 0x"
85  << std::hex << local_group_comm << std::dec
86  << ", rank = " << temp_rank << ".";
87 
88  TLOG_DEBUG("MPISentry") << groupcommresult.str() << TLOG_ENDL;
89  }
90  else
91  {
92  groupcommresult << "Failed to create the local MPI communicator group for "
93  << "task type #" << type << ", status code = " << status << ".";
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 }
MPISentry(int *argc_ptr, char ***argv_ptr)
MPISentry Constructor.
Definition: MPISentry.cc:9
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