Skip to content

Commit a9be713

Browse files
pcaspersjenkins
authored andcommitted
Merge remote-tracking branch 'origin/master' into QPR-11721
1 parent aa875b5 commit a9be713

15 files changed

Lines changed: 15372 additions & 2 deletions

Docker/Dockerfile-Boost

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ MAINTAINER Quaternion Risk Management
55
LABEL Description="Provide a base environment for building in C++ with Boost"
66

77
RUN apt-get update && apt-get upgrade -y \
8-
&& apt-get install -f -y build-essential wget libbz2-dev autoconf libtool dos2unix cmake zlib1g-dev \
8+
&& apt-get install -f -y build-essential wget libbz2-dev autoconf libtool dos2unix cmake zlib1g-dev opencl-headers ocl-icd-opencl-dev \
99
&& apt-get clean \
1010
&& rm -rf /var/lib/apt/lists/*
1111

Docker/Dockerfile-ORE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ RUN cd /ore \
2727
&& find -regex ".*\.\(sh\|in\|ac\|am\)" -exec dos2unix {} ';' \
2828
&& cp Docker/CMakeLists_ORE.txt CMakeLists.txt \
2929
&& mkdir -p build.ore && cd build.ore \
30-
&& cmake .. -DORE_BUILD_DOC=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-mavx2" -DORE_ENABLE_PARALLEL_UNIT_TEST_RUNNER=ON
30+
&& cmake .. -DORE_BUILD_DOC=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-mavx2" -DORE_ENABLE_PARALLEL_UNIT_TEST_RUNNER=ON -DORE_USE_ZLIB=ON
3131

3232
WORKDIR /ore/build.ore
3333
RUN --mount=type=cache,target=/ccache/ make -j ${num_cores} \

Docker/Dockerfile-ORE-App

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ COPY OREAnalytics/test /ore/OREAnalytics/test
1313
RUN mkdir /ore/App
1414

1515
COPY --from=env_ore /usr/local/bin /ore/App
16+
COPY --from=env_ore /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu
1617

1718
RUN true
1819

Docs/UserGuide/computeenvironment.tex

Lines changed: 538 additions & 0 deletions
Large diffs are not rendered by default.

QuantExt/qle/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,13 @@ instruments/tenorbasisswap.cpp
140140
instruments/varianceswap.cpp
141141
math/blockmatrixinverse.cpp
142142
math/bucketeddistribution.cpp
143+
math/computeenvironment.cpp
143144
math/deltagammavar.cpp
144145
math/differentialevolution_mt.cpp
145146
math/discretedistribution.cpp
146147
math/fillemptymatrix.cpp
147148
math/matrixfunctions.cpp
149+
math/openclenvironment.cpp
148150
math/randomvariable.cpp
149151
math/randomvariable_io.cpp
150152
methods/brownianbridgepathinterpolator.cpp
@@ -579,6 +581,7 @@ instruments/varianceswap.hpp
579581
interpolators/optioninterpolator2d.hpp
580582
math/blockmatrixinverse.hpp
581583
math/bucketeddistribution.hpp
584+
math/computeenvironment.hpp
582585
math/constantinterpolation.hpp
583586
math/covariancesalvage.hpp
584587
math/deltagammavar.hpp
@@ -591,10 +594,12 @@ math/logquadraticinterpolation.hpp
591594
math/matrixfunctions.hpp
592595
math/method_mt.hpp
593596
math/nadarayawatson.hpp
597+
math/openclenvironment.hpp
594598
math/problem_mt.hpp
595599
math/quadraticinterpolation.hpp
596600
math/randomvariable.hpp
597601
math/randomvariable_io.hpp
602+
math/randomvariable_opcodes.hpp
598603
math/stabilisedglls.hpp
599604
math/trace.hpp
600605
methods/brownianbridgepathinterpolator.hpp
@@ -917,6 +922,15 @@ writeAll("qle" "quantext.hpp" "auto_link.hpp" "${QuantExt_HDR}")
917922
add_library(${QLE_LIB_NAME} ${QuantExt_SRC})
918923
target_link_libraries(${QLE_LIB_NAME} ${QL_LIB_NAME} ${Boost_LIBRARIES})
919924

925+
if(ORE_ENABLE_OPENCL)
926+
if(APPLE)
927+
target_link_libraries(${QLE_LIB_NAME} "-framework OpenCL")
928+
else()
929+
find_package(OpenCL REQUIRED)
930+
target_link_libraries(${QLE_LIB_NAME} OpenCL::OpenCL)
931+
endif()
932+
endif()
933+
920934
install(DIRECTORY . DESTINATION include/qle
921935
FILES_MATCHING PATTERN "*.hpp" PATTERN "*.h")
922936

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
#include <qle/math/computeenvironment.hpp>
20+
#include <qle/math/openclenvironment.hpp>
21+
22+
#include <boost/algorithm/string/join.hpp>
23+
24+
#include <ql/errors.hpp>
25+
26+
#include <iostream>
27+
28+
#ifdef ORE_ENABLE_OPENCL
29+
#ifdef __APPLE__
30+
#include <OpenCL/cl.h>
31+
#else
32+
#include <CL/cl.h>
33+
#endif
34+
#endif
35+
36+
namespace QuantExt {
37+
38+
ComputeEnvironment::ComputeEnvironment() { reset(); }
39+
40+
ComputeEnvironment::~ComputeEnvironment() { releaseFrameworks(); }
41+
42+
void ComputeEnvironment::releaseFrameworks() {
43+
for (auto& f : frameworks_)
44+
delete f;
45+
frameworks_.clear();
46+
}
47+
48+
void ComputeEnvironment::reset() {
49+
currentContext_ = nullptr;
50+
releaseFrameworks();
51+
frameworks_.push_back(new OpenClFramework());
52+
}
53+
54+
std::set<std::string> ComputeEnvironment::getAvailableDevices() const {
55+
std::set<std::string> result;
56+
for (auto const& f : frameworks_) {
57+
auto tmp = f->getAvailableDevices();
58+
result.insert(tmp.begin(), tmp.end());
59+
}
60+
return result;
61+
}
62+
63+
bool ComputeEnvironment::hasContext() const { return currentContext_ != nullptr; }
64+
65+
void ComputeEnvironment::selectContext(const std::string& deviceName) {
66+
for (auto& f : frameworks_) {
67+
if (auto tmp = f->getAvailableDevices(); tmp.find(deviceName) != tmp.end()) {
68+
currentContext_ = f->getContext(deviceName);
69+
currentContext_->init();
70+
return;
71+
}
72+
}
73+
QL_FAIL("ComputeEnvironment::selectContext(): device '"
74+
<< deviceName << "' not found. Available devices: " << boost::join(getAvailableDevices(), ","));
75+
}
76+
77+
ComputeContext& ComputeEnvironment::context() { return *currentContext_; }
78+
79+
void ComputeContext::finalizeCalculation(std::vector<std::vector<float>>& output) {
80+
std::vector<float*> outputPtr(output.size());
81+
std::transform(output.begin(), output.end(), outputPtr.begin(),
82+
[](std::vector<float>& v) -> float* { return &v[0]; });
83+
finalizeCalculation(outputPtr);
84+
}
85+
86+
}; // namespace QuantExt
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)