Debian Codegraph @

src/atlas/grid/detail/distribution/DistributionArray.cc

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 #include "DistributionArray.h"
12
13 #include <algorithm>
14 #include <numeric>
15 #include <ostream>
16 #include <vector>
17
18 #include "eckit/types/Types.h"
19 #include "eckit/utils/Hash.h"
20
21 #include "atlas/grid/Grid.h"
22 #include "atlas/grid/Partitioner.h"
23 #include "atlas/parallel/omp/omp.h"
24 #include "atlas/util/Config.h"
25
26 namespace atlas {
27 namespace grid {
28 namespace detail {
29 namespace distribution {
30
31 namespace {
32 std::string distribution_type(int N, const Partitioner& p = Partitioner()) {
33 if (N == 1) {
34 return "serial";
35 }
36 if (not p) {
37 return "custom";
38 }
39 return p.type();
40 }
41 } // namespace
42
43 DistributionArray::DistributionArray(const Grid& grid, const eckit::Parametrisation& config):
44 DistributionArray(grid, Partitioner{config}) {}
45
46
47 DistributionArray::DistributionArray(const Grid& grid, const Partitioner& partitioner) {
48 part_.resize(grid.size());
49 partitioner.partition(grid, part_.data());
50 nb_partitions_ = partitioner.nb_partitions();
51
52 size_t size = part_.size();
53 nb_pts_.resize(nb_partitions_, 0);
54 atlas_omp_parallel {
55 std::vector<int> nb_pts(nb_partitions_, 0); // thread-local
56 atlas_omp_for(size_t j = 0; j < size; ++j) {
57 ++nb_pts[part_[j]];
58 }
59 atlas_omp_critical {
60 for (int p = 0; p < nb_partitions_; ++p) {
61 nb_pts_[p] += nb_pts[p];
62 }
63 }
64 }
65
66 ATLAS_ASSERT( std::accumulate(nb_pts_.begin(),nb_pts_.end(),idx_t{0}) == part_.size() );
67
68 max_pts_ = *std::max_element(nb_pts_.begin(), nb_pts_.end());
69 min_pts_ = *std::min_element(nb_pts_.begin(), nb_pts_.end());
70 type_ = distribution_type(nb_partitions_, partitioner);
71 }
72
73 DistributionArray::DistributionArray(int nb_partitions, idx_t npts, int part[], int part0) {
74 part_.assign(part, part + npts);
75 if (nb_partitions == 0) {
76 std::set<int> partset(part_.begin(), part_.end());
77 nb_partitions_ = static_cast<idx_t>(partset.size());
78 }
79 else {
80 nb_partitions_ = nb_partitions;
81 }
82 nb_pts_.resize(nb_partitions_, 0);
83 for (idx_t j = 0, size = static_cast<idx_t>(part_.size()); j < size; ++j) {
84 part_[j] -= part0;
85 ++nb_pts_[part_[j]];
86 }
87 max_pts_ = *std::max_element(nb_pts_.begin(), nb_pts_.end());
88 min_pts_ = *std::min_element(nb_pts_.begin(), nb_pts_.end());
89 type_ = distribution_type(nb_partitions_);
90 }
91
92 DistributionArray::DistributionArray(int nb_partitions, partition_t&& part):
93 nb_partitions_(nb_partitions), part_(std::move(part)), nb_pts_(nb_partitions_, 0) {
94 size_t size = part_.size();
95 int num_threads = atlas_omp_get_max_threads();
96 std::vector<std::vector<int> > nb_pts_per_thread(num_threads, std::vector<int>(nb_partitions_));
97 atlas_omp_parallel {
98 int thread = atlas_omp_get_thread_num();
99 auto& nb_pts = nb_pts_per_thread[thread];
100 atlas_omp_for(size_t j = 0; j < size; ++j) {
101 int p = part_[j];
102 ++nb_pts[p];
103 }
104 }
105 for (int thread = 0; thread < num_threads; ++thread) {
106 for (int p = 0; p < nb_partitions_; ++p) {
107 nb_pts_[p] += nb_pts_per_thread[thread][p];
108 }
109 }
110
111 max_pts_ = *std::max_element(nb_pts_.begin(), nb_pts_.end());
112 min_pts_ = *std::min_element(nb_pts_.begin(), nb_pts_.end());
113 type_ = distribution_type(nb_partitions_);
114 }
115
116 DistributionArray::~DistributionArray() = default;
117
118 void DistributionArray::print(std::ostream& s) const {
119 auto print_partition = [&](std::ostream& s) {
120 eckit::output_list<int> list_printer(s);
121 for (size_t i = 0, size = part_.size(); i < size; i++) {
122 list_printer.push_back(part_[i]);
123 }
124 };
125 s << "Distribution( "
126 << "type: " << type_ << ", nb_points: " << size() << ", nb_partitions: " << nb_pts_.size() << ", parts : ";
127 print_partition(s);
128 }
129
130 void DistributionArray::hash(eckit::Hash& hash) const {
131 for (size_t i = 0; i < part_.size(); i++) {
132 hash.add(part_[i]);
133 }
134 }
135
136
137 } // namespace distribution
138 } // namespace detail
139 } // namespace grid
140 } // namespace atlas