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
26#include <Kokkos_Core.hpp>
27
28#include <omp.h>
29
30#include <cstdint>
31#include <span>
32
33#if defined(__GNUG__) or defined(__clang__)
34#define ESPRESSO_ATTR_ALWAYS_INLINE [[gnu::always_inline]]
35#else
36#define ESPRESSO_ATTR_ALWAYS_INLINE
37#endif
38
54
65
66 AoSoA_pack() = default;
67
68 AoSoA_pack(std::size_t num_particles) { resize(num_particles); }
69
70 void resize(std::size_t num_particles) {
71 if (position.extent(0) == 0) {
72 // First allocation
73 position = PositionViewType("position", num_particles);
74 image = ImageViewType("image", num_particles);
75#ifdef ESPRESSO_ELECTROSTATICS
76 charge = ChargeViewType("charge", num_particles);
77#endif
78 id = IdViewType("id", num_particles);
79 type = TypeViewType("type", num_particles);
80#ifdef ESPRESSO_MASS
81 mass = MassViewType("mass", num_particles);
82#endif
83 flags = FlagsViewType("flags", num_particles);
84 velocity = PositionViewType("velocity", num_particles);
85#if defined(ESPRESSO_GAY_BERNE) or defined(ESPRESSO_DIPOLES)
86 director = DirectorViewType("director", num_particles);
87#endif
88#ifdef ESPRESSO_DIPOLES
89 dipm = DipmViewType("dipm", num_particles);
90#endif
91 } else {
92 // Reallocation
93 Kokkos::realloc(position, num_particles);
94 Kokkos::realloc(image, num_particles);
95#ifdef ESPRESSO_ELECTROSTATICS
96 Kokkos::realloc(charge, num_particles);
97#endif
98 Kokkos::realloc(id, num_particles);
99 Kokkos::realloc(type, num_particles);
100#ifdef ESPRESSO_MASS
101 Kokkos::realloc(mass, num_particles);
102#endif
103 Kokkos::realloc(flags, num_particles);
104 Kokkos::realloc(velocity, num_particles);
105#if defined(ESPRESSO_GAY_BERNE) or defined(ESPRESSO_DIPOLES)
106 Kokkos::realloc(director, num_particles);
107#endif
108#ifdef ESPRESSO_DIPOLES
109 Kokkos::realloc(dipm, num_particles);
110#endif
111 }
112 }
113
114 template <typename array_layout, typename T, std::size_t N>
115 std::span<T, N>
116 get_span_at(Kokkos::View<T *[N], array_layout, Kokkos::HostSpace> const &view,
117 std::size_t i) const {
118 return std::span<T, N>(const_cast<T *>(&view(i, 0)), N);
119 }
120
121 template <typename array_layout, typename T, std::size_t N>
123 Kokkos::View<T *[N], array_layout, Kokkos::HostSpace> const &view,
124 std::size_t i) const {
125 Utils::Vector<T, N> result;
126 auto const data = result.data();
127#if (defined(__GNUC__) or defined(__GNUG__)) && !defined(__clang__)
128#pragma GCC unroll 8
129#else
130#pragma omp unroll
131#endif
132 for (std::size_t j = 0ul; j < N; j += 1ul) {
133 data[j] = view(i, j);
134 }
135 return result;
136 }
137
138 template <typename array_layout, typename T, std::size_t N>
139 void
140 set_vector_at(Kokkos::View<T *[N], array_layout, Kokkos::HostSpace> &view,
141 std::size_t i, Utils::Vector<T, N> const &value) {
142#if (defined(__GNUC__) or defined(__GNUG__)) && !defined(__clang__)
143#pragma GCC unroll 8
144#else
145#pragma omp unroll
146#endif
147 for (std::size_t j = 0ul; j < N; j += 1ul) {
148 view(i, j) = value[j];
149 }
150 }
151
152 void set_has_exclusion(std::size_t i, bool value) {
153 flags(i) = value ? uint8_t{1} : uint8_t{0};
154 }
155
156 bool has_exclusion(std::size_t i) const { return flags(i) == uint8_t{1}; }
157};
DEVICE_QUALIFIER constexpr pointer data() noexcept
Definition Array.hpp:132
Kokkos::View< int *[3], Kokkos::LayoutRight, Kokkos::HostSpace > ImageViewType
Kokkos::View< double *, Kokkos::HostSpace > ChargeViewType
bool has_exclusion(std::size_t i) const
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
void set_vector_at(Kokkos::View< T *[N], array_layout, Kokkos::HostSpace > &view, std::size_t i, Utils::Vector< T, N > const &value)
PositionViewType position
AoSoA_pack(std::size_t num_particles)
Kokkos::View< int *, Kokkos::HostSpace > TypeViewType
Kokkos::View< double *[3], Kokkos::LayoutRight, Kokkos::HostSpace > PositionViewType
void set_has_exclusion(std::size_t i, bool value)
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 *, Kokkos::HostSpace > MassViewType
Kokkos::View< int *, Kokkos::HostSpace > IdViewType
VelocityViewType velocity
void resize(std::size_t num_particles)