LIBINT 2.9.0
rr.h
1/*
2 * Copyright (C) 2004-2024 Edward F. Valeev
3 *
4 * This file is part of Libint compiler.
5 *
6 * Libint compiler 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 compiler 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 compiler. 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 <bfset.h>
25#include <code.h>
26#include <default_params.h>
27#include <exception.h>
28#include <global_macros.h>
29#include <polyconstr.h>
30#include <singl_stack.h>
31#include <smart_ptr.h>
32#include <util_types.h>
33
34#include <iostream>
35#include <sstream>
36#include <stdexcept>
37#include <string>
38#include <vector>
39
40namespace libint2 {
41
42class DGVertex;
43class CodeContext;
44class ImplicitDimensions;
45class DirectedGraph;
46template <typename V>
47class AlgebraicOperator;
48
52template <typename RR>
53class RRStackBase : public SingletonStack<RR, std::string> {
54 private:
55 static std::shared_ptr<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 std::shared_ptr<RRStackBase<RR> >& Instance() {
70 if (!rrstack_) {
71 std::shared_ptr<RRStackBase<RR> > tmpstack(new RRStackBase<RR>);
72 rrstack_ = tmpstack;
73 }
74 return rrstack_;
75 }
76
77 virtual ~RRStackBase() {}
78
80 void add(const std::shared_ptr<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) { parent_type::remove(rr); }
88};
89
90template <typename RR>
91std::shared_ptr<RRStackBase<RR> > RRStackBase<RR>::rrstack_;
92
97 : public std::enable_shared_from_this<RecurrenceRelation> {
98 public:
100
102 virtual ~RecurrenceRelation();
103
109 virtual unsigned int num_children() const = 0;
111 virtual std::shared_ptr<DGVertex> rr_child(unsigned int i) const = 0;
113 virtual std::shared_ptr<DGVertex> rr_target() const = 0;
114
119 const std::shared_ptr<ExprType>& rr_expr() const { return expr_; }
120
127 virtual bool is_simple() const = 0;
131 virtual bool invariant_type() const;
137 virtual int partindex_direction() const { return 0; }
143 virtual BraketDirection braket_direction() const {
144 return BraketDirection::None;
145 }
149 size_t size_of_children() const;
150
156 const std::string& label() const;
157
161 virtual std::string description() const;
162
164 virtual void generate_code(const std::shared_ptr<CodeContext>& context,
165 const std::shared_ptr<ImplicitDimensions>& dims,
166 const std::string& funcname, std::ostream& decl,
167 std::ostream& def);
168
171 virtual void generate_generic_code(
172 const std::shared_ptr<CodeContext>& context,
173 const std::shared_ptr<ImplicitDimensions>& dims,
174 const std::string& funcname, std::ostream& decl, std::ostream& def);
175
177 virtual std::string spfunction_call(
178 const std::shared_ptr<CodeContext>& context,
179 const std::shared_ptr<ImplicitDimensions>& dims) const;
180
182 unsigned int nflops() const { return nflops_; }
183
188
189 protected:
190 unsigned int nflops_;
191 mutable std::string label_;
192 std::shared_ptr<ExprType> expr_;
194 void add_expr(const std::shared_ptr<ExprType>& a, int minus = 1);
196 virtual std::string generate_label() const = 0;
198 template <class RR>
200 // only register RRs with for shell sets
202 std::shared_ptr<RRStackBase<RecurrenceRelation> > rrstack =
204 std::shared_ptr<RR> this_ptr = std::const_pointer_cast<RR, const RR>(
205 std::static_pointer_cast<const RR, const RecurrenceRelation>(
206 std::enable_shared_from_this<
207 RecurrenceRelation>::shared_from_this()));
208 rrstack->find(this_ptr);
209#if DEBUG || DEBUG_CONSTRUCTION
210 std::cout << "register_with_rrstack: registered " << this_ptr->label()
211 << std::endl;
212#endif
213 return true;
214 }
215
216 private:
220 std::shared_ptr<DirectedGraph> generate_graph_(
221 const std::shared_ptr<DirectedGraph>& dg);
224 void assign_symbols_(std::shared_ptr<CodeSymbols>& S);
227 virtual std::shared_ptr<ImplicitDimensions> adapt_dims_(
228 const std::shared_ptr<ImplicitDimensions>& dims) const;
229
231 virtual bool has_generic(
232 const std::shared_ptr<CompilationParameters>& cparams) const;
234 virtual std::string generic_header() const;
237 virtual std::string generic_instance(
238 const std::shared_ptr<CodeContext>& context,
239 const std::shared_ptr<CodeSymbols>& args) const;
240};
241
242namespace algebra {
244std::shared_ptr<RecurrenceRelation::ExprType> operator+(
245 const std::shared_ptr<DGVertex>& A, const std::shared_ptr<DGVertex>& B);
246std::shared_ptr<RecurrenceRelation::ExprType> operator-(
247 const std::shared_ptr<DGVertex>& A, const std::shared_ptr<DGVertex>& B);
248std::shared_ptr<RecurrenceRelation::ExprType> operator*(
249 const std::shared_ptr<DGVertex>& A, const std::shared_ptr<DGVertex>& B);
250std::shared_ptr<RecurrenceRelation::ExprType> operator/(
251 const std::shared_ptr<DGVertex>& A, const std::shared_ptr<DGVertex>& B);
252const std::shared_ptr<RecurrenceRelation::ExprType>& operator+=(
253 std::shared_ptr<RecurrenceRelation::ExprType>& A,
254 const std::shared_ptr<DGVertex>& B);
255const std::shared_ptr<RecurrenceRelation::ExprType>& operator-=(
256 std::shared_ptr<RecurrenceRelation::ExprType>& A,
257 const std::shared_ptr<DGVertex>& B);
258const std::shared_ptr<RecurrenceRelation::ExprType>& operator*=(
259 std::shared_ptr<RecurrenceRelation::ExprType>& A,
260 const std::shared_ptr<DGVertex>& B);
261const std::shared_ptr<RecurrenceRelation::ExprType>& operator/=(
262 std::shared_ptr<RecurrenceRelation::ExprType>& A,
263 const std::shared_ptr<DGVertex>& B);
264
265class Entity;
266template <class T>
268template <class T>
270// seems to confound Intel compiler on Linux?
271// std::shared_ptr<RecurrenceRelation::ExprType> operator*(const
272// std::shared_ptr<Entity>& A,
273// const
274// std::shared_ptr<DGVertex>& B);
275template <typename T>
276std::shared_ptr<RecurrenceRelation::ExprType> operator*(
277 const std::shared_ptr<RTimeEntity<T> >& A,
278 const std::shared_ptr<DGVertex>& B);
279template <typename T>
280std::shared_ptr<RecurrenceRelation::ExprType> operator*(
281 const std::shared_ptr<CTimeEntity<T> >& A,
282 const std::shared_ptr<DGVertex>& B);
283}; // namespace algebra
284
285// Instantiate the RRStack
287
288}; // namespace libint2
289
290// #include <vrr_11_twoprep_11.h>
291// #include <hrr.h>
292// #include <shell_to_ints.h>
293// #include <iter.h>
294
295#endif
AlgebraicOperator is an algebraic operator that acts on objects of type T.
Definition algebra.h:47
RRStack implements a stack of RecurrenceRelation's which can only hold one instance of a given RR.
Definition rr.h:53
void remove(const data_type &rr)
removes rr from the stack
Definition rr.h:87
static std::shared_ptr< RRStackBase< RR > > & Instance()
Obtain the unique Instance of RRStack.
Definition rr.h:69
void add(const std::shared_ptr< RRStackBase< RR > > &rrs)
adds content of rrs to this stack
Definition rr.h:80
RecurrenceRelation describes all recurrence relations.
Definition rr.h:97
virtual std::shared_ptr< DGVertex > rr_target() const =0
Returns the target.
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:186
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:332
size_t size_of_children() const
Definition rr.cc:377
AlgebraicOperator< DGVertex > ExprType
Numerical expression of a recurrence relation is always expressed as an AlgebraicOperator<DGVertex>
Definition rr.h:117
virtual unsigned int num_children() const =0
num_children() returns the actual number of children.
const std::shared_ptr< ExprType > & rr_expr() const
Returns the expression.
Definition rr.h:119
virtual std::shared_ptr< DGVertex > rr_child(unsigned int i) const =0
Returns i-th child.
virtual BraketDirection braket_direction() const
Definition rr.h:143
bool register_with_rrstack()
Registers with the stack.
Definition rr.h:199
virtual std::string spfunction_call(const std::shared_ptr< CodeContext > &context, const std::shared_ptr< ImplicitDimensions > &dims) const
Generate a callback for this recurrence relation.
Definition rr.cc:339
const std::string & label() const
label() returns a unique, short, descriptive label of this RR (e.g.
Definition rr.cc:295
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.
void add_expr(const std::shared_ptr< ExprType > &a, int minus=1)
Adds a (or -a, if minus = -1) to expr_.
Definition rr.cc:305
virtual void generate_code(const std::shared_ptr< CodeContext > &context, const std::shared_ptr< ImplicitDimensions > &dims, const std::string &funcname, std::ostream &decl, std::ostream &def)
Generate declaration and definition for the recurrence relation.
Definition rr.cc:51
virtual void generate_generic_code(const std::shared_ptr< CodeContext > &context, const std::shared_ptr< 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:200
unsigned int nflops() const
Return the number of FLOPs per this recurrence relation.
Definition rr.h:182
SingletonStack<T,KeyType> helps to implement Singleton-like objects of type T.
Definition singl_stack.h:45
const value_type & find(const std::shared_ptr< RR > &obj)
Returns the pointer to the unique instance of object obj.
Definition singl_stack.h:81
void remove(const std::shared_ptr< RR > &obj)
Searches for obj on the stack and, if found, removes the unique instance.
Definition singl_stack.h:135
KeyTypes::InstanceID InstanceID
Specifies type for the instance index variables.
Definition singl_stack.h:50
map_type::iterator iter_type
use iter_type objects to iterate over the stack
Definition singl_stack.h:54
map_type::const_iterator citer_type
const version of iter_type
Definition singl_stack.h:56
Defaults definitions for various parameters assumed by Libint.
Definition algebra.cc:24
std::shared_ptr< CTimeEntity< typename ProductType< T, U >::result > > operator*(const std::shared_ptr< CTimeEntity< T > > &A, const std::shared_ptr< CTimeEntity< U > > &B)
Creates product A*B.
Definition entity.h:302
TrivialBFSet<T> defines static member result, which is true if T is a basis function set consisting o...
Definition bfset.h:906