Debian Codegraph @

src/atlas/parallel/detail/pack_index.h

raw
1 /*
2 * (C) Copyright 2025- ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #pragma once
12
13 #include "atlas/array/ArrayView.h"
14 #include "atlas/array/SVector.h"
15
16 namespace atlas::parallel::detail {
17
18 template <int ParallelDim, int Cnt, int CurrentDim>
19 struct pack_index {
20 template <typename DATA_TYPE, int RANK, typename... Idx>
21 ATLAS_HOST_DEVICE static void apply(idx_t& buf_idx, const idx_t node_idx,
22 const array::ArrayView<DATA_TYPE, RANK>& field, DATA_TYPE* send_buffer,
23 Idx... idxs) {
24 for (idx_t i = 0; i < field.template shape<CurrentDim>(); ++i) {
25 pack_index<ParallelDim, Cnt - 1, CurrentDim + 1>::apply(buf_idx, node_idx, field, send_buffer,
26 idxs..., i);
27 }
28 }
29 };
30
31 template <int ParallelDim>
32 struct pack_index<ParallelDim, 0, ParallelDim> {
33 template <typename DATA_TYPE, int RANK, typename... Idx>
34 ATLAS_HOST_DEVICE static void apply(idx_t& buf_idx, const idx_t node_idx,
35 const array::ArrayView<DATA_TYPE, RANK>& field, DATA_TYPE* send_buffer,
36 Idx... idxs) {
37 send_buffer[buf_idx++] = field(idxs...);
38 }
39 };
40
41 template <int ParallelDim, int Cnt>
42 struct pack_index<ParallelDim, Cnt, ParallelDim> {
43 template <typename DATA_TYPE, int RANK, typename... Idx>
44 ATLAS_HOST_DEVICE static void apply(idx_t& buf_idx, const idx_t node_idx,
45 const array::ArrayView<DATA_TYPE, RANK>& field, DATA_TYPE* send_buffer,
46 Idx... idxs) {
47 pack_index<ParallelDim, Cnt - 1, ParallelDim + 1>::apply(buf_idx, node_idx, field, send_buffer, idxs...,
48 node_idx);
49 }
50 };
51
52 template <int ParallelDim, int CurrentDim>
53 struct pack_index<ParallelDim, 0, CurrentDim> {
54 template <typename DATA_TYPE, int RANK, typename... Idx>
55 ATLAS_HOST_DEVICE static void apply(idx_t& buf_idx, const idx_t node_idx,
56 const array::ArrayView<DATA_TYPE, RANK>& field, DATA_TYPE* send_buffer,
57 Idx... idxs) {
58 send_buffer[buf_idx++] = field(idxs...);
59 }
60 };
61
62 template <int ParallelDim, int Cnt, int CurrentDim>
63 struct unpack_index {
64 template <typename DATA_TYPE, int RANK, typename... Idx>
65 ATLAS_HOST_DEVICE static void apply(idx_t& buf_idx, const idx_t node_idx, const DATA_TYPE* recv_buffer,
66 array::ArrayView<DATA_TYPE, RANK>& field, Idx... idxs) {
67 for (idx_t i = 0; i < field.template shape<CurrentDim>(); ++i) {
68 unpack_index<ParallelDim, Cnt - 1, CurrentDim + 1>::apply(buf_idx, node_idx, recv_buffer, field,
69 idxs..., i);
70 }
71 }
72 };
73
74 template <int ParallelDim>
75 struct unpack_index<ParallelDim, 0, ParallelDim> {
76 template <typename DATA_TYPE, int RANK, typename... Idx>
77 ATLAS_HOST_DEVICE static void apply(idx_t& buf_idx, const idx_t node_idx, const DATA_TYPE* recv_buffer,
78 array::ArrayView<DATA_TYPE, RANK>& field, Idx... idxs) {
79 field(idxs...) = recv_buffer[buf_idx++];
80 }
81 };
82
83 template <int ParallelDim, int Cnt>
84 struct unpack_index<ParallelDim, Cnt, ParallelDim> {
85 template <typename DATA_TYPE, int RANK, typename... Idx>
86 ATLAS_HOST_DEVICE static void apply(idx_t& buf_idx, const idx_t node_idx, const DATA_TYPE* recv_buffer,
87 array::ArrayView<DATA_TYPE, RANK>& field, Idx... idxs) {
88 unpack_index<ParallelDim, Cnt - 1, ParallelDim + 1>::apply(buf_idx, node_idx, recv_buffer, field, idxs...,
89 node_idx);
90 }
91 };
92
93 template <int ParallelDim, int CurrentDim>
94 struct unpack_index<ParallelDim, 0, CurrentDim> {
95 template <typename DATA_TYPE, int RANK, typename... Idx>
96 ATLAS_HOST_DEVICE static void apply(idx_t& buf_idx, const idx_t node_idx, const DATA_TYPE* recv_buffer,
97 array::ArrayView<DATA_TYPE, RANK>& field, Idx... idxs) {
98 field(idxs...) = recv_buffer[buf_idx++];
99 }
100 };
101
102 } // namespace atlas::parallel::detail