Debian Codegraph @

src/atlas/parallel/Locate.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 <string>
14 #include <string_view>
15 #include <vector>
16 #include <functional>
17
18 #include "atlas/mdspan.h"
19 #include "atlas/util/vector.h"
20
21 namespace atlas::parallel {
22
23 class Locator {
24 public:
25
26 // Helper class that gives bracket-operator to a simple functor returning type T for a given index
27 template <typename T>
28 struct fspan {
29 using value_type = std::remove_const_t<T>;
30 size_t size() const { return size_; }
31 value_type operator[](size_t i) const {
32 return (*func_)(i);
33 }
34 fspan(const std::function<value_type(size_t)>* func, std::size_t size) :
35 func_(func), size_(size) {
36 }
37 private:
38 const std::function<value_type(size_t)>* func_{nullptr};
39 std::size_t size_{0};
40 };
41
42 template<class T, std::size_t Extents = dynamic_extent, class Layout = layout_right, class Accessor = default_accessor<T>>
43 using span = mdspan<T, extents<size_t,Extents>, Layout, Accessor>;
44
45 static void locate_partition(
46 // context
47 fspan<const int> distribution, int distribution_base,
48 // input
49 span<const gidx_t> global_index, const gidx_t global_index_base,
50 // output
51 span<int> partition, const int partition_base);
52
53 static void locate_partition(
54 // context
55 span<const int> distribution, int distribution_base,
56 // input
57 span<const gidx_t> global_index, const gidx_t global_index_base,
58 // output
59 span<int> partition, const int partition_base);
60
61 // Given global_index, find the corresponding partition and remote_index
62 // This could be costly as it involves memory and communication
63 static void locate_remote_index(
64 // context
65 std::string_view mpi_comm,
66 span<const gidx_t> my_glb_idx, const gidx_t my_global_index_base, span<const int> my_ghost,
67 // input
68 span<const gidx_t> global_index, const gidx_t global_index_base,
69 span<const int> partition, const int partition_base,
70 // output
71 span<idx_t> remote_index, const idx_t remote_index_base
72 );
73
74 // Given global_index, find the corresponding partition and remote_index
75 // This could be costly as it involves memory and communication
76 // local information: my_size, my_glb_idx, my_ghost
77 // global information: distribution
78 // requested indices to locate: size, global_index
79 // output of locate: partition, remote_index, remote_index_base
80 static void locate(
81 // context
82 std::string_view mpi_comm,
83 span<const gidx_t> my_glb_idx, const gidx_t my_global_index_base, span<const int> my_ghost,
84 fspan<const int> distribution, const int distribution_base,
85 // input
86 span<const gidx_t> global_index, const gidx_t global_index_base,
87 // output
88 span<int> partition, const int partition_base,
89 span<idx_t> remote_index, const idx_t remote_index_base);
90
91 static void locate(
92 // context
93 std::string_view mpi_comm,
94 span<const gidx_t> my_glb_idx, const gidx_t my_global_index_base, span<const int> my_ghost,
95 span<const int> distribution, const int distribution_base,
96 // input
97 span<const gidx_t> global_index, const gidx_t global_index_base,
98 // output
99 span<int> partition, const int partition_base,
100 span<idx_t> remote_index, const idx_t remote_index_base);
101
102 virtual void locate(
103 // input
104 span<const gidx_t> global_index, const gidx_t global_index_base,
105 // output
106 span<int> partition, const int partition_base,
107 span<idx_t> remote_index, const idx_t remote_index_base) const = 0;
108
109 virtual void locate(
110 // input
111 const std::vector<gidx_t> global_index, const gidx_t global_index_base,
112 // output
113 std::vector<int>& partition, const int partition_base,
114 std::vector<idx_t>& remote_index, const idx_t remote_index_base) const {
115 locate(
116 span<const gidx_t>{global_index.data(), global_index.size()}, global_index_base,
117 span<int>{partition.data(), partition.size()}, partition_base,
118 span<idx_t>{remote_index.data(), remote_index.size()}, remote_index_base
119 );
120 }
121
122 };
123 } // namespace atlas::parallel