Source

exafmm / gpu / include / b40c / radix_sort / enactor.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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/******************************************************************************
 * 
 * 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/
 * 
 ******************************************************************************/

/******************************************************************************
 * Radix sorting enactor
 ******************************************************************************/

#pragma once

#include <b40c/util/spine.cuh>
#include <b40c/util/ping_pong_storage.cuh>
#include <b40c/util/numeric_traits.cuh>
#include <b40c/util/io/modified_load.cuh>
#include <b40c/util/io/modified_store.cuh>
#include <b40c/util/kernel_props.cuh>
#include <b40c/util/spine.cuh>
#include <b40c/util/vector_types.cuh>
#include <b40c/util/error_utils.cuh>

#include <b40c/radix_sort/sort_utils.cuh>
#include <b40c/radix_sort/policy.cuh>
#include <b40c/radix_sort/upsweep/kernel_policy.cuh>
#include <b40c/radix_sort/upsweep/kernel.cuh>

#include <b40c/radix_sort/spine/kernel_policy.cuh>
#include <b40c/radix_sort/spine/kernel.cuh>
#include <b40c/radix_sort/spine/tex_ref.cuh>

#include <b40c/radix_sort/downsweep/kernel_policy.cuh>
#include <b40c/radix_sort/downsweep/kernel.cuh>
#include <b40c/radix_sort/downsweep/tex_ref.cuh>

namespace b40c {
namespace radix_sort {


/******************************************************************************
 * Problem instance
 ******************************************************************************/

/**
 * Problem instance
 */
template <
	typename DoubleBuffer,
	typename SizeT>
struct ProblemInstance
{
	DoubleBuffer		&storage;
	SizeT				num_elements;

	util::Spine			&spine;
	int			 		max_grid_size;
	int 				ptx_arch;
	int 				sm_arch;
	int					sm_count;
	bool				debug;

	/**
	 * Constructor
	 */
	ProblemInstance(
		DoubleBuffer	&storage,
		SizeT			num_elements,
		util::Spine		&spine,
		int			 	max_grid_size,
		int 			ptx_arch,
		int 			sm_arch,
		int				sm_count,
		bool			debug) :
			spine(spine),
			storage(storage),
			num_elements(num_elements),
			max_grid_size(max_grid_size),
			ptx_arch(ptx_arch),
			sm_arch(sm_arch),
			sm_count(sm_count),
			debug(debug)
	{}
};


/******************************************************************************
 * Sorting pass
 ******************************************************************************/

/**
 * Sorting pass
 */
template <
	typename KeyType,
	typename ValueType,
	typename SizeT>
struct SortingPass
{
	//---------------------------------------------------------------------
	// Type definitions
	//---------------------------------------------------------------------

	// Converted key type
	typedef typename KeyTraits<KeyType>::ConvertedKeyType ConvertedKeyType;

	// Kernel function types
	typedef void (*UpsweepKernelFunc)(SizeT*, ConvertedKeyType*, ConvertedKeyType*, util::CtaWorkDistribution<SizeT>);
	typedef void (*SpineKernelFunc)(SizeT*, SizeT*, int);
	typedef void (*DownsweepKernelFunc)(SizeT*, ConvertedKeyType*, ConvertedKeyType*, ValueType*, ValueType*, util::CtaWorkDistribution<SizeT>);

	// Texture binding function types
	typedef cudaError_t (*BindKeyTexFunc)(void *, void *, size_t);
	typedef cudaError_t (*BindValueTexFunc)(void *, void *, size_t);
	typedef cudaError_t (*BindTexSpineFunc)(void *, size_t);


	//---------------------------------------------------------------------
	// Methods
	//---------------------------------------------------------------------

	/**
	 * Dispatch
	 */
	template <typename ProblemInstance>
	static cudaError_t Dispatch(
		ProblemInstance							problem_instance,
		int 									radix_bits,
		util::KernelProps<UpsweepKernelFunc> 	&upsweep_props,
		util::KernelProps<SpineKernelFunc> 		&spine_props,
		util::KernelProps<DownsweepKernelFunc> 	&downsweep_props,
		BindKeyTexFunc 							bind_key_texture_func,
		BindValueTexFunc 						bind_value_texture_func,
		BindTexSpineFunc 						bind_spine_texture_func,
		int 									log_schedule_granularity,
		int										upsweep_tile_elements,
		int										spine_tile_elements,
		int										downsweep_tile_elements,
		bool									smem_8byte_banks,
		bool									unform_grid_size,
		bool									uniform_smem_allocation)
	{
		cudaError_t error = cudaSuccess;

		do {
			// Compute sweep grid size
			int schedule_granularity = 1 << log_schedule_granularity;
			int sweep_grid_size = downsweep_props.OversubscribedGridSize(
				schedule_granularity,
				problem_instance.num_elements,
				problem_instance.max_grid_size);

			// Compute spine elements (rounded up to nearest tile size)
			SizeT spine_elements = CUB_ROUND_UP_NEAREST(
				sweep_grid_size << radix_bits,
				spine_tile_elements);

			// Make sure our spine is big enough
			error = problem_instance.spine.Setup(sizeof(SizeT) * spine_elements);
			if (error) break;

			// Obtain a CTA work distribution
			util::CtaWorkDistribution<SizeT> work(
				problem_instance.num_elements,
				sweep_grid_size,
				log_schedule_granularity);

			if (problem_instance.debug) {
				work.Print();
			}

			// Bind key textures
			if (bind_key_texture_func != NULL) {
				error = bind_key_texture_func(
					problem_instance.storage.d_keys[problem_instance.storage.selector],
					problem_instance.storage.d_keys[problem_instance.storage.selector ^ 1],
					sizeof(ConvertedKeyType) * problem_instance.num_elements);
				if (error) break;
			}

			// Bind value textures
			if (bind_value_texture_func != NULL) {
				error = bind_value_texture_func(
					problem_instance.storage.d_values[problem_instance.storage.selector],
					problem_instance.storage.d_values[problem_instance.storage.selector ^ 1],
					sizeof(ValueType) * problem_instance.num_elements);
				if (error) break;
			}

			// Bind spine textures
			if (bind_spine_texture_func != NULL) {
				error = bind_spine_texture_func(
					problem_instance.spine(),
					sizeof(SizeT) * spine_elements);
				if (error) break;
			}

			// Operational details
			int dynamic_smem[3] = 	{0, 0, 0};
			int grid_size[3] = 		{sweep_grid_size, 1, sweep_grid_size};

			// Grid size tuning
			if (unform_grid_size) {
				// Make sure that all kernels launch the same number of CTAs
				grid_size[1] = grid_size[0];
			}

			// Smem allocation tuning
			if (uniform_smem_allocation) {

				// Make sure all kernels have the same overall smem allocation
				int max_static_smem = CUB_MAX(
					upsweep_props.kernel_attrs.sharedSizeBytes,
					CUB_MAX(
						spine_props.kernel_attrs.sharedSizeBytes,
						downsweep_props.kernel_attrs.sharedSizeBytes));

				dynamic_smem[0] = max_static_smem - upsweep_props.kernel_attrs.sharedSizeBytes;
				dynamic_smem[1] = max_static_smem - spine_props.kernel_attrs.sharedSizeBytes;
				dynamic_smem[2] = max_static_smem - downsweep_props.kernel_attrs.sharedSizeBytes;

			} else {

				// Compute smem padding for upsweep to make upsweep occupancy a multiple of downsweep occupancy
				dynamic_smem[0] = upsweep_props.SmemPadding(downsweep_props.max_cta_occupancy);
			}

			if (problem_instance.debug) {
				printf(
					"Upsweep:   tile size(%d), occupancy(%d), grid_size(%d), threads(%d), dynamic smem(%d)\n"
					"Spine:     tile size(%d), occupancy(%d), grid_size(%d), threads(%d), dynamic smem(%d)\n"
					"Downsweep: tile size(%d), occupancy(%d), grid_size(%d), threads(%d), dynamic smem(%d)\n",
					upsweep_tile_elements, upsweep_props.max_cta_occupancy, grid_size[0], upsweep_props.threads, dynamic_smem[0],
					spine_tile_elements, spine_props.max_cta_occupancy, grid_size[1], spine_props.threads, dynamic_smem[1],
					downsweep_tile_elements, downsweep_props.max_cta_occupancy, grid_size[2], downsweep_props.threads, dynamic_smem[2]);
				fflush(stdout);
			}

			// Upsweep reduction into spine
			upsweep_props.kernel_func<<<grid_size[0], upsweep_props.threads, dynamic_smem[0]>>>(
				(SizeT*) problem_instance.spine(),
				(ConvertedKeyType *) problem_instance.storage.d_keys[problem_instance.storage.selector],
				(ConvertedKeyType *) problem_instance.storage.d_keys[problem_instance.storage.selector ^ 1],
				work);

			if (problem_instance.debug) {
				error = cudaThreadSynchronize();
				if (error = util::B40CPerror(error, "Upsweep kernel failed ", __FILE__, __LINE__)) break;
			}

			// Spine scan
			spine_props.kernel_func<<<grid_size[1], spine_props.threads, dynamic_smem[1]>>>(
				(SizeT*) problem_instance.spine(),
				(SizeT*) problem_instance.spine(),
				spine_elements);

			if (problem_instance.debug) {
				error = cudaThreadSynchronize();
				if (error = util::B40CPerror(error, "Spine kernel failed ", __FILE__, __LINE__)) break;
			}

			// Set shared mem bank mode
			enum cudaSharedMemConfig old_config;
			cudaDeviceGetSharedMemConfig(&old_config);
			cudaDeviceSetSharedMemConfig(smem_8byte_banks ?
				cudaSharedMemBankSizeEightByte :		// 64-bit bank mode
				cudaSharedMemBankSizeFourByte);			// 32-bit bank mode

			// Downsweep scan from spine
			downsweep_props.kernel_func<<<grid_size[2], downsweep_props.threads, dynamic_smem[2]>>>(
				(SizeT *) problem_instance.spine(),
				(ConvertedKeyType *) problem_instance.storage.d_keys[problem_instance.storage.selector],
				(ConvertedKeyType *) problem_instance.storage.d_keys[problem_instance.storage.selector ^ 1],
				problem_instance.storage.d_values[problem_instance.storage.selector],
				problem_instance.storage.d_values[problem_instance.storage.selector ^ 1],
				work);

			if (problem_instance.debug) {
				error = cudaThreadSynchronize();
				if (error = util::B40CPerror(error, "Downsweep kernel failed ", __FILE__, __LINE__)) break;
			}

			// Restore smem bank mode
			cudaDeviceSetSharedMemConfig(old_config);

		} while(0);

		return error;
	}


	/**
	 * Dispatch
	 */
	template <
		typename HostPassPolicy,
		typename DevicePassPolicy,
		typename ProblemInstance>
	static cudaError_t Dispatch(ProblemInstance &problem_instance)
	{
		typedef typename HostPassPolicy::UpsweepPolicy 		UpsweepPolicy;
		typedef typename HostPassPolicy::SpinePolicy 		SpinePolicy;
		typedef typename HostPassPolicy::DownsweepPolicy 	DownsweepPolicy;
		typedef typename HostPassPolicy::DispatchPolicy	 	DispatchPolicy;

		// Wrapper of downsweep texture types
		typedef downsweep::Textures<
			KeyType,
			ValueType,
			(1 << DownsweepPolicy::LOG_THREAD_ELEMENTS)> DownsweepTextures;

		// Downsweep key texture type
		typedef typename DownsweepTextures::KeyTexType KeyTexType;

		// Downsweep value texture type
		typedef typename DownsweepTextures::ValueTexType ValueTexType;

		// Upsweep kernel properties
		util::KernelProps<UpsweepKernelFunc> upsweep_props(
			upsweep::Kernel<typename DevicePassPolicy::UpsweepPolicy>,
			UpsweepPolicy::THREADS,
			problem_instance.sm_arch,
			problem_instance.sm_count);

		// Spine kernel properties
		util::KernelProps<SpineKernelFunc> spine_props(
			spine::Kernel<typename DevicePassPolicy::SpinePolicy>,
			SpinePolicy::THREADS,
			problem_instance.sm_arch,
			problem_instance.sm_count);

		// Downsweep kernel properties
		util::KernelProps<DownsweepKernelFunc> downsweep_props(
			downsweep::Kernel<typename DevicePassPolicy::DownsweepPolicy>,
			DownsweepPolicy::THREADS,
			problem_instance.sm_arch,
			problem_instance.sm_count);

		// Schedule granularity
		int log_schedule_granularity = CUB_MAX(
			int(UpsweepPolicy::LOG_TILE_ELEMENTS),
			int(DownsweepPolicy::LOG_TILE_ELEMENTS));

		// Texture binding for downsweep keys
		BindKeyTexFunc bind_key_texture_func =
			downsweep::TexKeys<KeyTexType>::BindTexture;

		// Texture binding for downsweep values
		BindValueTexFunc bind_value_texture_func =
			downsweep::TexValues<ValueTexType>::BindTexture;

		// Texture binding for spine
		BindTexSpineFunc bind_spine_texture_func =
			spine::TexSpine<SizeT>::BindTexture;

		return Dispatch(
			problem_instance,
			DispatchPolicy::RADIX_BITS,
			upsweep_props,
			spine_props,
			downsweep_props,
			bind_key_texture_func,
			bind_value_texture_func,
			bind_spine_texture_func,
			log_schedule_granularity,
			(1 << UpsweepPolicy::LOG_TILE_ELEMENTS),
			(1 << SpinePolicy::LOG_TILE_ELEMENTS),
			(1 << DownsweepPolicy::LOG_TILE_ELEMENTS),
			DownsweepPolicy::SMEM_8BYTE_BANKS,
			DispatchPolicy::UNIFORM_GRID_SIZE,
			DispatchPolicy::UNIFORM_SMEM_ALLOCATION);
	}


	/**
	 * Dispatch.  Custom tuning interface.
	 */
	template <
		typename PassPolicy,
		typename ProblemInstance>
	static cudaError_t Dispatch(ProblemInstance &problem_instance)
	{
		return Dispatch<PassPolicy, PassPolicy>(problem_instance);
	}


	//---------------------------------------------------------------------
	// Preconfigured pass dispatch
	//---------------------------------------------------------------------

	/**
	 * Specialized pass policies
	 */
	template <
		int TUNE_ARCH,
		int BITS_REMAINING,
		int CURRENT_BIT,
		int CURRENT_PASS>
	struct TunedPassPolicy;


	/**
	 * SM20
	 */
	template <int BITS_REMAINING, int CURRENT_BIT, int CURRENT_PASS>
	struct TunedPassPolicy<200, BITS_REMAINING, CURRENT_BIT, CURRENT_PASS>
	{
		enum {
			RADIX_BITS 		= CUB_MIN(BITS_REMAINING, ((BITS_REMAINING + 4) % 5 > 3) ? 5 : 4),
			KEYS_ONLY 		= util::Equals<ValueType, util::NullType>::VALUE,
			EARLY_EXIT 		= false,
			LARGE_DATA		= (sizeof(KeyType) > 4) || (sizeof(ValueType) > 4),
		};

		// Dispatch policy
		typedef radix_sort::DispatchPolicy <
			200,								// TUNE_ARCH
			RADIX_BITS,							// RADIX_BITS
			false, 								// UNIFORM_SMEM_ALLOCATION
			true> 								// UNIFORM_GRID_SIZE
				DispatchPolicy;

		// Upsweep kernel policy
		typedef upsweep::KernelPolicy<
			RADIX_BITS,						// RADIX_BITS
			CURRENT_BIT,					// CURRENT_BIT
			CURRENT_PASS,					// CURRENT_PASS
			8,								// MIN_CTA_OCCUPANCY	The targeted SM occupancy to feed PTXAS in order to influence how it does register allocation
			7,								// LOG_THREADS			The number of threads (log) to launch per CTA.  Valid range: 5-10
			LARGE_DATA ? 1 : 2,				// LOG_LOAD_VEC_SIZE	The vector-load size (log) for each load (log).  Valid range: 0-2
			1,								// LOG_LOADS_PER_TILE	The number of loads (log) per tile.  Valid range: 0-2
			b40c::util::io::ld::NONE,		// READ_MODIFIER		Load cache-modifier.  Valid values: NONE, ca, cg, cs
			b40c::util::io::st::NONE,		// WRITE_MODIFIER		Store cache-modifier.  Valid values: NONE, wb, cg, cs
			EARLY_EXIT>						// EARLY_EXIT			Whether or not to early-terminate a sorting pass if we detect all keys have the same digit in that pass's digit place
				UpsweepPolicy;

		// Spine-scan kernel policy
		typedef spine::KernelPolicy<
			8,								// LOG_THREADS			The number of threads (log) to launch per CTA.  Valid range: 5-10
			2,								// LOG_LOAD_VEC_SIZE	The vector-load size (log) for each load (log).  Valid range: 0-2
			2,								// LOG_LOADS_PER_TILE	The number of loads (log) per tile.  Valid range: 0-2
			b40c::util::io::ld::NONE,		// READ_MODIFIER		Load cache-modifier.  Valid values: NONE, ca, cg, cs
			b40c::util::io::st::NONE>		// WRITE_MODIFIER		Store cache-modifier.  Valid values: NONE, wb, cg, cs
				SpinePolicy;

		// Downsweep kernel policy
		typedef typename util::If<
			(!LARGE_DATA),
			downsweep::KernelPolicy<
				RADIX_BITS,						// RADIX_BITS
				CURRENT_BIT,					// CURRENT_BIT
				CURRENT_PASS,					// CURRENT_PASS
				KEYS_ONLY ? 4 : 2,				// MIN_CTA_OCCUPANCY		The targeted SM occupancy to feed PTXAS in order to influence how it does register allocation
				KEYS_ONLY ? 7 : 8,				// LOG_THREADS				The number of threads (log) to launch per CTA.
				KEYS_ONLY ? 4 : 4,				// LOG_ELEMENTS_PER_TILE	The number of keys (log) per thread
				b40c::util::io::ld::NONE,		// READ_MODIFIER			Load cache-modifier.  Valid values: NONE, ca, cg, cs
				b40c::util::io::st::NONE,		// WRITE_MODIFIER			Store cache-modifier.  Valid values: NONE, wb, cg, cs
				downsweep::SCATTER_TWO_PHASE,	// SCATTER_STRATEGY			Whether or not to perform a two-phase scatter (scatter to smem first to recover some locality before scattering to global bins)
				false,							// SMEM_8BYTE_BANKS
				EARLY_EXIT>,					// EARLY_EXIT				Whether or not to early-terminate a sorting pass if we detect all keys have the same digit in that pass's digit place
			downsweep::KernelPolicy<
				RADIX_BITS,						// RADIX_BITS
				CURRENT_BIT,					// CURRENT_BIT
				CURRENT_PASS,					// CURRENT_PASS
				2,								// MIN_CTA_OCCUPANCY		The targeted SM occupancy to feed PTXAS in order to influence how it does register allocation
				8,								// LOG_THREADS				The number of threads (log) to launch per CTA.
				3,								// LOG_ELEMENTS_PER_TILE	The number of keys (log) per thread
				b40c::util::io::ld::NONE,		// READ_MODIFIER			Load cache-modifier.  Valid values: NONE, ca, cg, cs
				b40c::util::io::st::NONE,		// WRITE_MODIFIER			Store cache-modifier.  Valid values: NONE, wb, cg, cs
				downsweep::SCATTER_TWO_PHASE,	// SCATTER_STRATEGY			Whether or not to perform a two-phase scatter (scatter to smem first to recover some locality before scattering to global bins)
				false,							// SMEM_8BYTE_BANKS
				EARLY_EXIT> >::Type 			// EARLY_EXIT				Whether or not to early-terminate a sorting pass if we detect all keys have the same digit in that pass's digit place
					DownsweepPolicy;
	};





	/**
	 * Opaque pass policy
	 */
	template <int BITS_REMAINING, int CURRENT_BIT, int CURRENT_PASS>
	struct OpaquePassPolicy
	{
		// The appropriate tuning arch-id from the arch-id targeted by the
		// active compiler pass.
		static const int OPAQUE_ARCH = 200;
/*
			(__CUB_CUDA_ARCH__ >= 200) ?
				200 :
				(__CUB_CUDA_ARCH__ >= 130) ?
					130 :
					100;
*/
		typedef TunedPassPolicy<OPAQUE_ARCH, BITS_REMAINING, CURRENT_BIT, CURRENT_PASS> TunedPolicy;

		struct UpsweepPolicy : 		TunedPolicy::UpsweepPolicy {};
		struct SpinePolicy : 		TunedPolicy::SpinePolicy {};
		struct DownsweepPolicy : 	TunedPolicy::DownsweepPolicy {};
		struct DispatchPolicy : 	TunedPolicy::DispatchPolicy {};
	};


	/**
	 * Helper structure for iterating passes.
	 */
	template <int PTX_ARCH, int BITS_REMAINING, int CURRENT_BIT, int CURRENT_PASS>
	struct IteratePasses
	{
		// Dispatch pass
		template <typename ProblemInstance>
		static cudaError_t Dispatch(ProblemInstance &problem_instance)
		{
			typedef TunedPassPolicy<PTX_ARCH, BITS_REMAINING, CURRENT_BIT, CURRENT_PASS> TunedPolicy;

			typedef OpaquePassPolicy<BITS_REMAINING, CURRENT_BIT, CURRENT_PASS> OpaquePolicy;

			const int RADIX_BITS = TunedPolicy::DispatchPolicy::RADIX_BITS;

			cudaError_t error = cudaSuccess;
			do {
				if (problem_instance.debug) {
					printf("\nCurrent bit(%d), Pass(%d), Radix bits(%d), PTX arch(%d), SM arch(%d)\n",
						CURRENT_BIT, CURRENT_PASS, RADIX_BITS, PTX_ARCH, problem_instance.sm_arch);
					fflush(stdout);
				}

				// Dispatch current pass
				error = SortingPass::Dispatch<TunedPolicy, OpaquePolicy>(problem_instance);
				if (error) break;

				// Dispatch next pass
				error = IteratePasses<
					PTX_ARCH,
					BITS_REMAINING - RADIX_BITS,
					CURRENT_BIT + RADIX_BITS,
					CURRENT_PASS + 1>::Dispatch(problem_instance);
				if (error) break;

			} while (0);

			return error;
		}
	};


	/**
	 * Helper structure for iterating passes. (Termination)
	 */
	template <int PTX_ARCH, int CURRENT_BIT, int NUM_PASSES>
	struct IteratePasses<PTX_ARCH, 0, CURRENT_BIT, NUM_PASSES>
	{
		// Dispatch pass
		template <typename ProblemInstance>
		static cudaError_t Dispatch(ProblemInstance &problem_instance)
		{
			// We moved data between storage buffers at every pass
			problem_instance.storage.selector =
				(problem_instance.storage.selector + NUM_PASSES) & 0x1;

			return cudaSuccess;
		}
	};


	/**
	 * Dispatch
	 */
	template <
		int BITS_REMAINING,
		int CURRENT_BIT,
		typename ProblemInstance>
	static cudaError_t DispatchPasses(ProblemInstance &problem_instance)
	{
		if (problem_instance.ptx_arch >= 200) {

			return IteratePasses<200, BITS_REMAINING, CURRENT_BIT, 0>::Dispatch(problem_instance);
/*
		} else if (problem_instance.ptx_arch >= 130) {

			return IteratePasses<130, BITS_REMAINING, CURRENT_BIT, 0>::Dispatch(problem_instance);

		} else {

			return IteratePasses<100, BITS_REMAINING, CURRENT_BIT, 0>::Dispatch(problem_instance);
		}
*/
		} else {
			return cudaErrorNotYetImplemented;
		}
	}
};


/******************************************************************************
 * Radix sorting enactor class
 ******************************************************************************/

/**
 * Radix sorting enactor class
 */
class Enactor
{
protected:

	//---------------------------------------------------------------------
	// Members
	//---------------------------------------------------------------------

	// Temporary device storage needed for reducing partials produced
	// by separate CTAs
	util::Spine spine;

	// Device properties
	const util::CudaProperties cuda_props;

public:

	/**
	 * Constructor
	 */
	Enactor() {}


	/**
	 * Enact a sort.
	 *
	 * @param problem_storage
	 * 		Instance of b40c::util::PingPongStorage type describing the details of the
	 * 		problem to sort.
	 * @param num_elements
	 * 		The number of elements in problem_storage to sort (starting at offset 0)
	 * @param max_grid_size
	 * 		Optional upper-bound on the number of CTAs to launch.
	 *
	 * @return cudaSuccess on success, error enumeration otherwise
	 */
	template <
		int BITS_REMAINING,
		int CURRENT_BIT,
		typename DoubleBuffer>
	cudaError_t Sort(
		DoubleBuffer& 	problem_storage,
		int 			num_elements,
		int 			max_grid_size = 0,
		bool 			debug = false)
	{
		typedef typename DoubleBuffer::KeyType KeyType;
		typedef typename DoubleBuffer::ValueType ValueType;
		typedef SortingPass<KeyType, ValueType, int> SortingPass;

		// Create problem instance
		ProblemInstance<DoubleBuffer, int> problem_instance(
			problem_storage,
			num_elements,
			spine,
			max_grid_size,
			cuda_props.kernel_ptx_version,
			cuda_props.device_sm_version,
			cuda_props.device_props.multiProcessorCount,
			debug);

		// Dispatch sorting passes
		return SortingPass::template DispatchPasses<
			BITS_REMAINING,
			CURRENT_BIT>(problem_instance);
	}

};





} // namespace radix_sort
} // namespace b40c