summaryrefslogtreecommitdiff
path: root/UNI/baselines/gpu/ds.h
blob: 1fa73de59a6f201212bcca299f2866b00d2f51e7 (plain)
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
/***************************************************************************
 *cr
 *cr            (C) Copyright 2015 The Board of Trustees of the
 *cr                        University of Illinois
 *cr                         All Rights Reserved
 *cr
 ***************************************************************************/
/*
  In-Place Data Sliding Algorithms for Many-Core Architectures, presented in ICPP’15

  Copyright (c) 2015 University of Illinois at Urbana-Champaign. 
  All rights reserved.

  Permission to use, copy, modify and distribute this software and its documentation for 
  educational purpose is hereby granted without fee, provided that the above copyright 
  notice and this permission notice appear in all copies of this software and that you do 
  not sell the software.

  THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,EXPRESS, IMPLIED OR 
  OTHERWISE.

  Authors: Juan Gómez-Luna (el1goluj@uco.es, gomezlun@illinois.edu), Li-Wen Chang (lchang20@illinois.edu)
*/

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <math.h>
#include <sys/time.h>
#include <vector>

#ifdef FLOAT
#define T float
#elif INT
#define T int
#elif INT64
#define T int64_t
#else
#define T double
#endif

#ifdef THREADS
#define L_DIM THREADS
#else 
#define L_DIM 1024
#endif

#ifdef COARSENING
#define REGS COARSENING
#else
#ifdef FLOAT
#define REGS 16
#elif INT
#define REGS 16
#else
#define REGS 8 
#endif
#endif

#ifdef ATOMIC
#define ATOM 1
#else
#define ATOM 0
#endif

#define WARP_SIZE 32

#define PRINT 0

// Dynamic allocation of runtime workgroup id
__device__ int dynamic_wg_id(volatile unsigned int *flags, const int num_flags){
  __shared__ int gid_;
  if (threadIdx.x == 0) gid_ = atomicAdd((unsigned int*)&flags[num_flags + 1], 1);
  __syncthreads();
  int my_s = gid_;
  return my_s;
}

// Set global synchronization (regular DS)
__device__ void ds_sync(volatile unsigned int *flags, const int my_s){
#if ATOM
  if (threadIdx.x == 0){
    while (atomicOr((unsigned int*)&flags[my_s], 0) == 0){}
    atomicOr((unsigned int*)&flags[my_s + 1], 1);
  }
#else
  if (threadIdx.x == 0){
    while (flags[my_s] == 0){}
    flags[my_s + 1] = 1;
  }
#endif
  __syncthreads();
}

// Set global synchronization (irregular DS)
__device__ void ds_sync_irregular(volatile unsigned int *flags, const int my_s, int *count){
#if ATOM
  if (threadIdx.x == 0){
    while (atomicOr((unsigned int*)&flags[my_s], 0) == 0){}
    int flag = flags[my_s];
    atomicAdd((unsigned int*)&flags[my_s + 1], flag + *count);
    *count = flag - 1;
  }
#else
  if (threadIdx.x == 0){
    while (flags[my_s] == 0){}
    int flag = flags[my_s];
    flags[my_s + 1] = flag + *count;
    *count = flag - 1;
  }
#endif
  __syncthreads();
}

// Set global synchronization (irregular DS Partition)
__device__ void ds_sync_irregular_partition(volatile unsigned int *flags1, volatile unsigned int *flags2, const int my_s, int *count1, int *count2){
#if ATOM
  if (threadIdx.x == 0){
    while (atomicOr((unsigned int*)&flags1[my_s], 0) == 0){}
    int flag2 = flags2[my_s];
    atomicAdd((unsigned int*)&flags2[my_s + 1], flag2 + *count);
    int flag1 = flags1[my_s];
    atomicAdd((unsigned int*)&flags1[my_s + 1], flag1 + *count);
    *count1 = flag1 - 1;
    *count2 = flag2 - 1;
  }
#else
  if (threadIdx.x == 0){
    while (flags1[my_s] == 0){}
    int flag2 = flags2[my_s];
    flags2[my_s + 1] = flag2 + *count2;
    int flag1 = flags1[my_s];
    flags1[my_s + 1] = flag1 + *count1;
    *count1 = flag1 - 1;
    *count2 = flag2 - 1;
  }
#endif
  __syncthreads();
}

// Reduction kernel (CUDA SDK reduce6)
template <class S>
__device__ void reduction(S *count, S local_cnt){
    __shared__ S sdata[L_DIM];

    unsigned int tid = threadIdx.x;
    S mySum = local_cnt;

    // each thread puts its local sum into shared memory
    sdata[tid] = local_cnt;
    __syncthreads();

    // do reduction in shared mem
    if ((blockDim.x >= 1024) && (tid < 512)){
        sdata[tid] = mySum = mySum + sdata[tid + 512];
    }
    __syncthreads();

    if ((blockDim.x >= 512) && (tid < 256)){
        sdata[tid] = mySum = mySum + sdata[tid + 256];
    }
    __syncthreads();

    if ((blockDim.x >= 256) && (tid < 128)){
            sdata[tid] = mySum = mySum + sdata[tid + 128];
    }
     __syncthreads();

    if ((blockDim.x >= 128) && (tid <  64)){
       sdata[tid] = mySum = mySum + sdata[tid +  64];
    }
    __syncthreads();

#if (__CUDA_ARCH__ >= 300 )
    if ( tid < 32 ){
        // Fetch final intermediate sum from 2nd warp
        if (blockDim.x >=  64) mySum += sdata[tid + 32];
        // Reduce final warp using shuffle
        #pragma unroll
        for (int offset = WARP_SIZE/2; offset > 0; offset /= 2){
            //mySum += __shfl_down(mySum, offset);
            mySum += __shfl_xor(mySum, offset);
        }
    }
#else
    // fully unroll reduction within a single warp
    if ((blockDim.x >=  64) && (tid < 32)){
        sdata[tid] = mySum = mySum + sdata[tid + 32];
    }
    __syncthreads();

    if ((blockDim.x >=  32) && (tid < 16)){
        sdata[tid] = mySum = mySum + sdata[tid + 16];
    }
    __syncthreads();

    if ((blockDim.x >=  16) && (tid <  8)){
        sdata[tid] = mySum = mySum + sdata[tid +  8];
    }
    __syncthreads();

    if ((blockDim.x >=   8) && (tid <  4)){
        sdata[tid] = mySum = mySum + sdata[tid +  4];
    }
    __syncthreads();

    if ((blockDim.x >=   4) && (tid <  2)){
        sdata[tid] = mySum = mySum + sdata[tid +  2];
    }
    __syncthreads();

    if ((blockDim.x >=   2) && ( tid <  1)){
        sdata[tid] = mySum = mySum + sdata[tid +  1];
    }
    __syncthreads();
#endif

    // write result for this block to global mem
    if (tid == 0) *count = mySum;
}

// Binary prefix-sum (GPU Computing Gems)
__device__ inline int lane_id(void) { return threadIdx.x % WARP_SIZE; }
__device__ inline int warp_id(void) { return threadIdx.x / WARP_SIZE; }

__device__ unsigned int warp_prefix_sums(bool p){
  unsigned int b = __ballot(p);
  return __popc(b & ((1 << lane_id()) - 1));
}

__device__ int warp_scan(int val, volatile int *s_data){
#if (__CUDA_ARCH__ < 300 )
  int idx = 2 * threadIdx.x - (threadIdx.x & (WARP_SIZE - 1));
  s_data[idx] = 0;
  idx += WARP_SIZE;
  int t = s_data[idx] = val;
  s_data[idx] = t = t + s_data[idx - 1];
  s_data[idx] = t = t + s_data[idx - 2];
  s_data[idx] = t = t + s_data[idx - 4];
  s_data[idx] = t = t + s_data[idx - 8];
  s_data[idx] = t = t + s_data[idx - 16];
  return s_data[idx - 1];
#else
  int x = val;
  #pragma unroll
  for(int offset = 1; offset < 32; offset <<= 1){
  // From GTC: Kepler shuffle tips and tricks:
#if 0
    int y = __shfl_up(x, offset);
    if(lane_id() >= offset)
      x += y;
#else
    asm volatile("{"
        " .reg .s32 r0;"
        " .reg .pred p;"
        " shfl.up.b32 r0|p, %0, %1, 0x0;"
        " @p add.s32 r0, r0, %0;"
        " mov.s32 %0, r0;"
        "}" : "+r"(x) : "r"(offset));
#endif
  }
  return x - val;
#endif
}

__device__ int block_binary_prefix_sums(int* count, int x){

  __shared__ int sdata[L_DIM];

  // A. Exclusive scan within each warp
  int warpPrefix = warp_prefix_sums(x);

  // B. Store in shared memory
  if(lane_id() == WARP_SIZE - 1)
    sdata[warp_id()] = warpPrefix + x;
  __syncthreads();

  // C. One warp scans in shared memory
  if(threadIdx.x < WARP_SIZE)
    sdata[threadIdx.x] = warp_scan(sdata[threadIdx.x], sdata);
  __syncthreads();

  // D. Each thread calculates it final value
  int thread_out_element = warpPrefix + sdata[warp_id()];
  int output = thread_out_element + *count;
  __syncthreads();
  if(threadIdx.x == blockDim.x - 1)
    *count += (thread_out_element + x);

  return output;
}