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

#include <ored/scripting/computationgraphbuilder.hpp>

+ Collaboration diagram for ComputationGraphBuilder:

Classes

struct  PayLogEntry
 

Public Member Functions

 ComputationGraphBuilder (ComputationGraph &g, const std::vector< std::string > &opLabels, const ASTNodePtr root, const QuantLib::ext::shared_ptr< Context > context, const QuantLib::ext::shared_ptr< ModelCG > model=nullptr)
 
void run (const bool generatePayLog, const bool includePastCashflows=false, const std::string &script="", bool interactive=false)
 
const std::set< std::size_t > & keepNodes () const
 
const std::vector< PayLogEntry > & payLogEntries () const
 

Private Attributes

ComputationGraphg_
 
const std::vector< std::string > opLabels_
 
const ASTNodePtr root_
 
const QuantLib::ext::shared_ptr< Contextcontext_
 
const QuantLib::ext::shared_ptr< ModelCGmodel_
 
std::set< std::size_t > keepNodes_
 
std::vector< PayLogEntrypayLogEntries_
 

Detailed Description

Definition at line 36 of file computationgraphbuilder.hpp.

Constructor & Destructor Documentation

◆ ComputationGraphBuilder()

ComputationGraphBuilder ( ComputationGraph g,
const std::vector< std::string > &  opLabels,
const ASTNodePtr  root,
const QuantLib::ext::shared_ptr< Context context,
const QuantLib::ext::shared_ptr< ModelCG model = nullptr 
)

Definition at line 49 of file computationgraphbuilder.hpp.

51 : g_(g), opLabels_(opLabels), root_(root), context_(context), model_(model) {}
const QuantLib::ext::shared_ptr< Context > context_
const std::vector< std::string > opLabels_
const QuantLib::ext::shared_ptr< ModelCG > model_

Member Function Documentation

◆ run()

void run ( const bool  generatePayLog,
const bool  includePastCashflows = false,
const std::string &  script = "",
bool  interactive = false 
)

Definition at line 1485 of file computationgraphbuilder.cpp.

1486 {
1487
1488 keepNodes_.clear();
1489 payLogEntries_.clear();
1490
1491 ASTNode* loc;
1492 ASTRunner runner(g_, opLabels_, model_, generatePayLog, generatePayLog && includePastCashflows, script, interactive,
1494
1495 randomvariable_output_pattern pattern;
1496 if (model_ == nullptr || model_->type() == ModelCG::Type::MC) {
1497 pattern = randomvariable_output_pattern(randomvariable_output_pattern::pattern::expectation);
1498 } else if (model_->type() == ModelCG::Type::FD) {
1499 pattern = randomvariable_output_pattern(randomvariable_output_pattern::pattern::left_middle_right);
1500 } else {
1501 QL_FAIL("model type not handled when setting output pattern for random variables");
1502 }
1503
1504 DLOG("run computation graph builder, context is:");
1505 DLOGGERSTREAM(pattern << *context_);
1506
1507 if (interactive) {
1508 std::cerr << pattern << "\nInitial Context: \n" << (*context_) << std::endl;
1509 }
1510
1511 boost::timer::cpu_timer timer;
1512 try {
1513 reset(root_);
1514 root_->accept(runner);
1515 timer.stop();
1516 QL_REQUIRE(runner.value.size() == 1, "ComputationGraphBuilder::run(): value stack has wrong size ("
1517 << runner.value.size() << "), should be 1");
1518 QL_REQUIRE(runner.filter.size() == 1, "ComputationGraphBuilder::run(): filter stack has wrong size ("
1519 << runner.filter.size() << "), should be 1");
1520 QL_REQUIRE(runner.value_node.size() == 1, "ComputationGraphBuilder::run(): value_node stack has wrong size ("
1521 << runner.value_node.size() << "), should be 1");
1522 QL_REQUIRE(runner.filter_node.size() == 1, "ComputationGraphBuilder::run(): filter_node stack has wrong size ("
1523 << runner.filter_node.size() << "), should be 1");
1524 DLOG("computation graph builder successfully finished, context after run is:");
1525
1526 if (interactive) {
1527 std::cerr << "\ncomputation graph builder finished without errors. Context after run:" << std::endl;
1528 }
1529
1530 } catch (const std::exception& e) {
1531 std::ostringstream errorMessage;
1532 errorMessage << "Error during computation graph building: " << e.what() << " at "
1533 << (loc ? to_string(loc->locationInfo) : "(last visited ast node not known)") << ": "
1534 << printCodeContext(script, loc, true);
1535
1536 std::ostringstream strippedErrorMsg;
1537 strippedErrorMsg << "Error during computation graph building: " << e.what() << " at "
1538 << (loc ? to_string(loc->locationInfo) : "(last visited ast node not known)");
1539
1540 DLOGGERSTREAM(strippedErrorMsg.str());
1542 DLOGGERSTREAM("Context when hitting the error:\n");
1543 DLOGGERSTREAM(pattern << *context_);
1544 DLOGGERSTREAM("SSA-Form when hitting the error:\n");
1545 DLOGGERSTREAM(pattern << ssaForm(g_, opLabels_));
1546
1547 if (interactive) {
1548 std::cerr << strippedErrorMsg.str() << "\n";
1549 std::cerr << printCodeContext(script, loc);
1550 std::cerr << "Context when hitting the error:" << std::endl;
1551 std::cerr << (*context_) << std::endl;
1552 std::cerr << "SSA-Form when hitting the error:";
1553 std::cerr << pattern << ssaForm(g_, opLabels_);
1554 std::cin.get();
1555 }
1556
1557 QL_FAIL(errorMessage.str());
1558 }
1559
1560 DLOGGERSTREAM(pattern << *context_);
1561 DLOG("computation graph builder running time: " << timer.elapsed().wall / 1E3 << " mus");
1562
1563 if (interactive) {
1564 std::cerr << pattern << "<<<<\n" << *context_ << ">>>>\n" << std::endl;
1565 std::cerr << pattern << "<<<<\n" << ssaForm(g_, opLabels_) << ">>>>\n" << std::endl;
1566 std::cin.get();
1567 }
1568}
std::string script
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
#define DLOGGERSTREAM(text)
Definition: log.hpp:632
std::string ssaForm(const ComputationGraph &g, const std::vector< std::string > &opCodeLabels, const std::vector< T > &values)
std::string printCodeContext(std::string script, const ASTNode *loc, bool compact)
void reset(const ASTNodePtr root)
Definition: astresetter.cpp:44
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ keepNodes()

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

Definition at line 54 of file computationgraphbuilder.hpp.

54{ return keepNodes_; }
+ Here is the caller graph for this function:

◆ payLogEntries()

const std::vector< PayLogEntry > & payLogEntries ( ) const

Definition at line 55 of file computationgraphbuilder.hpp.

55{ return payLogEntries_; }
+ Here is the caller graph for this function:

Member Data Documentation

◆ g_

ComputationGraph& g_
private

Definition at line 58 of file computationgraphbuilder.hpp.

◆ opLabels_

const std::vector<std::string> opLabels_
private

Definition at line 60 of file computationgraphbuilder.hpp.

◆ root_

const ASTNodePtr root_
private

Definition at line 61 of file computationgraphbuilder.hpp.

◆ context_

const QuantLib::ext::shared_ptr<Context> context_
private

Definition at line 62 of file computationgraphbuilder.hpp.

◆ model_

const QuantLib::ext::shared_ptr<ModelCG> model_
private

Definition at line 63 of file computationgraphbuilder.hpp.

◆ keepNodes_

std::set<std::size_t> keepNodes_
private

Definition at line 65 of file computationgraphbuilder.hpp.

◆ payLogEntries_

std::vector<PayLogEntry> payLogEntries_
private

Definition at line 66 of file computationgraphbuilder.hpp.