coord.h
Go to the documentation of this file.
/***************************************************************************************************
* Copyright (c) 2017-2018, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
* * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
**************************************************************************************************/
#pragma once
#include <cutlass/cutlass.h>
namespace cutlass {
struct Identity {
enum Kind { Additive = 0, Multiplicative = 1 };
};
template <int N_>
struct Coord {
//
// Type and constant definitions
//
static int const N = N_;
//
// Data members
//
int idx[N];
//
// Methods
//
CUTLASS_HOST_DEVICE
Coord(int value = 0) {
for (int i = 0; i < N; ++i) {
idx[i] = value;
}
}
CUTLASS_HOST_DEVICE
Coord(int _idx[]) {
for (int i = 0; i < N; ++i) {
idx[i] = _idx[i];
}
}
CUTLASS_HOST_DEVICE
Coord operator+(Coord const& b) const {
Coord c;
for (int i = 0; i < N; ++i) {
c.idx[i] = idx[i] + b.idx[i];
}
return c;
}
CUTLASS_HOST_DEVICE
Coord operator-(Coord const& b) const {
Coord c;
for (int i = 0; i < N; ++i) {
c.idx[i] = idx[i] - b.idx[i];
}
return c;
}
CUTLASS_HOST_DEVICE
Coord operator*(Coord const& b) const {
Coord c;
for (int i = 0; i < N; ++i) {
c.idx[i] = idx[i] * b.idx[i];
}
return c;
}
CUTLASS_HOST_DEVICE
Coord operator/(Coord const& b) const {
Coord c;
for (int i = 0; i < N; ++i) {
c.idx[i] = idx[i] / b.idx[i];
}
return c;
}
CUTLASS_HOST_DEVICE
Coord& operator+=(Coord const& b) {
for (int i = 0; i < N; ++i) {
idx[i] += b.idx[i];
}
return *this;
}
CUTLASS_HOST_DEVICE
Coord& operator-=(Coord const& b) {
for (int i = 0; i < N; ++i) {
idx[i] -= b.idx[i];
}
return *this;
}
CUTLASS_HOST_DEVICE
Coord& operator*=(Coord const& b) {
for (int i = 0; i < N; ++i) {
idx[i] *= b.idx[i];
}
return *this;
}
CUTLASS_HOST_DEVICE
Coord& operator/=(Coord const& b) {
for (int i = 0; i < N; ++i) {
idx[i] /= b.idx[i];
}
return *this;
}
CUTLASS_HOST_DEVICE int& operator[](int dim) { return idx[dim]; }
CUTLASS_HOST_DEVICE int const& operator[](int dim) const { return idx[dim]; }
template <typename T>
CUTLASS_HOST_DEVICE T dot(Coord const& b, T sum) const {
for (int i = 0; i < N; ++i) {
sum += idx[i] * b.idx[i];
}
return sum;
}
template <typename T>
CUTLASS_HOST_DEVICE T dot(Coord const& b) const {
T sum = T(0);
for (int i = 0; i < N; ++i) {
sum += idx[i] * b.idx[i];
}
return sum;
}
template <int Dim>
CUTLASS_HOST_DEVICE int& at() {
return idx[Dim];
}
CUTLASS_HOST_DEVICE
int& at(int dim) { return idx[dim]; }
template <int Dim>
CUTLASS_HOST_DEVICE int const& at() const {
return idx[Dim];
}
CUTLASS_HOST_DEVICE
int const& at(int dim) const { return idx[dim]; }
CUTLASS_HOST_DEVICE
bool operator==(Coord<N> const& b) const {
bool equal = true;
for (int i = 0; equal && i < N; ++i) {
equal = (idx[i] == b.idx[i]);
}
return equal;
}
CUTLASS_HOST_DEVICE
bool operator!=(Coord<N> const& b) const { return !(*this == b); }
CUTLASS_HOST_DEVICE
Coord& clamp(Coord<N> const& max, Coord<N> const& min = Coord<N>()) {
for (int i = 0; i < N; ++i) {
idx[i] = __NV_STD_MAX(__NV_STD_MIN(idx[i], max.idx[i]), min.idx[i]);
}
return *this;
}
CUTLASS_HOST_DEVICE
int count() const {
int product = idx[0];
for (int i = 1; i < N; ++i) {
product *= idx[i];
}
return product;
}
};
CUTLASS_HOST_DEVICE
Coord<1> make_Coord(int _0) {
int values[1] = {_0};
return Coord<1>(values);
}
CUTLASS_HOST_DEVICE
Coord<2> make_Coord(int _0, int _1) {
int values[2] = {_0, _1};
return Coord<2>(values);
}
CUTLASS_HOST_DEVICE
Coord<3> make_Coord(int _0, int _1, int _2) {
int values[3] = {_0, _1, _2};
return Coord<3>(values);
}
CUTLASS_HOST_DEVICE
Coord<4> make_Coord(int _0, int _1, int _2, int _3) {
int values[4] = {_0, _1, _2, _3};
return Coord<4>(values);
}
CUTLASS_HOST_DEVICE
Coord<2> get_Coord_hw(Coord<3> const& coord) { return make_Coord(coord[1], coord[2]); }
CUTLASS_HOST_DEVICE
Coord<2> get_Coord_hw(Coord<4> const& coord) { return make_Coord(coord[1], coord[2]); }
CUTLASS_HOST_DEVICE
Coord<3> get_Coord_hwc(Coord<4> const& coord) { return make_Coord(coord[1], coord[2], coord[3]); }
CUTLASS_HOST_DEVICE
Coord<3> get_Coord_dhw(Coord<4> const& coord) { return make_Coord(coord[0], coord[1], coord[2]); }
} // namespace cutlass
cutlass::Coord::operator[]
CUTLASS_HOST_DEVICE int const & operator[](int dim) const
Member access operator.
Definition: coord.h:162
cutlass::Coord::count
CUTLASS_HOST_DEVICE int count() const
Returns the product of all elements.
Definition: coord.h:228
cutlass::Identity
Describes identity elements.
Definition: coord.h:38
cutlass::platform::max
CUTLASS_HOST_DEVICE constexpr const T & max(const T &a, const T &b)
std::max
Definition: platform.h:207
cutlass
Definition: convert.h:33
cutlass::Coord::operator==
CUTLASS_HOST_DEVICE bool operator==(Coord< N > const &b) const
Determines if two Coord<> objects are equal.
Definition: coord.h:205
cutlass::Coord::operator+=
CUTLASS_HOST_DEVICE Coord & operator+=(Coord const &b)
In-place addition.
Definition: coord.h:124
cutlass::Coord::operator!=
CUTLASS_HOST_DEVICE bool operator!=(Coord< N > const &b) const
Not equal.
Definition: coord.h:215
cutlass::make_Coord
CUTLASS_HOST_DEVICE Coord< 1 > make_Coord(int _0)
Helper to make a 2-element coordinate.
Definition: coord.h:241
cutlass::get_Coord_hwc
CUTLASS_HOST_DEVICE Coord< 3 > get_Coord_hwc(Coord< 4 > const &coord)
Getter.
Definition: coord.h:279
cutlass::get_Coord_dhw
CUTLASS_HOST_DEVICE Coord< 3 > get_Coord_dhw(Coord< 4 > const &coord)
Getter.
Definition: coord.h:283
cutlass::Coord::clamp
CUTLASS_HOST_DEVICE Coord & clamp(Coord< N > const &max, Coord< N > const &min=Coord< N >())
Clamps a coordinate to a range specified by maximum and minimum values.
Definition: coord.h:219
cutlass::Coord::at
CUTLASS_HOST_DEVICE int const & at() const
Gets the index of a given Coord element.
Definition: coord.h:195
cutlass::Coord::operator/
CUTLASS_HOST_DEVICE Coord operator/(Coord const &b) const
Element-wise division.
Definition: coord.h:114
cutlass::Identity::Kind
Kind
Definition: coord.h:41
cutlass::Coord::dot
CUTLASS_HOST_DEVICE T dot(Coord const &b, T sum) const
Computes the dot product of two Coord instances.
Definition: coord.h:166
cutlass::Coord::Coord
CUTLASS_HOST_DEVICE Coord(int _idx[])
Constructs from an array of integers.
Definition: coord.h:76
__NV_STD_MAX
#define __NV_STD_MAX(a, b)
Select maximum(a, b)
Definition: platform.h:155
cutlass::Coord::at
CUTLASS_HOST_DEVICE int & at(int dim)
Access via index; may limit unrolling potential.
Definition: coord.h:191
cutlass::Coord::operator[]
CUTLASS_HOST_DEVICE int & operator[](int dim)
Member access operator.
Definition: coord.h:159
cutlass::Coord::operator-=
CUTLASS_HOST_DEVICE Coord & operator-=(Coord const &b)
In-place subtraction.
Definition: coord.h:133
cutlass::Coord::operator*
CUTLASS_HOST_DEVICE Coord operator*(Coord const &b) const
Element-wise multiplication.
Definition: coord.h:104
cutlass::Coord::Coord
CUTLASS_HOST_DEVICE Coord(int value=0)
Default ctor initializes uniformly.
Definition: coord.h:68
cutlass::get_Coord_hw
CUTLASS_HOST_DEVICE Coord< 2 > get_Coord_hw(Coord< 3 > const &coord)
Getter.
Definition: coord.h:271
cutlass::Coord::N
static int const N
Definition: coord.h:53
__NV_STD_MIN
#define __NV_STD_MIN(a, b)
Select minimum(a, b)
Definition: platform.h:160
cutlass::Coord::dot
CUTLASS_HOST_DEVICE T dot(Coord const &b) const
Computes the dot product of two Coord instances.
Definition: coord.h:175
cutlass::Coord::operator-
CUTLASS_HOST_DEVICE Coord operator-(Coord const &b) const
Element-wise subtraction.
Definition: coord.h:94
CUTLASS_HOST_DEVICE
#define CUTLASS_HOST_DEVICE
Definition: cutlass.h:46
cutlass::platform::min
CUTLASS_HOST_DEVICE constexpr const T & min(const T &a, const T &b)
std::min
Definition: platform.h:201
cutlass::Identity::Multiplicative
Definition: coord.h:41
cutlass::Coord
Statically-sized array specifying Coords within a tensor.
Definition: coord.h:48
cutlass::Coord::at
CUTLASS_HOST_DEVICE int & at()
Gets the index of a given Coord element.
Definition: coord.h:185
cutlass::Coord::idx
int idx[N]
Indices.
Definition: coord.h:60
cutlass::Identity::Additive
Definition: coord.h:41
cutlass::Coord::at
CUTLASS_HOST_DEVICE int const & at(int dim) const
Access via index; may limit unrolling potential.
Definition: coord.h:201
cutlass.h
Basic include for CUTLASS macros.
cutlass::Coord::operator*=
CUTLASS_HOST_DEVICE Coord & operator*=(Coord const &b)
In-place multiplication.
Definition: coord.h:142
cutlass::Coord::operator+
CUTLASS_HOST_DEVICE Coord operator+(Coord const &b) const
Element-wise addition.
Definition: coord.h:84
cutlass::Coord::operator/=
CUTLASS_HOST_DEVICE Coord & operator/=(Coord const &b)
In-place division.
Definition: coord.h:151