-
Notifications
You must be signed in to change notification settings - Fork 9
/
queueCollector.cuh
342 lines (303 loc) · 11.5 KB
/
queueCollector.cuh
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
// Project Whippletree
// http://www.icg.tugraz.at/project/parallel
//
// Copyright (C) 2014 Institute for Computer Graphics and Vision,
// Graz University of Technology
//
// Author(s): Markus Steinberger - steinberger ( at ) icg.tugraz.at
// Michael Kenzel - kenzel ( at ) icg.tugraz.at
// Pedro Boechat - boechat ( at ) icg.tugraz.at
// Bernhard Kerbl - kerbl ( at ) icg.tugraz.at
// Mark Dokter - dokter ( at ) icg.tugraz.at
// Dieter Schmalstieg - schmalstieg ( at ) icg.tugraz.at
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#pragma once
#include "queueInterface.cuh"
#include "tools/common.cuh"
#include "queueHelpers.cuh"
template<uint TQueueSize, bool TAssertOnOverflow = true, bool TWarpOptimization = true, bool ConcurencyGuarantee = false, bool OverflowUnderflowCheck = true>
class QueueCollectorStub
{
protected:
static const uint QueueSize = TQueueSize;
volatile int readyCount;
int writeCount, front, back, backCounter;
__inline__ __device__ void init()
{
readyCount = writeCount = front = back = backCounter = 0;
}
__inline__ __device__ void printName()
{
printf("%s\n", __PRETTY_FUNCTION__);
}
static std::string name()
{
return (TWarpOptimization?std::string("CollectorWarpOptimized"):std::string("Collector")) + ((OverflowUnderflowCheck == false) ? std::string("NoCheck") : std::string(""));
}
// 得到enqueue的位置,作为函数返回值
// 同时更新back的值
template<int TThreadsPerElement>
__inline__ __device__ int2 enqueuePrep(int2 last)
{
if(TWarpOptimization)
{
//combine
uint mask = __ballot(1);
uint ourcount = __popc(mask)/TThreadsPerElement;
int mypos = __popc(Tools::lanemask_lt() & mask);
int wpos = -1;
if(mypos == 0)
{
if(OverflowUnderflowCheck)
{
int t = atomicAdd(&writeCount, ourcount);
if(t + ourcount >= TQueueSize)
{
if(TAssertOnOverflow)
{
printf("ERROR queue out of elements %d %d %d %d\n",writeCount,readyCount,front,back);
Tools::trap();
}
atomicSub(&writeCount, ourcount);
}
else
wpos = atomicAdd(&back, ourcount);
}
else
wpos = atomicAdd(&back, ourcount);
}
//get source
int src = __ffs(mask)-1;
wpos = warpBroadcast<32>(wpos, src);
//wpos = __shfl(wpos, src);
if(wpos == -1)
return make_int2(-1,0);
return make_int2(wpos + mypos/TThreadsPerElement, ourcount);
}
else
{
int pos = -1;
if(TThreadsPerElement == 1 || Tools::laneid() % TThreadsPerElement == 0)
{
if(OverflowUnderflowCheck)
{
int t = atomicAdd(&writeCount, 1);
if(t + 1 >= TQueueSize)
{
if(TAssertOnOverflow)
{
printf("ERROR queue out of elements %d %d %d %d\n",writeCount,readyCount,front,back);
//printf("ERROR queue out of elements %d+%d .. %d >%d\n", wpos, ourcount, wpos + ourcount - *static_cast<volatile uint*>(&front), QueueSize);
Tools::trap();
}
atomicSub(&writeCount, 1);
}
else
pos = atomicAdd(&back, 1);
}
else
pos = atomicAdd(&back, 1);
}
if(TThreadsPerElement > 1)
{
pos = warpBroadcast<TThreadsPerElement>(pos, 0);
//pos = __shfl(pos, 0, TThreadsPerElement);
}
if(pos != -1)
return make_int2(pos, 1);
else
return make_int2(pos, 0);
}
}
// TODO: 没有看懂,仿佛是更新readyCount为已经就位的数量?pos_ourcount是什么意思?
template<int TthreadsPerElement>
__inline__ __device__ void enqueueEnd(int2 pos_ourcount)
{
if(TWarpOptimization)
{
int mypos = __popc(Tools::lanemask_lt() & __ballot(1));
if(mypos == 0)
{
//this would guarantee that
if(ConcurencyGuarantee)
while(atomicCAS(&backCounter, pos_ourcount.x, pos_ourcount.x + pos_ourcount.y) != pos_ourcount.x)
__threadfence();
if(OverflowUnderflowCheck)
atomicAdd((int*)&readyCount,pos_ourcount.y);
}
}
else
if(TthreadsPerElement == 1 || Tools::laneid() % TthreadsPerElement == 0)
{
if(ConcurencyGuarantee)
while(atomicCAS(&backCounter, pos_ourcount.x, pos_ourcount.x + 1) != pos_ourcount.x)
__threadfence();
if(OverflowUnderflowCheck)
atomicAdd((int*)&readyCount,1);
}
}
__inline__ __device__ uint2 dequeuePrep(int num)
{
return make_uint2(0,0);
}
__inline__ __device__ void dequeueEnd(uint2 offset_take)
{
}
// 准备读取最多maxnum个数据,并更新readycount,
// only_read_all表示是否readycount不小于maxnum时才读取,true表示是
// 返回准备读取的数量
__inline__ __device__ int reserveRead(int maxnum, bool only_read_all = false)
{
printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
__shared__ int num;
if(threadIdx.x == 0)
{
if(OverflowUnderflowCheck)
{
int lnum = atomicSub((int*)&readyCount, maxnum);
if(lnum < maxnum && only_read_all)
{
atomicAdd((int*)&readyCount, maxnum);
num = 0;
}
else if(lnum < maxnum)
{
atomicAdd((int*)&readyCount, min(maxnum, maxnum - lnum));
num = max(0,min(lnum, maxnum));
}
else
num = maxnum;
}
else
{
num = maxnum;
}
}
__syncthreads();
return num;
}
// 读取num个元素,更新writeCount的值
__inline__ __device__ void finishRead(int id, int num)
{
if(OverflowUnderflowCheck && threadIdx.x == 0)
{
int prev = atomicSub(&writeCount, num);
//printf("finishread %d %d : %d->%d %d %d %d\n",id,num, prev, prev-num,readyCount,front,back);
}
}
public:
__inline__ __device__ void printName()
{
printf("%s\n", __PRETTY_FUNCTION__);
}
// 就位的元素的数量
__inline__ __device__ int size() const
{
printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
return readyCount;
}
};
template<uint TElementSize, uint TQueueSize, class TAdditionalData = void, bool TAssertOnOverflow = true, bool TWarpOptimization = true, bool OverflowUnderflowCheck = true>
class QueueCollector : public QueueBuilder<TElementSize, TQueueSize, TAdditionalData, QueueCollectorStub<TQueueSize, TAssertOnOverflow, TWarpOptimization, false, OverflowUnderflowCheck>, QueueStorage<TElementSize, TAdditionalData, TQueueSize> >
{
public:
__inline__ __device__ void printName()
{
printf("%s\n", __PRETTY_FUNCTION__);
}
__inline__ __device__ int dequeue(void* data, TAdditionalData* addtionalData, int maxnum)
{
printf("Error: QueueCollector does not implement dequeue!\n");
return 0;
}
__inline__ __device__ int reserveRead(int maxnum, bool only_read_all = false)
{
printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
return QueueCollectorStub<TQueueSize, TAssertOnOverflow, TWarpOptimization, false, OverflowUnderflowCheck>::reserveRead(maxnum, only_read_all);
}
__inline__ __device__ void finishRead(int id, int num)
{
return QueueCollectorStub<TQueueSize, TAssertOnOverflow, TWarpOptimization, false, OverflowUnderflowCheck>::finishRead(id, num);
}
// 更新additionalData[pos]的值为additionalStorage[front+pos]
// data的值为storage+front+pos,即当前队列中第pos个值的指针
// 准备读取num个值,front+=num
// 返回准备读取的数量num
__inline__ __device__ int startRead(void*& data, TAdditionalData* addtionalData, int pos, int num)
{
__shared__ int offset;
if(num > 0)
{
if(threadIdx.x == 0)
offset = atomicAdd(&front, num);
__syncthreads();
if(pos < num)
data = readDataPointers(addtionalData + pos, offset + pos);
}
return num;
}
};
template<uint TElementSize, uint TQueueSize, bool TAssertOnOverflow, bool TWarpOptimization, bool OverflowUnderflowCheck>
class QueueCollector<TElementSize, TQueueSize, void, TAssertOnOverflow, TWarpOptimization, OverflowUnderflowCheck> :
public QueueBuilder<TElementSize, TQueueSize, void, QueueCollectorStub<TQueueSize, TAssertOnOverflow, TWarpOptimization,
false, OverflowUnderflowCheck>, QueueStorage<TElementSize, void, TQueueSize> >
{
public:
__inline__ __device__ void printName()
{
printf("%s\n", __PRETTY_FUNCTION__);
}
__inline__ __device__ int dequeue(void* data, int maxnum)
{
printf("Error: QueueCollector does not implement dequeue!\n");
return 0;
}
__inline__ __device__ int reserveRead(int maxnum, bool only_read_all = false)
{
printf("%s in %s, at line %d\n", __FUNCTION__, __FILE__, __LINE__);
return QueueCollectorStub<TQueueSize, TAssertOnOverflow, TWarpOptimization, false, OverflowUnderflowCheck>::reserveRead(maxnum, only_read_all);
}
__inline__ __device__ void finishRead(int id, int num)
{
return QueueCollectorStub<TQueueSize, TAssertOnOverflow, TWarpOptimization, false, OverflowUnderflowCheck>::finishRead(id, num);
}
// data的值为storage+front+pos,即当前队列中第pos个值的指针
// 准备读取num个值,front+=num
// 返回准备读取的数量num
__inline__ __device__ int startRead(void*& data, int pos, int num)
{
__shared__ int offset;
if(num > 0)
{
if(threadIdx.x == 0)
offset = atomicAdd(&front, num);
__syncthreads();
if(pos < num)
data = readDataPointers(offset + pos);
}
return num;
}
};
template<uint TElementSize, uint TQueueSize, class TAdditionalData> class QueueCollectorNoOpt_t : public QueueCollector<TElementSize, TQueueSize, TAdditionalData, true, false> { };
template<uint TElementSize, uint TQueueSize, class TAdditionalData> class QueueCollector_t : public QueueCollector<TElementSize, TQueueSize, TAdditionalData, true> { };
template<uint TElementSize, uint TQueueSize, class TAdditionalData> class QueueCollectorNoOverflow_t : public QueueCollector<TElementSize, TQueueSize, TAdditionalData, false> { };
template<uint TElementSize, uint TQueueSize, class TAdditionalData> class QueueCollectorNoOptNoCheck_t : public QueueCollector<TElementSize, TQueueSize, TAdditionalData, true, false, false> { };
template<uint TElementSize, uint TQueueSize, class TAdditionalData> class QueueCollectorNoCheck_t : public QueueCollector<TElementSize, TQueueSize, TAdditionalData, true, true, false> { };