MarlinMT  0.1.0
BookStoreManager.cc
Go to the documentation of this file.
1 
2 // -- marlinmt headers
4 #include <marlinmt/Application.h>
5 #include <marlinmt/Exceptions.h>
6 #include <marlinmt/Processor.h>
7 
8 // -- MarlinMTBook headers
10 
11 // -- std headers
12 #include <string>
13 
14 // -- unix specific includes
15 #include <sys/types.h>
16 #include <unistd.h>
17 
18 namespace marlinmt {
19 
20 #define INSTANCIATIONS_HIST(type) \
21  template book::Handle<book::Entry<type>> BookStoreManager::getObject<type>(\
22  const book::EntryKey&) const ;\
23  \
24  template book::Handle<book::Entry<type>> BookStoreManager::bookHist<type>(\
25  const std::filesystem::path&,\
26  const std::string_view&,\
27  const std::string_view&,\
28  const std::array<\
29  const AxisConfig<typename type::Precision_t>*,\
30  type::Dimension>&,\
31  const BookFlag_t&)
32 
42 
43  //--------------------------------------------------------------------------
44 
46  Component("BookStoreManager") {
47  setName("BookStore") ;
48  }
49 
50  //--------------------------------------------------------------------------
51 
53  book::StoreWriter writer ( _outputFile.get() ) ;
54  _bookStore.storeList( writer, _entriesToWrite.begin(), _entriesToWrite.end());
55  }
56 
57  //--------------------------------------------------------------------------
58 
60  auto &config = application().configuration() ;
61  if( config.hasSection("bookstore") ) {
62  const auto &section = config.section("bookstore") ;
63  setParameters( section ) ;
64  }
65  // possible options for flags
66  static const std::map<std::string, BookFlag_t> flags {
67  {"share", BookFlags::MultiShared},
68  {"copy", BookFlags::MultiCopy},
69  {"default", BookFlags::MultiShared},
70  } ;
71  BookFlag_t memoryLayout( 0 ) ;
72  std::string memLayoutStr = _defaultMemLayout.get() ;
73  details::to_lower( memLayoutStr ) ;
74  // if one thread only to layout is also the same: single
75  if( 1 == application().cmdLineParseResult()._nthreads ) {
76  memoryLayout = BookFlags::Single ;
77  }
78  else {
79  auto iter = flags.find( memLayoutStr ) ;
80  if( flags.end() == iter ) {
81  MARLINMT_THROW( "Unknown memory layout flag '" + _defaultMemLayout.get() + "'" ) ;
82  }
83  memoryLayout = iter->second ;
84  }
85  _defaultFlag = BookFlags::Store | memoryLayout ;
86  }
87 
88  //--------------------------------------------------------------------------
89 
90  template<typename HistT>
92  const std::filesystem::path &path,
93  const std::string_view &name,
94  const std::string_view &title,
95  const std::array<
97  HistT::Dimension> &axesconfig,
98  const BookFlag_t &flag) {
99 
100  const BookFlag_t& usedFlag = flag == BookFlags::Default
101  ? _defaultFlag
102  : flag;
103 
104  using Entry_t = book::Handle<book::Entry<HistT>>;
105 
106  bool store = usedFlag.contains(book::Flags::Book::Store);
107 
108  BookFlag_t flagsToPass = usedFlag & book::Masks::Book::MemoryLayout ;
110  if ( 1 == nthreads ) {
111  flagsToPass = book::Flags::Book::Single;
112  }
113 
114  try {
115  Entry_t res = getObject<HistT>(getKey(path, name)) ;
116  const book::EntryKey& key = res.key();
117  if ( key.flags != flagsToPass
118  || key.type != std::type_index(typeid(HistT))) {
119  MARLINMT_THROW("try to book to the same spot again");
120  }
121  return res;
122  } catch (const BookStoreManager::ObjectNotFound& ) {}
123 
124  book::EntryData<HistT> data(title, axesconfig);
125 
127 
128  if( flagsToPass.contains(book::Flags::Book::MultiCopy)) {
129  entry = _bookStore.book( path, name, data.multiCopy(nthreads) ) ;
130  }
131  else if ( flagsToPass.contains(book::Flags::Book::MultiShared)) {
132  entry = _bookStore.book( path, name, data.multiShared(nthreads) ) ;
133  }
134  else if ( flagsToPass.contains(book::Flags::Book::Single)) {
135  if ( nthreads != 1) {
136  _logger->log<ERROR>() << "Single Memory layout can't be used"
137  " with concurrency! \n"
138  "\tuse MarlinMT for workflows without concurrency";
139  MARLINMT_THROW("single only supported without concurrency!!");
140  }
141  entry = _bookStore.book( path, name, data.single() ) ;
142  }
143  else {
144  MARLINMT_THROW("Try to book without MemoryLayout Flag");
145  }
146 
147  if (store) {
148  addToWrite( entry.key() ) ;
149  }
150 
151  return entry;
152  }
153 
154  //--------------------------------------------------------------------------
155 
156  template<typename T>
158  const book::EntryKey &key) const {
159  if (key.type == std::type_index(typeid(T))) {
160  return _bookStore.entry<T>(key);
161  }
162  throw ObjectNotFound("try to access Object which wrong type!");
163  }
164 
165  //--------------------------------------------------------------------------
166 
168  _entriesToWrite.insert(key);
169  }
170 
171  //--------------------------------------------------------------------------
172 
174  _entriesToWrite.erase(key);
175  }
176 
177  //--------------------------------------------------------------------------
178 
180  const std::filesystem::path &path,
181  const std::string_view &name) const
182  {
185  .setPath(path.string())
186  .setName(name) ) ;
187  if (!res.valid()) {
188  throw ObjectNotFound("Object for key not found.");
189  }
190  return res.key();
191  }
192 
193 }
static constexpr std::size_t Dimension
Dimension of the histogram.
Definition: Base.h:187
Data selection to identify and manage an Entry.
Definition: EntryData.h:21
constexpr Flag_t MemoryLayout(Flags::value(Flags::Book::Single)|Flags::value(Flags::Book::MultiShared)|Flags::value(Flags::Book::MultiCopy))
Mask for Flags with memory layout options.
StringParameter _defaultMemLayout
Output file name to store objects.
const std::string & name() const
Get the component name.
Definition: Component.cc:23
bool valid() const
check if WeakEntry is usable.
Definition: Selection.cc:14
Container for data to construct and setup booked object.
Definition: EntryData.h:66
unsigned int nthreads(const std::string &str)
Definition: Utils.h:580
const EntryKey & key() const
get key from Entry.
Definition: Selection.cc:18
book::Handle< book::Entry< HistT > > bookHist(const std::filesystem::path &path, const std::string_view &name, const std::string_view &title, const std::array< const AxisConfig< typename HistT::Precision_t > *, HistT::Dimension > &axesconfig, const BookFlag_t &flags)
Book a histogram 1D, float type.
book::types::H3F Hist3F
void setName(const std::string &n)
Set the component name.
Definition: Component.cc:29
void setParameters(const ConfigSection &section, bool throwIfNotFound=false)
Set the parameters from the configuration section.
Definition: Component.cc:124
WeakEntry findFirst(const Condition &cond) const
find first Entry which match the condition.
Definition: BookStore.cc:63
LoggerPtr _logger
The logger instance.
Definition: Component.h:173
const CmdLineParseResult & cmdLineParseResult() const
Get the command line parsing result (after init)
Definition: Application.cc:165
book::types::H1F Hist1F
book::types::H1D Hist1D
constexpr Flag_t Store(1U<< 3U)
store object in file at end of lifetime
book::types::H1I Hist1I
book::types::H3D Hist3D
Flag type for flags in marlinmt::book.
Definition: Flags.h:15
std::set< book::EntryKey > _entriesToWrite
List of entry keys for Entries which should be stored at end of lifetime.
void initialize() override
Initialize the book store manager.
Handle< Entry< T > > entry(const EntryKey &key) const
get access to entry from key.
Definition: BookStore.h:491
void writeToDisk() const
reads output File from global StoreOutputFile.
book::types::H2F Hist2F
Generalized histogram class.
Definition: Base.h:151
#define MARLINMT_THROW(message)
Definition: Exceptions.h:8
constexpr Flag_t MultiShared(1U<< 1U)
create one instance witch concurrent access.
unsigned int _nthreads
The number of threads.
Definition: CmdLineParser.h:29
book::Handle< book::Entry< T > > getObject(const book::EntryKey &key) const
access object managed by this store.
Wrapper for weak pointer to Entry.
Definition: Selection.h:29
void removeFromWrite(const book::EntryKey &key)
remove entry key from write list.
Component class.
Definition: Component.h:22
collection for Axis Description
Definition: Base.h:23
Handle< Entry< typename T::Object_t > > book(const std::filesystem::path &path, const std::string_view &name, const T &data)
book new object.
Definition: BookStore.h:389
ConfigSection & section(const std::string &sn)
Get a section by name.
bool contains(const Flag_t &f) const
check if the flags is a subset of the other.
Definition: Flags.h:77
const Application & application() const
Get the application in which the component is registered.
Definition: Component.cc:83
book::types::H2I Hist2I
void storeList(StoreWriter &writer, Itr begin, Itr end) const
stores only Objects which key is listed.
Definition: BookStore.h:471
T get() const
Get the parameter value.
Definition: Parameter.h:556
thrown when try to access not existing object.
INSTANCIATIONS_HIST(Hist1F)
helper to create a Condition.
Definition: Condition.h:92
constexpr Flag_t MultiCopy(1U<< 2U)
create multiple instances of booked object (if possible) to avoid sync points
void to_lower(std::string &str)
Definition: Utils.h:592
constexpr Flag_t Default(0)
use default arguments
vanilla Handle.
Definition: Handle.h:54
const book::EntryKey & getKey(const std::filesystem::path &path, const std::string_view &name) const
receive key from Entry with given path and name.
void addToWrite(const book::EntryKey &key)
add entry key to write list.
book::types::H2D Hist2D
BookFlag_t _defaultFlag
default flag, used if flag == BookFlags::Default.
book::BookStore _bookStore
The book store.
constexpr Flag_t Single(1U<< 0U)
vanilla object.
const Configuration & configuration() const
Get the main application configuration object.
Definition: Application.cc:278
book::types::H3I Hist3I
std::type_index type
Type of object stored in Entry.
Definition: EntryData.h:34
StringParameter _outputFile
Output file name to store objects.