|
| 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 | + |
| 25 | +#include <ql/patterns/singleton.hpp> |
| 26 | + |
| 27 | +#include <set> |
| 28 | + |
| 29 | +namespace QuantExt { |
| 30 | + |
| 31 | +class ComputeContext; |
| 32 | +class ComputeFramework; |
| 33 | + |
| 34 | +class ComputeEnvironment : public QuantLib::Singleton<ComputeEnvironment> { |
| 35 | +public: |
| 36 | + ComputeEnvironment(); |
| 37 | + ~ComputeEnvironment(); |
| 38 | + std::set<std::string> getAvailableDevices() const; |
| 39 | + bool hasContext() const; |
| 40 | + void selectContext(const std::string& deviceName); |
| 41 | + ComputeContext& context(); |
| 42 | + void reset(); |
| 43 | + |
| 44 | +private: |
| 45 | + void releaseFrameworks(); |
| 46 | + |
| 47 | + std::vector<ComputeFramework*> frameworks_; |
| 48 | + ComputeContext* currentContext_; |
| 49 | +}; |
| 50 | + |
| 51 | +class ComputeFramework { |
| 52 | +public: |
| 53 | + virtual ~ComputeFramework() {} |
| 54 | + virtual std::set<std::string> getAvailableDevices() const = 0; |
| 55 | + virtual ComputeContext* getContext(const std::string& deviceName) = 0; |
| 56 | +}; |
| 57 | + |
| 58 | +class ComputeContext { |
| 59 | +public: |
| 60 | + struct DebugInfo { |
| 61 | + unsigned long numberOfOperations = 0; |
| 62 | + unsigned long nanoSecondsDataCopy = 0; |
| 63 | + unsigned long nanoSecondsProgramBuild = 0; |
| 64 | + unsigned long nanoSecondsCalculation = 0; |
| 65 | + }; |
| 66 | + |
| 67 | + virtual ~ComputeContext() {} |
| 68 | + virtual void init() = 0; |
| 69 | + |
| 70 | + virtual std::pair<std::size_t, bool> initiateCalculation(const std::size_t n, const std::size_t id = 0, |
| 71 | + const std::size_t version = 0, |
| 72 | + const bool debug = false) = 0; |
| 73 | + |
| 74 | + virtual std::size_t createInputVariable(float v) = 0; |
| 75 | + virtual std::size_t createInputVariable(float* v) = 0; |
| 76 | + virtual std::vector<std::vector<std::size_t>> createInputVariates(const std::size_t dim, const std::size_t steps, |
| 77 | + const std::uint32_t seed) = 0; |
| 78 | + |
| 79 | + virtual std::size_t applyOperation(const std::size_t randomVariableOpCode, |
| 80 | + const std::vector<std::size_t>& args) = 0; |
| 81 | + virtual void freeVariable(const std::size_t id) = 0; |
| 82 | + virtual void declareOutputVariable(const std::size_t id) = 0; |
| 83 | + |
| 84 | + virtual void finalizeCalculation(std::vector<float*>& output) = 0; |
| 85 | + |
| 86 | + // debug info |
| 87 | + |
| 88 | + virtual const DebugInfo& debugInfo() const = 0; |
| 89 | + |
| 90 | + // convenience methods |
| 91 | + |
| 92 | + void finalizeCalculation(std::vector<std::vector<float>>& output); |
| 93 | +}; |
| 94 | + |
| 95 | +} // namespace QuantExt |
0 commit comments