LIBINT 2.9.0
dgvertex.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_dgvertex_h_
22#define _libint2_src_bin_libint_dgvertex_h_
23
24#include <list>
25#include <vector>
26// #include <dg.h>
27#include <dgarc.h>
28#include <drtree.h>
29#include <exception.h>
30#include <global_macros.h>
31#include <hashable.h>
32#include <key.h>
33#include <memory.h>
34#include <smart_ptr.h>
35
36#include <iostream>
37#include <string>
38
39namespace libint2 {
40
41class DirectedGraph;
42
44class DGVertex : public Hashable<KeyTypes::InstanceID, ComputeKey> {
45 public:
50 typedef MemoryManager::Size Size;
64 typedef Hashable<KeyType, ComputeKey>::KeyReturnType KeyReturnType;
66 typedef std::list<std::shared_ptr<DGArc> > ArcSetType;
67
77 DGVertex(ClassID tid);
79 DGVertex(ClassID tid, const std::vector<std::shared_ptr<DGArc> >& parents,
80 const std::vector<std::shared_ptr<DGArc> >& children);
82 DGVertex(const DGVertex& v);
83 virtual ~DGVertex();
84
86 void make_a_target();
88 bool is_a_target() const { return target_; };
95 virtual void add_exit_arc(const std::shared_ptr<DGArc>&);
99 virtual void del_exit_arcs();
103 void replace_exit_arc(const std::shared_ptr<DGArc>& A,
104 const std::shared_ptr<DGArc>& B);
109 void detach();
110
111 const ArcSetType& exit_arcs() const { return children_; }
112 const ArcSetType& entry_arcs() const { return parents_; }
114 unsigned int num_entry_arcs() const;
116 ArcSetType::const_iterator first_entry_arc() const {
117 return parents_.begin();
118 }
120 ArcSetType::const_iterator plast_entry_arc() const { return parents_.end(); }
122 unsigned int num_exit_arcs() const;
124 ArcSetType::const_iterator first_exit_arc() const {
125 return children_.begin();
126 }
128 ArcSetType::const_iterator plast_exit_arc() const { return children_.end(); }
130 const std::shared_ptr<DGArc>& exit_arc(
131 const std::shared_ptr<DGVertex>& v) const;
132
134 virtual KeyReturnType key() const = 0;
138 virtual bool equiv(const std::shared_ptr<DGVertex>&) const = 0;
139
143 bool precomputed() const;
144
148 virtual unsigned int size() const = 0;
149
153 virtual const std::string& label() const = 0;
157 virtual const std::string& id() const = 0;
162 virtual std::string description() const = 0;
164 virtual void print(std::ostream& os) const;
165
167 const DirectedGraph* dg() const { return dg_; }
170 void dg(const DirectedGraph* d) { dg_ = d; }
173 const std::string& graph_label() const;
175 void set_graph_label(const std::string& graph_label);
176
178 const std::shared_ptr<DRTree>& subtree() const { return subtree_; }
179
180 //
181 // NOTE : the following functions probably belong to a separate class, such as
182 // Entity!
183 //
184
189 void refer_this_to(const std::shared_ptr<DGVertex>& V);
191 bool refers_to_another() const { return referred_vertex_ != 0; }
193 const std::string& symbol() const;
195 void set_symbol(const std::string& symbol);
197 bool symbol_set() const;
200 void reset_symbol();
203 Address address() const;
205 void set_address(const Address& address);
207 bool address_set() const;
212 void need_to_compute(bool ntc);
216 bool need_to_compute() const;
217#if CHECK_SAFETY
218 bool declared() const { return precomputed() ? true : declared_; }
219 void declared(bool d) { declared_ = d; }
220#endif
221
223 void prepare_to_traverse();
226 unsigned int tag();
228 void schedule() { scheduled_ = true; }
230 bool scheduled() const { return scheduled_; }
233 std::shared_ptr<DGVertex> postcalc() const { return postcalc_; };
235 void set_postcalc(const std::shared_ptr<DGVertex>& postcalc) {
236 postcalc_ = postcalc;
237 };
238
240 void reset();
243 virtual void unregister() const;
244
245 public:
253 virtual bool this_precomputed() const = 0;
254
255 private:
257 const DirectedGraph* dg_;
259 std::string graph_label_;
260
262 const DGVertex* referred_vertex_;
264 std::vector<const DGVertex*> refs_;
266 void register_reference(const DGVertex*);
267
269 std::string symbol_;
271 Address address_;
272 // Whether this vertex needs to be computed
273 bool need_to_compute_;
274#if CHECK_SAFETY
275 // has the symbol been declared in the code?
276 bool declared_;
277#endif
278
280 ArcSetType parents_;
283 ArcSetType children_;
284
285 // Whether this is a "target" vertex, i.e. the target of a calculation
286 bool target_;
287 // If set to true -- traversal has started and add_entry... cannot be called
288 bool can_add_arcs_;
289
296 void del_exit_arc(const std::shared_ptr<DGArc>& c);
298 void add_entry_arc(const std::shared_ptr<DGArc>&);
300 void del_entry_arc(const std::shared_ptr<DGArc>&);
301
303 // These members used in traversal and scheduling algorithms
305
308 unsigned int num_tagged_arcs_;
311 std::shared_ptr<DGVertex> postcalc_;
313 bool scheduled_;
314
316 // Refback to subtree which contains this vertex
318
319 // note that this is a refback (back reference to the "owning" object) so
320 // changing it does not change class invariant -- hence mutable
321
324 mutable std::shared_ptr<DRTree> subtree_;
326 friend void DRTree::add_vertex(const std::shared_ptr<DGVertex>& vertex);
327 friend void DRTree::detach_from(const std::shared_ptr<DGVertex>& v);
328};
329
330//
331// Nonmember predicates
332//
335 bool operator()(const std::shared_ptr<DGVertex>& V);
336};
339 bool operator()(const std::shared_ptr<DGVertex>& V);
340};
343 bool operator()(const std::shared_ptr<DGVertex>& V);
344};
345
346inline DGVertexKey key(const DGVertex& v) {
347 return DGVertexKey(v.typeid_, v.instid_);
348}
349
350}; // namespace libint2
351
352#endif
This is a vertex of a Directed Graph (DG)
Definition dgvertex.h:44
void prepare_to_traverse()
prepare_to_traverse() must be called before traversal of the graph starts
Definition dgvertex.cc:244
void reset()
Resets the vertex, releasing all arcs.
Definition dgvertex.cc:277
const std::string & symbol() const
returns the code symbol. can throw SymbolNotSet
Definition dgvertex.cc:354
void set_symbol(const std::string &symbol)
sets the code symbol
Definition dgvertex.cc:380
void detach()
this function detaches the vertex from other vertices.
Definition dgvertex.cc:233
bool address_set() const
returns true if the address has been set
Definition dgvertex.cc:407
MemoryManager::Size Size
The size of a block the stack during computation is described using this type.
Definition dgvertex.h:50
virtual bool equiv(const std::shared_ptr< DGVertex > &) const =0
equiv(const DGVertex* aVertex) returns true if this vertex is equivalent to *aVertex.
bool is_a_target() const
is_a_target() returns true if this vertex is a target
Definition dgvertex.h:88
ArcSetType::const_iterator plast_entry_arc() const
returns parents::end()
Definition dgvertex.h:120
void reset_symbol()
this function void the symbol, i.e.
Definition dgvertex.cc:393
bool scheduled() const
scheduled() returns true if the vertex has been scheduled
Definition dgvertex.h:230
virtual void unregister() const
If vertex is a singleton then remove it from the SingletonManager.
Definition dgvertex.cc:460
ClassID typeid_
typeid stores the ClassID of the concrete type.
Definition dgvertex.h:71
NotSet< std::string > SymbolNotSet
Exception thrown if code symbol is not set.
Definition dgvertex.h:56
virtual void del_exit_arcs()
del_exit_arcs() removes all exit arcs from this and corresponding children vertices.
Definition dgvertex.cc:129
bool symbol_set() const
returns true if the symbol has been set
Definition dgvertex.cc:373
const DirectedGraph * dg() const
Returns pointer to the DirectedGraph to which this DGVertex belongs to.
Definition dgvertex.h:167
virtual const std::string & label() const =0
label() returns a unique, short, descriptive label of DGVertex (e.g.
virtual KeyReturnType key() const =0
computes key
unsigned int num_exit_arcs() const
returns the number of children
Definition dgvertex.cc:254
virtual unsigned int size() const =0
Returns the amount of memory (in floating-point words) to be allocated for the vertex.
KeyTypes::InstanceID KeyType
DGVertex provides function key() which computes key of type KeyType and returns it using KeyReturnTyp...
Definition dgvertex.h:63
ArcSetType::const_iterator plast_exit_arc() const
returns children::end()
Definition dgvertex.h:128
NotSet< Address > AddressNotSet
Exception thrown if address is not set.
Definition dgvertex.h:52
const std::shared_ptr< DRTree > & subtree() const
Returns the subtree to which this vertex belongs.
Definition dgvertex.h:178
virtual const std::string & id() const =0
id() returns a very short label of DGVertex which is (almost) guaranteed to be a symbol (e....
void replace_exit_arc(const std::shared_ptr< DGArc > &A, const std::shared_ptr< DGArc > &B)
replace_exit_arc() replaces A with B.
Definition dgvertex.cc:155
DGVertex(ClassID tid)
Sets typeid to tid.
Definition dgvertex.cc:32
bool refers_to_another() const
refers to another vertex?
Definition dgvertex.h:191
void dg(const DirectedGraph *d)
Sets pointer to the DirectedGraph to which this DGVertex belongs to.
Definition dgvertex.h:170
void set_postcalc(const std::shared_ptr< DGVertex > &postcalc)
Sets postcalc.
Definition dgvertex.h:235
const std::string & graph_label() const
returns the label used for this vertex when visualizing graph.
Definition dgvertex.cc:302
void not_need_to_compute()
shortcut to need_to_compute(false)
Definition dgvertex.h:214
KeyTypes::ClassID ClassID
Type identifier.
Definition dgvertex.h:58
virtual bool this_precomputed() const =0
this_precomputed() is used by precomputed() to determine whether this object really is precomputed.
void set_graph_label(const std::string &graph_label)
sets the graph label
Definition dgvertex.cc:309
bool need_to_compute() const
returns true if this index needs to be computed.
Definition dgvertex.cc:418
std::shared_ptr< DGVertex > postcalc() const
Returns pointer to vertex to be computed after this vertex, 0 if this is the last vertex.
Definition dgvertex.h:233
ArcSetType::const_iterator first_exit_arc() const
returns children::begin()
Definition dgvertex.h:124
ArcSetType::const_iterator first_entry_arc() const
returns parents::begin()
Definition dgvertex.h:116
InstanceID instid_
instid stores the InstanceID of the object.
Definition dgvertex.h:75
virtual void print(std::ostream &os) const
print(os) prints vertex info to os
Definition dgvertex.cc:433
bool precomputed() const
precomputed() returns whether this DGVertex is precomputed.
Definition dgvertex.cc:425
DGVertex(ClassID tid, const std::vector< std::shared_ptr< DGArc > > &parents, const std::vector< std::shared_ptr< DGArc > > &children)
Sets typeid to tid.
unsigned int tag()
tag() tags the vertex and returns the total number of tags this vertex has received
Definition dgvertex.cc:250
std::list< std::shared_ptr< DGArc > > ArcSetType
ArcSetType is a container used to maintain entry and exit arcs.
Definition dgvertex.h:66
NotSet< std::string > GraphLabelNotSet
Exception thrown if graph label is not set.
Definition dgvertex.h:54
virtual std::string description() const =0
description() returns a full, human-readable description of DGVertex (e.g.
Address address() const
returns the address of this quantity on Libint's stack.
Definition dgvertex.cc:395
void refer_this_to(const std::shared_ptr< DGVertex > &V)
refer_this_to(V) makes this vertex act like a reference to V so that calls to symbol() and address() ...
Definition dgvertex.cc:313
const std::shared_ptr< DGArc > & exit_arc(const std::shared_ptr< DGVertex > &v) const
return arc connecting this to v, otherwise null pointer
Definition dgvertex.cc:264
unsigned int num_entry_arcs() const
returns the number of parents
Definition dgvertex.cc:252
virtual void add_exit_arc(const std::shared_ptr< DGArc > &)
add_exit_arc(arc) adds arc as an arc connecting to children of this vertex.
Definition dgvertex.cc:82
void schedule()
schedule() marks the vertex as scheduled, hence its code exists
Definition dgvertex.h:228
void set_address(const Address &address)
sets the address of this quantity on Libint's stack
Definition dgvertex.cc:414
KeyTypes::InstanceID InstanceID
Instance identifier.
Definition dgvertex.h:60
MemoryManager::Address Address
The address on the stack during computation is described using this type.
Definition dgvertex.h:47
void make_a_target()
make_a_target() marks this vertex as a target
Definition dgvertex.cc:80
void detach_from(const std::shared_ptr< DGVertex > &v)
recurively detach v from this
Definition drtree.cc:77
void add_vertex(const std::shared_ptr< DGVertex > &v)
will try to add v to this subtree. Should not be used by the user
Definition drtree.cc:50
DirectedGraph is an implementation of a directed graph composed of vertices represented by DGVertex o...
Definition dg.h:65
Objects of Hashable<T> class provide hashing function key() which computes keys of type KeyType.
Definition hashable.h:79
intptr_t Address
Negative Address is used to denote an invalid address – hence signed integer.
Definition src/bin/libint/memory.h:156
This exception used to indicate that some property is not set.
Definition exception.h:72
Type/Instance combination serves as a key to quickly compare 2 polymorphic Singletons.
Definition key.h:35
Defaults definitions for various parameters assumed by Libint.
Definition algebra.cc:24
TypeAndInstance< KeyTypes::ClassID, KeyTypes::InstanceID > DGVertexKey
this composite hashing key works for DGVertex
Definition key.h:116
return true if V is an Integral in an unrolled target IntegralSet
Definition dgvertex.h:342
mpz_class InstanceID
some classes need to have distinct instances to have unique InstanceID's, e.g.
Definition key.h:86
unsigned int ClassID
distinct classes have unique ClassID's
Definition key.h:83
return false if V is an unrolled IntegralSet
Definition dgvertex.h:338
return true if V is an unrolled IntegralSet
Definition dgvertex.h:334