ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
kokkos_helpers.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025-2026 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <algorithm>
23#include <type_traits>
24
25#include <Kokkos_Core.hpp>
26
27/**
28 * @brief Wrapper for ``Kokkos::deep_copy`` that skips fork/join when
29 * the number of threads is 1.
30 */
32kokkos_deep_copy(auto const &exec_space, auto const &view, auto const &value) {
33 using View = std::remove_cvref_t<decltype(view)>;
34 if constexpr (Kokkos::SpaceAccessibility<
35 Kokkos::DefaultHostExecutionSpace,
36 typename View::memory_space>::accessible) {
37 if (Kokkos::num_threads() == 1 and view.span_is_contiguous()) {
38 std::fill_n(view.data(), view.span(),
39 static_cast<typename View::value_type>(value));
40 return;
41 }
42 }
43 Kokkos::deep_copy(exec_space, view, value);
44}
45
46/**
47 * @brief Wrapper for ``Kokkos::parallel_for`` that skips fork/join when
48 * the number of threads is 1.
49 */
50template <class ExecutionSpace = Kokkos::DefaultHostExecutionSpace>
52kokkos_parallel_range_for(auto const &name, auto start, auto end,
53 auto const &kernel) {
54 if (Kokkos::num_threads() > 1) {
55 Kokkos::RangePolicy<ExecutionSpace> policy(start, end);
56 Kokkos::parallel_for(name, policy, kernel);
57 } else {
58 for (auto p_index = start; p_index < end; ++p_index) {
59 kernel(p_index);
60 }
61 }
62}
#define ESPRESSO_ATTR_ALWAYS_INLINE
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
ESPRESSO_ATTR_ALWAYS_INLINE void kokkos_parallel_range_for(auto const &name, auto start, auto end, auto const &kernel)
Wrapper for Kokkos::parallel_for that skips fork/join when the number of threads is 1.
ESPRESSO_ATTR_ALWAYS_INLINE void kokkos_deep_copy(auto const &exec_space, auto const &view, auto const &value)
Wrapper for Kokkos::deep_copy that skips fork/join when the number of threads is 1.