Debian Codegraph @

src/atlas/array/helpers/ArrayCopier.h

raw
1 /*
2 * (C) Crown Copyright 2021 Met Office
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 */
7
8 #pragma once
9
10 #include "atlas/array.h"
11
12 //------------------------------------------------------------------------------
13
14 namespace atlas {
15 namespace array {
16 namespace helpers {
17
18 //------------------------------------------------------------------------------
19
20 /// Helper to copy from source array to target array.
21 template <typename Value, unsigned int Rank>
22 struct array_copier;
23
24 //------------------------------------------------------------------------------
25
26 // Recursive function to copy array elements across all dimensions.
27 template <typename Value, unsigned int Rank, unsigned int Dim>
28 struct array_copier_impl {
29 template <typename SourceView, typename TargetView, typename... DimIndex>
30 static void apply(const SourceView& sourceArr, TargetView& targetArr, const std::array<idx_t, Rank>& shape,
31 DimIndex... idxs) {
32 for (idx_t i = 0; i < shape[Dim]; ++i) {
33 array_copier_impl<Value, Rank, Dim + 1>::apply(sourceArr, targetArr, shape, idxs..., i);
34 }
35 }
36 };
37
38 // End of recursion when Dim == Rank
39 template <typename Value, unsigned int Rank>
40 struct array_copier_impl<Value, Rank, Rank> {
41 template <typename SourceView, typename TargetView, typename... DimIndex>
42 static void apply(const SourceView& sourceArr, TargetView& targetArr, const std::array<idx_t, Rank>& shape,
43 DimIndex... idxs) {
44 targetArr(idxs...) = sourceArr(idxs...);
45 }
46 };
47
48 //------------------------------------------------------------------------------
49
50 template <typename Value, unsigned int Rank>
51 struct array_copier {
52 // Copy from source array to target array.
53 static void apply(const ArrayView<Value, Rank>& sourceArr, ArrayView<Value, Rank>& targetArr) {
54 array_copier_impl<Value, Rank, 0u>::apply(sourceArr, targetArr, shape(sourceArr, targetArr));
55 }
56 // Copy from const source array to target array.
57 static void apply(const ArrayView<const Value, Rank>& sourceArr, ArrayView<Value, Rank>& targetArr) {
58 array_copier_impl<Value, Rank, 0u>::apply(sourceArr, targetArr, shape(sourceArr, targetArr));
59 }
60
61 template <typename SourceView, typename TargetView>
62 static void apply(const SourceView& sourceArr, TargetView& targetArr) {
63 array_copier_impl<Value, Rank, 0u>::apply(sourceArr, targetArr, shape(sourceArr, targetArr));
64 }
65
66 private:
67 // Make an array shape that is compatible with sourceArr and targetArr.
68 // This is useful for arrays from fields with different sized halos.
69 template <typename SourceView, typename TargetView>
70 static std::array<idx_t, Rank> shape(const SourceView& sourceArr, const TargetView& targetArr) {
71 auto arrShape = std::array<idx_t, Rank>{};
72 for (unsigned int Dim = 0; Dim < Rank; ++Dim) {
73 arrShape[Dim] = std::min(sourceArr.shape(Dim), targetArr.shape(Dim));
74 }
75 return arrShape;
76 }
77 };
78
79 //------------------------------------------------------------------------------
80
81 } // namespace helpers
82 } // namespace array
83 } // namespace atlas