aGrUM  0.21.0
a C++ library for (probabilistic) graphical models
scheduleCombination.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief A generic interface to combine efficiently several ScheduleMultiDims
24  *
25  * MultiDimCombination is a generic interface designed to combine efficiently
26  * several ScheduleMultiDims, that is, to compute expressions like
27  * T1 op T2 op T3 op .... op Tn, where the Ti's are either the ScheduleMultiDims
28  * or just MultiDimImplementations that will be later wrapped into
29  * ScheduleMultiDims and op is an operator or a function taking in argument two
30  * such objects and producing a new (combined) Ti object. By default, the
31  * combination operation "op" is assumed to be COMMUTATIVE and ASSOCIATIVE.
32  *
33  * To be quite generic, the MultiDimCombination takes in argument the function
34  * that produces the result of the combination of two multidimensional objects.
35  * The following code gives an example of the usage of MultiDimCombinations:
36  * @code
37  * // a function used to combine two MultiDimImplementation<float>'s:
38  * MultiDimImplementation<float>* add ( const MultiDimImplementation<float>& t1,
39  * const MultiDimImplementation<float>& t2
40  *) {
41  * return new MultiDimImplementation<float> (t1 + t2);
42  * }
43  *
44  * // another function used to combine two MultiDimImplementation<float>'s:
45  * MultiDimImplementation<float>*
46  * mult ( const MultiDimImplementation<float>& t1,
47  * const MultiDimImplementation<float>& t2 ) {
48  * return new MultiDimImplementation<float> (t1 * t2);
49  * }
50  *
51  *
52  * MultiDimImplementation<float> t1, t2, t3;
53  * Set<const MultiDimImplementation<float>*> set;
54  * set << &t1 << &t2 << &t3;
55  * ScheduleCombinationBasic<float> Comb ( add );
56  * ScheduleMultiDim<float> combined_table = Comb.combine ( set );
57  *
58  *
59  * // do the same thing only with ScheduleMultiDims
60  * MultiDimImplementation<float> tt4, tt5;
61  * ScheduleMultiDim<float> t4 ( &tt4 ), t5 ( &tt5 );
62  * Set<const ScheduleMultiDim<float>*> set;
63  * set << &t4 << &t5;
64  * ScheduleCombinationBasic<float> Comb ( add );
65  * ScheduleMultiDim<float> combined_table2 = Comb.combine ( set );
66  *
67  * // change the operator to apply
68  * Comb.setCombineFunction ( mult );
69  * ScheduleMultiDim<float> combined_table3 = Comb.combine ( set );
70  *
71  * @endcode
72  *
73  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
74  */
75 
76 #ifndef GUM_SCHEDULE_COMBINATION_H
77 #define GUM_SCHEDULE_COMBINATION_H
78 
79 #include <agrum/tools/core/set.h>
80 #include <agrum/tools/graphicalModels/inference/scheduler/schedule.h>
81 #include <agrum/tools/graphicalModels/inference/scheduler/scheduleMultiDim.h>
82 #include <agrum/tools/multidim/implementations/multiDimImplementation.h>
83 #include <utility>
84 
85 namespace gum {
86 
87  template < typename GUM_SCALAR >
89  public:
90  // ############################################################################
91  /// @name Constructors / Destructors
92  // ############################################################################
93  /// @{
94 
95  /// default constructor
97 
98  /// copy constructor
99  ScheduleCombination(const ScheduleCombination< GUM_SCALAR >&);
100 
101  /// destructor
102  virtual ~ScheduleCombination();
103 
104  /// virtual constructor
105  /** @return a new fresh ScheduleCombinator with the same combination
106  * function. */
107  virtual ScheduleCombination< GUM_SCALAR >* newFactory() const = 0;
108 
109  /// @}
110 
111  // ############################################################################
112  /// @name Accessors/Modifiers
113  // ############################################################################
114  /// @{
115 
116  // adds operations to an already created schedule
117  /** @returns the ScheduleMultiDim resulting from the combination
118  * @throws InvalidArgumentsNumber exception is thrown if the set passed in
119  * argument contains less than two elements */
120  virtual ScheduleMultiDim< GUM_SCALAR >
121  combine(const Set< const ScheduleMultiDim< GUM_SCALAR >* >& set,
123  = 0;
125  combine(const Set< const MultiDimImplementation< GUM_SCALAR >* >& set,
127  template < template < typename > class TABLE >
128  ScheduleMultiDim< GUM_SCALAR > combine(const Set< const TABLE< GUM_SCALAR >* >& set,
130 
131  /// changes the function used for combining two TABLES
135  = 0;
136 
137  /// returns the combination function currently used by the combinator
138  virtual MultiDimImplementation< GUM_SCALAR >* (
139  *combineFunction())(const MultiDimImplementation< GUM_SCALAR >&,
140  const MultiDimImplementation< GUM_SCALAR >&)
141  = 0;
142 
143  /** @brief returns a rough estimate of the number of operations that will be
144  * performed to compute the combination */
145  virtual float nbOperations(const Set< const ScheduleMultiDim< GUM_SCALAR >* >& set,
146  const Schedule< GUM_SCALAR >& schedule)
147  = 0;
148  float nbOperations(const Set< const MultiDimImplementation< GUM_SCALAR >* >& set,
149  const Schedule< GUM_SCALAR >& schedule);
150  template < template < typename > class TABLE >
151  float nbOperations(const Set< const TABLE< GUM_SCALAR >* >& set,
152  const Schedule< GUM_SCALAR >& schedule);
153 
154  /// returns the memory consumption used during the combination
155  /** Actually, this function does not return a precise account of the memory
156  * used by the multidimCombination but a rough estimate based on the sizes
157  * of the tables involved in the combination.
158  * @return a pair of memory consumption: the first one is the maximum
159  * amount of memory used during the combination and the second one is the
160  * amount of memory still used at the end of the function ( the memory used
161  * by
162  * the resulting table ) */
163  virtual std::pair< long, long >
164  memoryUsage(const Set< const ScheduleMultiDim< GUM_SCALAR >* >& set,
165  const Schedule< GUM_SCALAR >& schedule)
166  = 0;
167  std::pair< long, long >
168  memoryUsage(const Set< const MultiDimImplementation< GUM_SCALAR >* >& set,
169  const Schedule< GUM_SCALAR >& schedule);
170  template < template < typename > class TABLE >
171  std::pair< long, long > memoryUsage(const Set< const TABLE< GUM_SCALAR >* >& set,
172  const Schedule< GUM_SCALAR >& schedule);
173 
174  /// @}
175 
176  private:
177  /// forbid copy operators
178  ScheduleCombination< GUM_SCALAR >& operator=(const ScheduleCombination< GUM_SCALAR >&);
179  };
180 
181 } /* namespace gum */
182 
183 // always include the template implementation
184 #include <agrum/tools/graphicalModels/inference/scheduler/scheduleCombination_tpl.h>
185 
186 #endif /* GUM_SCHEDULE_COMBINATION_H */
ScheduleCombination(const ScheduleCombination< GUM_SCALAR > &)
copy constructor
ScheduleCombination()
default constructor
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
float nbOperations(const Set< const TABLE< GUM_SCALAR > * > &set, const Schedule< GUM_SCALAR > &schedule)
ScheduleCombination< GUM_SCALAR > & operator=(const ScheduleCombination< GUM_SCALAR > &)
forbid copy operators
virtual void setCombineFunction(MultiDimImplementation< GUM_SCALAR > *(*combine)(const MultiDimImplementation< GUM_SCALAR > &, const MultiDimImplementation< GUM_SCALAR > &))=0
changes the function used for combining two TABLES
ScheduleMultiDim< GUM_SCALAR > combine(const Set< const MultiDimImplementation< GUM_SCALAR > * > &set, Schedule< GUM_SCALAR > &schedule)
virtual ScheduleCombination< GUM_SCALAR > * newFactory() const =0
virtual constructor
virtual ~ScheduleCombination()
destructor
std::pair< long, long > memoryUsage(const Set< const MultiDimImplementation< GUM_SCALAR > * > &set, const Schedule< GUM_SCALAR > &schedule)
float nbOperations(const Set< const MultiDimImplementation< GUM_SCALAR > * > &set, const Schedule< GUM_SCALAR > &schedule)
std::pair< long, long > memoryUsage(const Set< const TABLE< GUM_SCALAR > * > &set, const Schedule< GUM_SCALAR > &schedule)
ScheduleMultiDim< GUM_SCALAR > combine(const Set< const TABLE< GUM_SCALAR > * > &set, Schedule< GUM_SCALAR > &schedule)