-
Notifications
You must be signed in to change notification settings - Fork 0
/
turtle-multi-sort.cuh
209 lines (160 loc) · 6.24 KB
/
turtle-multi-sort.cuh
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
#include<chrono>
#include<vector>
#include<iostream>
#include"turtle-globals.cuh"
namespace HelperForMultiSorter
{
// when shared-memory performance is to be tested for multi-sorting
#define USE_SHARED_MEM 1
#define USE_INTERLEAVED_MEM
template<int ArrSize, int BlockSize>
__device__ int ind(const int index, const int threadId)
{
#ifdef USE_INTERLEAVED_MEM
return (index * BlockSize) + threadId;
#else
return index + (ArrSize * threadId);
#endif
}
template<typename Type, int ArrSize, int BlockSize>
__device__ void swap(Type* __restrict__ buf, const int index1, const int index2, const int threadId)
{
//printf(" (%i %i %i) ", threadId, buf[convertToInterleavedIndex(index1, threadId)], buf[convertToInterleavedIndex(index2, threadId)]);
const Type tmp = buf[ind<ArrSize, BlockSize>(index1, threadId)];
buf[ind<ArrSize, BlockSize>(index1, threadId)] = buf[ind<ArrSize, BlockSize>(index2, threadId)];
buf[ind<ArrSize, BlockSize>(index2, threadId)] = tmp;
}
template<typename Type, int ArrSize, int BlockSize>
__device__ void insert(Type* __restrict__ buf, const int index, const int threadId)
{
const int parentIndex = ((index + 1) / 2) - 1;
if (parentIndex >= 0 && parentIndex != index)
{
if (buf[ind<ArrSize, BlockSize>(parentIndex, threadId)] < buf[ind<ArrSize, BlockSize>(index, threadId)])
{
swap<Type, ArrSize, BlockSize>(buf, parentIndex, index, threadId);
insert<Type, ArrSize, BlockSize>(buf, parentIndex, threadId);
}
}
}
template<typename Type, int ArrSize, int BlockSize>
__device__ void remove(Type* __restrict__ buf, const int index, const int currentSize, const int threadIndex)
{
const int leftChildIndex = ((index + 1) * 2) - 1;
const int rightChildIndex = leftChildIndex + 1;
if (leftChildIndex < currentSize && rightChildIndex < currentSize)
{
if (buf[ind<ArrSize, BlockSize>(leftChildIndex, threadIndex)] > buf[ind<ArrSize, BlockSize>(rightChildIndex, threadIndex)])
{
if (buf[ind<ArrSize, BlockSize>(index, threadIndex)] < buf[ind<ArrSize, BlockSize>(leftChildIndex, threadIndex)])
{
swap<Type, ArrSize, BlockSize>(buf, index, leftChildIndex, threadIndex);
remove<Type, ArrSize, BlockSize>(buf, leftChildIndex, currentSize, threadIndex);
}
}
else
{
if (buf[ind<ArrSize, BlockSize>(index, threadIndex)] < buf[ind<ArrSize, BlockSize>(rightChildIndex, threadIndex)])
{
swap<Type, ArrSize, BlockSize>(buf, index, rightChildIndex, threadIndex);
remove<Type, ArrSize, BlockSize>(buf, rightChildIndex, currentSize, threadIndex);
}
}
}
else if (leftChildIndex < currentSize)
{
if (buf[ind<ArrSize, BlockSize>(index, threadIndex)] < buf[ind<ArrSize, BlockSize>(leftChildIndex, threadIndex)])
{
swap<Type, ArrSize, BlockSize>(buf, index, leftChildIndex, threadIndex);
remove<Type, ArrSize, BlockSize>(buf, leftChildIndex, currentSize, threadIndex);
}
}
}
template<typename Type, int ArrSize, int BlockSize>
__global__ void multiHeapSortWithShared(Type* __restrict__ data, Type* dataInterleaved)
{
int tid = threadIdx.x;
int gid = blockIdx.x;
__shared__ Type mem[ArrSize * BlockSize];
__shared__ Type memTmp[ArrSize * BlockSize];
for (int i = 0; i < ArrSize; i++)
memTmp[tid + BlockSize * i] = data[gid * BlockSize * ArrSize + tid + BlockSize * i];
__syncthreads();
for (int i = 0; i < ArrSize; i++)
mem[ind<ArrSize, BlockSize>(i, tid)] = memTmp[tid * ArrSize + i];
__syncthreads();
// single-thread sort
// insert elements 1 by 1
for (int i = 1; i < ArrSize; i++)
{
insert<Type, ArrSize, BlockSize>(mem, i, tid);
}
// remove elements 1 by 1
for (int i = 0; i < ArrSize; i++)
{
const int currentSize = ArrSize - i;
// remove root
int tmp = mem[ind<ArrSize, BlockSize>(0, tid)];
mem[ind<ArrSize, BlockSize>(0, tid)] = mem[ind<ArrSize, BlockSize>(currentSize - 1, tid)];
remove<Type, ArrSize, BlockSize>(mem, 0, currentSize - 1, tid);
mem[ind<ArrSize, BlockSize>(currentSize - 1, tid)] = tmp;
}
__syncthreads();
for (int i = 0; i < ArrSize; i++)
memTmp[tid * ArrSize + i] = mem[ind<ArrSize, BlockSize>(i, tid)];
__syncthreads();
for (int i = 0; i < ArrSize; i++)
data[gid * BlockSize * ArrSize + tid + BlockSize * i] = memTmp[tid + BlockSize * i];
__syncthreads();
}
template<typename Type, int ArrSize, int BlockSize>
__global__ void multiHeapSort(Type* __restrict__ data, Type* dataInterleaved)
{
int tid = threadIdx.x;
int gid = blockIdx.x;
Type* mem = dataInterleaved + (gid * ArrSize * BlockSize);
// single-thread sort
// insert elements 1 by 1
for (int i = 1; i < ArrSize; i++)
{
insert<Type, ArrSize, BlockSize>(mem, i, tid);
}
// remove elements 1 by 1
for (int i = 0; i < ArrSize; i++)
{
const int currentSize = ArrSize - i;
// remove root
int tmp = mem[ind<ArrSize, BlockSize>(0, tid)];
mem[ind<ArrSize, BlockSize>(0, tid)] = mem[ind<ArrSize, BlockSize>(currentSize - 1, tid)];
remove<Type, ArrSize, BlockSize>(mem, 0, currentSize - 1, tid);
mem[ind<ArrSize, BlockSize>(currentSize - 1, tid)] = tmp;
}
__syncthreads();
for (int i = 0; i < ArrSize; i++)
data[gid * BlockSize * ArrSize + tid * ArrSize + i] = mem[ind<ArrSize, BlockSize>(i, tid)];
}
}
namespace Turtle
{
namespace Multi
{
template<typename Type, int ArrSize, int BlockSize,bool UseSharedMemory>
struct MultiSorter
{
private:
public:
void MultiSort(const int numArrays, Type* hostData, Type* deviceData, Type* deviceDataInterleaved)
{
const int nElementsTotal = numArrays * ArrSize;
TurtleGlobals::gpuErrchk(cudaMemcpy((void*)deviceData, hostData, nElementsTotal * sizeof(Type), cudaMemcpyHostToDevice));
if(UseSharedMemory)
HelperForMultiSorter::multiHeapSortWithShared<Type, ArrSize* UseSharedMemory + !UseSharedMemory, BlockSize* UseSharedMemory+!UseSharedMemory> << < numArrays / BlockSize, BlockSize >> > (deviceData, deviceDataInterleaved);
else
HelperForMultiSorter::multiHeapSort<Type, ArrSize, BlockSize> << < numArrays / BlockSize, BlockSize >> > (deviceData, deviceDataInterleaved);
TurtleGlobals::gpuErrchk(cudaDeviceSynchronize());
TurtleGlobals::gpuErrchk(cudaMemcpy(hostData, (void*)deviceData, nElementsTotal * sizeof(Type), cudaMemcpyDeviceToHost));
}
};
}
}
#undef USE_INTERLEAVED_MEM