LIBINT  2.1.0-stable
rr.h
1 /*
2  * This file is a part of Libint.
3  * Copyright (C) 2004-2014 Edward F. Valeev
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see http://www.gnu.org/licenses/.
17  *
18  */
19 
20 #ifndef _libint2_src_bin_libint_rr_h_
21 #define _libint2_src_bin_libint_rr_h_
22 
23 #include <iostream>
24 #include <sstream>
25 #include <string>
26 #include <vector>
27 #include <stdexcept>
28 #include <exception.h>
29 #include <bfset.h>
30 #include <smart_ptr.h>
31 #include <polyconstr.h>
32 #include <singl_stack.h>
33 #include <code.h>
34 #include <default_params.h>
35 #include <util_types.h>
36 #include <global_macros.h>
37 
38 using namespace std;
39 
40 namespace libint2 {
41 
42  class DGVertex;
43  class CodeContext;
44  class ImplicitDimensions;
45  class DirectedGraph;
46  template <typename V> class AlgebraicOperator;
47 
51  template <typename RR>
52  class RRStackBase : public SingletonStack<RR,std::string>
53  {
54  private:
55 
56  static SafePtr<RRStackBase<RR> > rrstack_;
57 
58  // private constructor because it's a Singleton
59  RRStackBase() : parent_type(& RR::label) {}
60 
61  public:
63  typedef typename parent_type::data_type data_type;
64  typedef typename parent_type::value_type value_type;
65  typedef typename parent_type::iter_type iter_type;
66  typedef typename parent_type::citer_type citer_type;
67  typedef typename parent_type::InstanceID InstanceID;
68 
70  static SafePtr<RRStackBase<RR> >& Instance() {
71  if (!rrstack_) {
72  SafePtr<RRStackBase<RR> > tmpstack(new RRStackBase<RR>);
73  rrstack_ = tmpstack;
74  }
75  return rrstack_;
76  }
77 
78  virtual ~RRStackBase() {}
79 
81  void add(const SafePtr<RRStackBase<RR> >& rrs) {
82  for(citer_type it=rrs->begin(); it != rrs->end(); it++) {
83  find((*it).second.second);
84  }
85  }
86 
88  void remove(const data_type& rr) {
89  parent_type::remove(rr);
90  }
91  };
92 
93  template <typename RR>
94  SafePtr<RRStackBase<RR> > RRStackBase<RR>::rrstack_;
95 
96 
100  class RecurrenceRelation : public EnableSafePtrFromThis<RecurrenceRelation> {
101  public:
103 
105  virtual ~RecurrenceRelation();
106 
112  virtual unsigned int num_children() const =0;
114  virtual SafePtr<DGVertex> rr_child(unsigned int i) const =0;
116  virtual SafePtr<DGVertex> rr_target() const =0;
117 
122  const SafePtr<ExprType>& rr_expr() const { return expr_; }
123 
130  virtual bool is_simple() const =0;
134  virtual bool invariant_type() const;
139  virtual int partindex_direction() const { return 0; }
143  size_t size_of_children() const;
144 
150  const std::string& label() const;
151 
155  virtual std::string description() const;
156 
158  virtual void generate_code(const SafePtr<CodeContext>& context,
159  const SafePtr<ImplicitDimensions>& dims,
160  const std::string& funcname,
161  std::ostream& decl, std::ostream& def);
162 
165  virtual void generate_generic_code(const SafePtr<CodeContext>& context,
166  const SafePtr<ImplicitDimensions>& dims,
167  const std::string& funcname,
168  std::ostream& decl, std::ostream& def);
169 
171  virtual std::string spfunction_call(const SafePtr<CodeContext>& context,
172  const SafePtr<ImplicitDimensions>& dims) const;
173 
175  unsigned int nflops() const { return nflops_; }
176 
179 
180  protected:
181  unsigned int nflops_;
182  mutable std::string label_;
183  SafePtr<ExprType> expr_;
185  void add_expr(const SafePtr<ExprType>& a, int minus=1);
187  virtual std::string generate_label() const =0;
189  template <class RR> bool register_with_rrstack() {
190  // only register RRs with for shell sets
192  return false;
193  SafePtr<RRStackBase<RecurrenceRelation> > rrstack = RRStackBase<RecurrenceRelation>::Instance();
194  SafePtr<RR> this_ptr =
195  const_pointer_cast<RR,const RR>(
196  static_pointer_cast<const RR, const RecurrenceRelation>(
197  EnableSafePtrFromThis<RecurrenceRelation>::SafePtr_from_this()
198  )
199  );
200  rrstack->find(this_ptr);
201 #if DEBUG || DEBUG_CONSTRUCTION
202  std::cout << "register_with_rrstack: registered " << this_ptr->label() << std::endl;
203 #endif
204  return true;
205  }
206  private:
207 
210  SafePtr<DirectedGraph> generate_graph_(const SafePtr<DirectedGraph>& dg);
213  void assign_symbols_(SafePtr<CodeSymbols>& S);
216  virtual SafePtr<ImplicitDimensions> adapt_dims_(const SafePtr<ImplicitDimensions>& dims) const;
217 
219  virtual bool has_generic(const SafePtr<CompilationParameters>& cparams) const;
221  virtual std::string generic_header() const;
223  virtual std::string generic_instance(const SafePtr<CodeContext>& context, const SafePtr<CodeSymbols>& args) const;
224 
225  };
226 
227  namespace algebra {
229  SafePtr<RecurrenceRelation::ExprType> operator+(const SafePtr<DGVertex>& A,
230  const SafePtr<DGVertex>& B);
231  SafePtr<RecurrenceRelation::ExprType> operator-(const SafePtr<DGVertex>& A,
232  const SafePtr<DGVertex>& B);
233  SafePtr<RecurrenceRelation::ExprType> operator*(const SafePtr<DGVertex>& A,
234  const SafePtr<DGVertex>& B);
235  SafePtr<RecurrenceRelation::ExprType> operator/(const SafePtr<DGVertex>& A,
236  const SafePtr<DGVertex>& B);
237  const SafePtr<RecurrenceRelation::ExprType>& operator+=(SafePtr<RecurrenceRelation::ExprType>& A,
238  const SafePtr<DGVertex>& B);
239  const SafePtr<RecurrenceRelation::ExprType>& operator-=(SafePtr<RecurrenceRelation::ExprType>& A,
240  const SafePtr<DGVertex>& B);
241  const SafePtr<RecurrenceRelation::ExprType>& operator*=(SafePtr<RecurrenceRelation::ExprType>& A,
242  const SafePtr<DGVertex>& B);
243  const SafePtr<RecurrenceRelation::ExprType>& operator/=(SafePtr<RecurrenceRelation::ExprType>& A,
244  const SafePtr<DGVertex>& B);
245 
246  class Entity;
247  template <class T> class RTimeEntity;
248  template <class T> class CTimeEntity;
249  // seems to confound Intel compiler on Linux?
250  //SafePtr<RecurrenceRelation::ExprType> operator*(const SafePtr<Entity>& A,
251  // const SafePtr<DGVertex>& B);
252  template<typename T> SafePtr<RecurrenceRelation::ExprType> operator*(const SafePtr<RTimeEntity<T> >& A,
253  const SafePtr<DGVertex>& B);
254  template<typename T> SafePtr<RecurrenceRelation::ExprType> operator*(const SafePtr<CTimeEntity<T> >& A,
255  const SafePtr<DGVertex>& B);
256  };
257 
258  // Instantiate the RRStack
260 
261 };
262 
263 //#include <vrr_11_twoprep_11.h>
264 //#include <hrr.h>
265 //#include <shell_to_ints.h>
266 //#include <iter.h>
267 
268 #endif
269 
SingletonStack<T,KeyType> helps to implement Singleton-like objects of type T.
Definition: singl_stack.h:42
void add(const SafePtr< RRStackBase< RR > > &rrs)
adds content of rrs to this stack
Definition: rr.h:81
TrivialBFSet<T> defines static member result, which is true if T is a basis function set consisting o...
Definition: bfset.h:879
const SafePtr< ExprType > & rr_expr() const
Returns the expression.
Definition: rr.h:122
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:23
Definition: stdarray.h:18
SafePtr< CTimeEntity< typename ProductType< T, U >::result > > operator*(const SafePtr< CTimeEntity< T > > &A, const SafePtr< CTimeEntity< U > > &B)
Creates product A*B.
Definition: entity.h:277
KeyTypes::InstanceID InstanceID
Specifies type for the instance index variables.
Definition: singl_stack.h:48
map_type::iterator iter_type
use iter_type objects to iterate over the stack
Definition: singl_stack.h:52
void inst_id(const SingletonStack< RecurrenceRelation, string >::InstanceID &i)
RecurrenceRelation is managed by SingletonStack but doesn&#39;t need to keep track of instance ID...
Definition: rr.h:178
Entity is a base class for all objects that exist at compile or runtime of the generated code...
Definition: entity.h:78
AlgebraicOperator is an algebraic operator that acts on objects of type T.
Definition: algebra.h:47
unsigned int nflops() const
Return the number of FLOPs per this recurrence relation.
Definition: rr.h:175
map_type::const_iterator citer_type
const version of iter_type
Definition: singl_stack.h:54
bool register_with_rrstack()
Registers with the stack.
Definition: rr.h:189
RecurrenceRelation describes all recurrence relations.
Definition: rr.h:100
AlgebraicOperator< DGVertex > ExprType
Numerical expression of a recurrence relation is always expressed as an AlgebraicOperator<DGVertex> ...
Definition: rr.h:120
RRStack implements a stack of RecurrenceRelation&#39;s which can only hold one instance of a given RR...
Definition: rr.h:52
Definition: rr.h:247
virtual int partindex_direction() const
Definition: rr.h:139
Definition: rr.h:248
static SafePtr< RRStackBase< RR > > & Instance()
Obtain the unique Instance of RRStack.
Definition: rr.h:70