Debian Codegraph @

src/atlas/parallel/detail/Packer.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 <stdexcept>
14 #include <string>
15 #include <vector>
16
17 #include "atlas/array/ArrayView.h"
18 #include "atlas/array/SVector.h"
19 #include "atlas/runtime/Exception.h"
20
21 namespace atlas::parallel::detail {
22
23 template <int ParallelDim, typename DATA_TYPE, int RANK>
24 struct HostPacker {
25 static void pack(const int sendcnt, array::SVector<int> const& sendmap,
26 const array::ArrayView<DATA_TYPE, RANK>& array, DATA_TYPE* send_buffer, int send_buffer_size);
27 static void unpack(const int recvcnt, array::SVector<int> const& recvmap, const DATA_TYPE* recv_buffer, int recv_buffer_size,
28 array::ArrayView<DATA_TYPE, RANK>& array);
29 };
30
31 template <int ParallelDim, typename DATA_TYPE, int RANK>
32 struct HostAdjointPacker {
33 static void unpack(const int recvcnt, array::SVector<int> const& recvmap, const DATA_TYPE* recv_buffer,
34 int recv_buffer_size, array::ArrayView<DATA_TYPE, RANK>& array);
35 };
36
37 template <int ParallelDim, typename DATA_TYPE, int RANK>
38 struct HostZeroer {
39 static void zero(const int sendcnt, array::SVector<int> const& sendmap, array::ArrayView<DATA_TYPE, RANK>& array,
40 DATA_TYPE* recv_buffer, int recv_buffer_size);
41 };
42
43 #if ATLAS_HAVE_GPU
44 template <int ParallelDim, typename DATA_TYPE, int RANK>
45 struct DevicePacker {
46 static void pack(const int sendcnt, array::SVector<int> const& sendmap,
47 const array::ArrayView<DATA_TYPE, RANK>& array,
48 DATA_TYPE* send_buffer, int send_buffer_size);
49
50 static void unpack(const int sendcnt, array::SVector<int> const& recvmap,
51 const DATA_TYPE* recv_buffer, int recv_buffer_size,
52 array::ArrayView<DATA_TYPE, RANK>& array);
53 };
54 #else
55 template <int ParallelDim, typename DATA_TYPE, int RANK>
56 using DevicePacker = HostPacker<ParallelDim, DATA_TYPE, RANK>;
57 #endif
58
59
60
61 template <int ParallelDim, typename DATA_TYPE, int RANK>
62 struct Packer {
63 static void pack(const int sendcnt, array::SVector<int> const& sendmap,
64 const array::ArrayView<DATA_TYPE, RANK>& array,
65 DATA_TYPE* send_buffer, int send_buffer_size,
66 bool on_device) {
67 if (not on_device) {
68 HostPacker<ParallelDim, DATA_TYPE, RANK>::pack(sendcnt, sendmap, array, send_buffer, send_buffer_size);
69 }
70 else {
71 DevicePacker<ParallelDim, DATA_TYPE, RANK>::pack(sendcnt, sendmap, array, send_buffer, send_buffer_size);
72 }
73 }
74
75 static void unpack(const int recvcnt, array::SVector<int> const& recvmap,
76 const DATA_TYPE* recv_buffer, int recv_buffer_size,
77 array::ArrayView<DATA_TYPE, RANK>& array,
78 bool on_device) {
79 if (not on_device) {
80 HostPacker<ParallelDim, DATA_TYPE, RANK>::unpack(recvcnt, recvmap, recv_buffer, recv_buffer_size, array);
81 }
82 else {
83 DevicePacker<ParallelDim, DATA_TYPE, RANK>::unpack(recvcnt, recvmap, recv_buffer, recv_buffer_size, array);
84 }
85 }
86 };
87
88 template <int ParallelDim, typename DATA_TYPE, int RANK>
89 struct AdjointPacker {
90 static void pack(const int sendcnt, array::SVector<int> const& sendmap,
91 const array::ArrayView<DATA_TYPE, RANK>& array,
92 DATA_TYPE* send_buffer, int send_buffer_size,
93 bool on_device) {
94 Packer<ParallelDim, DATA_TYPE, RANK>::pack(sendcnt, sendmap, array, send_buffer, send_buffer_size, on_device);
95 }
96 static void unpack(const int recvcnt, array::SVector<int> const& recvmap, const DATA_TYPE* recv_buffer,
97 int recv_buffer_size, array::ArrayView<DATA_TYPE, RANK>& array,
98 bool on_device) {
99 if (on_device) {
100 ATLAS_NOTIMPLEMENTED;
101 }
102 else {
103 HostAdjointPacker<ParallelDim, DATA_TYPE, RANK>::unpack(recvcnt, recvmap, recv_buffer, recv_buffer_size, array);
104 }
105 }
106 };
107
108
109 template <int ParallelDim, typename DATA_TYPE, int RANK>
110 struct Zeroer {
111 static void zero(const int sendcnt, array::SVector<int> const& sendmap, array::ArrayView<DATA_TYPE, RANK>& array,
112 DATA_TYPE* recv_buffer, int recv_buffer_size,
113 bool on_device) {
114 if (on_device) {
115 ATLAS_NOTIMPLEMENTED;
116 }
117 else {
118 HostZeroer<ParallelDim, DATA_TYPE, RANK>::zero(sendcnt, sendmap, array, recv_buffer, recv_buffer_size);
119 }
120 }
121 };
122
123 //----------------------------------------------------------------------------------------------------------------------
124
125 } // namespace atlas::parallel::detail