ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
aosoa_pack.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 <config/config.hpp>
23
25
27
28#include <Kokkos_Core.hpp>
29
30#include <omp.h>
31
32#include <cstdint>
33#include <span>
34
35#if defined(__GNUG__) or defined(__clang__)
36#define ESPRESSO_ATTR_ALWAYS_INLINE [[gnu::always_inline]]
37#else
38#define ESPRESSO_ATTR_ALWAYS_INLINE
39#endif
40
43 Kokkos::View<double *[3], Kokkos::LayoutRight, Kokkos::HostSpace>;
45 Kokkos::View<double *[3], Kokkos::LayoutRight, Kokkos::HostSpace>;
47 Kokkos::View<double *[3], Kokkos::LayoutRight, Kokkos::HostSpace>;
49 Kokkos::View<int *[3], Kokkos::LayoutRight, Kokkos::HostSpace>;
50 using ChargeViewType = Kokkos::View<double *, Kokkos::HostSpace>;
51 using DipmViewType = Kokkos::View<double *, Kokkos::HostSpace>;
52 using IdViewType = Kokkos::View<int *, Kokkos::HostSpace>;
53 using TypeViewType = Kokkos::View<int *, Kokkos::HostSpace>;
54 using MassViewType = Kokkos::View<double *, Kokkos::HostSpace>;
55 using FlagsViewType = Kokkos::View<uint8_t *, Kokkos::HostSpace>;
56
67
68 AoSoA_pack() = default;
69
71
73 void resize(std::size_t num_particles) {
74 if (position.extent(0) == 0) {
75 // First allocation
78#ifdef ESPRESSO_ELECTROSTATICS
80#endif
81 id = IdViewType("id", num_particles);
83#ifdef ESPRESSO_MASS
85#endif
88#if defined(ESPRESSO_GAY_BERNE) or defined(ESPRESSO_DIPOLES)
90#endif
91#ifdef ESPRESSO_DIPOLES
93#endif
94 } else {
95 // Reallocation
96 Kokkos::realloc(position, num_particles);
97 Kokkos::realloc(image, num_particles);
98#ifdef ESPRESSO_ELECTROSTATICS
99 Kokkos::realloc(charge, num_particles);
100#endif
101 Kokkos::realloc(id, num_particles);
102 Kokkos::realloc(type, num_particles);
103#ifdef ESPRESSO_MASS
104 Kokkos::realloc(mass, num_particles);
105#endif
106 Kokkos::realloc(flags, num_particles);
107 Kokkos::realloc(velocity, num_particles);
108#if defined(ESPRESSO_GAY_BERNE) or defined(ESPRESSO_DIPOLES)
109 Kokkos::realloc(director, num_particles);
110#endif
111#ifdef ESPRESSO_DIPOLES
112 Kokkos::realloc(dipm, num_particles);
113#endif
114 }
115 }
116
117 template <typename array_layout, typename T, std::size_t N>
118 DEVICE_QUALIFIER std::span<T, N>
119 get_span_at(Kokkos::View<T *[N], array_layout, Kokkos::HostSpace> const &view,
120 std::size_t i) const {
121 return std::span<T, N>(const_cast<T *>(&view(i, 0)), N);
122 }
123
124 template <typename array_layout, typename T, std::size_t N>
126 Kokkos::View<T *[N], array_layout, Kokkos::HostSpace> const &view,
127 std::size_t i) const {
128 Utils::Vector<T, N> result;
129 auto const data = result.data();
130#if !defined(__NVCOMPILER) && !defined(__CUDACC__)
131#if defined(__clang__)
132#pragma omp unroll
133#elif defined(__GNUC__) or defined(__GNUG__)
134#pragma GCC unroll 8
135#endif
136#endif
137 for (std::size_t j = 0ul; j < N; j += 1ul) {
138 data[j] = view(i, j);
139 }
140 return result;
141 }
142
143 template <typename array_layout, typename T, std::size_t N>
145 set_vector_at(Kokkos::View<T *[N], array_layout, Kokkos::HostSpace> &view,
146 std::size_t i, Utils::Vector<T, N> const &value) {
147#if !defined(__NVCOMPILER) && !defined(__CUDACC__)
148#if defined(__clang__)
149#pragma omp unroll
150#elif defined(__GNUC__) or defined(__GNUG__)
151#pragma GCC unroll 8
152#endif
153#endif
154 for (std::size_t j = 0ul; j < N; j += 1ul) {
155 view(i, j) = value[j];
156 }
157 }
158
159 DEVICE_QUALIFIER void set_has_exclusion(std::size_t i, bool value) {
160 flags(i) = value ? uint8_t{1} : uint8_t{0};
161 }
162
163 DEVICE_QUALIFIER bool has_exclusion(std::size_t i) const {
164 return flags(i) == uint8_t{1};
165 }
166};
DEVICE_QUALIFIER constexpr pointer data() noexcept
Definition Array.hpp:132
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
#define HOST_ONLY_QUALIFIER
#define DEVICE_QUALIFIER
DEVICE_QUALIFIER void set_has_exclusion(std::size_t i, bool value)
Kokkos::View< int *[3], Kokkos::LayoutRight, Kokkos::HostSpace > ImageViewType
Kokkos::View< double *, Kokkos::HostSpace > ChargeViewType
DEVICE_QUALIFIER std::span< T, N > get_span_at(Kokkos::View< T *[N], array_layout, Kokkos::HostSpace > const &view, std::size_t i) const
Kokkos::View< uint8_t *, Kokkos::HostSpace > FlagsViewType
Kokkos::View< double *, Kokkos::HostSpace > DipmViewType
Kokkos::View< double *[3], Kokkos::LayoutRight, Kokkos::HostSpace > DirectorViewType
DEVICE_QUALIFIER bool has_exclusion(std::size_t i) const
PositionViewType position
AoSoA_pack(std::size_t num_particles)
DEVICE_QUALIFIER void set_vector_at(Kokkos::View< T *[N], array_layout, Kokkos::HostSpace > &view, std::size_t i, Utils::Vector< T, N > const &value)
Kokkos::View< int *, Kokkos::HostSpace > TypeViewType
Kokkos::View< double *[3], Kokkos::LayoutRight, Kokkos::HostSpace > PositionViewType
DEVICE_QUALIFIER Utils::Vector< T, N > get_vector_at(Kokkos::View< T *[N], array_layout, Kokkos::HostSpace > const &view, std::size_t i) const
DirectorViewType director
Kokkos::View< double *[3], Kokkos::LayoutRight, Kokkos::HostSpace > VelocityViewType
HOST_ONLY_QUALIFIER void resize(std::size_t num_particles)
Kokkos::View< double *, Kokkos::HostSpace > MassViewType
Kokkos::View< int *, Kokkos::HostSpace > IdViewType
VelocityViewType velocity