-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAOFlagger.cu
266 lines (238 loc) · 9.01 KB
/
AOFlagger.cu
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#define TRUE 1
#define FALSE 0
#define BASE_SENSITIVITY 1.0f
#define MAX_ITERS 7
#define FIRST_THRESHOLD 6.0f
#define SIR_VALUE 0.4f
// Swap utility function
__device__ inline void swap(float * data, unsigned int x, unsigned int y) {
float temp = data[x];
data[x] = data[y];
data[y] = temp;
}
// Sort an array in place
__global__ void bitonic_sort(float * values, unsigned int n, unsigned int nr_flagged) {
const int tid = threadIdx.x;
for ( int k = 2; k <= n; k *= 2) {
for ( int j = k / 2; j > 0; j /= 2) {
int ixj = tid ^ j;
if ( ixj > tid ) {
if ( (tid & k) == 0 ) {
if ( values[tid] > values[ixj] ) {
swap(values[tid], values[ixj]);
}
} else {
if ( values[tid] < values[ixj] ) {
swap(values[tid], values[ixj]);
}
}
}
__syncthreads();
}
}
// return values[nr_flagged + (n - nr_flagged) / 2];
}
__global__ void sum_values(float * values) {
unsigned int tid = threadIdx.x;
for ( unsigned int s = blockDim.x / 2; s > 32; s >>= 1 ) {
if ( tid < s ) {
values[tid] += values[tid + s];
}
__syncthreads();
}
if ( tid < 32 ) {
values[tid] += values[tid + 32];
values[tid] += values[tid + 16];
values[tid] += values[tid + 8];
values[tid] += values[tid + 4];
values[tid] += values[tid + 2];
values[tid] += values[tid + 1];
}
// return values[0];
}
__global__ void count_flags(unsigned int * nr_flagged, float * flags) {
unsigned int tid = threadIdx.x;
if ( flags[tid] == TRUE ) {
atomicAdd(nr_flagged, 1);
}
}
__global__ void sum_threshold(float * values, float * flags, float median, float stddev, int n) {
int window = 1;
int tid = threadIdx.x;
float factor = stddev * BASE_SENSITIVITY;
float sum;
int pos;
float threshold;
for ( int i = 0; i < MAX_ITERS; i++ ) {
threshold = fma((FIRST_THRESHOLD * powf(1.5f, i) / window), factor, median);
sum = 0.0f;
if ( tid % window == 0 ) {
for ( pos = tid; pos < tid + window; pos++ ) {
if ( flags[pos] != TRUE ) {
sum += values[pos];
} else {
sum += threshold;
}
}
if ( sum >= window * threshold ) {
for ( pos = tid; pos < tid + window; pos++ ) {
flags[pos] = TRUE;
}
}
}
window *= 2;
}
}
__global__ void sir_operator(float * d_flags, int n) {
float * flags = &(d_flags[(blockIdx.x * n)]);
float credit = 0.0f;
float w;
float max_credit0;
for ( int i = 0; i < n; i++ ) {
w = flags[i] ? SIR_VALUE : SIR_VALUE - 1.0f;
max_credit0 = credit > 0.0f ? credit : 0.0f;
credit = max_credit0 + w;
flags[i] = credit >= 0.0f;
}
credit = 0;
for ( int i = n - 1; i > 0; i-- ) {
w = flags[i] ? SIR_VALUE : SIR_VALUE - 1.0f;
max_credit0 = credit > 0.0f ? credit : 0.0f;
credit = max_credit0 + w;
flags[i] = credit >= 0.0f | flags[i];
}
}
// MODIFIED, not equivalent to Linus code because our data structures are different
__global__ void reduce_freq(float * values, float * results, unsigned int number_of_channels,
unsigned int number_of_samples) {
extern __shared__ float shared[];
float result = 0;
// NOTE: Terrible memory access pattern
for ( unsigned int channel = threadIdx.x; channel < number_of_channels; channel += blockDim.x) {
result += values[(channel * number_of_samples) + blockIdx.x];
}
shared[threadIdx.x] = result;
__syncthreads();
result = sum_values(shared);
if ( threadIdx.x == 0 ) {
results[blockIdx.x] = result;
}
}
__device__ void winsorize(float * shared, int nr_flagged, int n) {
if ( threadIdx.x < (0.1f * (n - nr_flagged) + nr_flagged) ) {
shared[threadIdx.x] = shared[(int)(0.1f * (n - nr_flagged) + nr_flagged)];
}
if ( threadIdx.x > (0.9f * (n - nr_flagged) + nr_flagged) ) {
shared[threadIdx.x] = shared[(int)(0.9f * (n - nr_flagged) + nr_flagged)];
}
}
// MODIFIED, not equivalent to Linus code because our data structures are different
__global__ void reduce_time(float * values, float * results, unsigned int number_of_samples) {
extern __shared__ float shared[];
float result = 0;
for ( unsigned int sample = threadIdx.x; sample < number_of_samples; sample++ ) {
result += values[(blockIdx.x * number_of_samples) + sample];
}
shared[threadIdx.x] = result;
__syncthreads();
result = sum_values(shared);
if ( threadIdx.x == 0 ) {
results[blockDim.x] = result;
}
}
// MODIFIED, not equivalent to Linus code because our data structures are different
__global__ void flagger_freq(float * values, float * global_flags, unsigned int * nr_flagged,
unsigned int number_of_channels, unsigned int number_of_samples) {
extern __shared__ float shared[];
float * local_flags = (float *) &(shared[number_of_channels]);
unsigned int tid = threadIdx.x;
float median;
float stddev;
// NOTE: terrible memory access pattern
shared[tid] = values[(threadIdx.x * number_of_samples) + blockIdx.x];
local_flags[tid] = 0;
__syncthreads();
for ( unsigned int i = 0; i < 2; i++ ) {
float sum = 0;
float squared_sum = 0;
unsigned int local_nr_flagged = nr_flagged[blockIdx.x];
median = bitonic_sort(shared, number_of_channels, local_nr_flagged);
if ( tid >= local_nr_flagged ) {
winsorize(shared, local_nr_flagged, number_of_channels);
}
__syncthreads();
sum = sum_values(shared);
// NOTE: terrible memory access pattern
shared[tid] = values[(threadIdx.x * number_of_samples) + blockIdx.x];
if ( local_flags[tid] ) {
shared[tid] = 0;
}
__syncthreads();
bitonic_sort(shared, number_of_channels, local_nr_flagged);
if ( tid >= local_nr_flagged ) {
winsorize(shared, local_nr_flagged, number_of_channels);
shared[tid] *= shared[tid];
}
__syncthreads();
squared_sum = sum_values(shared);
stddev = sqrtf(squared_sum / number_of_channels - (sum / number_of_channels * sum / number_of_channels));
// NOTE: terrible memory access pattern
shared[tid] = values[(threadIdx.x * number_of_samples) + blockIdx.x];
if ( local_flags[tid] ) {
shared[tid] = 0;
}
__syncthreads();
sum_threshold(shared, local_flags, median, stddev, number_of_channels);
nr_flagged[blockIdx.x] = 0;
count_flags(&(nr_flagged[blockIdx.x]), local_flags);
}
// NOTE: terrible memory access pattern
global_flags[(threadIdx.x * number_of_samples) + blockIdx.x]
= local_flags[tid] | global_flags[(threadIdx.x * number_of_samples) + blockIdx.x];
}
// MODIFIED, not equivalent to Linus code because our data structures are different
__global__ void flagger_time(float * values, float * global_flags, unsigned int * nr_flagged,
unsigned int number_of_samples) {
extern __shared__ float shared[];
float * local_flags = (float *) &(shared[number_of_samples]);
unsigned int tid = threadIdx.x;
float median;
float stddev;
shared[tid] = values[(blockIdx.x * number_of_samples) + threadIdx.x];
local_flags[tid] = 0;
__syncthreads();
for ( unsigned int i = 0; i < 2; i++ ) {
float sum = 0;
float squared_sum = 0;
unsigned int local_nr_flagged = nr_flagged[blockIdx.x];
median = bitonic_sort(shared, number_of_samples, local_nr_flagged);
if ( tid >= local_nr_flagged ) {
winsorize(shared, local_nr_flagged, number_of_samples);
}
__syncthreads();
sum = sum_values(shared);
shared[tid] = values[(blockIdx.x * number_of_samples) + threadIdx.x];
if ( local_flags[tid] ) {
shared[tid] = 0;
}
__syncthreads();
bitonic_sort(shared, number_of_samples, local_nr_flagged);
if ( tid >= local_nr_flagged ) {
winsorize(shared, local_nr_flagged, number_of_samples);
shared[tid] *= shared[tid];
}
__syncthreads();
squared_sum = sum_values(shared);
stddev = sqrtf(squared_sum / number_of_samples - (sum / number_of_samples * sum / number_of_samples));
shared[tid] = values[(blockIdx.x * number_of_samples) + threadIdx.x];
if ( local_flags[tid] ) {
shared[tid] = 0;
}
__syncthreads();
sum_threshold(shared, local_flags, median, stddev, number_of_samples);
nr_flagged[blockIdx.x] = 0;
count_flags(&(nr_flagged[blockIdx.x]), local_flags);
}
global_flags[(blockIdx.x * number_of_samples) + threadIdx.x]
= local_flags[tid] | global_flags[(blockIdx.x * number_of_samples) + threadIdx.x];
}