1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
18 // Peng Xiao, pengxiao@outlook.com
20 // Redistribution and use in source and binary forms, with or without modification,
21 // are permitted provided that the following conditions are met:
23 // * Redistribution's of source code must retain the above copyright notice,
24 // this list of conditions and the following disclaimer.
26 // * Redistribution's in binary form must reproduce the above copyright notice,
27 // this list of conditions and the following disclaimer in the documentation
28 // and/or other oclMaterials provided with the distribution.
30 // * The name of the copyright holders may not be used to endorse or promote products
31 // derived from this software without specific prior written permission.
33 // This software is provided by the copyright holders and contributors as is and
34 // any express or implied warranties, including, but not limited to, the implied
35 // warranties of merchantability and fitness for a particular purpose are disclaimed.
36 // In no event shall the Intel Corporation or contributors be liable for any direct,
37 // indirect, incidental, special, exemplary, or consequential damages
38 // (including, but not limited to, procurement of substitute goods or services;
39 // loss of use, data, or profits; or business interruption) however caused
40 // and on any theory of liability, whether in contract, strict liability,
41 // or tort (including negligence or otherwise) arising in any way out of
42 // the use of this software, even if advised of the possibility of such damage.
46 #pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable
48 #ifndef N // number of radices
65 // from Thrust::b40c, link:
66 // https://github.com/thrust/thrust/blob/master/thrust/system/cuda/detail/detail/b40c/radixsort_key_conversion.h
67 __inline uint convertKey(uint converted_key)
70 unsigned int mask = (converted_key & 0x80000000) ? 0xffffffff : 0x80000000;
71 converted_key ^= mask;
73 const uint SIGN_MASK = 1u << ((sizeof(int) * 8) - 1);
74 converted_key ^= SIGN_MASK;
82 // exclusive scan, need to be optimized as this is too naive...
84 void naiveScanAddition(
86 __global int * output,
90 if(get_global_id(0) == 0)
93 for(int i = 1; i < size; i ++)
95 output[i] = output[i - 1] + input[i - 1];
100 // following is ported from
101 // https://github.com/HSA-Libraries/Bolt/blob/master/include/bolt/cl/sort_uint_kernels.cl
103 void histogramRadixN (
104 __global K_T* unsortedKeys,
105 __global int * buckets,
109 const int RADIX_T = N;
110 const int RADICES_T = (1 << RADIX_T);
111 const int NUM_OF_ELEMENTS_PER_WORK_ITEM_T = RADICES_T;
112 const int MASK_T = (1 << RADIX_T) - 1;
113 int localBuckets[16] = {0,0,0,0,0,0,0,0,
115 int globalId = get_global_id(0);
116 int numOfGroups = get_num_groups(0);
118 /* Calculate thread-histograms */
119 for(int i = 0; i < NUM_OF_ELEMENTS_PER_WORK_ITEM_T; ++i)
121 uint value = convertKey(as_uint(unsortedKeys[mad24(globalId, NUM_OF_ELEMENTS_PER_WORK_ITEM_T, i)]));
122 value = (value >> shiftCount) & MASK_T;
124 localBuckets[RADICES_T - value - 1]++;
126 localBuckets[value]++;
130 for(int i = 0; i < NUM_OF_ELEMENTS_PER_WORK_ITEM_T; ++i)
132 buckets[mad24(i, RADICES_T * numOfGroups, globalId) ] = localBuckets[i];
138 __global K_T* unsortedKeys,
139 __global V_T* unsortedVals,
140 __global int* scanedBuckets,
142 __global K_T* sortedKeys,
143 __global V_T* sortedVals
146 const int RADIX_T = N;
147 const int RADICES_T = (1 << RADIX_T);
148 const int MASK_T = (1<<RADIX_T) -1;
150 int globalId = get_global_id(0);
151 int numOfGroups = get_num_groups(0);
152 const int NUM_OF_ELEMENTS_PER_WORK_GROUP_T = numOfGroups << N;
155 /*Load the index to local memory*/
156 for(int i = 0; i < RADICES_T; ++i)
159 localIndex[i] = scanedBuckets[mad24(RADICES_T - i - 1, NUM_OF_ELEMENTS_PER_WORK_GROUP_T, globalId)];
161 localIndex[i] = scanedBuckets[mad24(i, NUM_OF_ELEMENTS_PER_WORK_GROUP_T, globalId)];
164 /* Permute elements to appropriate location */
165 for(int i = 0; i < RADICES_T; ++i)
167 int old_idx = mad24(globalId, RADICES_T, i);
168 K_T ovalue = unsortedKeys[old_idx];
169 uint value = convertKey(as_uint(ovalue));
170 uint maskedValue = (value >> shiftCount) & MASK_T;
171 uint index = localIndex[maskedValue];
172 sortedKeys[index] = ovalue;
173 sortedVals[index] = unsortedVals[old_idx];
174 localIndex[maskedValue] = index + 1;