CUTLASS
CUDA Templates for Linear Algebra Subroutines and Solvers
array.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  **************************************************************************************************/
30 #pragma once
31 #include "cutlass/cutlass.h"
32 #include "cutlass/numeric_types.h"
33 
34 namespace cutlass {
35 
37 
39 template <
40  typename T,
41  int N,
42  bool RegisterSized = sizeof_bits<T>::value >= 32
43 >
44 class Array;
45 
47 
49 template <typename T, int N, bool RegisterSized>
50 struct sizeof_bits<Array<T, N, RegisterSized> > {
51  static int const value =
52  sizeof(typename Array<T, N, RegisterSized>::Storage) * 8 * Array<T, N, RegisterSized>::kStorageElements;
53 };
54 
56 
59 constexpr bool ispow2(unsigned x) {
60  return !(x & (x - 1));
61 }
62 
64 
67 constexpr unsigned floor_pow_2(unsigned x) {
68  return ispow2(x) ? x : floor_pow_2(x >> 1);
69 }
70 
72 
74 template <
75  typename T,
76  int N
77 >
78 class Array<T, N, true> {
79 public:
80 
82  using Storage = T;
83 
85  using Element = T;
86 
88  //static std::size_t const kStorageElements = N;
89  static size_t const kStorageElements = N;
90 
92  static size_t const kElements = N;
93 
94  //
95  // C++ standard members
96  //
97 
98  typedef T value_type;
99  typedef size_t size_type;
100  typedef ptrdiff_t difference_type;
101  typedef value_type &reference;
102  typedef value_type const & const_reference;
103  typedef value_type *pointer;
104  typedef value_type const * const_pointer;
105 
106  //
107  // Iterators
108  //
109 
111  class iterator {
112 
114  T *ptr_;
115 
116  public:
117 
119  iterator(): ptr_(nullptr) { }
120 
122  iterator(T *_ptr): ptr_(_ptr) { }
123 
125  iterator &operator++() {
126  ++ptr_;
127  return *this;
128  }
129 
131  iterator &operator--() {
132  --ptr_;
133  return *this;
134  }
135 
137  iterator operator++(int) {
138  iterator ret(*this);
139  ++ptr_;
140  return ret;
141  }
142 
144  iterator operator--(int) {
145  iterator ret(*this);
146  --ptr_;
147  return ret;
148  }
149 
151  T &operator*() const {
152  return *ptr_;
153  }
154 
156  bool operator==(iterator const &other) const {
157  return ptr_ == other.ptr_;
158  }
159 
161  bool operator!=(iterator const &other) const {
162  return ptr_ != other.ptr_;
163  }
164  };
165 
167  class const_iterator {
168 
170  T *ptr_;
171 
172  public:
173 
175  const_iterator(): ptr_(nullptr) { }
176 
178  const_iterator(T const *_ptr): ptr_(_ptr) { }
179 
181  const_iterator &operator++() {
182  ++ptr_;
183  return *this;
184  }
185 
187  const_iterator &operator--() {
188  --ptr_;
189  return *this;
190  }
191 
193  const_iterator operator++(int) {
194  const_iterator ret(*this);
195  ++ptr_;
196  return ret;
197  }
198 
200  const_iterator operator--(int) {
201  const_iterator ret(*this);
202  --ptr_;
203  return ret;
204  }
205 
207  T const &operator*() const {
208  return *ptr_;
209  }
210 
212  bool operator==(const_iterator const &other) const {
213  return ptr_ == other.ptr_;
214  }
215 
217  bool operator!=(const_iterator const &other) const {
218  return ptr_ != other.ptr_;
219  }
220  };
221 
223  class reverse_iterator {
224 
226  T *ptr_;
227 
228  public:
229 
232 
234  reverse_iterator(T *_ptr): ptr_(_ptr) { }
235 
237  reverse_iterator &operator++() {
238  --ptr_;
239  return *this;
240  }
241 
243  reverse_iterator &operator--() {
244  ++ptr_;
245  return *this;
246  }
247 
249  reverse_iterator operator++(int) {
250  iterator ret(*this);
251  --ptr_;
252  return ret;
253  }
254 
256  reverse_iterator operator--(int) {
257  iterator ret(*this);
258  ++ptr_;
259  return ret;
260  }
261 
263  T &operator*() const {
264  return *(ptr_ - 1);
265  }
266 
268  bool operator==(reverse_iterator const &other) const {
269  return ptr_ == other.ptr_;
270  }
271 
273  bool operator!=(reverse_iterator const &other) const {
274  return ptr_ != other.ptr_;
275  }
276  };
277 
279  class const_reverse_iterator {
280 
282  T const *ptr_;
283 
284  public:
285 
288 
290  const_reverse_iterator(T const *_ptr): ptr_(_ptr) { }
291 
293  const_reverse_iterator &operator++() {
294  --ptr_;
295  return *this;
296  }
297 
299  const_reverse_iterator &operator--() {
300  ++ptr_;
301  return *this;
302  }
303 
305  const_reverse_iterator operator++(int) {
306  const_reverse_iterator ret(*this);
307  --ptr_;
308  return ret;
309  }
310 
312  const_reverse_iterator operator--(int) {
313  const_reverse_iterator ret(*this);
314  ++ptr_;
315  return ret;
316  }
317 
319  T const &operator*() const {
320  return *(ptr_ - 1);
321  }
322 
324  bool operator==(const_iterator const &other) const {
325  return ptr_ == other.ptr_;
326  }
327 
329  bool operator!=(const_iterator const &other) const {
330  return ptr_ != other.ptr_;
331  }
332  };
333 
334 private:
335 
337  Storage storage[kElements];
338 
339 public:
340 
342  Array() { }
343 
345  Array(Array const &x) {
347  for (int i = 0; i < kElements; ++i) {
348  storage[i] = x.storage[i];
349  }
350  }
351 
354  void clear() {
355  fill(T(0));
356  }
357 
359  reference at(size_type pos) {
360  return reinterpret_cast<reference>(storage[pos]);
361  }
362 
364  const_reference at(size_type pos) const {
365  return reinterpret_cast<const_reference>(storage[pos]);
366  }
367 
369  reference operator[](size_type pos) {
370  return reinterpret_cast<reference>(storage[pos]);
371  }
372 
374  const_reference operator[](size_type pos) const {
375  return reinterpret_cast<const_reference>(storage[pos]);
376  }
377 
379  reference front() {
380  return reinterpret_cast<reference>(storage[0]);
381  }
382 
384  const_reference front() const {
385  return reinterpret_cast<const_reference>(storage[0]);
386  }
387 
389  reference back() {
390  return reinterpret_cast<reference>(storage[kStorageElements - 1]);
391  }
392 
394  const_reference back() const {
395  return reinterpret_cast<const_reference>(storage[kStorageElements - 1]);
396  }
397 
399  pointer data() {
400  return reinterpret_cast<pointer>(storage);
401  }
402 
404  const_pointer data() const {
405  return reinterpret_cast<const_pointer>(storage);
406  }
407 
409  pointer raw_data() {
410  return reinterpret_cast<pointer>(storage);
411  }
412 
414  const_pointer raw_data() const {
415  return reinterpret_cast<const_pointer>(storage);
416  }
417 
418 
420  constexpr bool empty() const {
421  return !kElements;
422  }
423 
425  constexpr size_type size() const {
426  return kElements;
427  }
428 
430  constexpr size_type max_size() const {
431  return kElements;
432  }
433 
435  void fill(T const &value) {
437  for (int i = 0; i < kElements; ++i) {
438  storage[i] = static_cast<Storage>(value);
439  }
440  }
441 
443  iterator begin() {
444  return iterator(storage);
445  }
446 
448  const_iterator cbegin() const {
449  return const_iterator(storage);
450  }
451 
453  iterator end() {
454  return iterator(reinterpret_cast<pointer>(storage + kStorageElements));
455  }
456 
458  const_iterator cend() const {
459  return const_iterator(reinterpret_cast<const_pointer>(storage + kStorageElements));
460  }
461 
463  reverse_iterator rbegin() {
464  return reverse_iterator(reinterpret_cast<pointer>(storage + kStorageElements));
465  }
466 
468  const_reverse_iterator crbegin() const {
469  return const_reverse_iterator(reinterpret_cast<const_pointer>(storage + kStorageElements));
470  }
471 
473  reverse_iterator rend() {
474  return reverse_iterator(reinterpret_cast<pointer>(storage));
475  }
476 
478  const_reverse_iterator crend() const {
479  return const_reverse_iterator(reinterpret_cast<const_pointer>(storage));
480  }
481 
482  //
483  // Comparison operators
484  //
485 
486 };
487 
489 
490 } // namespace cutlass
491 
493 
494 #include "cutlass/array_subbyte.h"
495 
497 
498 namespace cutlass {
499 
501 
503 template <
505  typename T,
507  int N,
509  int Alignment = sizeof_bits<T>::value * N / 8
510 >
511 class alignas(Alignment) AlignedArray: public Array<T, N> {
512 public:
513 
514 };
515 
517 
518 } // namespace cutlass
519 
521 
value_type * pointer
Definition: array.h:103
CUTLASS_HOST_DEVICE bool operator!=(iterator const &other) const
Definition: array.h:161
CUTLASS_HOST_DEVICE const_reverse_iterator()
Definition: array.h:287
CUTLASS_HOST_DEVICE const_iterator(T const *_ptr)
Definition: array.h:178
CUTLASS_HOST_DEVICE const_iterator()
Definition: array.h:175
CUTLASS_HOST_DEVICE constexpr bool empty() const
Definition: array.h:420
CUTLASS_HOST_DEVICE const_reverse_iterator crbegin() const
Definition: array.h:468
ptrdiff_t difference_type
Definition: array.h:100
CUTLASS_HOST_DEVICE reverse_iterator & operator--()
Definition: array.h:243
Definition: aligned_buffer.h:35
#define constexpr
Definition: platform.h:137
static int const value
Definition: numeric_types.h:43
CUTLASS_HOST_DEVICE const_iterator operator++(int)
Definition: array.h:193
CUTLASS_HOST_DEVICE void fill(T const &value)
Definition: array.h:435
Statically sized array of elements that accommodates all CUTLASS-supported numeric types and is safe ...
CUTLASS_HOST_DEVICE iterator end()
Definition: array.h:453
CUTLASS_HOST_DEVICE pointer data()
Definition: array.h:399
CUTLASS_HOST_DEVICE iterator begin()
Definition: array.h:443
CUTLASS_HOST_DEVICE reverse_iterator rbegin()
Definition: array.h:463
Aligned array type.
Definition: array.h:511
CUTLASS_HOST_DEVICE const_reference operator[](size_type pos) const
Definition: array.h:374
CUTLASS_HOST_DEVICE pointer raw_data()
Definition: array.h:409
CUTLASS_HOST_DEVICE reverse_iterator rend()
Definition: array.h:473
CUTLASS_HOST_DEVICE const_iterator cend() const
Definition: array.h:458
CUTLASS_HOST_DEVICE const_reverse_iterator(T const *_ptr)
Definition: array.h:290
CUTLASS_HOST_DEVICE reverse_iterator operator--(int)
Definition: array.h:256
size_t size_type
Definition: array.h:99
CUTLASS_HOST_DEVICE const_reverse_iterator & operator++()
Definition: array.h:293
CUTLASS_HOST_DEVICE constexpr size_type size() const
Definition: array.h:425
CUTLASS_HOST_DEVICE reference back()
Definition: array.h:389
CUTLASS_HOST_DEVICE bool operator!=(reverse_iterator const &other) const
Definition: array.h:273
CUTLASS_HOST_DEVICE reverse_iterator operator++(int)
Definition: array.h:249
CUTLASS_HOST_DEVICE bool operator!=(const_iterator const &other) const
Definition: array.h:217
CUTLASS_HOST_DEVICE bool operator!=(const_iterator const &other) const
Definition: array.h:329
#define CUTLASS_PRAGMA_UNROLL
Definition: cutlass.h:110
value_type const & const_reference
Definition: array.h:102
CUTLASS_HOST_DEVICE reverse_iterator & operator++()
Definition: array.h:237
CUTLASS_HOST_DEVICE T const & operator*() const
Definition: array.h:207
value_type & reference
Definition: array.h:101
CUTLASS_HOST_DEVICE bool operator==(iterator const &other) const
Definition: array.h:156
CUTLASS_HOST_DEVICE const_reverse_iterator crend() const
Definition: array.h:478
CUTLASS_HOST_DEVICE reverse_iterator()
Definition: array.h:231
CUTLASS_HOST_DEVICE T & operator*() const
Definition: array.h:151
Defines the size of an element in bits.
Definition: numeric_types.h:42
CUTLASS_HOST_DEVICE iterator operator++(int)
Definition: array.h:137
CUTLASS_HOST_DEVICE bool operator==(reverse_iterator const &other) const
Definition: array.h:268
#define nullptr
nullptr
Definition: platform.h:144
CUTLASS_HOST_DEVICE iterator & operator++()
Definition: array.h:125
T Storage
Storage type.
Definition: array.h:82
CUTLASS_HOST_DEVICE reverse_iterator(T *_ptr)
Definition: array.h:234
CUTLASS_HOST_DEVICE iterator(T *_ptr)
Definition: array.h:122
CUTLASS_HOST_DEVICE const_iterator & operator++()
Definition: array.h:181
CUTLASS_HOST_DEVICE const_reference back() const
Definition: array.h:394
CUTLASS_HOST_DEVICE iterator & operator--()
Definition: array.h:131
CUTLASS_HOST_DEVICE const_reference at(size_type pos) const
Definition: array.h:364
#define CUTLASS_HOST_DEVICE
Definition: cutlass.h:89
Top-level include for all CUTLASS numeric types.
CUTLASS_HOST_DEVICE T & operator*() const
Definition: array.h:263
CUTLASS_HOST_DEVICE bool operator==(const_iterator const &other) const
Definition: array.h:212
CUTLASS_HOST_DEVICE reference front()
Definition: array.h:379
T Element
Element type.
Definition: array.h:85
CUTLASS_HOST_DEVICE const_pointer raw_data() const
Definition: array.h:414
CUTLASS_HOST_DEVICE constexpr size_type max_size() const
Definition: array.h:430
CUTLASS_HOST_DEVICE const_pointer data() const
Definition: array.h:404
CUTLASS_HOST_DEVICE bool operator==(const_iterator const &other) const
Definition: array.h:324
CUTLASS_HOST_DEVICE iterator()
Definition: array.h:119
CUTLASS_HOST_DEVICE constexpr bool ispow2(unsigned x)
Returns true if the argument is a power of 2.
Definition: array.h:59
CUTLASS_HOST_DEVICE Array(Array const &x)
Definition: array.h:345
CUTLASS_HOST_DEVICE iterator operator--(int)
Definition: array.h:144
CUTLASS_HOST_DEVICE constexpr unsigned floor_pow_2(unsigned x)
Returns the largest power of two not greater than the argument.
Definition: array.h:67
CUTLASS_HOST_DEVICE const_reverse_iterator operator--(int)
Definition: array.h:312
value_type const * const_pointer
Definition: array.h:104
CUTLASS_HOST_DEVICE const_reverse_iterator & operator--()
Definition: array.h:299
T value_type
Definition: array.h:98
CUTLASS_HOST_DEVICE reference operator[](size_type pos)
Definition: array.h:369
CUTLASS_HOST_DEVICE const_iterator cbegin() const
Definition: array.h:448
CUTLASS_HOST_DEVICE Array()
Definition: array.h:342
CUTLASS_HOST_DEVICE const_iterator operator--(int)
Definition: array.h:200
CUTLASS_HOST_DEVICE const_reverse_iterator operator++(int)
Definition: array.h:305
Basic include for CUTLASS.
CUTLASS_HOST_DEVICE T const & operator*() const
Definition: array.h:319
CUTLASS_HOST_DEVICE const_reference front() const
Definition: array.h:384
CUTLASS_HOST_DEVICE reference at(size_type pos)
Definition: array.h:359
CUTLASS_HOST_DEVICE const_iterator & operator--()
Definition: array.h:187
CUTLASS_HOST_DEVICE void clear()
Efficient clear method.
Definition: array.h:354