LIBINT  2.1.0-stable
dgvertex.h
1 /*
2  * This file is a part of Libint.
3  * Copyright (C) 2004-2014 Edward F. Valeev
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see http://www.gnu.org/licenses/.
17  *
18  */
19 
20 #ifndef _libint2_src_bin_libint_dgvertex_h_
21 #define _libint2_src_bin_libint_dgvertex_h_
22 
23 #include <list>
24 #include <vector>
25 //#include <dg.h>
26 #include <global_macros.h>
27 #include <drtree.h>
28 #include <dgarc.h>
29 #include <iostream>
30 #include <string>
31 #include <smart_ptr.h>
32 #include <memory.h>
33 #include <exception.h>
34 #include <hashable.h>
35 #include <key.h>
36 
37 namespace libint2 {
38 
39  class DirectedGraph;
40 
42  class DGVertex : public Hashable<KeyTypes::InstanceID,ComputeKey> {
43  public:
47  typedef MemoryManager::Size Size;
61  typedef Hashable<KeyType,ComputeKey>::KeyReturnType KeyReturnType;
63  typedef std::list< SafePtr<DGArc> > ArcSetType;
64 
67  ClassID typeid_;
70  InstanceID instid_;
72  DGVertex(ClassID tid);
74  DGVertex(ClassID tid, const std::vector<SafePtr<DGArc> >& parents, const std::vector<SafePtr<DGArc> >& children);
76  DGVertex(const DGVertex& v);
77  virtual ~DGVertex();
78 
80  void make_a_target();
82  bool is_a_target() const { return target_;};
89  virtual void add_exit_arc(const SafePtr<DGArc>&);
93  virtual void del_exit_arcs();
96  void replace_exit_arc(const SafePtr<DGArc>& A, const SafePtr<DGArc>& B);
101  void detach();
102 
103  const ArcSetType& exit_arcs() const { return children_; }
104  const ArcSetType& entry_arcs() const { return parents_; }
106  unsigned int num_entry_arcs() const;
108  ArcSetType::const_iterator first_entry_arc() const { return parents_.begin(); }
110  ArcSetType::const_iterator plast_entry_arc() const { return parents_.end(); }
112  unsigned int num_exit_arcs() const;
114  ArcSetType::const_iterator first_exit_arc() const { return children_.begin(); }
116  ArcSetType::const_iterator plast_exit_arc() const { return children_.end(); }
118  const SafePtr<DGArc>& exit_arc(const SafePtr<DGVertex>& v) const;
119 
121  virtual KeyReturnType key() const =0;
125  virtual bool equiv(const SafePtr<DGVertex>&) const =0;
126 
130  bool precomputed() const;
131 
134  virtual unsigned int size() const =0;
135 
138  virtual const std::string& label() const =0;
142  virtual const std::string& id() const =0;
146  virtual std::string description() const =0;
148  virtual void print(std::ostream& os) const;
149 
151  const DirectedGraph* dg() const { return dg_; }
153  void dg(const DirectedGraph* d) { dg_ = d; }
155  const std::string& graph_label() const;
157  void set_graph_label(const std::string& graph_label);
158 
160  const SafePtr<DRTree>& subtree() const { return subtree_; }
161 
162  //
163  // NOTE : the following functions probably belong to a separate class, such as Entity!
164  //
165 
170  void refer_this_to(const SafePtr<DGVertex>& V);
172  bool refers_to_another() const { return referred_vertex_ != 0; }
174  const std::string& symbol() const;
176  void set_symbol(const std::string& symbol);
178  bool symbol_set() const;
180  void reset_symbol();
182  Address address() const;
184  void set_address(const Address& address);
186  bool address_set() const;
191  void need_to_compute(bool ntc);
195  bool need_to_compute() const;
196 #if CHECK_SAFETY
197  bool declared() const { return precomputed() ? true : declared_; }
198  void declared(bool d) { declared_ = d; }
199 #endif
200 
202  void prepare_to_traverse();
204  unsigned int tag();
206  void schedule() { scheduled_ = true; }
208  bool scheduled() const { return scheduled_; }
210  SafePtr<DGVertex> postcalc() const { return postcalc_; };
212  void set_postcalc(const SafePtr<DGVertex>& postcalc) { postcalc_ = postcalc; };
213 
215  void reset();
217  virtual void unregister() const;
218 
219  public:
220 
228  virtual bool this_precomputed() const =0;
229 
230  private:
232  const DirectedGraph* dg_;
234  std::string graph_label_;
235 
237  const DGVertex* referred_vertex_;
239  std::vector<const DGVertex*> refs_;
241  void register_reference(const DGVertex*);
242 
244  std::string symbol_;
246  Address address_;
247  // Whether this vertex needs to be computed
248  bool need_to_compute_;
249 #if CHECK_SAFETY
250  // has the symbol been declared in the code?
251  bool declared_;
252 #endif
253 
255  ArcSetType parents_;
257  ArcSetType children_;
258 
259  // Whether this is a "target" vertex, i.e. the target of a calculation
260  bool target_;
261  // If set to true -- traversal has started and add_entry... cannot be called
262  bool can_add_arcs_;
263 
270  void del_exit_arc(const SafePtr<DGArc>& c);
272  void add_entry_arc(const SafePtr<DGArc>&);
274  void del_entry_arc(const SafePtr<DGArc>&);
275 
277  // These members used in traversal and scheduling algorithms
279 
281  unsigned int num_tagged_arcs_;
283  SafePtr<DGVertex> postcalc_;
285  bool scheduled_;
286 
287 
289  // Refback to subtree which contains this vertex
291 
292  // note that this is a refback (back reference to the "owning" object) so changing it
293  // does not change class invariant -- hence mutable
294 
296  mutable SafePtr<DRTree> subtree_;
298  friend void DRTree::add_vertex(const SafePtr<DGVertex>& vertex);
299  friend void DRTree::detach_from(const SafePtr<DGVertex>& v);
300 
301  };
302 
303  //
304  // Nonmember predicates
305  //
307  struct UnrolledIntegralSet : public std::unary_function<const SafePtr<DGVertex>&,bool> {
308  bool operator()(const SafePtr<DGVertex>& V);
309  };
311  struct NotUnrolledIntegralSet : public std::unary_function<const SafePtr<DGVertex>&,bool> {
312  bool operator()(const SafePtr<DGVertex>& V);
313  };
315  struct IntegralInTargetIntegralSet : public std::unary_function<const SafePtr<DGVertex>&,bool> {
316  bool operator()(const SafePtr<DGVertex>& V);
317  };
318 
319  inline DGVertexKey key(const DGVertex& v) {
320  return DGVertexKey(v.typeid_,v.instid_);
321  }
322 
323 };
324 
325 #endif
326 
mpz_class InstanceID
some classes need to have distinct instances to have unique InstanceID&#39;s, e.g. generalized Singletons...
Definition: key.h:80
SafePtr< DGVertex > postcalc() const
Returns pointer to vertex to be computed after this vertex, 0 if this is the last vertex...
Definition: dgvertex.h:210
bool scheduled() const
scheduled() returns true if the vertex has been scheduled
Definition: dgvertex.h:208
const SafePtr< DRTree > & subtree() const
Returns the subtree to which this vertex belongs.
Definition: dgvertex.h:160
virtual void del_exit_arcs()
del_exit_arcs() removes all exit arcs from this and corresponding children vertices.
Definition: dgvertex.cc:114
void detach()
this function detaches the vertex from other vertices.
Definition: dgvertex.cc:214
ArcSetType::const_iterator first_entry_arc() const
returns parents::begin()
Definition: dgvertex.h:108
KeyTypes::ClassID ClassID
Type identifier.
Definition: dgvertex.h:55
Type/Instance combination serves as a key to quickly compare 2 polymorphic Singletons.
Definition: key.h:32
KeyTypes::InstanceID KeyType
DGVertex provides function key() which computes key of type KeyType and returns it using KeyReturnTyp...
Definition: dgvertex.h:60
const SafePtr< DGArc > & exit_arc(const SafePtr< DGVertex > &v) const
return arc connecting this to v, otherwise null pointer
Definition: dgvertex.cc:260
bool refers_to_another() const
refers to another vertex?
Definition: dgvertex.h:172
Address address() const
returns the address of this quantity on Libint&#39;s stack. can throw AddressNotSet
Definition: dgvertex.cc:402
MemoryManager::Address Address
The address on the stack during computation is described using this type.
Definition: dgvertex.h:45
void replace_exit_arc(const SafePtr< DGArc > &A, const SafePtr< DGArc > &B)
replace_exit_arc() replaces A with B.
Definition: dgvertex.cc:139
Defaults definitions for various parameters assumed by Libint.
Definition: algebra.cc:23
virtual const std::string & label() const =0
label() returns a unique, short, descriptive label of DGVertex (e.g.
Objects of Hashable<T> class provide hashing function key() which computes keys of type KeyType...
Definition: hashable.h:72
ArcSetType::const_iterator plast_exit_arc() const
returns children::end()
Definition: dgvertex.h:116
virtual bool this_precomputed() const =0
this_precomputed() is used by precomputed() to determine whether this object really is precomputed...
void set_address(const Address &address)
sets the address of this quantity on Libint&#39;s stack
Definition: dgvertex.cc:424
ArcSetType::const_iterator plast_entry_arc() const
returns parents::end()
Definition: dgvertex.h:110
virtual bool equiv(const SafePtr< DGVertex > &) const =0
equiv(const DGVertex* aVertex) returns true if this vertex is equivalent to *aVertex.
unsigned int tag()
tag() tags the vertex and returns the total number of tags this vertex has received ...
Definition: dgvertex.cc:234
const std::string & graph_label() const
returns the label used for this vertex when visualizing graph. can throw GraphLabelNotSet.
Definition: dgvertex.cc:300
unsigned int num_entry_arcs() const
returns the number of parents
Definition: dgvertex.cc:240
This is a vertex of a Directed Graph (DG)
Definition: dgvertex.h:42
unsigned int ClassID
distinct classes have unique ClassID&#39;s
Definition: key.h:78
InstanceID instid_
instid stores the InstanceID of the object.
Definition: dgvertex.h:70
This exception used to indicate that some property is not set.
Definition: exception.h:77
void set_graph_label(const std::string &graph_label)
sets the graph label
Definition: dgvertex.cc:309
void prepare_to_traverse()
prepare_to_traverse() must be called before traversal of the graph starts
Definition: dgvertex.cc:226
DGVertex(ClassID tid)
Sets typeid to tid.
Definition: dgvertex.cc:30
void reset_symbol()
this function void the symbol, i.e. it is no longer set after calling this member ...
Definition: dgvertex.cc:396
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:153
NotSet< Address > AddressNotSet
Exception thrown if address is not set.
Definition: dgvertex.h:49
void set_symbol(const std::string &symbol)
sets the code symbol
Definition: dgvertex.cc:383
virtual void print(std::ostream &os) const
print(os) prints vertex info to os
Definition: dgvertex.cc:455
NotSet< std::string > GraphLabelNotSet
Exception thrown if graph label is not set.
Definition: dgvertex.h:51
const std::string & symbol() const
returns the code symbol. can throw SymbolNotSet
Definition: dgvertex.cc:356
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:64
void make_a_target()
make_a_target() marks this vertex as a target
Definition: dgvertex.cc:58
void set_postcalc(const SafePtr< DGVertex > &postcalc)
Sets postcalc.
Definition: dgvertex.h:212
bool precomputed() const
precomputed() returns whether this DGVertex is precomputed.
Definition: dgvertex.cc:445
return true if V is an Integral in an unrolled target IntegralSet
Definition: dgvertex.h:315
TypeAndInstance< KeyTypes::ClassID, KeyTypes::InstanceID > DGVertexKey
this composite hashing key works for DGVertex
Definition: key.h:112
virtual std::string description() const =0
description() returns a full, human-readable description of DGVertex (e.g.
void not_need_to_compute()
shortcut to need_to_compute(false)
Definition: dgvertex.h:193
virtual void unregister() const
If vertex is a singleton then remove it from the SingletonManager. Must be reimplemented in derived s...
Definition: dgvertex.cc:485
intptr_t Address
Negative Address is used to denote an invalid address – hence signed integer.
Definition: src/bin/libint/memory.h:149
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:315
return true if V is an unrolled IntegralSet
Definition: dgvertex.h:307
unsigned int num_exit_arcs() const
returns the number of children
Definition: dgvertex.cc:246
KeyTypes::InstanceID InstanceID
Instance identifier.
Definition: dgvertex.h:57
const DirectedGraph * dg() const
Returns pointer to the DirectedGraph to which this DGVertex belongs to.
Definition: dgvertex.h:151
MemoryManager::Size Size
The size of a block the stack during computation is described using this type.
Definition: dgvertex.h:47
void schedule()
schedule() marks the vertex as scheduled, hence its code exists
Definition: dgvertex.h:206
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 need_to_compute() const
returns true if this index needs to be computed.
Definition: dgvertex.cc:436
return false if V is an unrolled IntegralSet
Definition: dgvertex.h:311
std::list< SafePtr< DGArc > > ArcSetType
ArcSetType is a container used to maintain entry and exit arcs.
Definition: dgvertex.h:63
virtual KeyReturnType key() const =0
computes key
bool symbol_set() const
returns true if the symbol has been set
Definition: dgvertex.cc:375
NotSet< std::string > SymbolNotSet
Exception thrown if code symbol is not set.
Definition: dgvertex.h:53
bool address_set() const
returns true if the address has been set
Definition: dgvertex.cc:416
void reset()
Resets the vertex, releasing all arcs.
Definition: dgvertex.cc:273
DirectedGraph is an implementation of a directed graph composed of vertices represented by DGVertex o...
Definition: dg.h:62
bool is_a_target() const
is_a_target() returns true if this vertex is a target
Definition: dgvertex.h:82
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:64
ArcSetType::const_iterator first_exit_arc() const
returns children::begin()
Definition: dgvertex.h:114
void detach_from(const SafePtr< DGVertex > &v)
recurively detach v from this
Definition: drtree.cc:94
ClassID typeid_
typeid stores the ClassID of the concrete type.
Definition: dgvertex.h:67
virtual unsigned int size() const =0
Returns the amount of memory (in floating-point words) to be allocated for the vertex.