Source

exafmm / gpu / include / b40c / util / scan / cooperative_scan.cuh

Full commit
  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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/******************************************************************************
 * 
 * Copyright 2010-2011 Duane Merrill
 * 
 * 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. 
 * 
 * For more information, see our Google Code project site: 
 * http://code.google.com/p/back40computing/
 * 
 * Thanks!
 * 
 ******************************************************************************/

/******************************************************************************
 * Cooperative tile reduction and scanning within CTAs
 ******************************************************************************/

#pragma once

#include <b40c/util/device_intrinsics.cuh>
#include <b40c/util/srts_grid.cuh>
#include <b40c/util/reduction/cooperative_reduction.cuh>
#include <b40c/util/scan/serial_scan.cuh>
#include <b40c/util/scan/warp_scan.cuh>

namespace b40c {
namespace util {
namespace scan {

/**
 * Cooperative reduction in raking smem grid hierarchies
 */
template <
	typename RakingDetails,
	typename SecondaryRakingDetails = typename RakingDetails::SecondaryRakingDetails>
struct CooperativeGridScan;



/**
 * Cooperative tile scan
 */
template <
	int VEC_SIZE,							// Length of vector-loads (e.g, vec-1, vec-2, vec-4)
	bool EXCLUSIVE = true>					// Whether or not this is an exclusive scan
struct CooperativeTileScan
{
	//---------------------------------------------------------------------
	// Iteration structures for extracting partials from raking lanes and
	// using them to seed scans of tile vectors
	//---------------------------------------------------------------------

	// Next lane/load
	template <int LANE, int TOTAL_LANES>
	struct ScanLane
	{
		template <typename RakingDetails, typename ReductionOp>
		static __device__ __forceinline__ void Invoke(
			RakingDetails raking_details,
			typename RakingDetails::T data[RakingDetails::SCAN_LANES][VEC_SIZE],
			ReductionOp scan_op)
		{
			// Retrieve partial reduction from raking grid
			typename RakingDetails::T exclusive_partial = raking_details.lane_partial[LANE][0];

			// Scan the partials in this lane/load
			SerialScan<VEC_SIZE, EXCLUSIVE>::Invoke(
				data[LANE], exclusive_partial, scan_op);

			// Next load
			ScanLane<LANE + 1, TOTAL_LANES>::Invoke(
				raking_details, data, scan_op);
		}
	};

	// Terminate
	template <int TOTAL_LANES>
	struct ScanLane<TOTAL_LANES, TOTAL_LANES>
	{
		template <typename RakingDetails, typename ReductionOp>
		static __device__ __forceinline__ void Invoke(
			RakingDetails raking_details,
			typename RakingDetails::T data[RakingDetails::SCAN_LANES][VEC_SIZE],
			ReductionOp scan_op) {}
	};


	//---------------------------------------------------------------------
	// Interface
	//---------------------------------------------------------------------

	/**
	 * Scan a single tile.  Total aggregate is computed and returned in all threads.
	 *
	 * No post-synchronization needed before grid reuse.
	 */
	template <typename RakingDetails, typename ReductionOp>
	static __device__ __forceinline__ typename RakingDetails::T ScanTile(
		RakingDetails raking_details,
		typename RakingDetails::T data[RakingDetails::SCAN_LANES][VEC_SIZE],
		ReductionOp scan_op)
	{
		// Reduce partials in each vector-load, placing resulting partial in raking smem grid lanes (one lane per load)
		reduction::CooperativeTileReduction<VEC_SIZE>::template ReduceLane<0, RakingDetails::SCAN_LANES>::Invoke(
			raking_details, data, scan_op);

		__syncthreads();

		CooperativeGridScan<RakingDetails>::ScanTile(
			raking_details, scan_op);

		__syncthreads();

		// Scan each vector-load, seeded with the resulting partial from its raking grid lane,
		ScanLane<0, RakingDetails::SCAN_LANES>::Invoke(
			raking_details, data, scan_op);

		// Return last thread's inclusive partial
		return raking_details.CumulativePartial();
	}

	/**
	 * Scan a single tile where carry is updated with the total aggregate only
	 * in raking threads.
	 *
	 * No post-synchronization needed before grid reuse.
	 */
	template <typename RakingDetails, typename ReductionOp>
	static __device__ __forceinline__ void ScanTileWithCarry(
		RakingDetails raking_details,
		typename RakingDetails::T data[RakingDetails::SCAN_LANES][VEC_SIZE],
		typename RakingDetails::T &carry,
		ReductionOp scan_op)
	{
		// Reduce partials in each vector-load, placing resulting partials in raking smem grid lanes (one lane per load)
		reduction::CooperativeTileReduction<VEC_SIZE>::template ReduceLane<0, RakingDetails::SCAN_LANES>::Invoke(
			raking_details, data, scan_op);

		__syncthreads();

		CooperativeGridScan<RakingDetails>::ScanTileWithCarry(
			raking_details, carry, scan_op);

		__syncthreads();

		// Scan each vector-load, seeded with the resulting partial from its raking grid lane,
		ScanLane<0, RakingDetails::SCAN_LANES>::Invoke(
			raking_details, data, scan_op);
	}


	/**
	 * Scan a single tile with atomic enqueue.  Returns updated queue offset.
	 *
	 * No post-synchronization needed before grid reuse.
	 */
	template <typename RakingDetails, typename ReductionOp>
	static __device__ __forceinline__ typename RakingDetails::T ScanTileWithEnqueue(
		RakingDetails raking_details,
		typename RakingDetails::T data[RakingDetails::SCAN_LANES][VEC_SIZE],
		typename RakingDetails::T* d_enqueue_counter,
		ReductionOp scan_op)
	{
		// Reduce partials in each vector-load, placing resulting partial in raking smem grid lanes (one lane per load)
		reduction::CooperativeTileReduction<VEC_SIZE>::template ReduceLane<0, RakingDetails::SCAN_LANES>::Invoke(
			raking_details, data, scan_op);

		__syncthreads();

		CooperativeGridScan<RakingDetails>::ScanTileWithEnqueue(
			raking_details, d_enqueue_counter, scan_op);

		__syncthreads();

		// Scan each vector-load, seeded with the resulting partial from its raking grid lane,
		ScanLane<0, RakingDetails::SCAN_LANES>::Invoke(
			raking_details, data, scan_op);

		return scan_op(raking_details.QueueReservation(), raking_details.CumulativePartial());
	}


	/**
	 * Scan a single tile with atomic enqueue.  Local aggregate is computed and
	 * returned in all threads.  Enqueue offset is returned in all threads.
	 *
	 * No post-synchronization needed before grid reuse.
	 */
	template <typename RakingDetails, typename ReductionOp>
	static __device__ __forceinline__ typename RakingDetails::T ScanTileWithEnqueue(
		RakingDetails raking_details,
		typename RakingDetails::T data[RakingDetails::SCAN_LANES][VEC_SIZE],
		typename RakingDetails::T *d_enqueue_counter,
		typename RakingDetails::T &enqueue_offset,
		ReductionOp scan_op)
	{
		// Reduce partials in each vector-load, placing resulting partial in raking smem grid lanes (one lane per load)
		reduction::CooperativeTileReduction<VEC_SIZE>::template ReduceLane<0, RakingDetails::SCAN_LANES>::Invoke(
			raking_details, data, scan_op);

		__syncthreads();

		CooperativeGridScan<RakingDetails>::ScanTileWithEnqueue(
			raking_details, d_enqueue_counter, enqueue_offset, scan_op);

		__syncthreads();

		// Scan each vector-load, seeded with the resulting partial from its raking grid lane,
		ScanLane<0, RakingDetails::SCAN_LANES>::Invoke(
			raking_details, data, scan_op);

		// Return last thread's inclusive partial
		return raking_details.CumulativePartial();
	}
};




/******************************************************************************
 * CooperativeGridScan
 ******************************************************************************/

/**
 * Cooperative raking grid reduction (specialized for last-level of raking grid)
 */
template <typename RakingDetails>
struct CooperativeGridScan<RakingDetails, NullType>
{
	typedef typename RakingDetails::T T;

	/**
	 * Scan in last-level raking grid.
	 */
	template <typename ReductionOp>
	static __device__ __forceinline__ void ScanTile(
		RakingDetails raking_details,
		ReductionOp scan_op)
	{
		if (threadIdx.x < RakingDetails::RAKING_THREADS) {

			// Raking reduction
			T inclusive_partial = reduction::SerialReduce<RakingDetails::PARTIALS_PER_SEG>::Invoke(
				raking_details.raking_segment, scan_op);

			// Exclusive warp scan
			T exclusive_partial = WarpScan<RakingDetails::LOG_RAKING_THREADS>::Invoke(
				inclusive_partial, raking_details.warpscan, scan_op);

			// Exclusive raking scan
			SerialScan<RakingDetails::PARTIALS_PER_SEG>::Invoke(
				raking_details.raking_segment, exclusive_partial, scan_op);

		}
	}


	/**
	 * Scan in last-level raking grid.  Carry-in/out is updated only in raking threads
	 */
	template <typename ReductionOp>
	static __device__ __forceinline__ void ScanTileWithCarry(
		RakingDetails raking_details,
		T &carry,
		ReductionOp scan_op)
	{
		if (threadIdx.x < RakingDetails::RAKING_THREADS) {

			// Raking reduction
			T inclusive_partial = reduction::SerialReduce<RakingDetails::PARTIALS_PER_SEG>::Invoke(
				raking_details.raking_segment, scan_op);

			// Exclusive warp scan, get total
			T warpscan_total;
			T exclusive_partial = WarpScan<RakingDetails::LOG_RAKING_THREADS>::Invoke(
				inclusive_partial, warpscan_total, raking_details.warpscan, scan_op);

			// Seed exclusive partial with carry-in
			exclusive_partial = scan_op(carry, exclusive_partial);

			// Exclusive raking scan
			SerialScan<RakingDetails::PARTIALS_PER_SEG>::Invoke(
				raking_details.raking_segment, exclusive_partial, scan_op);

			// Update carry
			carry = scan_op(carry, warpscan_total);			// Increment the CTA's running total by the full tile reduction
		}
	}


	/**
	 * Scan in last-level raking grid with atomic enqueue
	 */
	template <typename ReductionOp>
	static __device__ __forceinline__ void ScanTileWithEnqueue(
		RakingDetails raking_details,
		T *d_enqueue_counter,
		ReductionOp scan_op)
	{
		if (threadIdx.x < RakingDetails::RAKING_THREADS) {

			// Raking reduction
			T inclusive_partial = reduction::SerialReduce<RakingDetails::PARTIALS_PER_SEG>::Invoke(
				raking_details.raking_segment, scan_op);

			// Exclusive warp scan, get total
			T warpscan_total;
			T exclusive_partial = WarpScan<RakingDetails::LOG_RAKING_THREADS>::Invoke(
					inclusive_partial, warpscan_total, raking_details.warpscan, scan_op);

			// Atomic-increment the global counter with the total allocation
			T reservation_offset;
			if (threadIdx.x == 0) {
				reservation_offset = util::AtomicInt<T>::Add(
					d_enqueue_counter,
					warpscan_total);
				raking_details.warpscan[1][0] = reservation_offset;
			}

			// Seed exclusive partial with queue reservation offset
			reservation_offset = raking_details.warpscan[1][0];
			exclusive_partial = scan_op(reservation_offset, exclusive_partial);

			// Exclusive raking scan
			SerialScan<RakingDetails::PARTIALS_PER_SEG>::Invoke(
				raking_details.raking_segment, exclusive_partial, scan_op);
		}
	}

	/**
	 * Scan in last-level raking grid with atomic enqueue
	 */
	template <typename ReductionOp>
	static __device__ __forceinline__ void ScanTileWithEnqueue(
		RakingDetails raking_details,
		T *d_enqueue_counter,
		T &enqueue_offset,
		ReductionOp scan_op)
	{
		if (threadIdx.x < RakingDetails::RAKING_THREADS) {

			// Raking reduction
			T inclusive_partial = reduction::SerialReduce<RakingDetails::PARTIALS_PER_SEG>::Invoke(
				raking_details.raking_segment, scan_op);

			// Exclusive warp scan, get total
			T warpscan_total;
			T exclusive_partial = WarpScan<RakingDetails::LOG_RAKING_THREADS>::Invoke(
				inclusive_partial, warpscan_total, raking_details.warpscan, scan_op);

			// Atomic-increment the global counter with the total allocation
			if (threadIdx.x == 0) {
				enqueue_offset = util::AtomicInt<T>::Add(
					d_enqueue_counter,
					warpscan_total);
			}

			// Exclusive raking scan
			SerialScan<RakingDetails::PARTIALS_PER_SEG>::Invoke(
				raking_details.raking_segment, exclusive_partial, scan_op);
		}
	}
};


/**
 * Cooperative raking grid reduction for multi-level raking grids
 */
template <
	typename RakingDetails,
	typename SecondaryRakingDetails>
struct CooperativeGridScan
{
	typedef typename RakingDetails::T T;

	/**
	 * Scan in raking grid.
	 */
	template <typename ReductionOp>
	static __device__ __forceinline__ void ScanTile(
		RakingDetails raking_details,
		ReductionOp scan_op)
	{
		if (threadIdx.x < RakingDetails::RAKING_THREADS) {

			// Raking reduction
			T partial = reduction::SerialReduce<RakingDetails::PARTIALS_PER_SEG>::Invoke(
				raking_details.raking_segment, scan_op);

			// Place partial in next grid
			raking_details.secondary_details.lane_partial[0][0] = partial;
		}

		__syncthreads();

		// Collectively scan in next grid
		CooperativeGridScan<SecondaryRakingDetails>::ScanTile(
			raking_details.secondary_details, scan_op);

		__syncthreads();

		if (threadIdx.x < RakingDetails::RAKING_THREADS) {

			// Retrieve partial from next grid
			T exclusive_partial = raking_details.secondary_details.lane_partial[0][0];

			// Exclusive raking scan
			SerialScan<RakingDetails::PARTIALS_PER_SEG>::Invoke(
				raking_details.raking_segment, exclusive_partial, scan_op);
		}
	}

	/**
	 * Scan in raking grid.  Carry-in/out is updated only in raking threads (homogeneously)
	 */
	template <typename ReductionOp>
	static __device__ __forceinline__ void ScanTileWithCarry(
		RakingDetails raking_details,
		T &carry,
		ReductionOp scan_op)
	{
		if (threadIdx.x < RakingDetails::RAKING_THREADS) {

			// Raking reduction
			T partial = reduction::SerialReduce<RakingDetails::PARTIALS_PER_SEG>::Invoke(
				raking_details.raking_segment, scan_op);

			// Place partial in next grid
			raking_details.secondary_details.lane_partial[0][0] = partial;
		}

		__syncthreads();

		// Collectively scan in next grid
		CooperativeGridScan<SecondaryRakingDetails>::ScanTileWithCarry(
			raking_details.secondary_details, carry, scan_op);

		__syncthreads();

		if (threadIdx.x < RakingDetails::RAKING_THREADS) {

			// Retrieve partial from next grid
			T exclusive_partial = raking_details.secondary_details.lane_partial[0][0];

			// Exclusive raking scan
			SerialScan<RakingDetails::PARTIALS_PER_SEG>::Invoke(
				raking_details.raking_segment, exclusive_partial, scan_op);
		}
	}

	/**
	 * Scan in raking grid.  Carry-in/out is updated only in raking threads (homogeneously)
	 */
	template <typename ReductionOp>
	static __device__ __forceinline__ void ScanTileWithEnqueue(
		RakingDetails raking_details,
		T* d_enqueue_counter,
		ReductionOp scan_op)
	{
		if (threadIdx.x < RakingDetails::RAKING_THREADS) {

			// Raking reduction
			T partial = reduction::SerialReduce<RakingDetails::PARTIALS_PER_SEG>::Invoke(
				raking_details.raking_segment, scan_op);

			// Place partial in next grid
			raking_details.secondary_details.lane_partial[0][0] = partial;
		}

		__syncthreads();

		// Collectively scan in next grid
		CooperativeGridScan<SecondaryRakingDetails>::ScanTileWithEnqueue(
			raking_details.secondary_details, d_enqueue_counter, scan_op);

		__syncthreads();

		if (threadIdx.x < RakingDetails::RAKING_THREADS) {

			// Retrieve partial from next grid
			T exclusive_partial = raking_details.secondary_details.lane_partial[0][0];

			// Exclusive raking scan
			SerialScan<RakingDetails::PARTIALS_PER_SEG>::Invoke(
				raking_details.raking_segment, exclusive_partial, scan_op);
		}
	}
};



} // namespace scan
} // namespace util
} // namespace b40c