LIBINT  2.6.0
rr.h
1 /*
2  * Copyright (C) 2004-2019 Edward F. Valeev
3  *
4  * This file is part of Libint.
5  *
6  * Libint is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU 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  * Libint 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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Libint. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef _libint2_src_bin_libint_rr_h_
22 #define _libint2_src_bin_libint_rr_h_
23 
24 #include <iostream>
25 #include <sstream>
26 #include <string>
27 #include <vector>
28 #include <stdexcept>
29 #include <exception.h>
30 #include <bfset.h>
31 #include <smart_ptr.h>
32 #include <polyconstr.h>
33 #include <singl_stack.h>
34 #include <code.h>
35 #include <default_params.h>
36 #include <util_types.h>
37 #include <global_macros.h>
38 
39 using namespace std;
40 
41 namespace libint2 {
42 
43  class DGVertex;
44  class CodeContext;
45  class ImplicitDimensions;
46  class DirectedGraph;
47  template <typename V> class AlgebraicOperator;
48 
52  template <typename RR>
53  class RRStackBase : public SingletonStack<RR,std::string>
54  {
55  private:
56 
57  static SafePtr<RRStackBase<RR> > rrstack_;
58 
59  // private constructor because it's a Singleton
60  RRStackBase() : parent_type(& RR::label) {}
61 
62  public:
64  typedef typename parent_type::data_type data_type;
65  typedef typename parent_type::value_type value_type;
66  typedef typename parent_type::iter_type iter_type;
67  typedef typename parent_type::citer_type citer_type;
68  typedef typename parent_type::InstanceID InstanceID;
69 
71  static SafePtr<RRStackBase<RR> >& Instance() {
72  if (!rrstack_) {
73  SafePtr<RRStackBase<RR> > tmpstack(new RRStackBase<RR>);
74  rrstack_ = tmpstack;
75  }
76  return rrstack_;
77  }
78 
79  virtual ~RRStackBase() {}
80 
82  void add(const SafePtr<RRStackBase<RR> >& rrs) {
83  for(citer_type it=rrs->begin(); it != rrs->end(); it++) {
84  find((*it).second.second);
85  }
86  }
87 
89  void remove(const data_type& rr) {
90  parent_type::remove(rr);
91  }
92  };
93 
94  template <typename RR>
95  SafePtr<RRStackBase<RR> > RRStackBase<RR>::rrstack_;
96 
97 
101  class RecurrenceRelation : public EnableSafePtrFromThis<RecurrenceRelation> {
102  public:
104 
106  virtual ~RecurrenceRelation();
107 
113  virtual unsigned int num_children() const =0;
115  virtual SafePtr<DGVertex> rr_child(unsigned int i) const =0;
117  virtual SafePtr<DGVertex> rr_target() const =0;
118 
123  const SafePtr<ExprType>& rr_expr() const { return expr_; }
124 
131  virtual bool is_simple() const =0;
135  virtual bool invariant_type() const;
139  virtual int partindex_direction() const { return 0; }
144  virtual BraketDirection braket_direction() const { return BraketDirection::None; }
148  size_t size_of_children() const;
149 
155  const std::string& label() const;
156 
160  virtual std::string description() const;
161 
163  virtual void generate_code(const SafePtr<CodeContext>& context,
164  const SafePtr<ImplicitDimensions>& dims,
165  const std::string& funcname,
166  std::ostream& decl, std::ostream& def);
167 
170  virtual void generate_generic_code(const SafePtr<CodeContext>& context,
171  const SafePtr<ImplicitDimensions>& dims,
172  const std::string& funcname,
173  std::ostream& decl, std::ostream& def);
174 
176  virtual std::string spfunction_call(const SafePtr<CodeContext>& context,
177  const SafePtr<ImplicitDimensions>& dims) const;
178 
180  unsigned int nflops() const { return nflops_; }
181 
184 
185  protected:
186  unsigned int nflops_;
187  mutable std::string label_;
188  SafePtr<ExprType> expr_;
190  void add_expr(const SafePtr<ExprType>& a, int minus=1);
192  virtual std::string generate_label() const =0;
194  template <class RR> bool register_with_rrstack() {
195  // only register RRs with for shell sets
197  return false;
198  SafePtr<RRStackBase<RecurrenceRelation> > rrstack = RRStackBase<RecurrenceRelation>::Instance();
199  SafePtr<RR> this_ptr =
200  const_pointer_cast<RR,const RR>(
201  static_pointer_cast<const RR, const RecurrenceRelation>(
202  EnableSafePtrFromThis<RecurrenceRelation>::SafePtr_from_this()
203  )
204  );
205  rrstack->find(this_ptr);
206 #if DEBUG || DEBUG_CONSTRUCTION
207  std::cout << "register_with_rrstack: registered " << this_ptr->label() << std::endl;
208 #endif
209  return true;
210  }
211  private:
212 
215  SafePtr<DirectedGraph> generate_graph_(const SafePtr<DirectedGraph>& dg);
218  void assign_symbols_(SafePtr<CodeSymbols>& S);
221  virtual SafePtr<ImplicitDimensions> adapt_dims_(const SafePtr<ImplicitDimensions>& dims) const;
222 
224  virtual bool has_generic(const SafePtr<CompilationParameters>& cparams) const;
226  virtual std::string generic_header() const;
228  virtual std::string generic_instance(const SafePtr<CodeContext>& context, const SafePtr<CodeSymbols>& args) const;
229 
230  };
231 
232  namespace algebra {
234  SafePtr<RecurrenceRelation::ExprType> operator+(const SafePtr<DGVertex>& A,
235  const SafePtr<DGVertex>& B);
236  SafePtr<RecurrenceRelation::ExprType> operator-(const SafePtr<DGVertex>& A,
237  const SafePtr<DGVertex>& B);
238  SafePtr<RecurrenceRelation::ExprType> operator*(const SafePtr<DGVertex>& A,
239  const SafePtr<DGVertex>& B);
240  SafePtr<RecurrenceRelation::ExprType> operator/(const SafePtr<DGVertex>& A,
241  const SafePtr<DGVertex>& B);
242  const SafePtr<RecurrenceRelation::ExprType>& operator+=(SafePtr<RecurrenceRelation::ExprType>& A,
243  const SafePtr<DGVertex>& B);
244  const SafePtr<RecurrenceRelation::ExprType>& operator-=(SafePtr<RecurrenceRelation::ExprType>& A,
245  const SafePtr<DGVertex>& B);
246  const SafePtr<RecurrenceRelation::ExprType>& operator*=(SafePtr<RecurrenceRelation::ExprType>& A,
247  const SafePtr<DGVertex>& B);
248  const SafePtr<RecurrenceRelation::ExprType>& operator/=(SafePtr<RecurrenceRelation::ExprType>& A,
249  const SafePtr<DGVertex>& B);
250 
251  class Entity;
252  template <class T> class RTimeEntity;
253  template <class T> class CTimeEntity;
254  // seems to confound Intel compiler on Linux?
255  //SafePtr<RecurrenceRelation::ExprType> operator*(const SafePtr<Entity>& A,
256  // const SafePtr<DGVertex>& B);
257  template<typename T> SafePtr<RecurrenceRelation::ExprType> operator*(const SafePtr<RTimeEntity<T> >& A,
258  const SafePtr<DGVertex>& B);
259  template<typename T> SafePtr<RecurrenceRelation::ExprType> operator*(const SafePtr<CTimeEntity<T> >& A,
260  const SafePtr<DGVertex>& B);
261  };
262 
263  // Instantiate the RRStack
265 
266 };
267 
268 //#include <vrr_11_twoprep_11.h>
269 //#include <hrr.h>
270 //#include <shell_to_ints.h>
271 //#include <iter.h>
272 
273 #endif
274 
SingletonStack<T,KeyType> helps to implement Singleton-like objects of type T.
Definition: singl_stack.h:43
void add(const SafePtr< RRStackBase< RR > > &rrs)
adds content of rrs to this stack
Definition: rr.h:82
TrivialBFSet<T> defines static member result, which is true if T is a basis function set consisting o...
Definition: bfset.h:892
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:24
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:280
KeyTypes::InstanceID InstanceID
Specifies type for the instance index variables.
Definition: singl_stack.h:49
map_type::iterator iter_type
use iter_type objects to iterate over the stack
Definition: singl_stack.h:53
void inst_id(const SingletonStack< RecurrenceRelation, string >::InstanceID &i)
RecurrenceRelation is managed by SingletonStack but doesn't need to keep track of instance ID.
Definition: rr.h:183
virtual int partindex_direction() const
Definition: rr.h:139
AlgebraicOperator is an algebraic operator that acts on objects of type T.
Definition: algebra.h:48
unsigned int nflops() const
Return the number of FLOPs per this recurrence relation.
Definition: rr.h:180
void remove(const data_type &rr)
removes rr from the stack
Definition: rr.h:89
virtual BraketDirection braket_direction() const
Definition: rr.h:144
const SafePtr< ExprType > & rr_expr() const
Returns the expression.
Definition: rr.h:123
map_type::const_iterator citer_type
const version of iter_type
Definition: singl_stack.h:55
bool register_with_rrstack()
Registers with the stack.
Definition: rr.h:194
RecurrenceRelation describes all recurrence relations.
Definition: rr.h:101
AlgebraicOperator< DGVertex > ExprType
Numerical expression of a recurrence relation is always expressed as an AlgebraicOperator<DGVertex>
Definition: rr.h:121
RRStack implements a stack of RecurrenceRelation's which can only hold one instance of a given RR.
Definition: rr.h:53
Definition: rr.h:252
Definition: rr.h:253
static SafePtr< RRStackBase< RR > > & Instance()
Obtain the unique Instance of RRStack.
Definition: rr.h:71