"tests/test_dataset/test_scannet_dataset.py" did not exist on "ae0d138d44e5a90142642c9f3b2d057f6bc0d876"
reduction_operator.hpp 6.41 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*******************************************************************************
 *
 * MIT License
 *
 * Copyright (c) 2020 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 *******************************************************************************/
#ifndef CK_REDUCTION_OPERATOR_HPP
#define CK_REDUCTION_OPERATOR_HPP

29
30
#include "config.hpp"
#include "data_type.hpp"
31
32
33
34
35
36
37
38

namespace ck {

namespace reduce {

// Every binary operator used in reduction is represented by a templated functor class. Each functor
// class must provide at least
// three members:
39
// 1) GetIdentityValue() -- the interface to return the "identity element" for the binary
40
// operator, "identity element" is the unique
41
//                    element in the algebraic space that doesn't affect the value of other elements
42
43
44
//                    when operated against them, and the concept is similar to zero vector in
//                    vector space
//                    (http://pages.cs.wisc.edu/~matthewb/pages/notes/pdf/linearalgebra/VectorSpaces.pdf).
45
46
47
48
// 2) IsCompatibleInMemoryDataOperation() -- return true if the reduction task corresponding to this
// operator can use the InMemoryDataOperation to finalize, or else it return false 3) operator() --
// the first argument of the operator must be both an input & output, and the corresponding variable
// usually stores
49
50
51
52
53
54
55
56
57
58
59
60
61
//                  the accumulated result of many operator() calls; the second argument is only an
//                  input. For indexable binary
//                  operator, the second version of operator() has third argument (which is an
//                  output) to indicate whether the
//                  accumulated value (the first argument) has changed, in which case the recorded
//                  accumulated index also need be
//                  changed.

template <class T>
struct Add
{
    using dataType = T;

62
    __host__ __device__ static constexpr T GetIdentityValue() { return static_cast<T>(0.0f); };
63

64
65
66
67
68
69
70
    __device__ static constexpr bool
    IsCompatibleInMemoryDataOperation(InMemoryDataOperationEnum operation)
    {
        return operation == InMemoryDataOperationEnum::AtomicAdd ||
               operation == InMemoryDataOperationEnum::Set;
    };

71
    __host__ __device__ inline constexpr void operator()(T& a, T b) const { a = a + b; }
72
73
};

74
75
76
77
78
79
80
81
82
83
template <class T>
struct SquaredAdd
{
    using dataType = T;

    __host__ __device__ static constexpr T GetReductionZeroVal() { return static_cast<T>(0.0f); };

    __host__ __device__ inline constexpr void operator()(T& a, T b) const { a = a + b * b; }
};

84
85
86
87
88
template <class T>
struct Mul
{
    using dataType = T;

89
    __host__ __device__ static constexpr T GetIdentityValue() { return static_cast<T>(1.0f); };
90

91
92
93
94
95
96
    __device__ static constexpr bool
    IsCompatibleInMemoryDataOperation(InMemoryDataOperationEnum operation)
    {
        return operation == InMemoryDataOperationEnum::Set;
    };

97
    __host__ __device__ inline constexpr void operator()(T& a, T b) const { a = a * b; }
98
99
100
101
102
103
104
};

template <class T>
struct Max
{
    using dataType = T;

105
    __host__ __device__ static constexpr T GetIdentityValue()
106
107
108
    {
        return NumericLimits<T>::Lowest();
    };
109

110
111
112
113
114
115
116
    __device__ static constexpr bool
    IsCompatibleInMemoryDataOperation(InMemoryDataOperationEnum operation)
    {
        // ToChange: atomic_max to be added
        return operation == InMemoryDataOperationEnum::Set;
    };

117
    __host__ __device__ inline constexpr void operator()(T& a, T b) const
118
119
120
121
122
    {
        if(a < b)
            a = b;
    }

123
    __host__ __device__ inline constexpr void operator()(T& a, T b, bool& changed) const
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    {
        if(a < b)
        {
            a       = b;
            changed = true;
        }
    }
};

template <class T>
struct Min
{
    using dataType = T;

138
    __host__ __device__ static constexpr T GetIdentityValue() { return NumericLimits<T>::Max(); };
139

140
141
142
143
144
145
146
    __device__ static constexpr bool
    IsCompatibleInMemoryDataOperation(InMemoryDataOperationEnum operation)
    {
        // ToChange: atomic_min to be added
        return operation == InMemoryDataOperationEnum::Set;
    };

147
    __host__ __device__ inline constexpr void operator()(T& a, T b) const
148
149
150
151
152
    {
        if(a > b)
            a = b;
    }

153
    __host__ __device__ inline constexpr void operator()(T& a, T b, bool& changed) const
154
155
156
157
158
159
160
161
162
    {
        if(a > b)
        {
            a       = b;
            changed = true;
        }
    }
};

163
164
template <class T>
struct AMax
165
{
166
    using dataType = T;
167

168
    __host__ __device__ static constexpr T GetIdentityValue() { return static_cast<T>(0.0f); };
169

170
171
172
173
174
175
176
    __device__ static constexpr bool
    IsCompatibleInMemoryDataOperation(InMemoryDataOperationEnum operation)
    {
        // ToChange: atomic_max to be added
        return operation == InMemoryDataOperationEnum::Set;
    };

177
    __host__ __device__ inline constexpr void operator()(T& a, T b) const
178
179
180
181
182
    {
        if(a < b)
            a = b;
    }

183
    __host__ __device__ inline constexpr void operator()(T& a, T b, bool& changed) const
184
185
186
187
188
189
190
    {
        if(a < b)
        {
            a       = b;
            changed = true;
        }
    }
191
192
};

193
template <typename T>
194
T GetIdentityValueueForInMemoryDataOperation(InMemoryDataOperationEnum operation)
195
196
197
198
199
200
201
202
203
{
    T result = ck::type_convert<T>(0.0f);

    if(operation == InMemoryDataOperationEnum::AtomicMax)
        result = ck::NumericLimits<T>::Lowest();

    return (result);
};

204
205
206
207
208
}; // end of namespace reduce

} // end of namespace ck

#endif