-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsort.cl
185 lines (121 loc) · 4.72 KB
/
sort.cl
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
typedef int2 ElemT;
typedef int KeyT;
typedef int ValT;
inline KeyT getKey(ElemT v) { return v.x; }
inline ValT getVal(ElemT v) { return v.y; }
inline bool compare(ElemT a, ElemT b) { return getKey(a) < getKey(b); }
__kernel void bitonic_pass_kernel(__global ElemT* theArray, int stage, int passOfStage, int a_invertModeOn)
{
int j = get_global_id(0);
const int r = 1 << (passOfStage);
const int lmask = r - 1;
const int left = ((j >> passOfStage) << (passOfStage + 1)) + (j & lmask);
const int right = left + r;
const ElemT a = theArray[left];
const ElemT b = theArray[right];
const bool cmpRes = compare(a, b);
const ElemT minElem = cmpRes ? a : b;
const ElemT maxElem = cmpRes ? b : a;
const int oddEven = j >> stage;
const bool isSwap = (oddEven & 1) & a_invertModeOn;
const int minId = isSwap ? right : left;
const int maxId = isSwap ? left : right;
theArray[minId] = minElem;
theArray[maxId] = maxElem;
}
__kernel void bitonic_512(__global ElemT* theArray, int stage, int passOfStageBegin, int a_invertModeOn)
{
int tid = get_global_id(0);
int lid = get_local_id(0);
int blockId = (tid / 256);
__local ElemT s_array[512];
s_array[lid + 0] = theArray[blockId*512 + lid + 0];
s_array[lid + 256] = theArray[blockId*512 + lid + 256];
barrier(CLK_LOCAL_MEM_FENCE);
for (int passOfStage = passOfStageBegin; passOfStage >= 0; passOfStage--)
{
const int j = lid;
const int r = 1 << (passOfStage);
const int lmask = r - 1;
const int left = ((j >> passOfStage) << (passOfStage + 1)) + (j & lmask);
const int right = left + r;
const ElemT a = s_array[left];
const ElemT b = s_array[right];
const bool cmpRes = compare(a, b);
const ElemT minElem = cmpRes ? a : b;
const ElemT maxElem = cmpRes ? b : a;
const int oddEven = tid >> stage; // (j >> stage)
const bool isSwap = (oddEven & 1) & a_invertModeOn;
const int minId = isSwap ? right : left;
const int maxId = isSwap ? left : right;
s_array[minId] = minElem;
s_array[maxId] = maxElem;
barrier(CLK_LOCAL_MEM_FENCE);
}
theArray[blockId*512 + lid + 0] = s_array[lid + 0];
theArray[blockId*512 + lid + 256] = s_array[lid + 256];
}
__kernel void bitonic_1024(__global ElemT* theArray, int stage, int passOfStageBegin, int a_invertModeOn)
{
int tid = get_global_id(0);
int lid = get_local_id(0);
int blockId = tid / 512;
__local ElemT s_array[1024];
s_array[lid + 0 ] = theArray[blockId * 1024 + lid + 0];
s_array[lid + 512] = theArray[blockId * 1024 + lid + 512];
barrier(CLK_LOCAL_MEM_FENCE);
for (int passOfStage = passOfStageBegin; passOfStage >= 0; passOfStage--)
{
const int j = lid;
const int r = 1 << (passOfStage);
const int lmask = r - 1;
const int left = ((j >> passOfStage) << (passOfStage + 1)) + (j & lmask);
const int right = left + r;
const ElemT a = s_array[left];
const ElemT b = s_array[right];
const bool cmpRes = compare(a, b);
const ElemT minElem = cmpRes ? a : b;
const ElemT maxElem = cmpRes ? b : a;
const int oddEven = tid >> stage; // (j >> stage)
const bool isSwap = (oddEven & 1) & a_invertModeOn;
const int minId = isSwap ? right : left;
const int maxId = isSwap ? left : right;
s_array[minId] = minElem;
s_array[maxId] = maxElem;
barrier(CLK_LOCAL_MEM_FENCE);
}
theArray[blockId * 1024 + lid + 0] = s_array[lid + 0];
theArray[blockId * 1024 + lid + 512] = s_array[lid + 512];
}
__kernel void bitonic_2048(__global ElemT* theArray, int stage, int passOfStageBegin, int a_invertModeOn)
{
int tid = get_global_id(0);
int lid = get_local_id(0);
int blockId = tid / 1024;
__local ElemT s_array[2048];
s_array[lid + 0 ] = theArray[blockId * 2048 + lid + 0];
s_array[lid + 1024] = theArray[blockId * 2048 + lid + 1024];
barrier(CLK_LOCAL_MEM_FENCE);
for (int passOfStage = passOfStageBegin; passOfStage >= 0; passOfStage--)
{
const int j = lid;
const int r = 1 << (passOfStage);
const int lmask = r - 1;
const int left = ((j >> passOfStage) << (passOfStage + 1)) + (j & lmask);
const int right = left + r;
const ElemT a = s_array[left];
const ElemT b = s_array[right];
const bool cmpRes = compare(a, b);
const ElemT minElem = cmpRes ? a : b;
const ElemT maxElem = cmpRes ? b : a;
const int oddEven = tid >> stage; // (j >> stage)
const bool isSwap = (oddEven & 1) & a_invertModeOn;
const int minId = isSwap ? right : left;
const int maxId = isSwap ? left : right;
s_array[minId] = minElem;
s_array[maxId] = maxElem;
barrier(CLK_LOCAL_MEM_FENCE);
}
theArray[blockId * 2048 + lid + 0] = s_array[lid + 0];
theArray[blockId * 2048 + lid + 1024] = s_array[lid + 1024];
}