LIBINT 2.7.2
rr.h
1/*
2 * Copyright (C) 2004-2021 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
39namespace libint2 {
40
41 class DGVertex;
42 class CodeContext;
43 class ImplicitDimensions;
44 class DirectedGraph;
45 template <typename V> class AlgebraicOperator;
46
50 template <typename RR>
51 class RRStackBase : public SingletonStack<RR,std::string>
52 {
53 private:
54
55 static SafePtr<RRStackBase<RR> > rrstack_;
56
57 // private constructor because it's a Singleton
58 RRStackBase() : parent_type(& RR::label) {}
59
60 public:
62 typedef typename parent_type::data_type data_type;
63 typedef typename parent_type::value_type value_type;
64 typedef typename parent_type::iter_type iter_type;
65 typedef typename parent_type::citer_type citer_type;
66 typedef typename parent_type::InstanceID InstanceID;
67
69 static SafePtr<RRStackBase<RR> >& Instance() {
70 if (!rrstack_) {
71 SafePtr<RRStackBase<RR> > tmpstack(new RRStackBase<RR>);
72 rrstack_ = tmpstack;
73 }
74 return rrstack_;
75 }
76
77 virtual ~RRStackBase() {}
78
80 void add(const SafePtr<RRStackBase<RR> >& rrs) {
81 for(citer_type it=rrs->begin(); it != rrs->end(); it++) {
82 find((*it).second.second);
83 }
84 }
85
87 void remove(const data_type& rr) {
89 }
90 };
91
92 template <typename RR>
93 SafePtr<RRStackBase<RR> > RRStackBase<RR>::rrstack_;
94
95
99 class RecurrenceRelation : public EnableSafePtrFromThis<RecurrenceRelation> {
100 public:
102
104 virtual ~RecurrenceRelation();
105
111 virtual unsigned int num_children() const =0;
113 virtual SafePtr<DGVertex> rr_child(unsigned int i) const =0;
115 virtual SafePtr<DGVertex> rr_target() const =0;
116
121 const SafePtr<ExprType>& rr_expr() const { return expr_; }
122
129 virtual bool is_simple() const =0;
133 virtual bool invariant_type() const;
137 virtual int partindex_direction() const { return 0; }
142 virtual BraketDirection braket_direction() const { return BraketDirection::None; }
146 size_t size_of_children() const;
147
153 const std::string& label() const;
154
158 virtual std::string description() const;
159
161 virtual void generate_code(const SafePtr<CodeContext>& context,
162 const SafePtr<ImplicitDimensions>& dims,
163 const std::string& funcname,
164 std::ostream& decl, std::ostream& def);
165
168 virtual void generate_generic_code(const SafePtr<CodeContext>& context,
169 const SafePtr<ImplicitDimensions>& dims,
170 const std::string& funcname,
171 std::ostream& decl, std::ostream& def);
172
174 virtual std::string spfunction_call(const SafePtr<CodeContext>& context,
175 const SafePtr<ImplicitDimensions>& dims) const;
176
178 unsigned int nflops() const { return nflops_; }
179
182
183 protected:
184 unsigned int nflops_;
185 mutable std::string label_;
186 SafePtr<ExprType> expr_;
188 void add_expr(const SafePtr<ExprType>& a, int minus=1);
190 virtual std::string generate_label() const =0;
192 template <class RR> bool register_with_rrstack() {
193 // only register RRs with for shell sets
195 return false;
196 SafePtr<RRStackBase<RecurrenceRelation> > rrstack = RRStackBase<RecurrenceRelation>::Instance();
197 SafePtr<RR> this_ptr =
198 const_pointer_cast<RR,const RR>(
199 static_pointer_cast<const RR, const RecurrenceRelation>(
200 EnableSafePtrFromThis<RecurrenceRelation>::SafePtr_from_this()
201 )
202 );
203 rrstack->find(this_ptr);
204#if DEBUG || DEBUG_CONSTRUCTION
205 std::cout << "register_with_rrstack: registered " << this_ptr->label() << std::endl;
206#endif
207 return true;
208 }
209 private:
210
213 SafePtr<DirectedGraph> generate_graph_(const SafePtr<DirectedGraph>& dg);
216 void assign_symbols_(SafePtr<CodeSymbols>& S);
219 virtual SafePtr<ImplicitDimensions> adapt_dims_(const SafePtr<ImplicitDimensions>& dims) const;
220
222 virtual bool has_generic(const SafePtr<CompilationParameters>& cparams) const;
224 virtual std::string generic_header() const;
226 virtual std::string generic_instance(const SafePtr<CodeContext>& context, const SafePtr<CodeSymbols>& args) const;
227
228 };
229
230 namespace algebra {
232 SafePtr<RecurrenceRelation::ExprType> operator+(const SafePtr<DGVertex>& A,
233 const SafePtr<DGVertex>& B);
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 const SafePtr<RecurrenceRelation::ExprType>& operator+=(SafePtr<RecurrenceRelation::ExprType>& 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
249 class Entity;
250 template <class T> class RTimeEntity;
251 template <class T> class CTimeEntity;
252 // seems to confound Intel compiler on Linux?
253 //SafePtr<RecurrenceRelation::ExprType> operator*(const SafePtr<Entity>& A,
254 // const SafePtr<DGVertex>& B);
255 template<typename T> SafePtr<RecurrenceRelation::ExprType> operator*(const SafePtr<RTimeEntity<T> >& A,
256 const SafePtr<DGVertex>& B);
257 template<typename T> SafePtr<RecurrenceRelation::ExprType> operator*(const SafePtr<CTimeEntity<T> >& A,
258 const SafePtr<DGVertex>& B);
259 };
260
261 // Instantiate the RRStack
263
264};
265
266//#include <vrr_11_twoprep_11.h>
267//#include <hrr.h>
268//#include <shell_to_ints.h>
269//#include <iter.h>
270
271#endif
272
AlgebraicOperator is an algebraic operator that acts on objects of type T.
Definition: algebra.h:50
RRStack implements a stack of RecurrenceRelation's which can only hold one instance of a given RR.
Definition: rr.h:52
void remove(const data_type &rr)
removes rr from the stack
Definition: rr.h:87
static SafePtr< RRStackBase< RR > > & Instance()
Obtain the unique Instance of RRStack.
Definition: rr.h:69
void add(const SafePtr< RRStackBase< RR > > &rrs)
adds content of rrs to this stack
Definition: rr.h:80
RecurrenceRelation describes all recurrence relations.
Definition: rr.h:99
virtual SafePtr< DGVertex > rr_child(unsigned int i) const =0
Returns i-th child.
void inst_id(const SingletonStack< RecurrenceRelation, std::string >::InstanceID &i)
RecurrenceRelation is managed by SingletonStack but doesn't need to keep track of instance ID.
Definition: rr.h:181
virtual void generate_code(const SafePtr< CodeContext > &context, const SafePtr< ImplicitDimensions > &dims, const std::string &funcname, std::ostream &decl, std::ostream &def)
Generate declaration and definition for the recurrence relation.
Definition: rr.cc:59
virtual std::string description() const
description() returns a verbose description of this RR
Definition: rr.cc:300
virtual bool invariant_type() const
Returns true is the type of target and all children are exactly the same.
Definition: rr.cc:336
void add_expr(const SafePtr< ExprType > &a, int minus=1)
Adds a (or -a, if minus = -1) to expr_.
Definition: rr.cc:307
virtual void generate_generic_code(const SafePtr< CodeContext > &context, const SafePtr< ImplicitDimensions > &dims, const std::string &funcname, std::ostream &decl, std::ostream &def)
Generate declaration and definition for the recurrence relation using generic code (typically,...
Definition: rr.cc:197
size_t size_of_children() const
Definition: rr.cc:375
AlgebraicOperator< DGVertex > ExprType
Numerical expression of a recurrence relation is always expressed as an AlgebraicOperator<DGVertex>
Definition: rr.h:119
virtual SafePtr< DGVertex > rr_target() const =0
Returns the target.
virtual unsigned int num_children() const =0
num_children() returns the actual number of children.
virtual BraketDirection braket_direction() const
Definition: rr.h:142
const SafePtr< ExprType > & rr_expr() const
Returns the expression.
Definition: rr.h:121
bool register_with_rrstack()
Registers with the stack.
Definition: rr.h:192
const std::string & label() const
label() returns a unique, short, descriptive label of this RR (e.g.
Definition: rr.cc:293
virtual std::string spfunction_call(const SafePtr< CodeContext > &context, const SafePtr< ImplicitDimensions > &dims) const
Generate a callback for this recurrence relation.
Definition: rr.cc:342
virtual int partindex_direction() const
Definition: rr.h:137
virtual bool is_simple() const =0
Returns true is this recurrence relation is simple enough to optimize away.
virtual std::string generate_label() const =0
Generates the label.
unsigned int nflops() const
Return the number of FLOPs per this recurrence relation.
Definition: rr.h:178
SingletonStack<T,KeyType> helps to implement Singleton-like objects of type T.
Definition: singl_stack.h:44
const value_type & find(const SafePtr< RR > &obj)
Returns the pointer to the unique instance of object obj.
Definition: singl_stack.h:80
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 remove(const SafePtr< RR > &obj)
Searches for obj on the stack and, if found, removes the unique instance.
Definition: singl_stack.h:132
map_type::const_iterator citer_type
const version of iter_type
Definition: singl_stack.h:55
Definition: rr.h:251
Definition: rr.h:250
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
TrivialBFSet<T> defines static member result, which is true if T is a basis function set consisting o...
Definition: bfset.h:892