Hammer  1.0.0
Helicity Amplitude Module for Matrix Element Reweighting
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Optimize.cc
Go to the documentation of this file.
1 ///
2 /// @file Optimize.cc
3 /// @brief Tensor storage re-optimization algorithm
4 ///
5 
6 //**** This file is a part of the HAMMER library
7 //**** Copyright (C) 2016 - 2020 The HAMMER Collaboration
8 //**** HAMMER is licensed under version 3 of the GPL; see COPYING for details
9 //**** Please note the MCnet academic guidelines; see GUIDELINES for details
10 
11 // -*- C++ -*-
18 #include "Hammer/Exceptions.hh"
19 #include "Hammer/Tools/Utils.hh"
20 #include "Hammer/Math/Utils.hh"
21 
22 using namespace std;
23 
24 namespace Hammer {
25 
26  namespace MultiDimensional {
27 
28  using VTensor = VectorContainer;
29  using STensor = SparseContainer;
30  using OTensor = OuterContainer;
31  using Base = IContainer;
32 
33  namespace Ops {
34 
35  Optimize::Optimize() {
36  }
37 
38  Base* Optimize::operator()(STensor& a) {
39  if (shouldBeSparse(a.dataSize() / a.entrySize(), a.numValues())) {
40  return (Convert{false})(a);
41  }
42  return static_cast<Base*>(&a);
43  }
44 
45  Base* Optimize::operator()(VTensor& a) {
46  if (shouldBeSparse(a.dataSize() / a.entrySize(), a.numValues())) {
47  return (Convert{true})(a);
48  }
49  return static_cast<Base*>(&a);
50  }
51 
52  Base* Optimize::operator()(OTensor& a) {
53  // first check whether we need to collapse it to sparse or vector tensors
54  if(a.shouldBeEvaluated()) {
55  auto sparse = dynamic_cast<STensor*>((Convert{true})(a));
56  return (*this)(*sparse);
57  }
58  // otherwise loop over constituents and optimize those
59  auto ptrs = a.getUniquePtrs();
60  for (auto elem : ptrs) {
61  Base* res = nullptr;
62  auto sparse = dynamic_cast<STensor*>(elem);
63  /// @todo improve here with auto dispatch if more tensors will be added
64  if(sparse != nullptr) {
65  res = (*this)(*sparse);
66  }
67  else {
68  auto vec = dynamic_cast<VTensor*>(elem);
69  if (vec != nullptr) {
70  res = (*this)(*vec);
71  }
72  }
73  if (res != nullptr && res != elem) {
74  a.swapElement(elem, TensorData{res});
75  }
76  }
77  return static_cast<Base*>(&a);
78  }
79 
80  Base* Optimize::error(Base& a) {
81  return &a;
82  }
83 
84  } // namespace Ops
85 
86 
87  } // namespace MultiDimensional
88 
89 } // namespace Hammer
void swapElement(IContainer *oldContainer, TensorData newContainer)
Tensor storage re-optimization algorithm.
Non-sparse tensor data container.
std::unique_ptr< IContainer > TensorData
(Sum of) Outer product tensor data container
Hammer exception definitions.
Sparse tensor data container.
SparseContainer STensor
Definition: AddAt.cc:28
VectorContainer VTensor
Definition: AddAt.cc:27
bool shouldBeSparse(size_t fill, size_t total)
Definition: Optimize.hh:43
OuterContainer OTensor
Definition: AddAt.cc:29
Generic tensor indexing iterator.
Tensor storage type conversion algorithm.
std::set< IContainer * > getUniquePtrs(bool decoupleConjugates=false)