ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
index.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-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 <cassert>
23#include <cstddef>
24#include <iterator>
25#include <numeric>
26#include <stdexcept>
27
28#include "Vector.hpp"
29#include "device_qualifier.hpp"
30
31namespace Utils {
32
34
35template <MemoryOrder memory_order>
36DEVICE_QUALIFIER inline int get_linear_index(int a, int b, int c,
37 Vector3i const &adim) {
38 DEVICE_ASSERT((a >= 0) && (a < adim[0]));
39 DEVICE_ASSERT((b >= 0) && (b < adim[1]));
40 DEVICE_ASSERT((c >= 0) && (c < adim[2]));
41 if constexpr (memory_order == MemoryOrder::COLUMN_MAJOR) {
42 return a + adim[0] * (b + adim[1] * c);
43 }
44 return adim[1] * adim[2] * a + adim[2] * b + c;
45}
46
47/** Get the linear index from the position (@p a,@p b,@p c) in a 3D grid
48 * of dimensions @p adim.
49 *
50 * @return The linear index
51 * @param a , b , c Position in 3D space
52 * @param adim Dimensions of the underlying grid
53 * @param memory_order Row- or column-major
54 */
55DEVICE_QUALIFIER inline int
63
64DEVICE_QUALIFIER inline int
69
70template <MemoryOrder memory_order>
72 Vector3i const &adim) {
73 return get_linear_index<memory_order>(ind[0], ind[1], ind[2], adim);
74}
75
76/**
77 * @brief Linear index into a lower triangular matrix.
78 *
79 * This is row-major.
80 *
81 * @tparam T Integral type
82 * @param i row index
83 * @param j column index
84 * @return linear index
85 */
86template <class T> DEVICE_QUALIFIER T lower_triangular(T i, T j) {
87 /* i is a valid row index */
88 DEVICE_ASSERT(i >= 0);
89 /* j is in the lower triangle */
90 DEVICE_ASSERT(j >= 0 and j <= i);
91 return (i * (i + 1)) / 2 + j;
92}
93
94} // namespace Utils
Vector implementation and trait types for boost qvm interoperability.
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
#define DEVICE_ASSERT(A)
#define DEVICE_QUALIFIER
DEVICE_QUALIFIER T lower_triangular(T i, T j)
Linear index into a lower triangular matrix.
Definition index.hpp:86
DEVICE_QUALIFIER int get_linear_index(int a, int b, int c, Vector3i const &adim)
Definition index.hpp:36
MemoryOrder
Definition index.hpp:33