Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Attributes | Private Attributes | List of all members
ComputationGraph Class Reference

#include <qle/ad/computationgraph.hpp>

+ Collaboration diagram for ComputationGraph:

Public Types

enum class  VarDoesntExist { Nan , Create , Throw }
 

Public Member Functions

void clear ()
 
std::size_t size () const
 
std::size_t insert (const std::string &label=std::string())
 
std::size_t insert (const std::vector< std::size_t > &predecessors, const std::size_t opId, const std::string &label=std::string())
 
const std::vector< std::size_t > & predecessors (const std::size_t node) const
 
std::size_t opId (const std::size_t node) const
 
std::size_t maxNodeRequiringArg (const std::size_t node) const
 
std::size_t constant (const double c)
 
const std::map< double, std::size_t > & constants () const
 
bool isConstant (const std::size_t node) const
 
double constantValue (const std::size_t node) const
 
std::size_t variable (const std::string &name, const VarDoesntExist v=VarDoesntExist::Throw)
 
const std::map< std::string, std::size_t > & variables () const
 
void setVariable (const std::string &name, const std::size_t node)
 
void enableLabels (const bool b=true)
 
const std::map< std::size_t, std::set< std::string > > & labels () const
 
void startRedBlock ()
 
void endRedBlock ()
 
std::size_t redBlockId (const std::size_t node) const
 
const std::vector< std::pair< std::size_t, std::size_t > > & redBlockRanges () const
 
const std::set< std::size_t > & redBlockDependencies () const
 

Static Public Attributes

static std::size_t nan = std::numeric_limits<std::size_t>::max()
 

Private Attributes

std::vector< std::vector< std::size_t > > predecessors_
 
std::vector< std::size_t > opId_
 
std::vector< boolisConstant_
 
std::vector< double > constantValue_
 
std::vector< std::size_t > maxNodeRequiringArg_
 
std::vector< std::size_t > redBlockId_
 
std::map< double, std::size_t > constants_
 
std::map< std::string, std::size_t > variables_
 
std::map< std::string, std::size_t > variableVersion_
 
bool enableLabels_ = false
 
std::map< std::size_t, std::set< std::string > > labels_
 
std::size_t currentRedBlockId_ = 0
 
std::size_t nextRedBlockId_ = 0
 
std::vector< std::pair< std::size_t, std::size_t > > redBlockRange_
 
std::set< std::size_t > redBlockDependencies_
 

Detailed Description

Definition at line 35 of file computationgraph.hpp.

Member Enumeration Documentation

◆ VarDoesntExist

enum class VarDoesntExist
strong

Member Function Documentation

◆ clear()

void clear ( )

Definition at line 32 of file computationgraph.cpp.

32 {
33 predecessors_.clear();
34 opId_.clear();
36 redBlockId_.clear();
37 isConstant_.clear();
38 constantValue_.clear();
39 constants_.clear();
40 variables_.clear();
41 variableVersion_.clear();
42 labels_.clear();
43}
std::vector< bool > isConstant_
std::map< std::size_t, std::set< std::string > > labels_
std::map< std::string, std::size_t > variableVersion_
std::map< double, std::size_t > constants_
std::vector< double > constantValue_
std::vector< std::size_t > redBlockId_
std::map< std::string, std::size_t > variables_
std::vector< std::vector< std::size_t > > predecessors_
std::vector< std::size_t > opId_
std::vector< std::size_t > maxNodeRequiringArg_

◆ size()

std::size_t size ( ) const

Definition at line 45 of file computationgraph.cpp.

45{ return predecessors_.size(); }
+ Here is the caller graph for this function:

◆ insert() [1/2]

std::size_t insert ( const std::string &  label = std::string())

Definition at line 47 of file computationgraph.cpp.

47 {
48 std::size_t node = predecessors_.size();
49 predecessors_.push_back(std::vector<std::size_t>());
50 opId_.push_back(0);
51 maxNodeRequiringArg_.push_back(0);
53 isConstant_.push_back(false);
54 constantValue_.push_back(0.0);
55 if (enableLabels_ && !label.empty())
56 labels_[node].insert(label);
57 return node;
58}
+ Here is the caller graph for this function:

◆ insert() [2/2]

std::size_t insert ( const std::vector< std::size_t > &  predecessors,
const std::size_t  opId,
const std::string &  label = std::string() 
)

Definition at line 60 of file computationgraph.cpp.

61 {
62 std::size_t node = predecessors_.size();
63 predecessors_.push_back(predecessors);
64 opId_.push_back(opId);
65 for (auto const& p : predecessors) {
66 maxNodeRequiringArg_[p] = node;
67 }
68 maxNodeRequiringArg_.push_back(0);
70 if (currentRedBlockId_ != 0) {
71 for (auto const& p : predecessors) {
73 redBlockDependencies_.insert(p);
74 }
75 }
76 }
77 isConstant_.push_back(false);
78 constantValue_.push_back(0.0);
79 if (enableLabels_ && !label.empty())
80 labels_[node].insert(label);
81 return node;
82}
std::set< std::size_t > redBlockDependencies_
const std::vector< std::size_t > & predecessors(const std::size_t node) const
std::size_t redBlockId(const std::size_t node) const
std::size_t opId(const std::size_t node) const
+ Here is the call graph for this function:

◆ predecessors()

const std::vector< std::size_t > & predecessors ( const std::size_t  node) const

Definition at line 84 of file computationgraph.cpp.

84 {
85 return predecessors_[node];
86}
+ Here is the caller graph for this function:

◆ opId()

std::size_t opId ( const std::size_t  node) const

Definition at line 88 of file computationgraph.cpp.

88{ return opId_[node]; }
+ Here is the caller graph for this function:

◆ maxNodeRequiringArg()

std::size_t maxNodeRequiringArg ( const std::size_t  node) const

Definition at line 90 of file computationgraph.cpp.

90{ return maxNodeRequiringArg_[node]; }

◆ constant()

std::size_t constant ( const double  c)

Definition at line 92 of file computationgraph.cpp.

92 {
93 auto c = constants_.find(x);
94 if (c != constants_.end())
95 return c->second;
96 else {
97 std::size_t node = predecessors_.size();
98 constants_.insert(std::make_pair(x, node));
99 predecessors_.push_back(std::vector<std::size_t>());
100 opId_.push_back(0);
101 maxNodeRequiringArg_.push_back(0);
103 isConstant_.push_back(true);
104 constantValue_.push_back(x);
105 if (enableLabels_)
106 labels_[node].insert(std::to_string(x));
107 return node;
108 }
109}
+ Here is the caller graph for this function:

◆ constants()

const std::map< double, std::size_t > & constants ( ) const

Definition at line 111 of file computationgraph.cpp.

111{ return constants_; }

◆ isConstant()

bool isConstant ( const std::size_t  node) const

Definition at line 183 of file computationgraph.cpp.

183{ return isConstant_[node]; }
+ Here is the caller graph for this function:

◆ constantValue()

double constantValue ( const std::size_t  node) const

Definition at line 185 of file computationgraph.cpp.

185{ return constantValue_[node]; }
+ Here is the caller graph for this function:

◆ variable()

std::size_t variable ( const std::string &  name,
const VarDoesntExist  v = VarDoesntExist::Throw 
)

Definition at line 113 of file computationgraph.cpp.

113 {
114 auto c = variables_.find(name);
115 if (c != variables_.end())
116 return c->second;
117 else if (v == VarDoesntExist::Create) {
118 std::size_t node = predecessors_.size();
119 variables_.insert(std::make_pair(name, node));
120 variableVersion_[name] = 0;
121 if (enableLabels_)
122 labels_[node].insert(name + "(v" + std::to_string(++variableVersion_[name]) + ")");
123 predecessors_.push_back(std::vector<std::size_t>());
124 opId_.push_back(0);
125 maxNodeRequiringArg_.push_back(0);
127 isConstant_.push_back(false);
128 constantValue_.push_back(0.0);
129 return node;
130 } else if (v == VarDoesntExist::Nan) {
131 return nan;
132 } else if (v == VarDoesntExist::Throw) {
133 QL_FAIL("ComputationGraph::variable(" << name << ") not found.");
134 } else {
135 QL_FAIL("ComputationGraph::variable(): internal error, VarDoesntExist enum '" << static_cast<int>(v)
136 << "' not covered.");
137 }
138}
+ Here is the caller graph for this function:

◆ variables()

const std::map< std::string, std::size_t > & variables ( ) const

Definition at line 140 of file computationgraph.cpp.

140{ return variables_; }

◆ setVariable()

void setVariable ( const std::string &  name,
const std::size_t  node 
)

Definition at line 142 of file computationgraph.cpp.

142 {
143 auto v = variables_.find(name);
144 if (v != variables_.end()) {
145 if (v->second != node) {
146 if (enableLabels_)
147 labels_[node].insert(name + "(v" + std::to_string(++variableVersion_[name]) + ")");
148 v->second = node;
149 }
150 } else {
151 variableVersion_[name] = 0;
152 if (enableLabels_)
153 labels_[node].insert(name + "(v" + std::to_string(++variableVersion_[name]) + ")");
154 variables_[name] = node;
155 }
156}

◆ enableLabels()

void enableLabels ( const bool  b = true)

Definition at line 158 of file computationgraph.cpp.

158{ enableLabels_ = b; }

◆ labels()

const std::map< std::size_t, std::set< std::string > > & labels ( ) const

Definition at line 160 of file computationgraph.cpp.

160{ return labels_; }

◆ startRedBlock()

void startRedBlock ( )

Definition at line 162 of file computationgraph.cpp.

162 {
164 if (!redBlockRange_.empty())
165 redBlockRange_.back().second = size();
166 redBlockRange_.push_back(std::make_pair(size(), nan));
167}
std::vector< std::pair< std::size_t, std::size_t > > redBlockRange_
+ Here is the call graph for this function:

◆ endRedBlock()

void endRedBlock ( )

Definition at line 169 of file computationgraph.cpp.

169 {
170 QL_REQUIRE(currentRedBlockId_ > 0, "ComputationGraph::endRedBlock(): not in an active red block.");
172 redBlockRange_.back().second = size();
173}
+ Here is the call graph for this function:

◆ redBlockId()

std::size_t redBlockId ( const std::size_t  node) const

Definition at line 181 of file computationgraph.cpp.

181{ return redBlockId_[node]; }
+ Here is the caller graph for this function:

◆ redBlockRanges()

const std::vector< std::pair< std::size_t, std::size_t > > & redBlockRanges ( ) const

Definition at line 175 of file computationgraph.cpp.

175 {
176 return redBlockRange_;
177}

◆ redBlockDependencies()

const std::set< std::size_t > & redBlockDependencies ( ) const

Definition at line 179 of file computationgraph.cpp.

179{ return redBlockDependencies_; }

Member Data Documentation

◆ nan

std::size_t nan = std::numeric_limits<std::size_t>::max()
static

Definition at line 38 of file computationgraph.hpp.

◆ predecessors_

std::vector<std::vector<std::size_t> > predecessors_
private

Definition at line 70 of file computationgraph.hpp.

◆ opId_

std::vector<std::size_t> opId_
private

Definition at line 71 of file computationgraph.hpp.

◆ isConstant_

std::vector<bool> isConstant_
private

Definition at line 72 of file computationgraph.hpp.

◆ constantValue_

std::vector<double> constantValue_
private

Definition at line 73 of file computationgraph.hpp.

◆ maxNodeRequiringArg_

std::vector<std::size_t> maxNodeRequiringArg_
private

Definition at line 74 of file computationgraph.hpp.

◆ redBlockId_

std::vector<std::size_t> redBlockId_
private

Definition at line 75 of file computationgraph.hpp.

◆ constants_

std::map<double, std::size_t> constants_
private

Definition at line 77 of file computationgraph.hpp.

◆ variables_

std::map<std::string, std::size_t> variables_
private

Definition at line 79 of file computationgraph.hpp.

◆ variableVersion_

std::map<std::string, std::size_t> variableVersion_
private

Definition at line 80 of file computationgraph.hpp.

◆ enableLabels_

bool enableLabels_ = false
private

Definition at line 82 of file computationgraph.hpp.

◆ labels_

std::map<std::size_t, std::set<std::string> > labels_
private

Definition at line 83 of file computationgraph.hpp.

◆ currentRedBlockId_

std::size_t currentRedBlockId_ = 0
private

Definition at line 85 of file computationgraph.hpp.

◆ nextRedBlockId_

std::size_t nextRedBlockId_ = 0
private

Definition at line 86 of file computationgraph.hpp.

◆ redBlockRange_

std::vector<std::pair<std::size_t, std::size_t> > redBlockRange_
private

Definition at line 87 of file computationgraph.hpp.

◆ redBlockDependencies_

std::set<std::size_t> redBlockDependencies_
private

Definition at line 88 of file computationgraph.hpp.