ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
packing.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2024 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include <cstddef>
25#include <cstring>
26
27/**
28 * @brief Pack a 3D-block of size @c size starting at @c start of an input
29 * 3D-grid @c in with dimension @c dim into an output 3D-block @c out.
30 *
31 * The block is stored in 'row-major-order' or 'C-order', that means
32 * the first index is changing slowest when running through the linear array.
33 * The element (@c i0 (slow), @c i1 (mid), @c i2 (fast)) has the linear
34 * index <tt>li = i2 + size[2] * (i1 + (size[1] * i0))</tt>.
35 *
36 * @param[in] in pointer to input 3D-grid.
37 * @param[out] out pointer to output 3D-block.
38 * @param[in] start start index of the block in the input 3D-grid.
39 * @param[in] size size of the output 3D-block.
40 * @param[in] dim size of the input 3D-grid.
41 * @param[in] element size of a grid element (e.g. 1 for Real, 2 for Complex).
42 */
43template <typename FloatType>
44void fft_pack_block(FloatType const *const in, FloatType *const out,
45 int const *start, int const *size, int const *dim,
46 int element) {
47
48 auto const copy_size =
49 static_cast<std::size_t>(element * size[2]) * sizeof(FloatType);
50 /* offsets for indices in input grid */
51 auto const m_in_offset = element * dim[2];
52 auto const s_in_offset = element * (dim[2] * (dim[1] - size[1]));
53 /* offsets for indices in output grid */
54 auto const m_out_offset = element * size[2];
55 /* linear index of input grid, linear index of output grid */
56 int li_in = element * (start[2] + dim[2] * (start[1] + dim[1] * start[0]));
57 int li_out = 0;
58
59 for (int s = 0; s < size[0]; s++) {
60 for (int m = 0; m < size[1]; m++) {
61 memmove(&(out[li_out]), &(in[li_in]), copy_size);
62 li_in += m_in_offset;
63 li_out += m_out_offset;
64 }
65 li_in += s_in_offset;
66 }
67}
68
69/**
70 * @brief Unpack a 3D-block @c in of size @c size into an output
71 * 3D-grid @c out of size @c dim starting at position @c start.
72 *
73 * See also @ref fft_pack_block.
74 *
75 * @param[in] in pointer to input 3D-block.
76 * @param[out] out pointer to output 3D-grid.
77 * @param[in] start start index of the block in the input 3D-grid.
78 * @param[in] size size of the input 3D-block.
79 * @param[in] dim size of the output 3D-grid.
80 * @param[in] element size of a grid element (e.g. 1 for Real, 2 for Complex).
81 */
82template <typename FloatType>
83void fft_unpack_block(FloatType const *const in, FloatType *const out,
84 int const *start, int const *size, int const *dim,
85 int element) {
86
87 auto const copy_size =
88 static_cast<std::size_t>(element * size[2]) * sizeof(FloatType);
89 /* offsets for indices in output grid */
90 auto const m_out_offset = element * dim[2];
91 auto const s_out_offset = element * (dim[2] * (dim[1] - size[1]));
92 /* offset for indices in input grid */
93 auto const m_in_offset = element * size[2];
94 /* linear index of in grid, linear index of out grid */
95 int li_in = 0;
96 int li_out = element * (start[2] + dim[2] * (start[1] + dim[1] * start[0]));
97
98 for (int s = 0; s < size[0]; s++) {
99 for (int m = 0; m < size[1]; m++) {
100 memmove(&(out[li_out]), &(in[li_in]), copy_size);
101 li_in += m_in_offset;
102 li_out += m_out_offset;
103 }
104 li_out += s_out_offset;
105 }
106}
void fft_pack_block(FloatType const *const in, FloatType *const out, int const *start, int const *size, int const *dim, int element)
Pack a 3D-block of size size starting at start of an input 3D-grid in with dimension dim into an outp...
Definition packing.hpp:44
void fft_unpack_block(FloatType const *const in, FloatType *const out, int const *start, int const *size, int const *dim, int element)
Unpack a 3D-block in of size size into an output 3D-grid out of size dim starting at position start.
Definition packing.hpp:83