-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scan_SW.cu
executable file
·201 lines (162 loc) · 5.77 KB
/
Scan_SW.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
#ifndef _PRESCAN_CU_
#define _PRESCAN_CU_
// includes, kernels
#include <Scan_SW_kernel.cu>
#include <assert.h>
inline bool
isPowerOfTwo(int n)
{
return ((n&(n-1))==0) ;
}
inline int
floorPow2(int n)
{
#ifdef WIN32
// method 2
return 1 << (int)logb((float)n);
#else
// method 1
// float nf = (float)n;
// return 1 << (((*(int*)&nf) >> 23) - 127);
int exp;
frexp((float)n, &exp);
return 1 << (exp - 1);
#endif
}
#define BLOCK_SIZE 512
int** g_scanBlockSums;
int* g_scanStride;
unsigned int g_numEltsAllocated = 0;
unsigned int g_numLevelsAllocated = 0;
void preallocBlockSums(unsigned int maxNumElements)
{
assert(g_numEltsAllocated == 0); // shouldn't be called
g_numEltsAllocated = maxNumElements;
unsigned int blockSize = BLOCK_SIZE; // max size of the thread blocks
unsigned int numElts = maxNumElements;
int level = 0;
do
{
unsigned int numBlocks =
max(1, (int)ceil((float)numElts / (2.f * blockSize)));
if (numBlocks > 1)
{
level++;
}
numElts = numBlocks;
} while (numElts > 1);
g_scanBlockSums = (int**) malloc(level * sizeof(int*));
CUDA_SAFE_CALL(cudaMalloc((void**) &g_scanStride,
level* sizeof(int))); /////////////
g_numLevelsAllocated = level;
numElts = maxNumElements;
level = 0;
do
{
unsigned int numBlocks =
max(1, (int)ceil((float)numElts / (2.f * blockSize)));
if (numBlocks > 1)
{
CUDA_SAFE_CALL(cudaMalloc((void**) &g_scanBlockSums[level++],
numBlocks * sizeof(int)));
}
numElts = numBlocks;
} while (numElts > 1);
CUT_CHECK_ERROR("preallocBlockSums");
}
void deallocBlockSums()
{
for (unsigned int i = 0; i < g_numLevelsAllocated; i++)
{
cudaFree(g_scanBlockSums[i]);
}
CUT_CHECK_ERROR("deallocBlockSums");
free((void**)g_scanBlockSums);
cudaFree(g_scanStride);
g_scanBlockSums = 0;
g_numEltsAllocated = 0;
g_numLevelsAllocated = 0;
}
void prescanArrayRecursive(int *outArray,
const int *inArray,
int numElements,
int level)
{
unsigned int blockSize = BLOCK_SIZE; // max size of the thread blocks
unsigned int numBlocks =
max(1, (int)ceil((float)numElements / (2.f * blockSize)));
unsigned int numThreads;
if (numBlocks > 1)
numThreads = blockSize;
else if (isPowerOfTwo(numElements))
numThreads = numElements / 2;
else
numThreads = floorPow2(numElements);
unsigned int numEltsPerBlock = numThreads * 2;
// if this is a non-power-of-2 array, the last block will be non-full
// compute the smallest power of 2 able to compute its scan.
unsigned int numEltsLastBlock =
numElements - (numBlocks-1) * numEltsPerBlock;
unsigned int numThreadsLastBlock = max(1, numEltsLastBlock / 2);
unsigned int np2LastBlock = 0;
unsigned int sharedMemLastBlock = 0;
if (numEltsLastBlock != numEltsPerBlock)
{
np2LastBlock = 1;
if(!isPowerOfTwo(numEltsLastBlock))
numThreadsLastBlock = floorPow2(numEltsLastBlock);
unsigned int extraSpace = (2 * numThreadsLastBlock) / NUM_BANKS;
sharedMemLastBlock =
sizeof(int) * (2 * numThreadsLastBlock + extraSpace);
}
// padding space is used to avoid shared memory bank conflicts
unsigned int extraSpace = numEltsPerBlock / NUM_BANKS;
unsigned int sharedMemSize =
sizeof(int) * (numEltsPerBlock + extraSpace);
#ifdef DEBUG
if (numBlocks > 1)
{
assert(g_numEltsAllocated >= numElements);
}
#endif
// setup execution parameters
// if NP2, we process the last block separately
dim3 grid(max(1, numBlocks - np2LastBlock), 1, 1);
dim3 threads(numThreads, 1, 1);
// make sure there are no CUDA errors before we start
CUT_CHECK_ERROR("prescanArrayRecursive before kernels");
// execute the scan
if (numBlocks > 1)
{
prescan<true, false><<< grid, threads, sharedMemSize >>>(outArray,
inArray,
g_scanBlockSums[level],
g_scanStride,
numThreads * 2, 0, 0);
CUT_CHECK_ERROR("prescanWithBlockSums");
if (np2LastBlock)
{
prescan<true, true><<< 1, numThreadsLastBlock, sharedMemLastBlock >>>
(outArray, inArray, g_scanBlockSums[level], g_scanStride, numEltsLastBlock,
numBlocks - 1, numElements - numEltsLastBlock);
CUT_CHECK_ERROR("prescanNP2WithBlockSums");
}
}
else if (isPowerOfTwo(numElements))
{
prescan<false, false><<< grid, threads, sharedMemSize >>>(outArray, inArray,
0, 0,numThreads * 2, 0, 0);
CUT_CHECK_ERROR("prescan");
}
else
{
prescan<false, true><<< grid, threads, sharedMemSize >>>(outArray, inArray,
0, 0,numElements, 0, 0);
CUT_CHECK_ERROR("prescanNP2");
}
}
void prescanArray(int *outArray, int *inArray, int numElements)
{
prescanArrayRecursive(outArray, inArray, numElements, 0);
}
#endif // _PRESCAN_CU_