LIBINT  2.6.0
dgvertex.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_dgvertex_h_
22 #define _libint2_src_bin_libint_dgvertex_h_
23 
24 #include <list>
25 #include <vector>
26 //#include <dg.h>
27 #include <global_macros.h>
28 #include <drtree.h>
29 #include <dgarc.h>
30 #include <iostream>
31 #include <string>
32 #include <smart_ptr.h>
33 #include <memory.h>
34 #include <exception.h>
35 #include <hashable.h>
36 #include <key.h>
37 
38 namespace libint2 {
39 
40  class DirectedGraph;
41 
43  class DGVertex : public Hashable<KeyTypes::InstanceID,ComputeKey> {
44  public:
48  typedef MemoryManager::Size Size;
62  typedef Hashable<KeyType,ComputeKey>::KeyReturnType KeyReturnType;
64  typedef std::list< SafePtr<DGArc> > ArcSetType;
65 
73  DGVertex(ClassID tid);
75  DGVertex(ClassID tid, const std::vector<SafePtr<DGArc> >& parents, const std::vector<SafePtr<DGArc> >& children);
77  DGVertex(const DGVertex& v);
78  virtual ~DGVertex();
79 
81  void make_a_target();
83  bool is_a_target() const { return target_;};
90  virtual void add_exit_arc(const SafePtr<DGArc>&);
94  virtual void del_exit_arcs();
97  void replace_exit_arc(const SafePtr<DGArc>& A, const SafePtr<DGArc>& B);
102  void detach();
103 
104  const ArcSetType& exit_arcs() const { return children_; }
105  const ArcSetType& entry_arcs() const { return parents_; }
107  unsigned int num_entry_arcs() const;
109  ArcSetType::const_iterator first_entry_arc() const { return parents_.begin(); }
111  ArcSetType::const_iterator plast_entry_arc() const { return parents_.end(); }
113  unsigned int num_exit_arcs() const;
115  ArcSetType::const_iterator first_exit_arc() const { return children_.begin(); }
117  ArcSetType::const_iterator plast_exit_arc() const { return children_.end(); }
119  const SafePtr<DGArc>& exit_arc(const SafePtr<DGVertex>& v) const;
120 
122  virtual KeyReturnType key() const =0;
126  virtual bool equiv(const SafePtr<DGVertex>&) const =0;
127 
131  bool precomputed() const;
132 
135  virtual unsigned int size() const =0;
136 
139  virtual const std::string& label() const =0;
143  virtual const std::string& id() const =0;
147  virtual std::string description() const =0;
149  virtual void print(std::ostream& os) const;
150 
152  const DirectedGraph* dg() const { return dg_; }
154  void dg(const DirectedGraph* d) { dg_ = d; }
156  const std::string& graph_label() const;
158  void set_graph_label(const std::string& graph_label);
159 
161  const SafePtr<DRTree>& subtree() const { return subtree_; }
162 
163  //
164  // NOTE : the following functions probably belong to a separate class, such as Entity!
165  //
166 
171  void refer_this_to(const SafePtr<DGVertex>& V);
173  bool refers_to_another() const { return referred_vertex_ != 0; }
175  const std::string& symbol() const;
177  void set_symbol(const std::string& symbol);
179  bool symbol_set() const;
181  void reset_symbol();
183  Address address() const;
185  void set_address(const Address& address);
187  bool address_set() const;
192  void need_to_compute(bool ntc);
196  bool need_to_compute() const;
197 #if CHECK_SAFETY
198  bool declared() const { return precomputed() ? true : declared_; }
199  void declared(bool d) { declared_ = d; }
200 #endif
201 
203  void prepare_to_traverse();
205  unsigned int tag();
207  void schedule() { scheduled_ = true; }
209  bool scheduled() const { return scheduled_; }
211  SafePtr<DGVertex> postcalc() const { return postcalc_; };
213  void set_postcalc(const SafePtr<DGVertex>& postcalc) { postcalc_ = postcalc; };
214 
216  void reset();
218  virtual void unregister() const;
219 
220  public:
221 
229  virtual bool this_precomputed() const =0;
230 
231  private:
233  const DirectedGraph* dg_;
235  std::string graph_label_;
236 
238  const DGVertex* referred_vertex_;
240  std::vector<const DGVertex*> refs_;
242  void register_reference(const DGVertex*);
243 
245  std::string symbol_;
247  Address address_;
248  // Whether this vertex needs to be computed
249  bool need_to_compute_;
250 #if CHECK_SAFETY
251  // has the symbol been declared in the code?
252  bool declared_;
253 #endif
254 
256  ArcSetType parents_;
258  ArcSetType children_;
259 
260  // Whether this is a "target" vertex, i.e. the target of a calculation
261  bool target_;
262  // If set to true -- traversal has started and add_entry... cannot be called
263  bool can_add_arcs_;
264 
271  void del_exit_arc(const SafePtr<DGArc>& c);
273  void add_entry_arc(const SafePtr<DGArc>&);
275  void del_entry_arc(const SafePtr<DGArc>&);
276 
278  // These members used in traversal and scheduling algorithms
280 
282  unsigned int num_tagged_arcs_;
284  SafePtr<DGVertex> postcalc_;
286  bool scheduled_;
287 
288 
290  // Refback to subtree which contains this vertex
292 
293  // note that this is a refback (back reference to the "owning" object) so changing it
294  // does not change class invariant -- hence mutable
295 
297  mutable SafePtr<DRTree> subtree_;
299  friend void DRTree::add_vertex(const SafePtr<DGVertex>& vertex);
300  friend void DRTree::detach_from(const SafePtr<DGVertex>& v);
301 
302  };
303 
304  //
305  // Nonmember predicates
306  //
308  struct UnrolledIntegralSet : public std::unary_function<const SafePtr<DGVertex>&,bool> {
309  bool operator()(const SafePtr<DGVertex>& V);
310  };
312  struct NotUnrolledIntegralSet : public std::unary_function<const SafePtr<DGVertex>&,bool> {
313  bool operator()(const SafePtr<DGVertex>& V);
314  };
316  struct IntegralInTargetIntegralSet : public std::unary_function<const SafePtr<DGVertex>&,bool> {
317  bool operator()(const SafePtr<DGVertex>& V);
318  };
319 
320  inline DGVertexKey key(const DGVertex& v) {
321  return DGVertexKey(v.typeid_,v.instid_);
322  }
323 
324 };
325 
326 #endif
327 
mpz_class InstanceID
some classes need to have distinct instances to have unique InstanceID's, e.g. generalized Singletons
Definition: key.h:81
virtual void print(std::ostream &os) const
print(os) prints vertex info to os
Definition: dgvertex.cc:457
virtual void del_exit_arcs()
del_exit_arcs() removes all exit arcs from this and corresponding children vertices.
Definition: dgvertex.cc:115
const std::string & graph_label() const
returns the label used for this vertex when visualizing graph. can throw GraphLabelNotSet.
Definition: dgvertex.cc:301
void detach()
this function detaches the vertex from other vertices.
Definition: dgvertex.cc:215
KeyTypes::ClassID ClassID
Type identifier.
Definition: dgvertex.h:56
bool symbol_set() const
returns true if the symbol has been set
Definition: dgvertex.cc:376
virtual KeyReturnType key() const =0
computes key
Type/Instance combination serves as a key to quickly compare 2 polymorphic Singletons.
Definition: key.h:33
KeyTypes::InstanceID KeyType
DGVertex provides function key() which computes key of type KeyType and returns it using KeyReturnTyp...
Definition: dgvertex.h:61
ArcSetType::const_iterator plast_entry_arc() const
returns parents::end()
Definition: dgvertex.h:111
MemoryManager::Address Address
The address on the stack during computation is described using this type.
Definition: dgvertex.h:46
const SafePtr< DRTree > & subtree() const
Returns the subtree to which this vertex belongs.
Definition: dgvertex.h:161
virtual const std::string & id() const =0
id() returns a very short label of DGVertex which is (almost) guaranteed to be a symbol (e....
bool precomputed() const
precomputed() returns whether this DGVertex is precomputed.
Definition: dgvertex.cc:447
void replace_exit_arc(const SafePtr< DGArc > &A, const SafePtr< DGArc > &B)
replace_exit_arc() replaces A with B.
Definition: dgvertex.cc:140
unsigned int num_entry_arcs() const
returns the number of parents
Definition: dgvertex.cc:241
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:24
Objects of Hashable<T> class provide hashing function key() which computes keys of type KeyType.
Definition: hashable.h:73
bool is_a_target() const
is_a_target() returns true if this vertex is a target
Definition: dgvertex.h:83
ArcSetType::const_iterator plast_exit_arc() const
returns children::end()
Definition: dgvertex.h:117
const std::string & symbol() const
returns the code symbol. can throw SymbolNotSet
Definition: dgvertex.cc:357
void set_address(const Address &address)
sets the address of this quantity on Libint's stack
Definition: dgvertex.cc:426
unsigned int tag()
tag() tags the vertex and returns the total number of tags this vertex has received
Definition: dgvertex.cc:235
const DirectedGraph * dg() const
Returns pointer to the DirectedGraph to which this DGVertex belongs to.
Definition: dgvertex.h:152
This is a vertex of a Directed Graph (DG)
Definition: dgvertex.h:43
unsigned int ClassID
distinct classes have unique ClassID's
Definition: key.h:79
InstanceID instid_
instid stores the InstanceID of the object.
Definition: dgvertex.h:71
This exception used to indicate that some property is not set.
Definition: exception.h:78
void set_graph_label(const std::string &graph_label)
sets the graph label
Definition: dgvertex.cc:310
void prepare_to_traverse()
prepare_to_traverse() must be called before traversal of the graph starts
Definition: dgvertex.cc:227
DGVertex(ClassID tid)
Sets typeid to tid.
Definition: dgvertex.cc:31
void reset_symbol()
this function void the symbol, i.e. it is no longer set after calling this member
Definition: dgvertex.cc:398
void dg(const DirectedGraph *d)
Sets pointer to the DirectedGraph to which this DGVertex belongs to. Should be used with utmost cauti...
Definition: dgvertex.h:154
NotSet< Address > AddressNotSet
Exception thrown if address is not set.
Definition: dgvertex.h:50
Address address() const
returns the address of this quantity on Libint's stack. can throw AddressNotSet
Definition: dgvertex.cc:404
void set_symbol(const std::string &symbol)
sets the code symbol
Definition: dgvertex.cc:384
virtual void unregister() const
If vertex is a singleton then remove it from the SingletonManager. Must be reimplemented in derived s...
Definition: dgvertex.cc:487
NotSet< std::string > GraphLabelNotSet
Exception thrown if graph label is not set.
Definition: dgvertex.h:52
virtual void add_exit_arc(const SafePtr< DGArc > &)
add_exit_arc(arc) adds arc as an arc connecting to children of this vertex.
Definition: dgvertex.cc:65
bool refers_to_another() const
refers to another vertex?
Definition: dgvertex.h:173
bool scheduled() const
scheduled() returns true if the vertex has been scheduled
Definition: dgvertex.h:209
SafePtr< DGVertex > postcalc() const
Returns pointer to vertex to be computed after this vertex, 0 if this is the last vertex.
Definition: dgvertex.h:211
void make_a_target()
make_a_target() marks this vertex as a target
Definition: dgvertex.cc:59
void set_postcalc(const SafePtr< DGVertex > &postcalc)
Sets postcalc.
Definition: dgvertex.h:213
virtual const std::string & label() const =0
label() returns a unique, short, descriptive label of DGVertex (e.g.
return true if V is an Integral in an unrolled target IntegralSet
Definition: dgvertex.h:316
TypeAndInstance< KeyTypes::ClassID, KeyTypes::InstanceID > DGVertexKey
this composite hashing key works for DGVertex
Definition: key.h:113
void not_need_to_compute()
shortcut to need_to_compute(false)
Definition: dgvertex.h:194
virtual std::string description() const =0
description() returns a full, human-readable description of DGVertex (e.g.
intptr_t Address
Negative Address is used to denote an invalid address – hence signed integer.
Definition: src/bin/libint/memory.h:150
void refer_this_to(const SafePtr< 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:316
return true if V is an unrolled IntegralSet
Definition: dgvertex.h:308
KeyTypes::InstanceID InstanceID
Instance identifier.
Definition: dgvertex.h:58
virtual unsigned int size() const =0
Returns the amount of memory (in floating-point words) to be allocated for the vertex.
virtual bool this_precomputed() const =0
this_precomputed() is used by precomputed() to determine whether this object really is precomputed.
bool address_set() const
returns true if the address has been set
Definition: dgvertex.cc:418
MemoryManager::Size Size
The size of a block the stack during computation is described using this type.
Definition: dgvertex.h:48
void schedule()
schedule() marks the vertex as scheduled, hence its code exists
Definition: dgvertex.h:207
return false if V is an unrolled IntegralSet
Definition: dgvertex.h:312
std::list< SafePtr< DGArc > > ArcSetType
ArcSetType is a container used to maintain entry and exit arcs.
Definition: dgvertex.h:64
NotSet< std::string > SymbolNotSet
Exception thrown if code symbol is not set.
Definition: dgvertex.h:54
virtual bool equiv(const SafePtr< DGVertex > &) const =0
equiv(const DGVertex* aVertex) returns true if this vertex is equivalent to *aVertex.
const SafePtr< DGArc > & exit_arc(const SafePtr< DGVertex > &v) const
return arc connecting this to v, otherwise null pointer
Definition: dgvertex.cc:261
void reset()
Resets the vertex, releasing all arcs.
Definition: dgvertex.cc:274
ArcSetType::const_iterator first_entry_arc() const
returns parents::begin()
Definition: dgvertex.h:109
DirectedGraph is an implementation of a directed graph composed of vertices represented by DGVertex o...
Definition: dg.h:63
bool need_to_compute() const
returns true if this index needs to be computed.
Definition: dgvertex.cc:438
void add_vertex(const SafePtr< DGVertex > &v)
will try to add v to this subtree. Should not be used by the user
Definition: drtree.cc:65
void detach_from(const SafePtr< DGVertex > &v)
recurively detach v from this
Definition: drtree.cc:95
ClassID typeid_
typeid stores the ClassID of the concrete type.
Definition: dgvertex.h:68
ArcSetType::const_iterator first_exit_arc() const
returns children::begin()
Definition: dgvertex.h:115
unsigned int num_exit_arcs() const
returns the number of children
Definition: dgvertex.cc:247