artdaq  v2_03_00
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
Config.cc
1 #include "Config.hh"
2 #include "artdaq/DAQrate/infoFilename.hh"
3 #include "artdaq-core/Data/Fragment.hh"
4 #include <fhiclcpp/ParameterSet.h>
5 #include <fhiclcpp/make_ParameterSet.h>
6 
7 #include <cstring>
8 #include <iostream>
9 #include <fstream>
10 #include <string>
11 #include <cstdlib>
12 
13 #include <sys/types.h>
14 #include <regex.h>
15 
16 #include "boost/program_options.hpp"
17 namespace bpo = boost::program_options;
18 
19 const char* artdaq::Config::usage = "DetectorsPerNode SinksPerNode Run";
20 
21 
22 
23 // remember rank starts at zero
24 //run_time_(getArgRuntime(argc,argv)),
25 
26 artdaq::Config::Config(int rank, int total_procs, int buffer_count, size_t max_payload_size, int argc, char* argv[]):
27  rank_(rank)
28  , total_procs_(total_procs)
29  , detectors_(getArgDetectors(argc, argv))
30  , sources_(detectors_)
31  , sinks_(getArgSinks(argc, argv))
32  , detector_start_(0)
33  , source_start_(detectors_)
34  , sink_start_(detectors_ + sources_)
35  , event_queue_size_(getArgQueueSize(argc, argv))
36  , run_(getArgRun(argc, argv))
37  , buffer_count_(buffer_count)
38  , max_payload_size_(max_payload_size)
39  , type_((rank_ < detectors_) ? TaskDetector : ((rank_ < (detectors_ + sources_)) ? TaskSource : TaskSink))
40  , offset_(rank_ - ((type_ == TaskDetector) ? detector_start_ : (type_ == TaskSource) ? source_start_ : sink_start_))
41  , node_name_(getProcessorName())
42  , art_argc_(getArtArgc(argc, argv))
43  , art_argv_(getArtArgv(argc - art_argc_, argv))
44  , use_artapp_(getenv("ARTDAQ_DAQRATE_USE_ART") != 0)
45 {
46  int total_workers = (detectors_ + sinks_ + sources_);
47  if (total_procs_ != total_workers)
48  {
49  std::cerr << "total_procs " << total_procs_ << " != "
50  << "total_workers " << total_workers << "\n";
51  throw "total_procs != total_workers";
52  }
53 }
54 
56 {
57  std::string fname = artdaq::infoFilename("config_", rank_, run_);
58  std::ofstream ostr(fname.c_str());
59  printHeader(ostr);
60  ostr << *this << "\n";
61 }
62 
64 {
65  if (type_ == TaskSink) { throw "No destCount for a sink"; }
66  return type_ == TaskDetector ? sources_ : sinks_;
67 }
68 
70 {
71  if (type_ == TaskSink) { throw "No destStart for a sink"; }
72  return type_ == TaskDetector ? source_start_ : sink_start_;
73 }
74 
76 {
77  if (type_ == TaskDetector) { throw "No srcCount for a detector"; }
78  return type_ == TaskSink ? sources_ : detectors_;
79 }
80 
82 {
83  if (type_ == TaskDetector) { throw "No srcStart for a detector"; }
84  return type_ == TaskSink ? source_start_ : detector_start_;
85 }
86 
87 std::string artdaq::Config::typeName() const
88 {
89  static const char* names[] = {"Sink", "Source", "Detector"};
90  return names[type_];
91 }
92 
94 {
95  return offset_ + destStart();
96 }
97 
99 {
100  return offset_ + srcStart();
101 }
102 
103 int artdaq::Config::getArtArgc(int argc, char* argv[]) const
104 {
105  // Find the '--' in argv
106  int pos = 0;
107  for (; pos < argc; ++pos)
108  {
109  if (strcmp(argv[pos], "--") == 0) { break; }
110  }
111  return argc - pos;
112 }
113 
114 char** artdaq::Config::getArtArgv(int pos, char** argv) const
115 {
116  return argv + pos;
117 }
118 
119 void artdaq::Config::printHeader(std::ostream& ost) const
120 {
121  ost << "Rank TotalNodes "
122  << "DetectorsPerNode SourcesPerNode SinksPerNode "
123  << "BuilderNodes DetectorNodes Sources Sinks Detectors "
124  << "DetectorStart SourceStart SinkStart "
125  << "EventQueueSize "
126  << "Run "
127  << "Type Offset "
128  << "Nodename "
129  << "StartTime\n";
130 }
131 
132 void artdaq::Config::print(std::ostream& ost) const
133 {
134  ost << rank_ << " "
135  << sources_ << " "
136  << sinks_ << " "
137  << detectors_ << " "
138  << detector_start_ << " "
139  << source_start_ << " "
140  << sink_start_ << " "
141  << event_queue_size_ << " "
142  << run_ << " "
143  << typeName() << " "
144  << offset_ << " "
145  << node_name_;
146 }
147 
148 fhicl::ParameterSet artdaq::Config::makeParameterSet() const
149 {
150  std::stringstream ss;
151  if (type_ != TaskDetector)
152  {
153  ss << "sources: {";
154  int count = type_ == TaskSource ? detectors_ : sources_;
155  int start = type_ == TaskSource ? detector_start_ : source_start_;
156  for (int ii = 0; ii < count; ++ii)
157  {
158  ss << "s" << ii + start << ": { transferPluginType: MPI source_rank: " << ii + start << " max_fragment_size_words: " << max_payload_size_ << " buffer_count: " << buffer_count_ << "}";
159  }
160 
161  ss << "}";
162  }
163  if (type_ != TaskSink)
164  {
165  ss << " destinations: {";
166  int count = type_ == TaskDetector ? sources_ : sinks_;
167  int start = type_ == TaskDetector ? source_start_ : sink_start_;
168  for (int ii = 0; ii < count; ++ii)
169  {
170  ss << "d" << ii + start << ": { transferPluginType: MPI destination_rank: " << ii + start << " max_fragment_size_words: " << max_payload_size_ << " buffer_count: " << buffer_count_ << "}";
171  }
172 
173  ss << "}";
174  }
175 
176  fhicl::ParameterSet ps;
177  fhicl::make_ParameterSet(ss.str(), ps);
178  return ps;
179 }
180 
181 fhicl::ParameterSet artdaq::Config::getArtPset()
182 {
183  std::ostringstream descstr;
184  descstr << "-- <-c <config-file>>";
185  bpo::options_description desc(descstr.str());
186  desc.add_options()
187  ("config,c", bpo::value<std::string>(), "Configuration file.");
188  bpo::variables_map vm;
189  try
190  {
191  bpo::store(bpo::command_line_parser(art_argc_, art_argv_).
192  options(desc).allow_unregistered().run(), vm);
193  bpo::notify(vm);
194  }
195  catch (bpo::error const& e)
196  {
197  std::cerr << "Exception from command line processing in Config::getArtPset: " << e.what() << "\n";
198  throw "cmdline parsing error.";
199  }
200  if (!vm.count("config"))
201  {
202  std::cerr << "Expected \"-- -c <config-file>\" fhicl file specification.\n";
203  throw "cmdline parsing error.";
204  }
205  fhicl::ParameterSet pset;
206  cet::filepath_lookup lookup_policy("FHICL_FILE_PATH");
207  fhicl::make_ParameterSet(vm["config"].as<std::string>(), lookup_policy, pset);
208  auto ps = pset.get<fhicl::ParameterSet>("daq");
209  buffer_count_ = ps.get<int>("buffer_count", buffer_count_);
210  max_payload_size_ = ps.get<size_t>("max_fragment_size_words", max_payload_size_);
211 
212  return ps;
213 }
fhicl::ParameterSet getArtPset()
Get the ParameterSet to use to configure art.
Definition: Config.cc:181
void print(std::ostream &ost) const
Dump configuration information (space delimited) to a stream.
Definition: Config.cc:132
int getSrcFriend() const
Get the corresponding source for this destination.
Definition: Config.cc:98
static const char * usage
String for command-line arguments.
Definition: Config.hh:27
int srcCount() const
Get the number of sources for this process.
Definition: Config.cc:75
int destCount() const
Get the number of destinations for this process.
Definition: Config.cc:63
std::string typeName() const
Get the name of the type of this process.
Definition: Config.cc:87
int sources_
Count of source.
Definition: Config.hh:118
int sinks_
Count of sinks.
Definition: Config.hh:119
int getDestFriend() const
Get the corresponding destination for this source.
Definition: Config.cc:93
int srcStart() const
Get the rank of the first source for this process.
Definition: Config.cc:81
int total_procs_
Total number of processes.
Definition: Config.hh:115
int getArtArgc(int argc, char *argv[]) const
Gets the count of arguments after a – delimiter.
Definition: Config.cc:103
fhicl::ParameterSet makeParameterSet() const
Write a ParameterSet using configuration.
Definition: Config.cc:148
char ** getArtArgv(int argc, char *argv[]) const
Get the array of arguments after a – delimiter.
Definition: Config.cc:114
void printHeader(std::ostream &ost) const
Write configuration parameter names to a stream.
Definition: Config.cc:119
void writeInfo() const
Write information about this Config class to a file.
Definition: Config.cc:55
int detectors_
Count of detectors.
Definition: Config.hh:117
Config(int rank, int nprocs, int buffer_count, size_t max_payload_size, int argc, char *argv[])
Config Constructor.
Definition: Config.cc:26
int destStart() const
Get the rank of the first destination for this process.
Definition: Config.cc:69
std::string infoFilename(std::string const &prefix, int rank, int run)
Generate a filename using the given parameters.
Definition: infoFilename.cc:7