Debian Codegraph @

src/atlas/grid/detail/distribution/DistributionFunction.h

raw
1 /*
2 * (C) Copyright 2013 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 <vector>
15
16 #include "atlas/grid/detail/distribution/DistributionImpl.h"
17
18
19 namespace atlas {
20 namespace grid {
21 namespace detail {
22 namespace distribution {
23
24 class DistributionFunction : public DistributionImpl {
25 public:
26 DistributionFunction(const Grid&): DistributionImpl() {}
27 bool functional() const override { return true; }
28 size_t footprint() const override { return nb_pts_.size() * sizeof(nb_pts_[0]); }
29 const std::string& type() const override { return nb_partitions_ == 1 ? serial : type_; }
30 idx_t nb_partitions() const override { return nb_partitions_; }
31
32 const std::vector<idx_t>& nb_pts() const override { return nb_pts_; }
33
34 idx_t max_pts() const override { return max_pts_; }
35 idx_t min_pts() const override { return min_pts_; }
36
37 void print(std::ostream&) const override;
38
39 gidx_t size() const override { return size_; }
40
41 void hash(eckit::Hash&) const override;
42
43 protected:
44 gidx_t size_;
45 idx_t nb_partitions_;
46 std::vector<idx_t> nb_pts_;
47 idx_t max_pts_;
48 idx_t min_pts_;
49 std::string type_{"functional"};
50 std::string serial{"serial"};
51 };
52
53
54 template <typename Derived>
55 class DistributionFunctionT : public DistributionFunction {
56 public:
57 DistributionFunctionT(const Grid& grid): DistributionFunction(grid) {}
58 ATLAS_ALWAYS_INLINE int partition(gidx_t index) const override {
59 return static_cast<const Derived*>(this)->function(index);
60 }
61
62 ATLAS_ALWAYS_INLINE void partition(gidx_t begin, gidx_t end, int partitions[]) const override {
63 const Derived& derived = *static_cast<const Derived*>(this);
64 size_t i = 0;
65 for (gidx_t n = begin; n < end; ++n, ++i) {
66 partitions[i] = derived.function(n);
67 }
68 }
69 };
70
71 } // namespace distribution
72 } // namespace detail
73 } // namespace grid
74 } // namespace atlas