| CUTLASS CUDA Templates for Linear Algebra Subroutines and Solvers

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

relatively_equal.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 **************************************************************************************************/
25 /* \file
26 \brief Performs comparison between two elements with support for floating-point comparisons.
27 */
28
29 #pragma once
30
31 #include "numeric_types.h"
32
33 namespace cutlass {
34
36
37 template <typename T>
38 CUTLASS_HOST_DEVICE
39 bool relatively_equal(T a, T b, T epsilon, T nonzero_floor);
40
42
43 namespace detail {
44
45 // This floating-point comparison function implements the method described in
46 //
47 // https://floating-point-gui.de/errors/comparison/
48 //
49 template <typename T>
50 CUTLASS_HOST_DEVICE
51 bool relatively_equal_float(T a, T b, T epsilon, T nonzero_floor) {
52
53 using std::abs;
54
55 T abs_A = abs(a);
56 T abs_B = abs(b);
57 T diff = abs(a - b);
58 T zero = T(0);
59
60 if (a == b) {
61 return true;
62 }
63 else if (a == zero || b == zero || diff < nonzero_floor) {
64 return diff < epsilon * nonzero_floor;
65 }
66
67 return diff < epsilon * (abs_A + abs_B);
68 }
69
70 } // namespace detail
71
73
74 template <>
75 CUTLASS_HOST_DEVICE
76 bool relatively_equal<uint1b_t>(uint1b_t a, uint1b_t b, uint1b_t, uint1b_t) {
77 return (a == b);
78 }
79
80 template <>
81 CUTLASS_HOST_DEVICE
82 bool relatively_equal<int4b_t>(int4b_t a, int4b_t b, int4b_t, int4b_t) {
83 return (a == b);
84 }
85
86 template <>
87 CUTLASS_HOST_DEVICE
88 bool relatively_equal<uint4b_t>(uint4b_t a, uint4b_t b, uint4b_t, uint4b_t) {
89 return (a == b);
90 }
91
92 template <>
93 CUTLASS_HOST_DEVICE
94 bool relatively_equal<int8_t>(int8_t a, int8_t b, int8_t, int8_t) {
95 return (a == b);
96 }
97
98 template <>
99 CUTLASS_HOST_DEVICE
100 bool relatively_equal<uint8_t>(uint8_t a, uint8_t b, uint8_t, uint8_t) {
101 return (a == b);
102 }
103
104 template <>
105 CUTLASS_HOST_DEVICE
106 bool relatively_equal<int16_t>(int16_t a, int16_t b, int16_t, int16_t) {
107 return (a == b);
108 }
109
110 template <>
111 CUTLASS_HOST_DEVICE
112 bool relatively_equal<uint16_t>(uint16_t a, uint16_t b, uint16_t, uint16_t) {
113 return (a == b);
114 }
115
116 template <>
117 CUTLASS_HOST_DEVICE
118 bool relatively_equal<int32_t>(int32_t a, int32_t b, int32_t, int32_t) {
119 return (a == b);
120 }
121
122 template <>
123 CUTLASS_HOST_DEVICE
124 bool relatively_equal<uint32_t>(uint32_t a, uint32_t b, uint32_t, uint32_t) {
125 return (a == b);
126 }
127
128 template <>
129 CUTLASS_HOST_DEVICE
130 bool relatively_equal<int64_t>(int64_t a, int64_t b, int64_t, int64_t) {
131 return (a == b);
132 }
133
134 template <>
135 CUTLASS_HOST_DEVICE
136 bool relatively_equal<uint64_t>(uint64_t a, uint64_t b, uint64_t, uint64_t) {
137 return (a == b);
138 }
139
141
142 template <>
143 CUTLASS_HOST_DEVICE
144 bool relatively_equal<half_t>(half_t a, half_t b, half_t epsilon, half_t nonzero_floor) {
145 return detail::relatively_equal_float(a, b, epsilon, nonzero_floor);
146 }
147
148 template <>
149 CUTLASS_HOST_DEVICE
150 bool relatively_equal<float>(float a, float b, float epsilon, float nonzero_floor) {
151 return detail::relatively_equal_float(a, b, epsilon, nonzero_floor);
152 }
153
154
155 template <>
156 CUTLASS_HOST_DEVICE
157 bool relatively_equal<double>(double a, double b, double epsilon, double nonzero_floor) {
158 return detail::relatively_equal_float(a, b, epsilon, nonzero_floor);
159 }
160
162
163 } // namespace cutlass
cutlass::relatively_equal< int8_t >
CUTLASS_HOST_DEVICE bool relatively_equal< int8_t >(int8_t a, int8_t b, int8_t, int8_t)
Definition: relatively_equal.h:94
cutlass::relatively_equal< double >
CUTLASS_HOST_DEVICE bool relatively_equal< double >(double a, double b, double epsilon, double nonzero_floor)
Definition: relatively_equal.h:157
cutlass::uint4b_t
integer_subbyte< 4, false > uint4b_t
4-bit Unsigned integer type
Definition: integer_subbyte.h:158
cutlass
Definition: aligned_buffer.h:35
cutlass::abs
CUTLASS_HOST_DEVICE T abs(complex< T > const &z)
Returns the magnitude of the complex number.
Definition: complex.h:313
cutlass::uint1b_t
integer_subbyte< 1, false > uint1b_t
1-bit Unsigned integer type
Definition: integer_subbyte.h:152
cutlass::integer_subbyte
4-bit signed integer type
Definition: integer_subbyte.h:42
cutlass::relatively_equal
CUTLASS_HOST_DEVICE bool relatively_equal(T a, T b, T epsilon, T nonzero_floor)
cutlass::half_t
IEEE half-precision floating-point type.
Definition: half.h:126
cutlass::relatively_equal< uint8_t >
CUTLASS_HOST_DEVICE bool relatively_equal< uint8_t >(uint8_t a, uint8_t b, uint8_t, uint8_t)
Definition: relatively_equal.h:100
cutlass::relatively_equal< int32_t >
CUTLASS_HOST_DEVICE bool relatively_equal< int32_t >(int32_t a, int32_t b, int32_t, int32_t)
Definition: relatively_equal.h:118
cutlass::relatively_equal< uint16_t >
CUTLASS_HOST_DEVICE bool relatively_equal< uint16_t >(uint16_t a, uint16_t b, uint16_t, uint16_t)
Definition: relatively_equal.h:112
cutlass::relatively_equal< int4b_t >
CUTLASS_HOST_DEVICE bool relatively_equal< int4b_t >(int4b_t a, int4b_t b, int4b_t, int4b_t)
Definition: relatively_equal.h:82
cutlass::relatively_equal< int16_t >
CUTLASS_HOST_DEVICE bool relatively_equal< int16_t >(int16_t a, int16_t b, int16_t, int16_t)
Definition: relatively_equal.h:106
CUTLASS_HOST_DEVICE
#define CUTLASS_HOST_DEVICE
Definition: cutlass.h:89
numeric_types.h
Top-level include for all CUTLASS numeric types.
cutlass::detail::relatively_equal_float
CUTLASS_HOST_DEVICE bool relatively_equal_float(T a, T b, T epsilon, T nonzero_floor)
Definition: relatively_equal.h:51
cutlass::relatively_equal< uint32_t >
CUTLASS_HOST_DEVICE bool relatively_equal< uint32_t >(uint32_t a, uint32_t b, uint32_t, uint32_t)
Definition: relatively_equal.h:124
cutlass::relatively_equal< int64_t >
CUTLASS_HOST_DEVICE bool relatively_equal< int64_t >(int64_t a, int64_t b, int64_t, int64_t)
Definition: relatively_equal.h:130
cutlass::relatively_equal< float >
CUTLASS_HOST_DEVICE bool relatively_equal< float >(float a, float b, float epsilon, float nonzero_floor)
Definition: relatively_equal.h:150
cutlass::relatively_equal< uint64_t >
CUTLASS_HOST_DEVICE bool relatively_equal< uint64_t >(uint64_t a, uint64_t b, uint64_t, uint64_t)
Definition: relatively_equal.h:136
cutlass::relatively_equal< uint4b_t >
CUTLASS_HOST_DEVICE bool relatively_equal< uint4b_t >(uint4b_t a, uint4b_t b, uint4b_t, uint4b_t)
Definition: relatively_equal.h:88
cutlass::relatively_equal< uint1b_t >
CUTLASS_HOST_DEVICE bool relatively_equal< uint1b_t >(uint1b_t a, uint1b_t b, uint1b_t, uint1b_t)
Definition: relatively_equal.h:76
cutlass::int4b_t
integer_subbyte< 4, true > int4b_t
4-bit Integer type
Definition: integer_subbyte.h:155
cutlass::relatively_equal< half_t >
CUTLASS_HOST_DEVICE bool relatively_equal< half_t >(half_t a, half_t b, half_t epsilon, half_t nonzero_floor)
Definition: relatively_equal.h:144