artdaq_mpich_plugin  v1_00_06
builder.cc
1 #define TRACE_NAME "builder"
2 
3 #include "art/Framework/Art/artapp.h"
4 #include "artdaq-core/Generators/FragmentGenerator.hh"
5 #include "artdaq-core/Data/Fragment.hh"
6 #include "artdaq-core/Generators/makeFragmentGenerator.hh"
7 #include "MPIProg.hh"
8 #include "artdaq/DAQrate/DataSenderManager.hh"
9 #include "artdaq/DAQrate/DataReceiverManager.hh"
10 #include "artdaq-core/Core/SimpleMemoryReader.hh"
12 
13 #include <boost/program_options.hpp>
14 #include "fhiclcpp/make_ParameterSet.h"
15 namespace bpo = boost::program_options;
16 
17 #include <algorithm>
18 #include <cmath>
19 #include <cstdlib>
20 
21 extern "C"
22 {
23 #include <unistd.h>
24 }
25 
26 #include <iostream>
27 #include <memory>
28 #include <utility>
29 
30 extern "C"
31 {
32 #include <sys/time.h>
33 #include <sys/resource.h>
34 }
35 
39 class Builder : public MPIProg
40 {
41 public:
49  Builder(int argc, char* argv[], fhicl::ParameterSet pset, int key);
50 
54  void go();
55 
59  void sink();
60 
64  void detector();
65 
66 private:
67  enum class Role : int
68  {
69  DETECTOR,
70  SINK
71  };
72 
73  void printHost(const std::string& functionName) const;
74 
75  fhicl::ParameterSet daq_pset_;
76  bool const want_sink_;
77  bool const want_periodic_sync_;
78  MPI_Comm local_group_comm_;
79  Role builder_role_;
80 };
81 
82 Builder::Builder(int argc, char* argv[], fhicl::ParameterSet pset, int key) :
83  MPIProg(argc, argv)
84  , daq_pset_(pset)
85  , want_sink_(daq_pset_.get<bool>("want_sink", true))
86  , want_periodic_sync_(daq_pset_.get<bool>("want_periodic_sync", false))
87  , local_group_comm_()
88 {
89  std::vector<std::string> detectors;
90  daq_pset_.get_if_present("detectors", detectors);
91  if (static_cast<size_t>(my_rank) >= detectors.size())
92  {
93  builder_role_ = Role::SINK;
94  }
95  else
96  {
97  builder_role_ = Role::DETECTOR;
98  }
99  std::string type(pset.get<std::string>("transfer_plugin_type", "Shmem"));
100 
101  int senders = pset.get<int>("num_senders");
102  int receivers = pset.get<int>("num_receivers");
103  int buffer_count = pset.get<int>("buffer_count", 10);
104  int max_payload_size = pset.get<size_t>("fragment_size", 0x100000);
105 
106  std::string hostmap = "";
107  if (pset.has_key("hostmap"))
108  {
109  hostmap = " host_map: @local::hostmap";
110  }
111 
112  std::stringstream ss;
113  ss << pset.to_string();
114  ss << " sources: {";
115  for (int ii = 0; ii < senders; ++ii)
116  {
117  ss << "s" << ii << ": { transferPluginType: " << type << " source_rank: " << ii << " max_fragment_size_words: " << max_payload_size << " buffer_count: " << buffer_count << " shm_key_offset: " << std::to_string(key) << hostmap << "}";
118  }
119  ss << "} destinations: {";
120  for (int jj = senders; jj < senders + receivers; ++jj)
121  {
122  ss << "d" << jj << ": { transferPluginType: " << type << " destination_rank: " << jj << " max_fragment_size_words: " << max_payload_size << " buffer_count: " << buffer_count << " shm_key_offset: " << std::to_string(key) << hostmap << "}";
123  }
124  ss << "}";
125 
126  daq_pset_ = fhicl::ParameterSet();
127  make_ParameterSet(ss.str(), daq_pset_);
128 
129 
130 }
131 
133 {
134  //volatile bool loopForever = true;
135  //while(loopForever)
136  //{
137  // usleep(1000000);
138  //}
139 
140 
141  MPI_Barrier(MPI_COMM_WORLD);
142  //std::cout << "daq_pset_: " << daq_pset_.to_string() << std::endl << "conf_.makeParameterSet(): " << conf_.makeParameterSet().to_string() << std::endl;
143  MPI_Comm_split(MPI_COMM_WORLD, static_cast<int>(builder_role_), 0, &local_group_comm_);
144  switch (builder_role_)
145  {
146  case Role::SINK:
147  if (want_sink_)
148  {
149  sink();
150  }
151  else
152  {
153  std::string
154  msg("WARNING: a sink was instantiated despite want_sink being false:\n"
155  "set nsinks to 0 in invocation of daqrate?\n");
156  std::cerr << msg;
157  MPI_Barrier(MPI_COMM_WORLD);
158  }
159  break;
160  case Role::DETECTOR:
161  detector();
162  break;
163  default:
164  throw "No such node type";
165  }
166 }
167 
169 {
170  printHost("detector");
171  int detector_rank;
172  // Should be zero-based, detectors only.
173  MPI_Comm_rank(local_group_comm_, &detector_rank);
174  assert(!(detector_rank < 0));
175  std::ostringstream det_ps_name_loc;
176  std::vector<std::string> detectors;
177  bool detectors_present = daq_pset_.get_if_present("detectors", detectors);
178  size_t detectors_size = detectors.size();
179  if (!(detectors_present && detectors_size))
180  {
181  throw cet::exception("Configuration")
182  << "Unable to find required sequence of detector "
183  << "parameter set names, \"detectors\".";
184  }
185  fhicl::ParameterSet det_ps =
186  daq_pset_.get<fhicl::ParameterSet>(((detectors_size > static_cast<size_t>(detector_rank)) ? detectors[detector_rank] : detectors[0]));
187  std::unique_ptr<artdaq::FragmentGenerator> const
189  (det_ps.get<std::string>("generator"),
190  det_ps));
191  { // Block to handle lifetime of h, below.
192  artdaq::DataSenderManager h(daq_pset_);
193  MPI_Barrier(local_group_comm_);
194  // not using the run time method
195  // TimedLoop tl(conf_.run_time_);
196  size_t fragments_per_source = -1;
197  daq_pset_.get_if_present("fragments_per_source", fragments_per_source);
198  artdaq::FragmentPtrs frags;
199  size_t fragments_sent = 0;
200  while (fragments_sent < fragments_per_source && gen->getNext(frags))
201  {
202  if (!fragments_sent)
203  {
204  // Get the detectors lined up first time before we start the
205  // firehoses.
206  MPI_Barrier(local_group_comm_);
207  }
208  for (auto& fragPtr : frags)
209  {
210  std::cout << "Program::detector: Sending fragment " << fragments_sent + 1 << " of " << fragments_per_source << std::endl;
211  TLOG(TLVL_DEBUG) << "Program::detector: Sending fragment " << fragments_sent + 1 << " of " << fragments_per_source ;
212  h.sendFragment(std::move(*fragPtr));
213  if (++fragments_sent == fragments_per_source) { break; }
214  if (want_periodic_sync_ && (fragments_sent % 100) == 0)
215  {
216  // Don't get too far out of sync.
217  MPI_Barrier(local_group_comm_);
218  }
219  }
220  frags.clear();
221  }
222  TLOG(TLVL_DEBUG) << "detector waiting " << my_rank ;
223  }
224  TLOG(TLVL_DEBUG) << "detector done " << my_rank ;
225  MPI_Comm_free(&local_group_comm_);
226  MPI_Barrier(MPI_COMM_WORLD);
227 }
228 
230 {
231  printHost("sink");
232  {
233  usleep(1000 * my_rank);
234  // This scope exists to control the lifetime of 'events'
235  auto events = std::make_shared<artdaq::SharedMemoryEventManager>(daq_pset_, daq_pset_);
236  events->startRun(daq_pset_.get<int>("run_number", 100));
237  { // Block to handle scope of h, below.
238  artdaq::DataReceiverManager h(daq_pset_, events);
239  h.start_threads();
240  while (h.running_sources().size() > 0)
241  {
242  usleep(10000);
243  }
244  }
245 
246  TLOG(TLVL_DEBUG) << "All detectors are done, Sending endOfData Fragment" ;
247  // Make the reader application finish, and capture its return
248  // status.
249  bool endSucceeded = false;
250  endSucceeded = events->endOfData();
251  if (endSucceeded)
252  {
253  TLOG(TLVL_DEBUG) << "Sink: reader is done" ;
254  }
255  else
256  {
257  TLOG(TLVL_DEBUG) << "Sink: reader failed to complete because the "
258  << "endOfData marker could not be pushed onto the queue."
259  ;
260  }
261  } // end of lifetime of 'events'
262  TLOG(TLVL_DEBUG) << "Sink done " << my_rank ;
263  MPI_Barrier(MPI_COMM_WORLD);
264 }
265 
266 void Builder::printHost(const std::string& functionName) const
267 {
268  char* doPrint = getenv("PRINT_HOST");
269  if (doPrint == 0) { return; }
270  const int ARRSIZE = 80;
271  char hostname[ARRSIZE];
272  std::string hostString;
273  if (!gethostname(hostname, ARRSIZE))
274  {
275  hostString = hostname;
276  }
277  else
278  {
279  hostString = "unknown";
280  }
281  TLOG(TLVL_DEBUG) << "Running " << functionName
282  << " on host " << hostString
283  << " with rank " << my_rank << "."
284  ;
285 }
286 
287 void printUsage()
288 {
289  int myid = 0;
290  struct rusage usage;
291  getrusage(RUSAGE_SELF, &usage);
292  std::cout << myid << ":"
293  << " user=" << artdaq::TimeUtils::convertUnixTimeToSeconds(usage.ru_utime)
294  << " sys=" << artdaq::TimeUtils::convertUnixTimeToSeconds(usage.ru_stime)
295  << std::endl;
296 }
297 
298 int main(int argc, char* argv[])
299 {
301 
302  std::ostringstream descstr;
303  descstr << argv[0]
304  << " <-c <config-file>> <other-options> [<source-file>]+";
305  bpo::options_description desc(descstr.str());
306  desc.add_options()
307  ("config,c", bpo::value<std::string>(), "Configuration file.")
308  ("key,k", bpo::value<int>(), "Shared Memory Key")
309  ("help,h", "produce help message");
310  bpo::variables_map vm;
311  try {
312  bpo::store(bpo::command_line_parser(argc, argv).options(desc).run(), vm);
313  bpo::notify(vm);
314  }
315  catch (bpo::error const & e) {
316  std::cerr << "Exception from command line processing in " << argv[0]
317  << ": " << e.what() << "\n";
318  return -1;
319  }
320  if (vm.count("help")) {
321  std::cout << desc << std::endl;
322  return 1;
323  }
324  if (!vm.count("config")) {
325  std::cerr << "Exception from command line processing in " << argv[0]
326  << ": no configuration file given.\n"
327  << "For usage and an options list, please do '"
328  << argv[0] << " --help"
329  << "'.\n";
330  return 2;
331  }
332  int key = 0;
333  if (vm.count("key"))
334  {
335  key = vm["key"].as<int>();
336  }
337  fhicl::ParameterSet pset;
338  if (getenv("FHICL_FILE_PATH") == nullptr) {
339  std::cerr
340  << "INFO: environment variable FHICL_FILE_PATH was not set. Using \".\"\n";
341  setenv("FHICL_FILE_PATH", ".", 0);
342  }
343  cet::filepath_lookup_after1 lookup_policy("FHICL_FILE_PATH");
344  fhicl::make_ParameterSet(vm["config"].as<std::string>(), lookup_policy, pset);
345 
346  int rc = 1;
347  try
348  {
349  Builder p(argc, argv, pset,key);
350  std::cerr << "Started process " << my_rank << " of " << p.procs_ << ".\n";
351  p.go();
352  rc = 0;
353  }
354  catch (std::string& x)
355  {
356  std::cerr << "Exception (type string) caught in driver: "
357  << x
358  << '\n';
359  return 1;
360  }
361  catch (char const* m)
362  {
363  std::cerr << "Exception (type char const*) caught in driver: ";
364  if (m)
365  {
366  std::cerr << m;
367  }
368  else
369  {
370  std::cerr << "[the value was a null pointer, so no message is available]";
371  }
372  std::cerr << '\n';
373  }
374  return rc;
375 }
std::set< int > running_sources() const
void go()
Start the Builder application, using the type configuration to select which method to run...
Definition: builder.cc:132
A wrapper for a MPI program. Similar to MPISentry.
Definition: MPIProg.hh:10
void detector()
Generate data, and send it using DataSenderManager.
Definition: builder.cc:168
std::pair< int, TransferInterface::CopyStatus > sendFragment(Fragment &&frag)
double convertUnixTimeToSeconds(time_t inputUnixTime)
std::unique_ptr< FragmentGenerator > makeFragmentGenerator(std::string const &generator_plugin_spec, fhicl::ParameterSet const &ps)
void configureMessageFacility(char const *progname, bool useConsole=true, bool printDebug=false)
The Builder class runs the builder test.
Definition: builder.cc:39
std::list< FragmentPtr > FragmentPtrs
void sink()
Receive data from source via DataReceiverManager, send it to the EventStore (and art, if configured)
Definition: builder.cc:229
Builder(int argc, char *argv[], fhicl::ParameterSet pset, int key)
Builder Constructor.
Definition: builder.cc:82