Skip to content

Commit 40e45ab

Browse files
committed
feat: setting up merge logic for future threading
1 parent 859e9dd commit 40e45ab

6 files changed

Lines changed: 48 additions & 9 deletions

File tree

include/lfmc/engine/engine.hpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
11
#pragma once
22

3-
// TODO move headers into folders
3+
#include "lfmc/pipeline/pipeline.hpp"
4+
#include "lfmc/pipeline/pipeline_builder.hpp"
5+
6+
namespace lfmc {
7+
8+
// class Engine {
9+
// private:
10+
// Pipeline<StochasticProcess auto, NumericalScheme<StochasticProcess auto>> pipeline_;
11+
//
12+
// public:
13+
// Engine(Pipeline<StochasticProcess auto, NumericalScheme<StochasticProcess auto>> pipeline)
14+
// : pipeline_(std::move(pipeline)) {}
15+
//
16+
// std::expected<double, std::string> run() {
17+
// return pipeline_.run();
18+
// }
19+
// };
20+
21+
} // namespace lfmc

include/lfmc/estimator/control_variate_estimator.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ControlVariateEstimator : public Estimator {
2828
std::expected<void, std::string> add_payoffs(const std::vector<Payoffs>& payoffs) override;
2929
bool converged() const override;
3030
std::expected<double, std::string> result() const override;
31-
// void merge(Estimator const& other) override;
31+
std::expected<void, std::string> merge(Estimator const& other) override;
3232
};
3333

3434
} // namespace lfmc

include/lfmc/estimator/estimator.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Estimator {
1313
virtual std::expected<void, std::string> add_payoffs(const std::vector<Payoffs>& payoffs) = 0;
1414
virtual bool converged() const = 0;
1515
virtual std::expected<double, std::string> result() const = 0;
16-
// virtual void merge(Estimator const& other) = 0;
16+
virtual std::expected<void, std::string> merge(Estimator const& other) = 0;
1717
virtual ~Estimator() = default;
1818
};
1919

include/lfmc/estimator/monte_carlo_estimator.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class MonteCarloEstimator : public Estimator {
1818
std::expected<void, std::string> add_payoffs(const std::vector<Payoffs>& payoffs) override;
1919
bool converged() const override;
2020
std::expected<double, std::string> result() const override;
21-
// void merge(Estimator const& other) override;
21+
std::expected<void, std::string> merge(Estimator const& other) override;
2222
};
2323

2424
} // namespace lfmc

src/estimator/control_variate_estimator.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,19 @@ std::expected<double, std::string> ControlVariateEstimator::result() const {
5050
return mean_x - beta * (mean_y - control_expectation_);
5151
}
5252

53+
std::expected<void, std::string> ControlVariateEstimator::merge(Estimator const& other) {
54+
const auto* other_estimator = dynamic_cast<const ControlVariateEstimator*>(&other);
55+
if (!other_estimator) {
56+
return std::unexpected("Incompatible estimator type for merging");
57+
}
58+
59+
count += other_estimator->count;
60+
sum_x += other_estimator->sum_x;
61+
sum_y += other_estimator->sum_y;
62+
sum_xy += other_estimator->sum_xy;
63+
sum_yy += other_estimator->sum_yy;
64+
65+
return {};
66+
}
67+
5368
} // namespace lfmc

src/estimator/monte_carlo_estimator.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,16 @@ std::expected<double, std::string> MonteCarloEstimator::result() const {
3333
return {sum / static_cast<double>(count)};
3434
}
3535

36-
// void MonteCarloEstimator::merge(Estimator const& other) {
37-
// auto const& mcOther = dynamic_cast<MonteCarloEstimator const&>(other);
38-
// sum += mcOther.sum;
39-
// count += mcOther.count;
40-
// }
36+
std::expected<void, std::string> MonteCarloEstimator::merge(Estimator const& other) {
37+
const auto* other_estimator = dynamic_cast<const MonteCarloEstimator*>(&other);
38+
if (!other_estimator) {
39+
return std::unexpected("Incompatible estimator type for merging.");
40+
}
41+
42+
sum += other_estimator->sum;
43+
count += other_estimator->count;
44+
45+
return {};
46+
}
4147

4248
} // namespace lfmc

0 commit comments

Comments
 (0)