-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreduction.h
168 lines (128 loc) · 3.73 KB
/
reduction.h
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright (C) 2015-2018, Nils Moehrle
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the BSD 3-Clause license. See the LICENSE.txt file for details.
*/
#ifndef CACC_REDUCTION_HEADER
#define CACC_REDUCTION_HEADER
#include "defines.h"
#include "array.h"
#include "variable.h"
CACC_NAMESPACE_BEGIN
#ifndef REDUCTION_BLOCK_SIZE
#define REDUCTION_BLOCK_SIZE 256
#endif
#define REDUCTION_NAMESPACE_BEGIN namespace reduction {
#define REDUCTION_NAMESPACE_END }
REDUCTION_NAMESPACE_BEGIN
struct FAdd {
__device__ __forceinline__
float operator()(float lhs, float rhs) {
return lhs + rhs;
}
__device__ __forceinline__
void operator()(float *addr, float val) {
atomicAdd(addr, val);
}
};
struct FMin {
__device__ __forceinline__
float operator()(float lhs, float rhs) {
return min(lhs, rhs);
}
__device__ __forceinline__
void operator()(float *addr, float val) {
atomicMin((uint32_t *)addr, float_to_uint32(val));
}
};
struct FMax {
__device__ __forceinline__
float operator()(float lhs, float rhs) {
return max(lhs, rhs);
}
__device__ __forceinline__
void operator()(float *addr, float val) {
atomicMax((uint32_t *)addr, float_to_uint32(val));
}
};
template <typename F>
float convert_to_gmem_repr(float val) {
return val;
}
template <typename F>
float convert_to_host_repr(float val) {
return val;
}
template <>
float convert_to_gmem_repr<FMin>(float val) {
return float_to_uint32_as_float(val);
}
template <>
float convert_to_gmem_repr<FMax>(float val) {
return float_to_uint32_as_float(val);
}
template <>
float convert_to_host_repr<FMin>(float val) {
return uint32_as_float_to_float(val);
}
template <>
float convert_to_host_repr<FMax>(float val) {
return uint32_as_float_to_float(val);
}
template <typename F>
__inline__ __device__
float reduce_warp(float val) {
#pragma unroll
for (int stride = warpSize >> 1; stride > 0; stride >>= 1) {
val = F()(val, __shfl_down(val, stride));
}
return val;
}
template <typename F>
__global__
void reduce_kernel(float * values, float * ret, uint n, float init)
{
__shared__ float sm[32];
uint lane = threadIdx.x % warpSize;
uint tx = threadIdx.x;
uint wx = threadIdx.x / warpSize;
uint gstride = blockDim.x * gridDim.x;
float val = init;
for (uint i = blockIdx.x * blockDim.x + tx; i < n; i += gstride) {
val = F()(val, values[i]);
}
val = reduce_warp<F>(val);
if (lane == 0) sm[wx] = val;
__syncthreads();
val = (threadIdx.x < blockDim.x / warpSize) ? sm[lane] : init;
if (wx == 0) val = reduce_warp<F>(val);
if (tx == 0) F()(ret, val);
}
template <typename F>
float reduce(Array<float, DEVICE>::Ptr darray, float init) {
Array<float, DEVICE>::Data data = darray->cdata();
uint num_threads = cacc::divup(data.num_values, std::log2(data.num_values));
uint num_blocks = cacc::divup(num_threads, REDUCTION_BLOCK_SIZE);
Variable<float, DEVICE> dret(convert_to_gmem_repr<F>(init));
dim3 grid(num_blocks);
dim3 block(REDUCTION_BLOCK_SIZE);
reduce_kernel<F><<<grid, block>>>(
data.data_ptr, dret.cptr(), data.num_values, init
);
Variable<float, HOST> ret(dret);
return convert_to_host_repr<F>(ret.ref());
}
float sum(Array<float, DEVICE>::Ptr darray) {
return reduce<FAdd>(darray, 0.0f);
}
float min(Array<float, DEVICE>::Ptr darray) {
return reduce<FMin>(darray, std::numeric_limits<float>::max());
}
float max(Array<float, DEVICE>::Ptr darray) {
return reduce<FMax>(darray, std::numeric_limits<float>::lowest());
}
REDUCTION_NAMESPACE_END
CACC_NAMESPACE_END
#endif /* CACC_REDUCTION_HEADER */