artdaq_mpich_plugin  v1_00_08a
MPISentry.cc
1 #define TRACE_NAME "MPISentry"
2 #include "artdaq-mpich-plugin/Application/MPISentry.hh"
4 #include "artdaq/DAQdata/Globals.hh"
5 #include "cetlib_except/exception.h"
6 
7 #include <sstream>
8 
9 artdaq::MPISentry::MPISentry(int* argc_ptr, char*** argv_ptr) : threading_level_(0), rank_(-1), procs_(0) {
10  MPI_Init(argc_ptr, argv_ptr);
11  initialize_();
12 }
13 
14 artdaq::MPISentry::MPISentry(int* argc_ptr, char*** argv_ptr, int threading_level)
15  : threading_level_(0), rank_(-1), procs_(0) {
16  MPI_Init_thread(argc_ptr, argv_ptr, threading_level, &threading_level_);
17  initialize_();
18 
19  std::ostringstream threadresult;
20  threadresult << "MPI initialized with requested thread support level of " << threading_level
21  << ", actual support level = " << threading_level_ << ".";
22 
23  TLOG(TLVL_DEBUG) << threadresult.str();
24 
25  if (threading_level != threading_level_) throw cet::exception("MPISentry") << threadresult.str();
26 
27  TLOG(TLVL_DEBUG) << "size = " << procs_ << ", rank = " << rank_;
28 }
29 
30 artdaq::MPISentry::MPISentry(int* argc_ptr, char*** argv_ptr, int threading_level, artdaq::TaskType type,
31  MPI_Comm& local_group_comm)
32  : threading_level_(0), rank_(-1), procs_(0) {
33  MPI_Init_thread(argc_ptr, argv_ptr, threading_level, &threading_level_);
34  initialize_();
35 
36  std::ostringstream threadresult;
37  threadresult << "MPI initialized with requested thread support level of " << threading_level
38  << ", actual support level = " << threading_level_ << ".";
39 
40  TLOG(TLVL_DEBUG) << threadresult.str();
41 
42  if (threading_level != threading_level_) throw cet::exception("MPISentry") << threadresult.str();
43 
44  TLOG(TLVL_DEBUG) << "size = " << procs_ << ", rank = " << rank_;
45 
46  int status = MPI_Comm_split(MPI_COMM_WORLD, type, 0, &local_group_comm);
47 
48  if (status == MPI_SUCCESS) {
49  int temp_rank;
50  MPI_Comm_rank(local_group_comm, &temp_rank);
51 
52  TLOG(TLVL_DEBUG) << "Successfully created local communicator for type " << type << ", identifier = 0x" << std::hex
53  << local_group_comm << std::dec << ", rank = " << temp_rank << ".";
54  } else {
55  std::ostringstream groupcommresult;
56  groupcommresult << "Failed to create the local MPI communicator group for "
57  << "task type #" << type << ", status code = " << status << ".";
58  TLOG(TLVL_ERROR) << groupcommresult.str();
59  throw cet::exception("MPISentry") << groupcommresult.str();
60  }
61 }
62 
63 artdaq::MPISentry::~MPISentry() { MPI_Finalize(); }
64 
65 int artdaq::MPISentry::threading_level() const { return threading_level_; }
66 
67 int artdaq::MPISentry::rank() const { return rank_; }
68 
69 int artdaq::MPISentry::procs() const { return procs_; }
70 
71 void artdaq::MPISentry::initialize_() {
72  MPI_Comm_size(MPI_COMM_WORLD, &procs_);
73  MPI_Comm_rank(MPI_COMM_WORLD, &rank_);
74  my_rank = rank_;
75 }
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:67
int threading_level() const
Get the actual threading level.
Definition: MPISentry.cc:65
int procs() const
The number of processes in the MPI context.
Definition: MPISentry.cc:69
~MPISentry()
MPISentry Destructor. Calls MPI_Finalize.
Definition: MPISentry.cc:63