docs/coord_8h_source.html
| | CUTLASS
CUDA Templates for Linear Algebra Subroutines and Solvers |
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 <
39int Rank_,
40typename Index_ = int,
41typename LongIndex_ = int64_t
42 >
44
45 public:
46
47//
48// Type and constant definitions
49//
50
52static int const kRank = Rank_;
53
56
58using LongIndex = LongIndex_;
59
60 private:
61
62//
63// Data members
64//
65
68
69 public:
70
71//
72// Methods
73//
74
77explicit Coord(Index value = Index(0)) {
78for (int i = 0; i < kRank; ++i) {
79 idx[i] = value;
80 }
81 }
82
85Coord(Index const (&_idx)[kRank]) {
86for (int i = 0; i < kRank; ++i) {
87 idx[i] = _idx[i];
88 }
89 }
90
93Coord(Coord<kRank, Index, LongIndex> const &coord) {
94for (int i = 0; i < kRank; ++i) {
95 idx[i] = coord[i];
96 }
97 }
98
101template <int Slice>
103Coord<Slice> slice(int start = 0, Index identity = 0) const {
104Coord<Slice> result;
105for (int i = 0; i < Slice; ++i) {
106if (i + start < kRank) {
107 result[i] = idx[i + start];
108 }
109else {
110 result[i] = identity;
111 }
112 }
113return result;
114 }
115
118int min_dim_index() const {
119int i = 0;
120for (int j = 1; j < kRank; ++j) {
121if (idx[j] < idx[i]) {
122 i = j;
123 }
124 }
125return i;
126 }
127
130int max_dim_index() const {
131int i = 0;
132for (int j = 1; j < kRank; ++j) {
133if (idx[j] > idx[i]) {
134 i = j;
135 }
136 }
137return i;
138 }
139
142explicit operator bool() const {
143for (int i = 0; i < kRank; ++i) {
144if (idx[i]) {
145return true;
146 }
147 }
148return false;
149 }
150
154for (int i = 0; i < kRank; ++i) {
155if (idx[i]) {
156return false;
157 }
158 }
159return true;
160 }
161
164Coord operator+(Coord const& b) const {
165Coord c;
166for (int i = 0; i < kRank; ++i) {
167 c.idx[i] = idx[i] + b.idx[i];
168 }
169return c;
170 }
171
174Coord operator-(Coord const& b) const {
175Coord c;
176for (int i = 0; i < kRank; ++i) {
177 c.idx[i] = idx[i] - b.idx[i];
178 }
179return c;
180 }
181
184Coord operator*(Coord const& b) const {
185Coord c;
186for (int i = 0; i < kRank; ++i) {
187 c.idx[i] = idx[i] * b.idx[i];
188 }
189return c;
190 }
191
194Coord operator/(Coord const& b) const {
195Coord c;
196for (int i = 0; i < kRank; ++i) {
197 c.idx[i] = idx[i] / b.idx[i];
198 }
199return c;
200 }
201
204Coord& operator+=(Coord const& b) {
205for (int i = 0; i < kRank; ++i) {
206 idx[i] += b.idx[i];
207 }
208return *this;
209 }
210
213Coord& operator-=(Coord const& b) {
214for (int i = 0; i < kRank; ++i) {
215 idx[i] -= b.idx[i];
216 }
217return *this;
218 }
219
222Coord& operator*=(Coord const& b) {
223for (int i = 0; i < kRank; ++i) {
224 idx[i] *= b.idx[i];
225 }
226return *this;
227 }
228
231Coord& operator/=(Coord const& b) {
232for (int i = 0; i < kRank; ++i) {
233 idx[i] /= b.idx[i];
234 }
235return *this;
236 }
237
239CUTLASS_HOST_DEVICE Index& operator[](int dim) { return idx[dim]; }
240
242CUTLASS_HOST_DEVICE Index const& operator[](int dim) const { return idx[dim]; }
243
246LongIndex dot(Coord const& b, LongIndex sum = LongIndex(0)) const {
247for (int i = 0; i < kRank; ++i) {
248sum += idx[i] * b.idx[i];
249 }
250return sum;
251 }
252
254template <int Dim>
255CUTLASS_HOST_DEVICE Index& at() {
256return idx[Dim];
257 }
258
261Index& at(int dim) { return idx[dim]; }
262
264template <int Dim>
265CUTLASS_HOST_DEVICE Index const& at() const {
266return idx[Dim];
267 }
268
271Index const& at(int dim) const { return idx[dim]; }
272
275bool operator==(Coord const& b) const {
276bool equal = true;
277for (int i = 0; equal && i < kRank; ++i) {
278 equal = (idx[i] == b.idx[i]);
279 }
280return equal;
281 }
282
285bool operator!=(Coord const& b) const { return !(*this == b); }
286
289Coord& clamp(Coord const& max, Coord const& min = Coord()) {
290for (int i = 0; i < kRank; ++i) {
291 idx[i] = __NV_STD_MAX(__NV_STD_MIN(idx[i], max.idx[i]), min.idx[i]);
292 }
293return *this;
294 }
295
299Index sum_(idx[0]);
300for (int i = 1; i < kRank; ++i) {
301 sum_ += idx[i];
302 }
303return sum_;
304 }
305
308LongIndex product() const {
309LongIndex product_(idx[0]);
310for (int i = 1; i < kRank; ++i) {
311 product_ *= idx[i];
312 }
313return product_;
314 }
315
318bool operator<(Coord const &b) const {
319for (int i = 0; i < kRank; ++i) {
320if (!(idx[i] < b[i])) {
321return false;
322 }
323 }
324return true;
325 }
326
329bool operator<=(Coord const &b) const {
330for (int i = 0; i < kRank; ++i) {
331if (!(idx[i] <= b[i])) {
332return false;
333 }
334 }
335return true;
336 }
337
340bool operator>(Coord const &b) const {
341return !(*this <= b);
342 }
343
346bool operator>=(Coord const &b) const {
347return !(*this < b);
348 }
349 };
350
351 } // namespace cutlass
352
354
355 namespace cutlass {
356
358 template <int Rank, typename Index>
360 Coord<Rank, Index> operator/(Index s, Coord<Rank, Index> coord) {
362for (int i = 0; i < Rank; ++i) {
363 coord[i] = s / coord[i];
364 }
365return coord;
366 }
367
369 template <int Rank, typename Index>
371 Coord<Rank, Index> operator/(Coord<Rank, Index> coord, Index s) {
373for (int i = 0; i < Rank; ++i) {
374 coord[i] /= s;
375 }
376return coord;
377 }
378
380 //
381 // Integer-valued make_Coord
382 //
384
387 Coord<1> make_Coord(int _0) {
388int values[1] = {_0};
389return Coord<1>(values);
390 }
391
394 Coord<2> make_Coord(int _0, int _1) {
395int values[2] = {_0, _1};
396return Coord<2>(values);
397 }
398
401 Coord<3> make_Coord(int _0, int _1, int _2) {
402int values[3] = {_0, _1, _2};
403return Coord<3>(values);
404 }
405
408 Coord<4> make_Coord(int _0, int _1, int _2, int _3) {
409int values[4] = {_0, _1, _2, _3};
410return Coord<4>(values);
411 }
412
414
415 } // namespace cutlass
CUTLASS_HOST_DEVICE Coord operator-(Coord const &b) const
Element-wise subtraction.
Definition: coord.h:174
CUTLASS_HOST_DEVICE int min_dim_index() const
Returns the index of the dimension with least value.
Definition: coord.h:118
CUTLASS_HOST_DEVICE constexpr const T & max(const T &a, const T &b)
std::max
Definition: platform.h:189
CUTLASS_HOST_DEVICE Index const & at() const
Gets the index of a given Coord element.
Definition: coord.h:265
static int const kRank
Number of elements in Coord.
Definition: coord.h:52
CUTLASS_HOST_DEVICE bool operator==(Coord const &b) const
Determines if two Coord<> objects are equal.
Definition: coord.h:275
Definition: aligned_buffer.h:35
CUTLASS_HOST_DEVICE Coord operator+(Coord const &b) const
Element-wise addition.
Definition: coord.h:164
CUTLASS_HOST_DEVICE Index const & operator[](int dim) const
Member access operator.
Definition: coord.h:242
CUTLASS_HOST_DEVICE Coord(Index value=Index(0))
Default ctor initializes uniformly.
Definition: coord.h:77
CUTLASS_HOST_DEVICE bool operator!() const
Returns true if Coord is uniformly zero.
Definition: coord.h:153
CUTLASS_HOST_DEVICE Coord & operator*=(Coord const &b)
In-place multiplication.
Definition: coord.h:222
CUTLASS_HOST_DEVICE Coord< 1 > make_Coord(int _0)
Helper to make a 2-element coordinate.
Definition: coord.h:387
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_HOST_DEVICE bool operator<(Coord const &b) const
Less than operator.
Definition: coord.h:318
CUTLASS_HOST_DEVICE bool operator!=(Coord const &b) const
Not equal.
Definition: coord.h:285
CUTLASS_HOST_DEVICE Coord & operator-=(Coord const &b)
In-place subtraction.
Definition: coord.h:213
CUTLASS_HOST_DEVICE Index & at(int dim)
Access via index; may limit unrolling potential.
Definition: coord.h:261
CUTLASS_HOST_DEVICE Coord(Coord< kRank, Index, LongIndex > const &coord)
Copy constructor.
Definition: coord.h:93
int Index
Index type used to store elements.
Definition: coord.h:55
#define CUTLASS_PRAGMA_UNROLL
Definition: cutlass.h:110
#define __NV_STD_MAX(a, b)
Select maximum(a, b)
Definition: platform.h:163
CUTLASS_HOST_DEVICE LongIndex product() const
Returns the product of all elements.
Definition: coord.h:308
CUTLASS_HOST_DEVICE Index & operator[](int dim)
Member access operator.
Definition: coord.h:239
#define __NV_STD_MIN(a, b)
Select minimum(a, b)
Definition: platform.h:168
CUTLASS_HOST_DEVICE bool operator<=(Coord const &b) const
Less than or equals operator.
Definition: coord.h:329
CUTLASS_HOST_DEVICE bool operator>(Coord const &b) const
Greater than operator.
Definition: coord.h:340
CUTLASS_HOST_DEVICE Coord< Slice > slice(int start=0, Index identity=0) const
Definition: coord.h:103
CUTLASS_HOST_DEVICE Coord operator*(Coord const &b) const
Element-wise multiplication.
Definition: coord.h:184
CUTLASS_HOST_DEVICE Coord(Index const (&_idx)[kRank])
Constructs from an array of integers.
Definition: coord.h:85
#define CUTLASS_HOST_DEVICE
Definition: cutlass.h:89
CUTLASS_HOST_DEVICE Coord & operator/=(Coord const &b)
In-place division.
Definition: coord.h:231
CUTLASS_HOST_DEVICE constexpr const T & min(const T &a, const T &b)
std::min
Definition: platform.h:183
CUTLASS_HOST_DEVICE Coord operator/(Coord const &b) const
Element-wise division.
Definition: coord.h:194
CUTLASS_HOST_DEVICE Index sum() const
Returns the sum of all elements.
Definition: coord.h:298
Statically-sized array specifying Coords within a tensor.
Definition: coord.h:43
CUTLASS_HOST_DEVICE Index const & at(int dim) const
Access via index; may limit unrolling potential.
Definition: coord.h:271
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_HOST_DEVICE Index & at()
Gets the index of a given Coord element.
Definition: coord.h:255
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_HOST_DEVICE int max_dim_index() const
Returns the index of the dimension with greatest value.
Definition: coord.h:130
Basic include for CUTLASS.
CUTLASS_HOST_DEVICE bool operator>=(Coord const &b) const
Greater than or equals operator.
Definition: coord.h:346
Generated by 1.8.11