| CUTLASS CUDA Templates for Linear Algebra Subroutines and Solvers

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

coord.h
Go to the documentation of this file.
1 /***************************************************************************************************
2 * Copyright (c) 2017-2019, NVIDIA CORPORATION. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are permitted
5 * provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright notice, this list of
7 * conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright notice, this list of
9 * conditions and the following disclaimer in the documentation and/or other materials
10 * provided with the distribution.
11 * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12 * to endorse or promote products derived from this software without specific prior written
13 * permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21 * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 **************************************************************************************************/
29 #pragma once
30
31 #include "cutlass/cutlass.h"
32
33 namespace cutlass {
34
36
38 template <
39 int Rank_,
40 typename Index_ = int,
41 typename LongIndex_ = int64_t
42 >
43 struct Coord {
44
45 public:
46
47 //
48 // Type and constant definitions
49 //
50
52 static int const kRank = Rank_;
53
55 using Index = Index_;
56
58 using LongIndex = LongIndex_;
59
60 private:
61
62 //
63 // Data members
64 //
65
67 Index idx[kRank];
68
69 public:
70
71 //
72 // Methods
73 //
74
76 CUTLASS_HOST_DEVICE
77 explicit Coord(Index value = Index(0)) {
78 for (int i = 0; i < kRank; ++i) {
79 idx[i] = value;
80 }
81 }
82
84 CUTLASS_HOST_DEVICE
85 Coord(Index const (&_idx)[kRank]) {
86 for (int i = 0; i < kRank; ++i) {
87 idx[i] = _idx[i];
88 }
89 }
90
92 CUTLASS_HOST_DEVICE
93 Coord(Coord<kRank, Index, LongIndex> const &coord) {
94 for (int i = 0; i < kRank; ++i) {
95 idx[i] = coord[i];
96 }
97 }
98
101 template <int Slice>
102 CUTLASS_HOST_DEVICE
103 Coord<Slice> slice(int start = 0, Index identity = 0) const {
104 Coord<Slice> result;
105 for (int i = 0; i < Slice; ++i) {
106 if (i + start < kRank) {
107 result[i] = idx[i + start];
108 }
109 else {
110 result[i] = identity;
111 }
112 }
113 return result;
114 }
115
117 CUTLASS_HOST_DEVICE
118 int min_dim_index() const {
119 int i = 0;
120 for (int j = 1; j < kRank; ++j) {
121 if (idx[j] < idx[i]) {
122 i = j;
123 }
124 }
125 return i;
126 }
127
129 CUTLASS_HOST_DEVICE
130 int max_dim_index() const {
131 int i = 0;
132 for (int j = 1; j < kRank; ++j) {
133 if (idx[j] > idx[i]) {
134 i = j;
135 }
136 }
137 return i;
138 }
139
141 CUTLASS_HOST_DEVICE
142 explicit operator bool() const {
143 for (int i = 0; i < kRank; ++i) {
144 if (idx[i]) {
145 return true;
146 }
147 }
148 return false;
149 }
150
152 CUTLASS_HOST_DEVICE
153 bool operator!() const {
154 for (int i = 0; i < kRank; ++i) {
155 if (idx[i]) {
156 return false;
157 }
158 }
159 return true;
160 }
161
163 CUTLASS_HOST_DEVICE
164 Coord operator+(Coord const& b) const {
165 Coord c;
166 for (int i = 0; i < kRank; ++i) {
167 c.idx[i] = idx[i] + b.idx[i];
168 }
169 return c;
170 }
171
173 CUTLASS_HOST_DEVICE
174 Coord operator-(Coord const& b) const {
175 Coord c;
176 for (int i = 0; i < kRank; ++i) {
177 c.idx[i] = idx[i] - b.idx[i];
178 }
179 return c;
180 }
181
183 CUTLASS_HOST_DEVICE
184 Coord operator*(Coord const& b) const {
185 Coord c;
186 for (int i = 0; i < kRank; ++i) {
187 c.idx[i] = idx[i] * b.idx[i];
188 }
189 return c;
190 }
191
193 CUTLASS_HOST_DEVICE
194 Coord operator/(Coord const& b) const {
195 Coord c;
196 for (int i = 0; i < kRank; ++i) {
197 c.idx[i] = idx[i] / b.idx[i];
198 }
199 return c;
200 }
201
203 CUTLASS_HOST_DEVICE
204 Coord& operator+=(Coord const& b) {
205 for (int i = 0; i < kRank; ++i) {
206 idx[i] += b.idx[i];
207 }
208 return *this;
209 }
210
212 CUTLASS_HOST_DEVICE
213 Coord& operator-=(Coord const& b) {
214 for (int i = 0; i < kRank; ++i) {
215 idx[i] -= b.idx[i];
216 }
217 return *this;
218 }
219
221 CUTLASS_HOST_DEVICE
222 Coord& operator*=(Coord const& b) {
223 for (int i = 0; i < kRank; ++i) {
224 idx[i] *= b.idx[i];
225 }
226 return *this;
227 }
228
230 CUTLASS_HOST_DEVICE
231 Coord& operator/=(Coord const& b) {
232 for (int i = 0; i < kRank; ++i) {
233 idx[i] /= b.idx[i];
234 }
235 return *this;
236 }
237
239 CUTLASS_HOST_DEVICE Index& operator[](int dim) { return idx[dim]; }
240
242 CUTLASS_HOST_DEVICE Index const& operator[](int dim) const { return idx[dim]; }
243
245 CUTLASS_HOST_DEVICE
246 LongIndex dot(Coord const& b, LongIndex sum = LongIndex(0)) const {
247 for (int i = 0; i < kRank; ++i) {
248 sum += idx[i] * b.idx[i];
249 }
250 return sum;
251 }
252
254 template <int Dim>
255 CUTLASS_HOST_DEVICE Index& at() {
256 return idx[Dim];
257 }
258
260 CUTLASS_HOST_DEVICE
261 Index& at(int dim) { return idx[dim]; }
262
264 template <int Dim>
265 CUTLASS_HOST_DEVICE Index const& at() const {
266 return idx[Dim];
267 }
268
270 CUTLASS_HOST_DEVICE
271 Index const& at(int dim) const { return idx[dim]; }
272
274 CUTLASS_HOST_DEVICE
275 bool operator==(Coord const& b) const {
276 bool equal = true;
277 for (int i = 0; equal && i < kRank; ++i) {
278 equal = (idx[i] == b.idx[i]);
279 }
280 return equal;
281 }
282
284 CUTLASS_HOST_DEVICE
285 bool operator!=(Coord const& b) const { return !(*this == b); }
286
288 CUTLASS_HOST_DEVICE
289 Coord& clamp(Coord const& max, Coord const& min = Coord()) {
290 for (int i = 0; i < kRank; ++i) {
291 idx[i] = __NV_STD_MAX(__NV_STD_MIN(idx[i], max.idx[i]), min.idx[i]);
292 }
293 return *this;
294 }
295
297 CUTLASS_HOST_DEVICE
298 Index sum() const {
299 Index sum_(idx[0]);
300 for (int i = 1; i < kRank; ++i) {
301 sum_ += idx[i];
302 }
303 return sum_;
304 }
305
307 CUTLASS_HOST_DEVICE
308 LongIndex product() const {
309 LongIndex product_(idx[0]);
310 for (int i = 1; i < kRank; ++i) {
311 product_ *= idx[i];
312 }
313 return product_;
314 }
315
317 CUTLASS_HOST_DEVICE
318 bool operator<(Coord const &b) const {
319 for (int i = 0; i < kRank; ++i) {
320 if (!(idx[i] < b[i])) {
321 return false;
322 }
323 }
324 return true;
325 }
326
328 CUTLASS_HOST_DEVICE
329 bool operator<=(Coord const &b) const {
330 for (int i = 0; i < kRank; ++i) {
331 if (!(idx[i] <= b[i])) {
332 return false;
333 }
334 }
335 return true;
336 }
337
339 CUTLASS_HOST_DEVICE
340 bool operator>(Coord const &b) const {
341 return !(*this <= b);
342 }
343
345 CUTLASS_HOST_DEVICE
346 bool operator>=(Coord const &b) const {
347 return !(*this < b);
348 }
349 };
350
351 } // namespace cutlass
352
354
355 namespace cutlass {
356
358 template <int Rank, typename Index>
359 CUTLASS_HOST_DEVICE
360 Coord<Rank, Index> operator/(Index s, Coord<Rank, Index> coord) {
361 CUTLASS_PRAGMA_UNROLL
362 for (int i = 0; i < Rank; ++i) {
363 coord[i] = s / coord[i];
364 }
365 return coord;
366 }
367
369 template <int Rank, typename Index>
370 CUTLASS_HOST_DEVICE
371 Coord<Rank, Index> operator/(Coord<Rank, Index> coord, Index s) {
372 CUTLASS_PRAGMA_UNROLL
373 for (int i = 0; i < Rank; ++i) {
374 coord[i] /= s;
375 }
376 return coord;
377 }
378
380 //
381 // Integer-valued make_Coord
382 //
384
386 CUTLASS_HOST_DEVICE
387 Coord<1> make_Coord(int _0) {
388 int values[1] = {_0};
389 return Coord<1>(values);
390 }
391
393 CUTLASS_HOST_DEVICE
394 Coord<2> make_Coord(int _0, int _1) {
395 int values[2] = {_0, _1};
396 return Coord<2>(values);
397 }
398
400 CUTLASS_HOST_DEVICE
401 Coord<3> make_Coord(int _0, int _1, int _2) {
402 int values[3] = {_0, _1, _2};
403 return Coord<3>(values);
404 }
405
407 CUTLASS_HOST_DEVICE
408 Coord<4> make_Coord(int _0, int _1, int _2, int _3) {
409 int values[4] = {_0, _1, _2, _3};
410 return Coord<4>(values);
411 }
412
414
415 } // namespace cutlass
cutlass::Coord::operator-
CUTLASS_HOST_DEVICE Coord operator-(Coord const &b) const
Element-wise subtraction.
Definition: coord.h:174
cutlass::Coord::min_dim_index
CUTLASS_HOST_DEVICE int min_dim_index() const
Returns the index of the dimension with least value.
Definition: coord.h:118
cutlass::platform::max
CUTLASS_HOST_DEVICE constexpr const T & max(const T &a, const T &b)
std::max
Definition: platform.h:189
cutlass::Coord::at
CUTLASS_HOST_DEVICE Index const & at() const
Gets the index of a given Coord element.
Definition: coord.h:265
cutlass::Coord::kRank
static int const kRank
Number of elements in Coord.
Definition: coord.h:52
cutlass::Coord::operator==
CUTLASS_HOST_DEVICE bool operator==(Coord const &b) const
Determines if two Coord<> objects are equal.
Definition: coord.h:275
cutlass
Definition: aligned_buffer.h:35
cutlass::Coord::operator+
CUTLASS_HOST_DEVICE Coord operator+(Coord const &b) const
Element-wise addition.
Definition: coord.h:164
cutlass::Coord::operator[]
CUTLASS_HOST_DEVICE Index const & operator[](int dim) const
Member access operator.
Definition: coord.h:242
cutlass::Coord::Coord
CUTLASS_HOST_DEVICE Coord(Index value=Index(0))
Default ctor initializes uniformly.
Definition: coord.h:77
cutlass::Coord::operator!
CUTLASS_HOST_DEVICE bool operator!() const
Returns true if Coord is uniformly zero.
Definition: coord.h:153
cutlass::Coord::operator*=
CUTLASS_HOST_DEVICE Coord & operator*=(Coord const &b)
In-place multiplication.
Definition: coord.h:222
cutlass::make_Coord
CUTLASS_HOST_DEVICE Coord< 1 > make_Coord(int _0)
Helper to make a 2-element coordinate.
Definition: coord.h:387
cutlass::Coord::clamp
CUTLASS_HOST_DEVICE Coord & clamp(Coord const &max, Coord const &min=Coord())
Clamps a coordinate to a range specified by maximum and minimum values.
Definition: coord.h:289
cutlass::Coord::operator<
CUTLASS_HOST_DEVICE bool operator<(Coord const &b) const
Less than operator.
Definition: coord.h:318
cutlass::Coord::operator!=
CUTLASS_HOST_DEVICE bool operator!=(Coord const &b) const
Not equal.
Definition: coord.h:285
cutlass::Coord::operator-=
CUTLASS_HOST_DEVICE Coord & operator-=(Coord const &b)
In-place subtraction.
Definition: coord.h:213
cutlass::Coord::at
CUTLASS_HOST_DEVICE Index & at(int dim)
Access via index; may limit unrolling potential.
Definition: coord.h:261
cutlass::Coord::Coord
CUTLASS_HOST_DEVICE Coord(Coord< kRank, Index, LongIndex > const &coord)
Copy constructor.
Definition: coord.h:93
cutlass::Coord< 4 >::Index
int Index
Index type used to store elements.
Definition: coord.h:55
CUTLASS_PRAGMA_UNROLL
#define CUTLASS_PRAGMA_UNROLL
Definition: cutlass.h:110
__NV_STD_MAX
#define __NV_STD_MAX(a, b)
Select maximum(a, b)
Definition: platform.h:163
cutlass::Coord::product
CUTLASS_HOST_DEVICE LongIndex product() const
Returns the product of all elements.
Definition: coord.h:308
cutlass::Coord::operator[]
CUTLASS_HOST_DEVICE Index & operator[](int dim)
Member access operator.
Definition: coord.h:239
__NV_STD_MIN
#define __NV_STD_MIN(a, b)
Select minimum(a, b)
Definition: platform.h:168
cutlass::Coord::operator<=
CUTLASS_HOST_DEVICE bool operator<=(Coord const &b) const
Less than or equals operator.
Definition: coord.h:329
cutlass::Coord::operator>
CUTLASS_HOST_DEVICE bool operator>(Coord const &b) const
Greater than operator.
Definition: coord.h:340
cutlass::Coord::slice
CUTLASS_HOST_DEVICE Coord< Slice > slice(int start=0, Index identity=0) const
Definition: coord.h:103
cutlass::Coord::operator*
CUTLASS_HOST_DEVICE Coord operator*(Coord const &b) const
Element-wise multiplication.
Definition: coord.h:184
cutlass::Coord::Coord
CUTLASS_HOST_DEVICE Coord(Index const (&_idx)[kRank])
Constructs from an array of integers.
Definition: coord.h:85
CUTLASS_HOST_DEVICE
#define CUTLASS_HOST_DEVICE
Definition: cutlass.h:89
cutlass::Coord::operator/=
CUTLASS_HOST_DEVICE Coord & operator/=(Coord const &b)
In-place division.
Definition: coord.h:231
cutlass::platform::min
CUTLASS_HOST_DEVICE constexpr const T & min(const T &a, const T &b)
std::min
Definition: platform.h:183
cutlass::Coord::operator/
CUTLASS_HOST_DEVICE Coord operator/(Coord const &b) const
Element-wise division.
Definition: coord.h:194
cutlass::Coord::sum
CUTLASS_HOST_DEVICE Index sum() const
Returns the sum of all elements.
Definition: coord.h:298
cutlass::Coord
Statically-sized array specifying Coords within a tensor.
Definition: coord.h:43
cutlass::Coord::at
CUTLASS_HOST_DEVICE Index const & at(int dim) const
Access via index; may limit unrolling potential.
Definition: coord.h:271
cutlass::Coord::operator+=
CUTLASS_HOST_DEVICE Coord & operator+=(Coord const &b)
In-place addition.
Definition: coord.h:204
cutlass::Coord< 4 >::LongIndex
int64_t LongIndex
Type used to represent linear offsets.
Definition: coord.h:58
cutlass::Coord::at
CUTLASS_HOST_DEVICE Index & at()
Gets the index of a given Coord element.
Definition: coord.h:255
cutlass::Coord::dot
CUTLASS_HOST_DEVICE LongIndex dot(Coord const &b, LongIndex sum=LongIndex(0)) const
Computes the dot product with anotherCoord object.
Definition: coord.h:246
cutlass::Coord::max_dim_index
CUTLASS_HOST_DEVICE int max_dim_index() const
Returns the index of the dimension with greatest value.
Definition: coord.h:130
cutlass.h
Basic include for CUTLASS.
cutlass::Coord::operator>=
CUTLASS_HOST_DEVICE bool operator>=(Coord const &b) const
Greater than or equals operator.
Definition: coord.h:346