Blame view

tools/cub-1.8.0/cub/agent/agent_histogram.cuh 32.5 KB
8dcb6dfcb   Yannick Estève   first 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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
  /******************************************************************************
   * Copyright (c) 2011, Duane Merrill.  All rights reserved.
   * Copyright (c) 2011-2018, NVIDIA CORPORATION.  All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions are met:
   *     * Redistributions of source code must retain the above copyright
   *       notice, this list of conditions and the following disclaimer.
   *     * Redistributions in binary form must reproduce the above copyright
   *       notice, this list of conditions and the following disclaimer in the
   *       documentation and/or other materials provided with the distribution.
   *     * Neither the name of the NVIDIA CORPORATION nor the
   *       names of its contributors may be used to endorse or promote products
   *       derived from this software without specific prior written permission.
   *
   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   ******************************************************************************/
  
  /**
   * \file
   * cub::AgentHistogram implements a stateful abstraction of CUDA thread blocks for participating in device-wide histogram .
   */
  
  #pragma once
  
  #include <iterator>
  
  #include "../util_type.cuh"
  #include "../block/block_load.cuh"
  #include "../grid/grid_queue.cuh"
  #include "../iterator/cache_modified_input_iterator.cuh"
  #include "../util_namespace.cuh"
  
  /// Optional outer namespace(s)
  CUB_NS_PREFIX
  
  /// CUB namespace
  namespace cub {
  
  
  /******************************************************************************
   * Tuning policy
   ******************************************************************************/
  
  /**
   *
   */
  enum BlockHistogramMemoryPreference
  {
      GMEM,
      SMEM,
      BLEND
  };
  
  
  /**
   * Parameterizable tuning policy type for AgentHistogram
   */
  template <
      int                             _BLOCK_THREADS,                 ///< Threads per thread block
      int                             _PIXELS_PER_THREAD,             ///< Pixels per thread (per tile of input)
      BlockLoadAlgorithm              _LOAD_ALGORITHM,                ///< The BlockLoad algorithm to use
      CacheLoadModifier               _LOAD_MODIFIER,                 ///< Cache load modifier for reading input elements
      bool                            _RLE_COMPRESS,                  ///< Whether to perform localized RLE to compress samples before histogramming
      BlockHistogramMemoryPreference  _MEM_PREFERENCE,                ///< Whether to prefer privatized shared-memory bins (versus privatized global-memory bins)
      bool                            _WORK_STEALING>                 ///< Whether to dequeue tiles from a global work queue
  struct AgentHistogramPolicy
  {
      enum
      {
          BLOCK_THREADS           = _BLOCK_THREADS,                   ///< Threads per thread block
          PIXELS_PER_THREAD       = _PIXELS_PER_THREAD,               ///< Pixels per thread (per tile of input)
          IS_RLE_COMPRESS         = _RLE_COMPRESS,                    ///< Whether to perform localized RLE to compress samples before histogramming
          MEM_PREFERENCE          = _MEM_PREFERENCE,                  ///< Whether to prefer privatized shared-memory bins (versus privatized global-memory bins)
          IS_WORK_STEALING        = _WORK_STEALING,                   ///< Whether to dequeue tiles from a global work queue
      };
  
      static const BlockLoadAlgorithm     LOAD_ALGORITHM          = _LOAD_ALGORITHM;          ///< The BlockLoad algorithm to use
      static const CacheLoadModifier      LOAD_MODIFIER           = _LOAD_MODIFIER;           ///< Cache load modifier for reading input elements
  };
  
  
  /******************************************************************************
   * Thread block abstractions
   ******************************************************************************/
  
  /**
   * \brief AgentHistogram implements a stateful abstraction of CUDA thread blocks for participating in device-wide histogram .
   */
  template <
      typename    AgentHistogramPolicyT,     ///< Parameterized AgentHistogramPolicy tuning policy type
      int         PRIVATIZED_SMEM_BINS,           ///< Number of privatized shared-memory histogram bins of any channel.  Zero indicates privatized counters to be maintained in device-accessible memory.
      int         NUM_CHANNELS,                   ///< Number of channels interleaved in the input data.  Supports up to four channels.
      int         NUM_ACTIVE_CHANNELS,            ///< Number of channels actively being histogrammed
      typename    SampleIteratorT,                ///< Random-access input iterator type for reading samples
      typename    CounterT,                       ///< Integer type for counting sample occurrences per histogram bin
      typename    PrivatizedDecodeOpT,            ///< The transform operator type for determining privatized counter indices from samples, one for each channel
      typename    OutputDecodeOpT,                ///< The transform operator type for determining output bin-ids from privatized counter indices, one for each channel
      typename    OffsetT,                        ///< Signed integer type for global offsets
      int         PTX_ARCH = CUB_PTX_ARCH>        ///< PTX compute capability
  struct AgentHistogram
  {
      //---------------------------------------------------------------------
      // Types and constants
      //---------------------------------------------------------------------
  
      /// The sample type of the input iterator
      typedef typename std::iterator_traits<SampleIteratorT>::value_type SampleT;
  
      /// The pixel type of SampleT
      typedef typename CubVector<SampleT, NUM_CHANNELS>::Type PixelT;
  
      /// The quad type of SampleT
      typedef typename CubVector<SampleT, 4>::Type QuadT;
  
      /// Constants
      enum
      {
          BLOCK_THREADS           = AgentHistogramPolicyT::BLOCK_THREADS,
  
          PIXELS_PER_THREAD       = AgentHistogramPolicyT::PIXELS_PER_THREAD,
          SAMPLES_PER_THREAD      = PIXELS_PER_THREAD * NUM_CHANNELS,
          QUADS_PER_THREAD        = SAMPLES_PER_THREAD / 4,
  
          TILE_PIXELS             = PIXELS_PER_THREAD * BLOCK_THREADS,
          TILE_SAMPLES            = SAMPLES_PER_THREAD * BLOCK_THREADS,
  
          IS_RLE_COMPRESS            = AgentHistogramPolicyT::IS_RLE_COMPRESS,
  
          MEM_PREFERENCE          = (PRIVATIZED_SMEM_BINS > 0) ?
                                          AgentHistogramPolicyT::MEM_PREFERENCE :
                                          GMEM,
  
          IS_WORK_STEALING           = AgentHistogramPolicyT::IS_WORK_STEALING,
      };
  
      /// Cache load modifier for reading input elements
      static const CacheLoadModifier LOAD_MODIFIER = AgentHistogramPolicyT::LOAD_MODIFIER;
  
  
      /// Input iterator wrapper type (for applying cache modifier)
      typedef typename If<IsPointer<SampleIteratorT>::VALUE,
              CacheModifiedInputIterator<LOAD_MODIFIER, SampleT, OffsetT>,     // Wrap the native input pointer with CacheModifiedInputIterator
              SampleIteratorT>::Type                                           // Directly use the supplied input iterator type
          WrappedSampleIteratorT;
  
      /// Pixel input iterator type (for applying cache modifier)
      typedef CacheModifiedInputIterator<LOAD_MODIFIER, PixelT, OffsetT>
          WrappedPixelIteratorT;
  
      /// Qaud input iterator type (for applying cache modifier)
      typedef CacheModifiedInputIterator<LOAD_MODIFIER, QuadT, OffsetT>
          WrappedQuadIteratorT;
  
      /// Parameterized BlockLoad type for samples
      typedef BlockLoad<
              SampleT,
              BLOCK_THREADS,
              SAMPLES_PER_THREAD,
              AgentHistogramPolicyT::LOAD_ALGORITHM>
          BlockLoadSampleT;
  
      /// Parameterized BlockLoad type for pixels
      typedef BlockLoad<
              PixelT,
              BLOCK_THREADS,
              PIXELS_PER_THREAD,
              AgentHistogramPolicyT::LOAD_ALGORITHM>
          BlockLoadPixelT;
  
      /// Parameterized BlockLoad type for quads
      typedef BlockLoad<
              QuadT,
              BLOCK_THREADS,
              QUADS_PER_THREAD,
              AgentHistogramPolicyT::LOAD_ALGORITHM>
          BlockLoadQuadT;
  
      /// Shared memory type required by this thread block
      struct _TempStorage
      {
          CounterT histograms[NUM_ACTIVE_CHANNELS][PRIVATIZED_SMEM_BINS + 1];     // Smem needed for block-privatized smem histogram (with 1 word of padding)
  
          int tile_idx;
  
          // Aliasable storage layout
          union Aliasable
          {
              typename BlockLoadSampleT::TempStorage sample_load;     // Smem needed for loading a tile of samples
              typename BlockLoadPixelT::TempStorage pixel_load;       // Smem needed for loading a tile of pixels
              typename BlockLoadQuadT::TempStorage quad_load;         // Smem needed for loading a tile of quads
  
          } aliasable;
      };
  
  
      /// Temporary storage type (unionable)
      struct TempStorage : Uninitialized<_TempStorage> {};
  
  
      //---------------------------------------------------------------------
      // Per-thread fields
      //---------------------------------------------------------------------
  
      /// Reference to temp_storage
      _TempStorage &temp_storage;
  
      /// Sample input iterator (with cache modifier applied, if possible)
      WrappedSampleIteratorT d_wrapped_samples;
  
      /// Native pointer for input samples (possibly NULL if unavailable)
      SampleT* d_native_samples;
  
      /// The number of output bins for each channel
      int (&num_output_bins)[NUM_ACTIVE_CHANNELS];
  
      /// The number of privatized bins for each channel
      int (&num_privatized_bins)[NUM_ACTIVE_CHANNELS];
  
      /// Reference to gmem privatized histograms for each channel
      CounterT* d_privatized_histograms[NUM_ACTIVE_CHANNELS];
  
      /// Reference to final output histograms (gmem)
      CounterT* (&d_output_histograms)[NUM_ACTIVE_CHANNELS];
  
      /// The transform operator for determining output bin-ids from privatized counter indices, one for each channel
      OutputDecodeOpT (&output_decode_op)[NUM_ACTIVE_CHANNELS];
  
      /// The transform operator for determining privatized counter indices from samples, one for each channel
      PrivatizedDecodeOpT (&privatized_decode_op)[NUM_ACTIVE_CHANNELS];
  
      /// Whether to prefer privatized smem counters vs privatized global counters
      bool prefer_smem;
  
  
      //---------------------------------------------------------------------
      // Initialize privatized bin counters
      //---------------------------------------------------------------------
  
      // Initialize privatized bin counters
      __device__ __forceinline__ void InitBinCounters(CounterT* privatized_histograms[NUM_ACTIVE_CHANNELS])
      {
          // Initialize histogram bin counts to zeros
          #pragma unroll
          for (int CHANNEL = 0; CHANNEL < NUM_ACTIVE_CHANNELS; ++CHANNEL)
          {
              for (int privatized_bin = threadIdx.x; privatized_bin < num_privatized_bins[CHANNEL]; privatized_bin += BLOCK_THREADS)
              {
                  privatized_histograms[CHANNEL][privatized_bin] = 0;
              }
          }
  
          // Barrier to make sure all threads are done updating counters
          CTA_SYNC();
      }
  
  
      // Initialize privatized bin counters.  Specialized for privatized shared-memory counters
      __device__ __forceinline__ void InitSmemBinCounters()
      {
          CounterT* privatized_histograms[NUM_ACTIVE_CHANNELS];
  
          for (int CHANNEL = 0; CHANNEL < NUM_ACTIVE_CHANNELS; ++CHANNEL)
              privatized_histograms[CHANNEL] = temp_storage.histograms[CHANNEL];
  
          InitBinCounters(privatized_histograms);
      }
  
  
      // Initialize privatized bin counters.  Specialized for privatized global-memory counters
      __device__ __forceinline__ void InitGmemBinCounters()
      {
          InitBinCounters(d_privatized_histograms);
      }
  
  
      //---------------------------------------------------------------------
      // Update final output histograms
      //---------------------------------------------------------------------
  
      // Update final output histograms from privatized histograms
      __device__ __forceinline__ void StoreOutput(CounterT* privatized_histograms[NUM_ACTIVE_CHANNELS])
      {
          // Barrier to make sure all threads are done updating counters
          CTA_SYNC();
  
          // Apply privatized bin counts to output bin counts
          #pragma unroll
          for (int CHANNEL = 0; CHANNEL < NUM_ACTIVE_CHANNELS; ++CHANNEL)
          {
              int channel_bins = num_privatized_bins[CHANNEL];
              for (int privatized_bin = threadIdx.x; 
                      privatized_bin < channel_bins;  
                      privatized_bin += BLOCK_THREADS)
              {
                  int         output_bin  = -1;
                  CounterT    count       = privatized_histograms[CHANNEL][privatized_bin];
                  bool        is_valid    = count > 0;
  
                  output_decode_op[CHANNEL].template BinSelect<LOAD_MODIFIER>((SampleT) privatized_bin, output_bin, is_valid);
  
                  if (output_bin >= 0)
                  {
                      atomicAdd(&d_output_histograms[CHANNEL][output_bin], count);
                  }
  
              }
          }
      }
  
  
      // Update final output histograms from privatized histograms.  Specialized for privatized shared-memory counters
      __device__ __forceinline__ void StoreSmemOutput()
      {
          CounterT* privatized_histograms[NUM_ACTIVE_CHANNELS];
          for (int CHANNEL = 0; CHANNEL < NUM_ACTIVE_CHANNELS; ++CHANNEL)
              privatized_histograms[CHANNEL] = temp_storage.histograms[CHANNEL];
  
          StoreOutput(privatized_histograms);
      }
  
  
      // Update final output histograms from privatized histograms.  Specialized for privatized global-memory counters
      __device__ __forceinline__ void StoreGmemOutput()
      {
          StoreOutput(d_privatized_histograms);
      }
  
  
      //---------------------------------------------------------------------
      // Tile accumulation
      //---------------------------------------------------------------------
  
      // Accumulate pixels.  Specialized for RLE compression.
      __device__ __forceinline__ void AccumulatePixels(
          SampleT             samples[PIXELS_PER_THREAD][NUM_CHANNELS],
          bool                is_valid[PIXELS_PER_THREAD],
          CounterT*           privatized_histograms[NUM_ACTIVE_CHANNELS],
          Int2Type<true>      is_rle_compress)
      {
          #pragma unroll
          for (int CHANNEL = 0; CHANNEL < NUM_ACTIVE_CHANNELS; ++CHANNEL)
          {
              // Bin pixels
              int bins[PIXELS_PER_THREAD];
  
              #pragma unroll
              for (int PIXEL = 0; PIXEL < PIXELS_PER_THREAD; ++PIXEL)
              {
                  bins[PIXEL] = -1;
                  privatized_decode_op[CHANNEL].template BinSelect<LOAD_MODIFIER>(samples[PIXEL][CHANNEL], bins[PIXEL], is_valid[PIXEL]);
              }
  
              CounterT accumulator = 1;
  
              #pragma unroll
              for (int PIXEL = 0; PIXEL < PIXELS_PER_THREAD - 1; ++PIXEL)
              {
                  if (bins[PIXEL] != bins[PIXEL + 1])
                  {
                      if (bins[PIXEL] >= 0)
                          atomicAdd(privatized_histograms[CHANNEL] + bins[PIXEL], accumulator);
  
                       accumulator = 0;
                  }
                  accumulator++;
              }
  
              // Last pixel
              if (bins[PIXELS_PER_THREAD - 1] >= 0)
                  atomicAdd(privatized_histograms[CHANNEL] + bins[PIXELS_PER_THREAD - 1], accumulator);
          }
      }
  
  
      // Accumulate pixels.  Specialized for individual accumulation of each pixel.
      __device__ __forceinline__ void AccumulatePixels(
          SampleT             samples[PIXELS_PER_THREAD][NUM_CHANNELS],
          bool                is_valid[PIXELS_PER_THREAD],
          CounterT*           privatized_histograms[NUM_ACTIVE_CHANNELS],
          Int2Type<false>     is_rle_compress)
      {
          #pragma unroll
          for (int PIXEL = 0; PIXEL < PIXELS_PER_THREAD; ++PIXEL)
          {
              #pragma unroll
              for (int CHANNEL = 0; CHANNEL < NUM_ACTIVE_CHANNELS; ++CHANNEL)
              {
                  int bin = -1;
                  privatized_decode_op[CHANNEL].template BinSelect<LOAD_MODIFIER>(samples[PIXEL][CHANNEL], bin, is_valid[PIXEL]);
                  if (bin >= 0)
                      atomicAdd(privatized_histograms[CHANNEL] + bin, 1);
              }
          }
      }
  
  
      /**
       * Accumulate pixel, specialized for smem privatized histogram
       */
      __device__ __forceinline__ void AccumulateSmemPixels(
          SampleT             samples[PIXELS_PER_THREAD][NUM_CHANNELS],
          bool                is_valid[PIXELS_PER_THREAD])
      {
          CounterT* privatized_histograms[NUM_ACTIVE_CHANNELS];
  
          for (int CHANNEL = 0; CHANNEL < NUM_ACTIVE_CHANNELS; ++CHANNEL)
              privatized_histograms[CHANNEL] = temp_storage.histograms[CHANNEL];
  
          AccumulatePixels(samples, is_valid, privatized_histograms, Int2Type<IS_RLE_COMPRESS>());
      }
  
  
      /**
       * Accumulate pixel, specialized for gmem privatized histogram
       */
      __device__ __forceinline__ void AccumulateGmemPixels(
          SampleT             samples[PIXELS_PER_THREAD][NUM_CHANNELS],
          bool                is_valid[PIXELS_PER_THREAD])
      {
          AccumulatePixels(samples, is_valid, d_privatized_histograms, Int2Type<IS_RLE_COMPRESS>());
      }
  
  
  
      //---------------------------------------------------------------------
      // Tile loading
      //---------------------------------------------------------------------
  
      // Load full, aligned tile using pixel iterator (multi-channel)
      template <int _NUM_ACTIVE_CHANNELS>
      __device__ __forceinline__ void LoadFullAlignedTile(
          OffsetT                         block_offset,
          int                             valid_samples,
          SampleT                         (&samples)[PIXELS_PER_THREAD][NUM_CHANNELS],
          Int2Type<_NUM_ACTIVE_CHANNELS>  num_active_channels)
      {
          typedef PixelT AliasedPixels[PIXELS_PER_THREAD];
  
          WrappedPixelIteratorT d_wrapped_pixels((PixelT*) (d_native_samples + block_offset));
  
          // Load using a wrapped pixel iterator
          BlockLoadPixelT(temp_storage.aliasable.pixel_load).Load(
              d_wrapped_pixels,
              reinterpret_cast<AliasedPixels&>(samples));
      }
  
      // Load full, aligned tile using quad iterator (single-channel)
      __device__ __forceinline__ void LoadFullAlignedTile(
          OffsetT                         block_offset,
          int                             valid_samples,
          SampleT                         (&samples)[PIXELS_PER_THREAD][NUM_CHANNELS],
          Int2Type<1>                     num_active_channels)
      {
          typedef QuadT AliasedQuads[QUADS_PER_THREAD];
  
          WrappedQuadIteratorT d_wrapped_quads((QuadT*) (d_native_samples + block_offset));
  
          // Load using a wrapped quad iterator
          BlockLoadQuadT(temp_storage.aliasable.quad_load).Load(
              d_wrapped_quads,
              reinterpret_cast<AliasedQuads&>(samples));
      }
  
      // Load full, aligned tile
      __device__ __forceinline__ void LoadTile(
          OffsetT         block_offset,
          int             valid_samples,
          SampleT         (&samples)[PIXELS_PER_THREAD][NUM_CHANNELS],
          Int2Type<true>  is_full_tile,
          Int2Type<true>  is_aligned)
      {
          LoadFullAlignedTile(block_offset, valid_samples, samples, Int2Type<NUM_ACTIVE_CHANNELS>());
      }
  
      // Load full, mis-aligned tile using sample iterator
      __device__ __forceinline__ void LoadTile(
          OffsetT         block_offset,
          int             valid_samples,
          SampleT         (&samples)[PIXELS_PER_THREAD][NUM_CHANNELS],
          Int2Type<true>  is_full_tile,
          Int2Type<false> is_aligned)
      {
          typedef SampleT AliasedSamples[SAMPLES_PER_THREAD];
  
          // Load using sample iterator
          BlockLoadSampleT(temp_storage.aliasable.sample_load).Load(
              d_wrapped_samples + block_offset,
              reinterpret_cast<AliasedSamples&>(samples));
      }
  
      // Load partially-full, aligned tile using the pixel iterator
      __device__ __forceinline__ void LoadTile(
          OffsetT         block_offset,
          int             valid_samples,
          SampleT         (&samples)[PIXELS_PER_THREAD][NUM_CHANNELS],
          Int2Type<false> is_full_tile,
          Int2Type<true>  is_aligned)
      {
          typedef PixelT AliasedPixels[PIXELS_PER_THREAD];
  
          WrappedPixelIteratorT d_wrapped_pixels((PixelT*) (d_native_samples + block_offset));
  
          int valid_pixels = valid_samples / NUM_CHANNELS;
  
          // Load using a wrapped pixel iterator
          BlockLoadPixelT(temp_storage.aliasable.pixel_load).Load(
              d_wrapped_pixels,
              reinterpret_cast<AliasedPixels&>(samples),
              valid_pixels);
      }
  
      // Load partially-full, mis-aligned tile using sample iterator
      __device__ __forceinline__ void LoadTile(
          OffsetT         block_offset,
          int             valid_samples,
          SampleT         (&samples)[PIXELS_PER_THREAD][NUM_CHANNELS],
          Int2Type<false> is_full_tile,
          Int2Type<false> is_aligned)
      {
          typedef SampleT AliasedSamples[SAMPLES_PER_THREAD];
  
          BlockLoadSampleT(temp_storage.aliasable.sample_load).Load(
              d_wrapped_samples + block_offset,
              reinterpret_cast<AliasedSamples&>(samples),
              valid_samples);
      }
  
  
      //---------------------------------------------------------------------
      // Tile processing
      //---------------------------------------------------------------------
  
      // Consume a tile of data samples
      template <
          bool IS_ALIGNED,        // Whether the tile offset is aligned (quad-aligned for single-channel, pixel-aligned for multi-channel)
          bool IS_FULL_TILE>      // Whether the tile is full
      __device__ __forceinline__ void ConsumeTile(OffsetT block_offset, int valid_samples)
      {
          SampleT     samples[PIXELS_PER_THREAD][NUM_CHANNELS];
          bool        is_valid[PIXELS_PER_THREAD];
  
          // Load tile
          LoadTile(
              block_offset,
              valid_samples,
              samples,
              Int2Type<IS_FULL_TILE>(),
              Int2Type<IS_ALIGNED>());
  
          // Set valid flags
          #pragma unroll
          for (int PIXEL = 0; PIXEL < PIXELS_PER_THREAD; ++PIXEL)
              is_valid[PIXEL] = IS_FULL_TILE || (((threadIdx.x * PIXELS_PER_THREAD + PIXEL) * NUM_CHANNELS) < valid_samples);
  
          // Accumulate samples
  #if CUB_PTX_ARCH >= 120
          if (prefer_smem)
              AccumulateSmemPixels(samples, is_valid);
          else
              AccumulateGmemPixels(samples, is_valid);
  #else
          AccumulateGmemPixels(samples, is_valid);
  #endif
  
      }
  
  
      // Consume row tiles.  Specialized for work-stealing from queue
      template <bool IS_ALIGNED>
      __device__ __forceinline__ void ConsumeTiles(
          OffsetT             num_row_pixels,             ///< The number of multi-channel pixels per row in the region of interest
          OffsetT             num_rows,                   ///< The number of rows in the region of interest
          OffsetT             row_stride_samples,         ///< The number of samples between starts of consecutive rows in the region of interest
          int                 tiles_per_row,              ///< Number of image tiles per row
          GridQueue<int>      tile_queue,
          Int2Type<true>      is_work_stealing)
      {
  
          int         num_tiles                   = num_rows * tiles_per_row;
          int         tile_idx                    = (blockIdx.y  * gridDim.x) + blockIdx.x;
          OffsetT     num_even_share_tiles        = gridDim.x * gridDim.y;
  
          while (tile_idx < num_tiles)
          {
              int     row             = tile_idx / tiles_per_row;
              int     col             = tile_idx - (row * tiles_per_row);
              OffsetT row_offset      = row * row_stride_samples;
              OffsetT col_offset      = (col * TILE_SAMPLES);
              OffsetT tile_offset     = row_offset + col_offset;
  
              if (col == tiles_per_row - 1)
              {
                  // Consume a partially-full tile at the end of the row
                  OffsetT num_remaining = (num_row_pixels * NUM_CHANNELS) - col_offset;
                  ConsumeTile<IS_ALIGNED, false>(tile_offset, num_remaining);
              } 
              else
              {
                  // Consume full tile
                  ConsumeTile<IS_ALIGNED, true>(tile_offset, TILE_SAMPLES);
              }
  
              CTA_SYNC();
  
              // Get next tile
              if (threadIdx.x == 0)
                  temp_storage.tile_idx = tile_queue.Drain(1) + num_even_share_tiles;
  
              CTA_SYNC();
  
              tile_idx = temp_storage.tile_idx;
          }
      }
  
  
      // Consume row tiles.  Specialized for even-share (striped across thread blocks)
      template <bool IS_ALIGNED>
      __device__ __forceinline__ void ConsumeTiles(
          OffsetT             num_row_pixels,             ///< The number of multi-channel pixels per row in the region of interest
          OffsetT             num_rows,                   ///< The number of rows in the region of interest
          OffsetT             row_stride_samples,         ///< The number of samples between starts of consecutive rows in the region of interest
          int                 tiles_per_row,              ///< Number of image tiles per row
          GridQueue<int>      tile_queue,
          Int2Type<false>     is_work_stealing)
      {
          for (int row = blockIdx.y; row < num_rows; row += gridDim.y)
          {
              OffsetT row_begin   = row * row_stride_samples;
              OffsetT row_end     = row_begin + (num_row_pixels * NUM_CHANNELS);
              OffsetT tile_offset = row_begin + (blockIdx.x * TILE_SAMPLES);
  
              while (tile_offset < row_end)
              {
                  OffsetT num_remaining = row_end - tile_offset;
  
                  if (num_remaining < TILE_SAMPLES)
                  {
                      // Consume partial tile
                      ConsumeTile<IS_ALIGNED, false>(tile_offset, num_remaining);
                      break;
                  }
  
                  // Consume full tile
                  ConsumeTile<IS_ALIGNED, true>(tile_offset, TILE_SAMPLES);
                  tile_offset += gridDim.x * TILE_SAMPLES;
              }
          }
      }
  
  
      //---------------------------------------------------------------------
      // Parameter extraction
      //---------------------------------------------------------------------
  
      // Return a native pixel pointer (specialized for CacheModifiedInputIterator types)
      template <
          CacheLoadModifier   _MODIFIER,
          typename            _ValueT,
          typename            _OffsetT>
      __device__ __forceinline__ SampleT* NativePointer(CacheModifiedInputIterator<_MODIFIER, _ValueT, _OffsetT> itr)
      {
          return itr.ptr;
      }
  
      // Return a native pixel pointer (specialized for other types)
      template <typename IteratorT>
      __device__ __forceinline__ SampleT* NativePointer(IteratorT itr)
      {
          return NULL;
      }
  
  
  
      //---------------------------------------------------------------------
      // Interface
      //---------------------------------------------------------------------
  
  
      /**
       * Constructor
       */
      __device__ __forceinline__ AgentHistogram(
          TempStorage         &temp_storage,                                      ///< Reference to temp_storage
          SampleIteratorT     d_samples,                                          ///< Input data to reduce
          int                 (&num_output_bins)[NUM_ACTIVE_CHANNELS],            ///< The number bins per final output histogram
          int                 (&num_privatized_bins)[NUM_ACTIVE_CHANNELS],        ///< The number bins per privatized histogram
          CounterT*           (&d_output_histograms)[NUM_ACTIVE_CHANNELS],        ///< Reference to final output histograms
          CounterT*           (&d_privatized_histograms)[NUM_ACTIVE_CHANNELS],    ///< Reference to privatized histograms
          OutputDecodeOpT     (&output_decode_op)[NUM_ACTIVE_CHANNELS],           ///< The transform operator for determining output bin-ids from privatized counter indices, one for each channel
          PrivatizedDecodeOpT (&privatized_decode_op)[NUM_ACTIVE_CHANNELS])       ///< The transform operator for determining privatized counter indices from samples, one for each channel
      :
          temp_storage(temp_storage.Alias()),
          d_wrapped_samples(d_samples),
          num_output_bins(num_output_bins),
          num_privatized_bins(num_privatized_bins),
          d_output_histograms(d_output_histograms),
          privatized_decode_op(privatized_decode_op),
          output_decode_op(output_decode_op),
          d_native_samples(NativePointer(d_wrapped_samples)),
          prefer_smem((MEM_PREFERENCE == SMEM) ?
              true :                              // prefer smem privatized histograms
              (MEM_PREFERENCE == GMEM) ?
                  false :                         // prefer gmem privatized histograms
                  blockIdx.x & 1)                 // prefer blended privatized histograms
      {
          int blockId = (blockIdx.y * gridDim.x) + blockIdx.x;
  
          // Initialize the locations of this block's privatized histograms
          for (int CHANNEL = 0; CHANNEL < NUM_ACTIVE_CHANNELS; ++CHANNEL)
              this->d_privatized_histograms[CHANNEL] = d_privatized_histograms[CHANNEL] + (blockId * num_privatized_bins[CHANNEL]);
      }
  
  
      /**
       * Consume image
       */
      __device__ __forceinline__ void ConsumeTiles(
          OffsetT             num_row_pixels,             ///< The number of multi-channel pixels per row in the region of interest
          OffsetT             num_rows,                   ///< The number of rows in the region of interest
          OffsetT             row_stride_samples,         ///< The number of samples between starts of consecutive rows in the region of interest
          int                 tiles_per_row,              ///< Number of image tiles per row
          GridQueue<int>      tile_queue)                 ///< Queue descriptor for assigning tiles of work to thread blocks
      {
          // Check whether all row starting offsets are quad-aligned (in single-channel) or pixel-aligned (in multi-channel)
          int     quad_mask           = AlignBytes<QuadT>::ALIGN_BYTES - 1;
          int     pixel_mask          = AlignBytes<PixelT>::ALIGN_BYTES - 1;
          size_t  row_bytes           = sizeof(SampleT) * row_stride_samples;
  
          bool quad_aligned_rows      = (NUM_CHANNELS == 1) && (SAMPLES_PER_THREAD % 4 == 0) &&     // Single channel
                                          ((size_t(d_native_samples) & quad_mask) == 0) &&        // ptr is quad-aligned
                                          ((num_rows == 1) || ((row_bytes & quad_mask) == 0));    // number of row-samples is a multiple of the alignment of the quad
  
          bool pixel_aligned_rows     = (NUM_CHANNELS > 1) &&                                     // Multi channel
                                          ((size_t(d_native_samples) & pixel_mask) == 0) &&       // ptr is pixel-aligned
                                          ((row_bytes & pixel_mask) == 0);                        // number of row-samples is a multiple of the alignment of the pixel
  
          // Whether rows are aligned and can be vectorized
          if ((d_native_samples != NULL) && (quad_aligned_rows || pixel_aligned_rows))
              ConsumeTiles<true>(num_row_pixels, num_rows, row_stride_samples, tiles_per_row, tile_queue, Int2Type<IS_WORK_STEALING>());
          else
              ConsumeTiles<false>(num_row_pixels, num_rows, row_stride_samples, tiles_per_row, tile_queue, Int2Type<IS_WORK_STEALING>());
      }
  
  
      /**
       * Initialize privatized bin counters.  Specialized for privatized shared-memory counters
       */
      __device__ __forceinline__ void InitBinCounters()
      {
          if (prefer_smem)
              InitSmemBinCounters();
          else
              InitGmemBinCounters();
      }
  
  
      /**
       * Store privatized histogram to device-accessible memory.  Specialized for privatized shared-memory counters
       */
      __device__ __forceinline__ void StoreOutput()
      {
          if (prefer_smem)
              StoreSmemOutput();
          else
              StoreGmemOutput();
      }
  
  
  };
  
  
  
  
  }               // CUB namespace
  CUB_NS_POSTFIX  // Optional outer namespace(s)