Debian Codegraph @

src/atlas/grid/detail/distribution/BandsDistribution.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 "BandsDistribution.h"
12
13 #include <algorithm>
14 #include <cstdint>
15 #include <limits>
16
17 #include "atlas/grid/Grid.h"
18 #include "atlas/runtime/Exception.h"
19
20 namespace atlas {
21 namespace grid {
22 namespace detail {
23 namespace distribution {
24
25 template <typename Int>
26 BandsDistribution<Int>::BandsDistribution(const atlas::Grid& grid, atlas::idx_t nb_partitions, const std::string& type,
27 size_t blocksize):
28 DistributionFunctionT<BandsDistribution<Int>>(grid) {
29 this->type_ = type;
30 size_t gridsize = grid.size();
31 this->size_ = gridsize;
32 this->nb_partitions_ = nb_partitions;
33 nb_partitions_Int_ = nb_partitions;
34 blocksize_ = blocksize;
35
36 nb_blocks_ = gridsize / blocksize_;
37
38 if (gridsize % blocksize_) {
39 nb_blocks_++;
40 }
41
42 this->nb_pts_.reserve(nb_partitions_Int_);
43
44 for (idx_t iproc = 0; iproc < nb_partitions; iproc++) {
45 // Approximate values
46 gidx_t imin = blocksize_ * (((iproc + 0) * nb_blocks_) / nb_partitions_Int_);
47 gidx_t imax = blocksize_ * (((iproc + 1) * nb_blocks_) / nb_partitions_Int_);
48
49 while (imin > 0) {
50 if (function(imin - blocksize_) == iproc) {
51 imin -= blocksize_;
52 }
53 else {
54 break;
55 }
56 }
57
58 while (function(imin) < iproc) {
59 imin += blocksize_;
60 }
61
62 while (function(imax - 1) == iproc + 1) {
63 imax -= blocksize_;
64 }
65
66 while (imax + blocksize_ <= gridsize) {
67 if (function(imax) == iproc) {
68 imax += blocksize_;
69 }
70 else {
71 break;
72 }
73 }
74
75 imax = std::min(imax, (gidx_t)gridsize);
76 this->nb_pts_.push_back(imax - imin);
77 }
78
79 this->max_pts_ = *std::max_element(this->nb_pts_.begin(), this->nb_pts_.end());
80 this->min_pts_ = *std::min_element(this->nb_pts_.begin(), this->nb_pts_.end());
81
82 ATLAS_ASSERT(detectOverflow(gridsize, nb_partitions_Int_, blocksize_) == false);
83 }
84
85 template <typename Int>
86 bool BandsDistribution<Int>::detectOverflow(size_t gridsize, size_t nb_partitions, size_t blocksize) {
87 int64_t size = gridsize;
88 int64_t iblock = (size - 1) / int64_t(blocksize);
89 int64_t intermediate_product = iblock * int64_t(nb_partitions);
90 ATLAS_ASSERT(intermediate_product > 0, "Even 64 bits is insufficient to prevent overflow.");
91 if (intermediate_product > std::numeric_limits<Int>::max()) {
92 return true;
93 }
94 return false;
95 }
96
97
98 template class BandsDistribution<int32_t>;
99 template class BandsDistribution<int64_t>;
100
101 } // namespace distribution
102 } // namespace detail
103 } // namespace grid
104 } // namespace atlas