Repository navigation
Expand file tree
/
Copy pathzero_kernels.h
More file actions
452 lines (301 loc) · 11.8 KB
/
Copy pathzero_kernels.h
File metadata and controls
452 lines (301 loc) · 11.8 KB
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#ifndef KERNELS_H
#define KERNELS_H
/* Copyright 2012 Jeffrey Blanchard and Jared Tanner
*
* GPU Accelerated Greedy Algorithms for Compressed Sensing
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ************ GLOBAL FUNCTIONS (kernels) **************
// ************ Thresholding functions *************
__global__ void threshold_one(float *vec, float *vec_thres, int *bin, const int k_bin, const int n);
__global__ void threshold(float *vec, int *bin, const int k_bin, const int n);
// This is used in findSupportSet_sort
__global__ void threshold_and_support(float *vec, int *support, const int n, const float T);
// ******** Vector Definitions ************
__global__ void zero_vector_float(float *vec, const int n);
__global__ void zero_vector_int(int *vec, const int n);
__global__ void one_vector_float(float *vec, const int n);
__global__ void one_vector_int(int *vec, const int n);
// ********** Kernels for Linear Binning Operations when finding supports **************
__global__ void LinearBinning(float *vec, int *bin, int *bin_counters, const int num_bins, const int MaxBin, const int n, const float slope, const float intercept);
// ********** Kernels that deal with index sets ***************
// This kernel takes MATLAB 1-indexing and transforms to 0-indexing
void __global__ indexShiftDown(int * d_rows, const int m);
// This is a kernel to take the union of two supports for CSMPSP
void __global__ joinSupports(int * d_bin, int * d_bin_grad, const int k_bin, const int k_bin_grad, const int n);
// *********** Used in results functions (results.cu) *************
__global__ void check_support(float * vec_input, float * vec, const int n, int * support_counter);
// ************** Used in functions.cu ****************
// This is used in FindSupportSet_sort in order to sort the magnitudes
__global__ void magnitudeCopy(float *mag_vec, float *vec, const int n);
/*
**************************************************************
** VARIOUS KERNELS USED IN DEVELOPMENT BUT NO LONGER ACTIVE **
**************************************************************
__global__ void make_bins(float *vec, int *bin, const int num_bins, const int n, const float slope, const float intercept)
{
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
if ( xIndex < n ){
int bin_new_val;
float temp = abs(vec[xIndex]);
if ( temp > (intercept *.000001) ){
bin_new_val=slope * (intercept - temp);
}
else bin_new_val = num_bins;
bin[xIndex]=bin_new_val;
}
}
__global__ void count_bins(int *bin, int *bin_counters, const int num_bins, const int n)
{
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
if ( (xIndex < n) & (bin[xIndex]<num_bins) )
atomicAdd(bin_counters+bin[xIndex],1);
}
__global__ void make_and_count_bins(float *vec, int *bin, int *bin_counters, const int num_bins, const int n, const float slope, const float intercept)
{
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
float temp = abs(vec[xIndex]);
if ( xIndex < n ){
if ( temp > (intercept *.01) ){
bin[xIndex]=max(0.0f,slope * (intercept - temp));
atomicAdd(bin_counters+bin[xIndex],1);
}
else bin[xIndex] = slope * intercept + 1.0f;
}
}
// This is used to test behavior of skipping cudaThreadSync();
__global__ void count_zero_one(float *vec, float *data, const int n)
{
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
if ( (xIndex < n) ){
if (vec[xIndex] == 0)
atomicAdd(data,1);
else if (vec[xIndex] == 1)
atomicAdd(data+1,1);
}
}
__global__ void countRest(int *bin, int *bin_counters, const int num_bins, const int maxBin, const int n)
{
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
if ( (xIndex < n) & (bin[xIndex]<num_bins) )
if (bin[xIndex]>= maxBin) atomicAdd(bin_counters+bin[xIndex],1);
}
__global__ void make_and_count_bins_alt(float *vec, int *bin, int *bin_counters, const int num_bins, const int n, const float slope, const float intercept, const float )
{
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
float temp = abs(vec[xIndex]);
if ( xIndex < n ){
bin[xIndex]=max(0.0f,slope * (intercept - temp));
if ( temp > (intercept *.000001) ){
atomicAdd(bin_counters+bin[xIndex],1);
}
else bin[xIndex] = slope * intercept + 1.0f;
}
}
__global__ void update_bins(float *vec, int *bin, int *bin_counters, const int num_bins, const int n, const float slope, const float intercept)
{
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
if ( xIndex < n ){
int bin_new_val;
float temp = abs(vec[xIndex]);
if ( temp > (intercept *.000001) ){
bin_new_val=slope * (intercept - temp);
}
else bin_new_val = num_bins;
if ( bin[xIndex] != bin_new_val ){
if (bin[xIndex] < num_bins)
atomicAdd(bin_counters+bin[xIndex],-1);
if ( bin_new_val < num_bins )
atomicAdd(bin_counters+bin[xIndex],1);
bin[xIndex]=bin_new_val;
}
}
}
__global__ void dyadicAdd(int * counter, const int length, const int shift)
{
if (shift > 0) {
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
int adds = 2*shift;
int Index = adds*(xIndex+1)-1;
if (Index < length) {
counter[Index] = counter[Index] + counter[Index-shift];
}
}
}
// Soft thresholding used in Lee and Wright's SPARSA
__global__ void __soft(float* y, const float* x, float T, int m)
{
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
float x_e, y_e;
if(xIndex < m)
{
x_e = x[xIndex];
y_e = fmaxf(fabsf(x_e) - T, 0.f);
y[xIndex] = y_e / (y_e + T) * x_e;
}
}
__global__ void halve_bins(int *bin, const int n)
{
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
if ( xIndex < n )
bin[xIndex] = bin[xIndex]/2;
}
__global__ void add_adjacent(int *vec, int *vec_shorter, const int n)
{
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
if ( xIndex < n )
vec_shorter[xIndex] = vec[2 * xIndex] + vec[(2 * xIndex) +1];
}
__global__ void int_copy(int *vec_to, int *vec_from, const int n)
{
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
if ( xIndex < n )
vec_to[xIndex] = vec_from[xIndex];
}
__device__ float getAbsMax(float * d_vec, const int length)
{
int jj=0;
float segmentMax = 0;
for (jj=0; jj<length; jj++) {
if ( segmentMax < abs(d_vec[jj]) ) segmentMax = abs(d_vec[jj]);
}
return segmentMax;
}
__host__ __device__ float getMax(float * vec, const int length)
{
int jj=0;
float segmentMax = 0.0f;
for (jj=0; jj<length; jj++) {
if ( segmentMax < vec[jj] ) segmentMax = vec[jj];
}
return segmentMax;
}
__global__ void segmentMax(float* d_vec, float *segmentMaxes, const int length, const int HighLength, const int HighSegmentLength, const int threadsHigh, const int LowSegmentLength)
{
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
unsigned int startIndex, SegmentLength;
if ( (xIndex*HighSegmentLength > HighLength) & ( (HighLength + (xIndex-threadsHigh+1)*LowSegmentLength) < length ) ){
startIndex = HighLength + (xIndex-threadsHigh)*LowSegmentLength;
SegmentLength = LowSegmentLength;
}
else {
startIndex = xIndex*HighSegmentLength;
SegmentLength = HighSegmentLength;
}
segmentMaxes[xIndex] = getAbsMax(d_vec+startIndex, SegmentLength);
}
__global__ void GetSegmentMax(float * segmentMaxes, float* maxValue, const int length)
{
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
if (xIndex <1) {
float mxVal = getMax(segmentMaxes, length);
maxValue[0] = 0.5f; //mxVal;
}
}
// ** Speed up the counting by not using atomicAdd **
__device__ void MakeCountSegment(float *segment, int *bins, const int seglength, int *segCounter, const int countlength, const float low, const float high, const float slope)
{
int bin;
float temp;
for (int jj=0; jj<seglength; jj++){
temp = abs(segment[jj]);
if ( ( temp > low ) & ( temp < high ) ) {
bin = (int)ceil(slope*abs(high-temp));
}
else if (temp >= high) {
bin = 0;
}
else bin = countlength - 1;
bins[jj]=bin;
segCounter[bin] = segCounter[bin] + 1;
}
return;
}
__device__ void MakeCountSegment_sharedAtomic(float *segment, int *bins, const int seglength, int *segCounter, int *s_counter, const int countlength, const float low, const float high, const float slope)
{
int bin;
float temp;
for (int jj=0; jj<seglength; jj++){
temp = abs(segment[jj]);
if ( ( temp > low ) & ( temp < high ) ) {
bin = (int)ceil(slope*abs(high-temp));
}
else if (temp >= high) {
bin = 0;
}
else bin = countlength - 1;
bins[jj]=bin;
atomicAdd(s_counter+bin,1);
}
for (int jj=0; jj<countlength; jj++) segCounter[jj]=s_counter[jj];
return;
}
__global__ void make_and_count_seg(float *vec, int *bin, int *segcounter, const int length, const int countlength, const int HighLength, const int HighSegmentLength, const int threadsHigh, const int LowSegmentLength, const float low, const float high, const float slope)
{
int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
int startIndex, SegmentLength, startCountIndex;
startCountIndex = xIndex*countlength;
if ( (xIndex*HighSegmentLength > HighLength) & ( (HighLength + (xIndex-threadsHigh+1)*LowSegmentLength) < length ) ){
startIndex = HighLength + (xIndex-threadsHigh)*LowSegmentLength;
SegmentLength = LowSegmentLength;
}
else {
startIndex = xIndex*HighSegmentLength;
SegmentLength = HighSegmentLength;
}
MakeCountSegment(vec+startIndex, bin+startIndex, SegmentLength, segcounter+startCountIndex, countlength, low, high, slope);
}
__global__ void make_and_count_seg_sharedAtomic(float *vec, int *bin, int *segcounter, const int length, const int countlength, const int HighLength, const int HighSegmentLength, const int threadsHigh, const int LowSegmentLength, const float low, const float high, const float slope)
{
int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
int startIndex, SegmentLength, startCountIndex;
extern __shared__ int s_counter[];
startCountIndex = xIndex*countlength;
if ( (xIndex*HighSegmentLength > HighLength) & ( (HighLength + (xIndex-threadsHigh+1)*LowSegmentLength) < length ) ){
startIndex = HighLength + (xIndex-threadsHigh)*LowSegmentLength;
SegmentLength = LowSegmentLength;
}
else {
startIndex = xIndex*HighSegmentLength;
SegmentLength = HighSegmentLength;
}
MakeCountSegment_sharedAtomic(vec+startIndex, bin+startIndex, SegmentLength, segcounter+startCountIndex, s_counter, countlength, low, high, slope);
}
__global__ void segCountSum(int *counter, int *segcounter, const int countlength)
{
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
if (xIndex < countlength){
for (int jj=0; jj<countlength; jj++){
counter[xIndex] = counter[xIndex] + segcounter[xIndex + jj*countlength];
}
}
}
__global__ void segCountSum_shared(int *counter, int *segcounter, const int countlength)
{
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
extern __shared__ int s_counter[];
if (xIndex < countlength){
for (int jj=0; jj<countlength; jj++){
s_counter[xIndex] = s_counter[xIndex] + segcounter[xIndex + jj*countlength];
}
}
counter[xIndex] = s_counter[xIndex];
}
__global__ void magnitude(float *vec, const int n)
{
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
if (xIndex < n) { vec[xIndex] = abs(vec[xIndex]); }
}
*/
#endif