reweight.hpp
Go to the documentation of this file.
1 // Copyright 2024 Ekumen, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef BELUGA_ACTIONS_REWEIGHT_HPP
16 #define BELUGA_ACTIONS_REWEIGHT_HPP
17 
18 #include <algorithm>
19 #include <execution>
20 
23 
24 #include <range/v3/action/action.hpp>
25 #include <range/v3/algorithm/max_element.hpp>
26 #include <range/v3/view/common.hpp>
27 
33 namespace beluga::actions {
34 
35 namespace detail {
36 
40 
51  template <
52  class ExecutionPolicy,
53  class Range,
54  class Model,
55  std::enable_if_t<std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>, int> = 0,
56  std::enable_if_t<ranges::range<Range>, int> = 0>
57  constexpr auto operator()(ExecutionPolicy&& policy, Range& range, Model model) const -> Range& {
58  static_assert(beluga::is_particle_range_v<Range>);
59  auto states = range | beluga::views::states | ranges::views::common;
60  auto weights = range | beluga::views::weights | ranges::views::common;
61  std::transform(
62  policy, //
63  std::begin(states), //
64  std::end(states), //
65  std::begin(weights), //
66  std::begin(weights), //
67  [model = std::move(model)](const auto& s, auto w) { return w * model(s); });
68  return range;
69  }
70 
72  template <
73  class Range,
74  class Model,
75  class ExecutionPolicy,
76  std::enable_if_t<ranges::range<Range>, int> = 0,
77  std::enable_if_t<std::is_execution_policy_v<ExecutionPolicy>, int> = 0>
78  constexpr auto operator()(Range&& range, Model model, ExecutionPolicy policy) const -> Range& {
79  return (*this)(std::move(policy), std::forward<Range>(range), std::move(model));
80  }
81 
83  template <class ExecutionPolicy, class Model, std::enable_if_t<std::is_execution_policy_v<ExecutionPolicy>, int> = 0>
84  constexpr auto operator()(ExecutionPolicy policy, Model model) const {
85  return ranges::make_action_closure(ranges::bind_back(reweight_base_fn{}, std::move(model), std::move(policy)));
86  }
87 };
88 
90 struct reweight_fn : public reweight_base_fn {
91  using reweight_base_fn::operator();
92 
94  template <class Range, class Model, std::enable_if_t<ranges::range<Range>, int> = 0>
95  constexpr auto operator()(Range&& range, Model model) const -> Range& {
96  return (*this)(std::execution::seq, std::forward<Range>(range), std::move(model));
97  }
98 
100  template <class Model>
101  constexpr auto operator()(Model model) const {
102  return ranges::make_action_closure(ranges::bind_back(reweight_fn{}, std::move(model)));
103  }
104 };
105 
106 } // namespace detail
107 
110 
115 inline constexpr detail::reweight_fn reweight;
116 
117 } // namespace beluga::actions
118 
119 #endif
beluga::actions::detail::reweight_fn::operator()
constexpr auto operator()(Model model) const
Overload that returns a view closure to compose with other views.
Definition: reweight.hpp:101
beluga::actions
Definition: assign.hpp:28
particles.hpp
Implementation of views related to particle ranges.
beluga::actions::reweight
constexpr detail::reweight_fn reweight
Definition: reweight.hpp:115
beluga::actions::detail::reweight_base_fn::operator()
constexpr auto operator()(ExecutionPolicy &&policy, Range &range, Model model) const -> Range &
Overload that implements the reweight algorithm.
Definition: reweight.hpp:57
beluga::actions::detail::reweight_base_fn::operator()
constexpr auto operator()(ExecutionPolicy policy, Model model) const
Overload that returns a view closure to compose with other views.
Definition: reweight.hpp:84
beluga::actions::detail::reweight_fn
Implementation detail for a reweight range adaptor object with a default execution policy.
Definition: reweight.hpp:90
beluga::actions::detail::reweight_base_fn
Implementation detail for a reweight range adaptor object.
Definition: reweight.hpp:38
particle_traits.hpp
Implementation of traits for particle types, see the Particle named requirements.
beluga::policy
Forward declaration of policy.
Definition: policy.hpp:24
beluga::views::weights
constexpr auto weights
Definition: particles.hpp:34
beluga::actions::detail::reweight_fn::operator()
constexpr auto operator()(Range &&range, Model model) const -> Range &
Overload that defines a default execution policy.
Definition: reweight.hpp:95
beluga::actions::detail::reweight_base_fn::operator()
constexpr auto operator()(Range &&range, Model model, ExecutionPolicy policy) const -> Range &
Overload that re-orders arguments from a view closure.
Definition: reweight.hpp:78
beluga::views::states
constexpr auto states
Definition: particles.hpp:30


beluga
Author(s):
autogenerated on Tue Jul 16 2024 02:59:53