Program Listing for File reweight.hpp

Return to documentation for file (include/beluga/actions/reweight.hpp)

// Copyright 2024 Ekumen, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef BELUGA_ACTIONS_REWEIGHT_HPP
#define BELUGA_ACTIONS_REWEIGHT_HPP

#include <algorithm>
#include <execution>

#include <beluga/type_traits/particle_traits.hpp>
#include <beluga/views/particles.hpp>

#include <range/v3/action/action.hpp>
#include <range/v3/algorithm/max_element.hpp>
#include <range/v3/view/common.hpp>

namespace beluga::actions {

namespace detail {

struct reweight_base_fn {

  template <
      class ExecutionPolicy,
      class Range,
      class Model,
      std::enable_if_t<std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>, int> = 0,
      std::enable_if_t<ranges::range<Range>, int> = 0>
  constexpr auto operator()(ExecutionPolicy&& policy, Range& range, Model model) const -> Range& {
    static_assert(beluga::is_particle_range_v<Range>);
    auto states = range | beluga::views::states | ranges::views::common;
    auto weights = range | beluga::views::weights | ranges::views::common;
    std::transform(
        policy,               //
        std::begin(states),   //
        std::end(states),     //
        std::begin(weights),  //
        std::begin(weights),  //
        [model = std::move(model)](const auto& s, auto w) { return w * model(s); });
    return range;
  }

  template <
      class Range,
      class Model,
      class ExecutionPolicy,
      std::enable_if_t<ranges::range<Range>, int> = 0,
      std::enable_if_t<std::is_execution_policy_v<ExecutionPolicy>, int> = 0>
  constexpr auto operator()(Range&& range, Model model, ExecutionPolicy policy) const -> Range& {
    return (*this)(std::move(policy), std::forward<Range>(range), std::move(model));
  }

  template <class ExecutionPolicy, class Model, std::enable_if_t<std::is_execution_policy_v<ExecutionPolicy>, int> = 0>
  constexpr auto operator()(ExecutionPolicy policy, Model model) const {
    return ranges::make_action_closure(ranges::bind_back(reweight_base_fn{}, std::move(model), std::move(policy)));
  }
};

struct reweight_fn : public reweight_base_fn {
  using reweight_base_fn::operator();

  template <class Range, class Model, std::enable_if_t<ranges::range<Range>, int> = 0>
  constexpr auto operator()(Range&& range, Model model) const -> Range& {
    return (*this)(std::execution::seq, std::forward<Range>(range), std::move(model));
  }

  template <class Model>
  constexpr auto operator()(Model model) const {
    return ranges::make_action_closure(ranges::bind_back(reweight_fn{}, std::move(model)));
  }
};

}  // namespace detail


inline constexpr detail::reweight_fn reweight;

}  // namespace beluga::actions

#endif