NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Sort.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 #include "base/Array.hpp"
30 
31 namespace FW
32 {
33 //------------------------------------------------------------------------
34 // Generic quick sort implementation.
35 // Somewhat cumbersome to use - see below for convenience wrappers.
36 //
37 // Sort integers into ascending order:
38 //
39 // static bool myCompareFunc (void* data, int idxA, int idxB) { return (((S32*)data)[idxA] < ((S32*)data)[idxB]); }
40 // static void mySwapFunc (void* data, int idxA, int idxB) { swap(((S32*)data)[idxA], ((S32*)data)[idxB]); }
41 //
42 // Array<S32> myArray = ...;
43 // sort(myArray.getPtr(), 0, myArray.getSize(), myCompareFunc, mySwapFunc);
44 //------------------------------------------------------------------------
45 
46 typedef bool (*SortCompareFunc) (void* data, int idxA, int idxB); // Returns true if A should come before B.
47 typedef void (*SortSwapFunc) (void* data, int idxA, int idxB); // Swaps A and B.
48 
49 void sort(void* data, int start, int end, SortCompareFunc compareFunc, SortSwapFunc swapFunc, bool multicore = false);
50 
51 //------------------------------------------------------------------------
52 // Template-based wrappers.
53 // Use these if your type defines operator< and you want to sort in
54 // ascending order, OR if you want to explicitly define a custom
55 // comparator function.
56 //
57 // Sort integers into ascending order:
58 //
59 // Array<S32> myArray = ...;
60 // sort(myArray); // sort the full array
61 // sort(myArray, 0, myArray.getSize()); // specify range of elements
62 // sort(myArray.getPtr(), myArray.getSize()); // sort C array
63 //
64 // Sort integers into descending order:
65 //
66 // static bool myCompareFunc(void* data, int idxA, int idxB)
67 // {
68 // const S32& a = ((S32*)data)[idxA];
69 // const S32& b = ((S32*)data)[idxB];
70 // return (a > b);
71 // }
72 //
73 // Array<S32> myArray = ...;
74 // sort(myArray, myCompareFunc);
75 //------------------------------------------------------------------------
76 
77 template <class T> bool sortDefaultCompare (void* data, int idxA, int idxB);
78 template <class T> void sortDefaultSwap (void* data, int idxA, int idxB);
79 
80 template <class T> void sort(T* data, int num, SortCompareFunc compareFunc = sortDefaultCompare<T>, SortSwapFunc swapFunc = sortDefaultSwap<T>, bool multicore = false);
81 template <class T> void sort(Array<T>& data, SortCompareFunc compareFunc = sortDefaultCompare<T>, SortSwapFunc swapFunc = sortDefaultSwap<T>, bool multicore = false);
82 template <class T> void sort(Array<T>& data, int start, int end, SortCompareFunc compareFunc = sortDefaultCompare<T>, SortSwapFunc swapFunc = sortDefaultSwap<T>, bool multicore = false);
83 
84 //------------------------------------------------------------------------
85 // Macro-based wrappers.
86 // Use these if you want to use a custom comparator, but do not feel like
87 // defining a separate function for it.
88 //
89 // Sort integers into ascending order:
90 //
91 // Array<S32> myArray = ...;
92 // FW_SORT_ARRAY(myArray, S32, a < b); // sort the full array
93 // FW_SORT_SUBARRAY(myArray, 0, myArray.getSize(), S32, a < b); // specify range of elements
94 // FW_SORT(myArray.getPtr(), myArray.getSize(), S32, a < b); // sort C array
95 //
96 // Sort vectors in descending order based on their first component:
97 //
98 // Array<Vec2i> myArray = ...;
99 // FW_SORT_ARRAY(myArray, Vec2i, a.x > b.x);
100 //------------------------------------------------------------------------
101 
102 #define FW_SORT(PTR, NUM, TYPE, COMPARE) FW_SORT_IMPL(PTR, NUM, TYPE, COMPARE, false)
103 #define FW_SORT_ARRAY(ARRAY, TYPE, COMPARE) FW_SORT_IMPL(ARRAY.getPtr(), ARRAY.getSize(), TYPE, COMPARE, false)
104 #define FW_SORT_SUBARRAY(ARRAY, START, END, TYPE, COMPARE) FW_SORT_IMPL(ARRAY.getPtr(START), (END) - (START), TYPE, COMPARE, false)
105 
106 #define FW_SORT_MULTICORE(PTR, NUM, TYPE, COMPARE) FW_SORT_IMPL(PTR, NUM, TYPE, COMPARE, true)
107 #define FW_SORT_ARRAY_MULTICORE(ARRAY, TYPE, COMPARE) FW_SORT_IMPL(ARRAY.getPtr(), ARRAY.getSize(), TYPE, COMPARE, true)
108 #define FW_SORT_SUBARRAY_MULTICORE(ARRAY, START, END, TYPE, COMPARE) FW_SORT_IMPL(ARRAY.getPtr(START), (END) - (START), TYPE, COMPARE, true)
109 
110 //------------------------------------------------------------------------
111 // Wrapper implementation.
112 //------------------------------------------------------------------------
113 
114 template <class T> bool sortDefaultCompare(void* data, int idxA, int idxB)
115 {
116  return (((T*)data)[idxA] < ((T*)data)[idxB]);
117 }
118 
119 //------------------------------------------------------------------------
120 
121 template <class T> void sortDefaultSwap(void* data, int idxA, int idxB)
122 {
123  swap(((T*)data)[idxA], ((T*)data)[idxB]);
124 }
125 
126 //------------------------------------------------------------------------
127 
128 template <class T> void sort(Array<T>& data, SortCompareFunc compareFunc, SortSwapFunc swapFunc, bool multicore)
129 {
130  sort(data.getPtr(), 0, data.getSize(), compareFunc, swapFunc, multicore);
131 }
132 
133 //------------------------------------------------------------------------
134 
135 template <class T> void sort(Array<T>& data, int start, int end, SortCompareFunc compareFunc, SortSwapFunc swapFunc, bool multicore)
136 {
137  sort(data.getPtr(start), 0, end - start, compareFunc, swapFunc, multicore);
138 }
139 
140 //------------------------------------------------------------------------
141 
142 template <class T> void sort(T* data, int num, SortCompareFunc compareFunc, SortSwapFunc swapFunc, bool multicore)
143 {
144  sort(data, 0, num, compareFunc, swapFunc, multicore);
145 }
146 
147 //------------------------------------------------------------------------
148 
149 #define FW_SORT_IMPL(PTR, NUM, TYPE, COMPARE, MULTICORE) \
150  { \
151  struct SortLambda \
152  { \
153  static bool compareFunc(void* data, int idxA, int idxB) \
154  { \
155  TYPE& a = ((TYPE*)data)[idxA]; \
156  TYPE& b = ((TYPE*)data)[idxB]; \
157  return COMPARE; \
158  } \
159  }; \
160  FW::sort(PTR, NUM, SortLambda::compareFunc, FW::sortDefaultSwap<TYPE>, MULTICORE); \
161  }
162 
163 //------------------------------------------------------------------------
164 
165 int compareU32 (void* data, int idxA, int idxB);
166 void swapU32 (void* data, int idxA, int idxB);
167 
168 int compareU64 (void* data, int idxA, int idxB);
169 void swapU64 (void* data, int idxA, int idxB);
170 
171 int compareS32 (void* data, int idxA, int idxB);
172 void swapS32 (void* data, int idxA, int idxB);
173 
174 int compareS64 (void* data, int idxA, int idxB);
175 void swapS64 (void* data, int idxA, int idxB);
176 
177 int compareF32 (void* data, int idxA, int idxB);
178 void swapF32 (void* data, int idxA, int idxB);
179 
180 int compareF64 (void* data, int idxA, int idxB);
181 void swapF64 (void* data, int idxA, int idxB);
182 
183 //------------------------------------------------------------------------
184 }
int compareF64(void *data, int idxA, int idxB)
Definition: Sort.cpp:311
int compareU32(void *data, int idxA, int idxB)
Definition: Sort.cpp:241
void sortDefaultSwap(void *data, int idxA, int idxB)
Definition: Sort.hpp:121
void swapS32(void *data, int idxA, int idxB)
Definition: Sort.cpp:276
void sort(void *data, int start, int end, SortCompareFunc compareFunc, SortSwapFunc swapFunc, bool multicore=false)
Definition: Sort.cpp:203
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid * data
Definition: DLLImports.inl:319
void swapF64(void *data, int idxA, int idxB)
Definition: Sort.cpp:318
int compareS64(void *data, int idxA, int idxB)
Definition: Sort.cpp:283
int compareS32(void *data, int idxA, int idxB)
Definition: Sort.cpp:269
void swapU32(void *data, int idxA, int idxB)
Definition: Sort.cpp:248
int compareF32(void *data, int idxA, int idxB)
Definition: Sort.cpp:297
void swapU64(void *data, int idxA, int idxB)
Definition: Sort.cpp:262
int compareU64(void *data, int idxA, int idxB)
Definition: Sort.cpp:255
void(* SortSwapFunc)(void *data, int idxA, int idxB)
Definition: Sort.hpp:47
FW_CUDA_FUNC void swap(T &a, T &b)
Definition: Defs.hpp:183
void swapS64(void *data, int idxA, int idxB)
Definition: Sort.cpp:290
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx void
Definition: DLLImports.inl:58
const T * getPtr(S32idx=0) const
bool sortDefaultCompare(void *data, int idxA, int idxB)
Definition: Sort.hpp:114
S32 getSize(void) const
bool(* SortCompareFunc)(void *data, int idxA, int idxB)
Definition: Sort.hpp:46
void swapF32(void *data, int idxA, int idxB)
Definition: Sort.cpp:304