|
| 1 | +/* |
| 2 | + * Copyright 2016, Simula Research Laboratory |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 | + */ |
| 8 | +#pragma once |
| 9 | + |
| 10 | +#include <cuda_runtime.h> |
| 11 | +#include <cooperative_groups.h> |
| 12 | +#include <iso646.h> |
| 13 | +#include <stdio.h> |
| 14 | + |
| 15 | +#include "../common/assist.h" |
| 16 | + |
| 17 | +using namespace cooperative_groups; |
| 18 | + |
| 19 | +namespace popsift { |
| 20 | +namespace BitonicSort { |
| 21 | + |
| 22 | +template<class T> |
| 23 | +class Warp32 |
| 24 | +{ |
| 25 | + T* _array; |
| 26 | +public: |
| 27 | + __device__ inline |
| 28 | + Warp32( T* array ) : _array( array ) { } |
| 29 | + |
| 30 | + __device__ inline |
| 31 | + int sort32( int my_index ) |
| 32 | + { |
| 33 | + thread_block_tile<32> tile32 = tiled_partition<32>( this_thread_block() ); |
| 34 | + |
| 35 | + for( int outer=0; outer<5; outer++ ) { |
| 36 | + for( int inner=outer; inner>=0; inner-- ) { |
| 37 | + my_index = shiftit( tile32, my_index, inner, outer+1, false ); |
| 38 | + } |
| 39 | + } |
| 40 | + return my_index; |
| 41 | + } |
| 42 | + |
| 43 | + __device__ inline |
| 44 | + void sort64( int2& my_indeces ) |
| 45 | + { |
| 46 | + thread_block_tile<32> tile32 = tiled_partition<32>( this_thread_block() ); |
| 47 | + |
| 48 | + for( int outer=0; outer<5; outer++ ) { |
| 49 | + for( int inner=outer; inner>=0; inner-- ) { |
| 50 | + my_indeces.x = shiftit( tile32, my_indeces.x, inner, outer+1, false ); |
| 51 | + my_indeces.y = shiftit( tile32, my_indeces.y, inner, outer+1, true ); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if( _array[my_indeces.x] < _array[my_indeces.y] ) swap( my_indeces.x, my_indeces.y ); |
| 56 | + |
| 57 | + for( int outer=0; outer<5; outer++ ) { |
| 58 | + for( int inner=outer; inner>=0; inner-- ) { |
| 59 | + my_indeces.x = shiftit( tile32, my_indeces.x, inner, outer+1, false ); |
| 60 | + my_indeces.y = shiftit( tile32, my_indeces.y, inner, outer+1, false ); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | +private: |
| 66 | + __device__ inline |
| 67 | + int shiftit( thread_block_tile<32>& tile32, |
| 68 | + const int my_index, |
| 69 | + const int shift, const int direction, const bool increasing ) |
| 70 | + { |
| 71 | + const T my_val = _array[my_index]; |
| 72 | + const T other_val = tile32.shfl_xor( my_val, 1 << shift ); // popsift::shuffle_xor( my_val, 1 << shift ); |
| 73 | + const bool reverse = ( threadIdx.x & ( 1 << direction ) ); |
| 74 | + const bool id_less = ( ( threadIdx.x & ( 1 << shift ) ) == 0 ); |
| 75 | + const bool my_more = id_less ? ( my_val > other_val ) |
| 76 | + : ( my_val < other_val ); |
| 77 | + const bool must_swap = not ( my_more ^ reverse ^ increasing ); |
| 78 | + |
| 79 | + // return ( must_swap ? popsift::shuffle_xor( my_index, 1 << shift ) : my_index ); |
| 80 | + int lane = must_swap ? ( 1 << shift ) : 0; |
| 81 | + int retval = tile32.shfl_xor( my_index, lane ); |
| 82 | + return retval; |
| 83 | + } |
| 84 | + |
| 85 | + __device__ inline |
| 86 | + void swap( int& l, int& r ) |
| 87 | + { |
| 88 | + int m = r; |
| 89 | + r = l; |
| 90 | + l = m; |
| 91 | + } |
| 92 | +}; |
| 93 | +} // namespace popsift |
| 94 | +} // namespace BitonicSort |
| 95 | + |
0 commit comments