Debian Codegraph @

src/atlas/array/ArraySpec.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 <algorithm>
12
13 #include "atlas/array/ArrayDataStore.h"
14 #include "atlas/library/config.h"
15 #include "atlas/runtime/Exception.h"
16
17 namespace atlas {
18 namespace array {
19
20 namespace {
21 size_t compute_aligned_size(size_t size, size_t alignment) {
22 size_t div = size / alignment;
23 size_t mod = size % alignment;
24 size_t _aligned_size = div * alignment;
25 if (mod > 0) {
26 _aligned_size += alignment;
27 }
28 return _aligned_size;
29 }
30 } // namespace
31
32 ArraySpec::ArraySpec():
33 size_(), rank_(), allocated_size_(), datatype_(DataType::KIND_REAL64), contiguous_(true), default_layout_(true) {}
34
35 ArraySpec::ArraySpec(const ArrayShape& shape): ArraySpec(shape, ArrayAlignment()) {}
36 ArraySpec::ArraySpec(DataType datatype, const ArrayShape& shape): ArraySpec(shape) {
37 datatype_ = datatype;
38 }
39
40 ArraySpec::ArraySpec(const ArrayShape& shape, const ArrayAlignment& alignment): datatype_(DataType::KIND_REAL64) {
41 ArrayShape aligned_shape = shape;
42 aligned_shape.back() = compute_aligned_size(aligned_shape.back(), size_t(alignment));
43
44 rank_ = static_cast<int>(shape.size());
45 size_ = 1;
46 allocated_size_ = 1;
47 shape_.resize(rank_);
48 strides_.resize(rank_);
49 layout_.resize(rank_);
50 device_strides_.resize(rank_);
51 device_strides_[rank_ - 1] = 1;
52 for (int j = rank_ - 1; j >= 0; --j) {
53 shape_[j] = shape[j];
54 strides_[j] = allocated_size_;
55 layout_[j] = j;
56 size_ *= size_t(shape_[j]);
57 allocated_size_ *= size_t(aligned_shape[j]);
58 if( j < rank_ - 1) {
59 // Assume contiguous device data!
60 device_strides_[j] = strides_[j+1] * shape[j+1];
61 }
62 }
63 ATLAS_ASSERT(allocated_size_ == compute_aligned_size(size_t(shape_[0]) * size_t(strides_[0]), size_t(alignment)));
64 contiguous_ = (size_ == allocated_size_);
65 default_layout_ = true;
66 alignment_ = alignment;
67
68 #ifdef ATLAS_HAVE_FORTRAN
69 allocate_fortran_specs();
70 #endif
71 };
72
73 ArraySpec::ArraySpec(DataType datatype, const ArrayShape& shape, const ArrayAlignment& alignment):
74 ArraySpec(shape, alignment) {
75 datatype_ = datatype;
76 }
77
78 ArraySpec::ArraySpec(const ArrayShape& shape, const ArrayStrides& strides):
79 ArraySpec(shape, strides, ArrayAlignment()) {}
80
81 ArraySpec::ArraySpec(const ArrayShape& shape, const ArrayStrides& strides, const ArrayAlignment& alignment):
82 datatype_(DataType::KIND_REAL64) {
83 ATLAS_ASSERT(shape.size() == strides.size(), "dimensions of shape and stride don't match");
84
85 rank_ = static_cast<int>(shape.size());
86 size_ = 1;
87 shape_.resize(rank_);
88 strides_.resize(rank_);
89 layout_.resize(rank_);
90 device_strides_.resize(rank_);
91 device_strides_[rank_ - 1] = strides[rank_ - 1];
92 for (int j = rank_ - 1; j >= 0; --j) {
93 shape_[j] = shape[j];
94 strides_[j] = strides[j];
95 layout_[j] = j;
96 size_ *= size_t(shape_[j]);
97 if( j < rank_ - 1) {
98 // Assume contiguous device data!
99 device_strides_[j] = device_strides_[j+1] * shape[j+1];
100 }
101 }
102 allocated_size_ = compute_aligned_size(size_t(shape_[0]) * size_t(strides_[0]), size_t(alignment));
103 contiguous_ = (size_ == allocated_size_);
104 default_layout_ = true;
105
106 #ifdef ATLAS_HAVE_FORTRAN
107 allocate_fortran_specs();
108 #endif
109 }
110
111 ArraySpec::ArraySpec(DataType datatype, const ArrayShape& shape, const ArrayStrides& strides,
112 const ArrayAlignment& alignment):
113 ArraySpec(shape, strides, alignment) {
114 datatype_ = datatype;
115 }
116
117 ArraySpec::ArraySpec(const ArrayShape& shape, const ArrayStrides& strides, const ArrayLayout& layout):
118 ArraySpec(shape, strides, layout, ArrayAlignment()) {}
119
120 ArraySpec::ArraySpec(DataType datatype, const ArrayShape& shape, const ArrayStrides& strides,
121 const ArrayLayout& layout):
122 ArraySpec(shape, strides, layout) {
123 datatype_ = datatype;
124 }
125
126 ArraySpec::ArraySpec(const ArrayShape& shape, const ArrayStrides& strides, const ArrayLayout& layout,
127 const ArrayAlignment& alignment):
128 datatype_(DataType::KIND_REAL64) {
129 ATLAS_ASSERT(shape.size() == strides.size(), "dimensions of shape and stride don't match");
130
131 rank_ = static_cast<int>(shape.size());
132 size_ = 1;
133 shape_.resize(rank_);
134 strides_.resize(rank_);
135 layout_.resize(rank_);
136 device_strides_.resize(rank_);
137 device_strides_[rank_ - 1] = strides[rank_ - 1];
138 default_layout_ = true;
139 for (int j = rank_ - 1; j >= 0; --j) {
140 shape_[j] = shape[j];
141 strides_[j] = strides[j];
142 layout_[j] = layout[j];
143 size_ *= size_t(shape_[j]);
144 if (layout_[j] != idx_t(j)) {
145 default_layout_ = false;
146 }
147 if( j < rank_ - 1) {
148 // Assume contiguous device data!
149 device_strides_[j] = device_strides_[j+1] * shape[j+1];
150 }
151 }
152 allocated_size_ = compute_aligned_size(size_t(shape_[layout_[0]]) * size_t(strides_[layout_[0]]), size_t(alignment));
153 contiguous_ = (size_ == allocated_size_);
154
155 #ifdef ATLAS_HAVE_FORTRAN
156 allocate_fortran_specs();
157 #endif
158 }
159
160 ArraySpec::ArraySpec(DataType datatype, const ArrayShape& shape, const ArrayStrides& strides, const ArrayLayout& layout,
161 const ArrayAlignment& alignment):
162 ArraySpec(shape, strides, layout, alignment) {
163 datatype_ = datatype;
164 }
165 const std::vector<int>& ArraySpec::shapef() const {
166 return shapef_;
167 }
168
169 const std::vector<int>& ArraySpec::stridesf() const {
170 return stridesf_;
171 }
172
173 const std::vector<int>& ArraySpec::device_stridesf() const {
174 return device_stridesf_;
175 }
176
177 void ArraySpec::allocate_fortran_specs() {
178 shapef_.resize(rank_);
179 stridesf_.resize(rank_);
180 device_stridesf_.resize(rank_);
181 for (idx_t j = 0; j < rank_; ++j) {
182 shapef_[j] = shape_[rank_ - 1 - layout_[j]];
183 stridesf_[j] = strides_[rank_ - 1 - layout_[j]];
184 device_stridesf_[j] = device_strides_[rank_ - 1 - j];
185 }
186 }
187
188
189 static thread_local std::string label_;
190
191 std::string_view label::get() {
192 return label_;
193 }
194
195 void label::set(std::string_view s) {
196 label_.assign(s.data(),s.size());
197 }
198
199 } // namespace array
200 } // namespace atlas