Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
computeenvironment.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2023 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file qle/math/computeenvironment.hpp
20 \brief interface to compute envs
21*/
22
23#pragma once
24
26
27#include <ql/patterns/singleton.hpp>
28
29#include <boost/thread/shared_mutex.hpp>
30
31#include <cstdint>
32#include <set>
33
34namespace QuantExt {
35
36class ComputeContext;
37class ComputeFramework;
38
39class ComputeEnvironment : public QuantLib::Singleton<ComputeEnvironment> {
40public:
43 std::set<std::string> getAvailableDevices() const;
44 bool hasContext() const;
45 void selectContext(const std::string& deviceName);
47 void reset();
48
49private:
50 void releaseFrameworks();
51
52 std::vector<ComputeFramework*> frameworks_;
55};
56
58public:
59 virtual ~ComputeFramework() {}
60 virtual std::set<std::string> getAvailableDevices() const = 0;
61 virtual ComputeContext* getContext(const std::string& deviceName) = 0;
62};
63
65public:
66 struct Settings {
69 rngSeed(42), regressionOrder(4) {}
70 bool debug;
73 std::size_t rngSeed;
74 std::size_t regressionOrder;
75 };
76
77 struct DebugInfo {
78 unsigned long numberOfOperations = 0;
79 unsigned long nanoSecondsDataCopy = 0;
80 unsigned long nanoSecondsProgramBuild = 0;
81 unsigned long nanoSecondsCalculation = 0;
82 };
83
84 virtual ~ComputeContext() {}
85 virtual void init() = 0;
86
87 virtual std::pair<std::size_t, bool> initiateCalculation(const std::size_t n, const std::size_t id = 0,
88 const std::size_t version = 0,
89 const Settings settings = {}) = 0;
90 virtual void disposeCalculation(const std::size_t id) = 0;
91
92 virtual std::size_t createInputVariable(double v) = 0;
93 virtual std::size_t createInputVariable(double* v) = 0;
94 virtual std::vector<std::vector<std::size_t>> createInputVariates(const std::size_t dim,
95 const std::size_t steps) = 0;
96
97 virtual std::size_t applyOperation(const std::size_t randomVariableOpCode,
98 const std::vector<std::size_t>& args) = 0;
99 virtual void freeVariable(const std::size_t id) = 0;
100 virtual void declareOutputVariable(const std::size_t id) = 0;
101
102 virtual void finalizeCalculation(std::vector<double*>& output) = 0;
103
104 // get device info
105
106 virtual std::vector<std::pair<std::string, std::string>> deviceInfo() const { return {}; }
107 virtual bool supportsDoublePrecision() const { return false; }
108
109 // debug info
110
111 virtual const DebugInfo& debugInfo() const = 0;
112
113 // convenience methods
114
115 void finalizeCalculation(std::vector<std::vector<double>>& output);
116};
117
118template <class T> T* createComputeFrameworkCreator() { return new T; }
119
120class ComputeFrameworkRegistry : public QuantLib::Singleton<ComputeFrameworkRegistry, std::true_type> {
121public:
123 void add(const std::string& name, std::function<ComputeFramework*(void)> creator,
124 const bool allowOverwrite = false);
125 const std::vector<std::function<ComputeFramework*(void)>>& getAll() const;
126
127private:
128 mutable boost::shared_mutex mutex_;
129 std::vector<std::string> names_;
130 std::vector<std::function<ComputeFramework*(void)>> creators_;
131};
132
133} // namespace QuantExt
virtual bool supportsDoublePrecision() const
virtual void disposeCalculation(const std::size_t id)=0
virtual void declareOutputVariable(const std::size_t id)=0
virtual std::vector< std::vector< std::size_t > > createInputVariates(const std::size_t dim, const std::size_t steps)=0
virtual void finalizeCalculation(std::vector< double * > &output)=0
virtual std::size_t createInputVariable(double v)=0
virtual std::size_t applyOperation(const std::size_t randomVariableOpCode, const std::vector< std::size_t > &args)=0
virtual std::pair< std::size_t, bool > initiateCalculation(const std::size_t n, const std::size_t id=0, const std::size_t version=0, const Settings settings={})=0
virtual void init()=0
virtual const DebugInfo & debugInfo() const =0
virtual std::size_t createInputVariable(double *v)=0
virtual std::vector< std::pair< std::string, std::string > > deviceInfo() const
virtual void freeVariable(const std::size_t id)=0
std::set< std::string > getAvailableDevices() const
std::vector< ComputeFramework * > frameworks_
void selectContext(const std::string &deviceName)
virtual ComputeContext * getContext(const std::string &deviceName)=0
virtual std::set< std::string > getAvailableDevices() const =0
const std::vector< std::function< ComputeFramework *(void)> > & getAll() const
void add(const std::string &name, std::function< ComputeFramework *(void)> creator, const bool allowOverwrite=false)
std::vector< std::function< ComputeFramework *(void)> > creators_
base class for multi path generators
T * createComputeFrameworkCreator()
std::vector< Size > steps