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