-
Notifications
You must be signed in to change notification settings - Fork 88
/
generate_bitonic_sort.py
148 lines (130 loc) · 4.33 KB
/
generate_bitonic_sort.py
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
import requests
max_small_sort = 16
small_sort_only = True
def generate_small_sort(n, rev=False):
s = "http://jgamble.ripco.net/cgi-bin/nw.cgi?inputs=%d&algorithm=best&output=svg" % n
ret = requests.get(s)
content = ret.text
lines = [l for l in content.split('\n') if l.startswith('[[')]
swps = ""
for _l in lines:
l = _l[1:-1]
ll = l.split(']')
for _t in ll:
if '[' not in _t:
continue
t = _t.split('[')[-1]
i, j = t.split(',')
if not rev:
swps += "\tSORT_CSWAP(dst[%s], dst[%s]);\n" % (i, j)
else:
swps += "\tSORT_CSWAP(dst[%s], dst[%s]);\n" % (j, i)
swps += '\n'
if not rev:
f = """
#define BITONIC_SORT_%d SORT_MAKE_STR(bitonic_sort_%d)
static __inline void BITONIC_SORT_%d(SORT_TYPE *dst) {
%s}
""" % (n, n, n, swps[:-1])
else:
f = """
#define BITONIC_SORT_REVERSE_%d SORT_MAKE_STR(bitonic_sort_reverse_%d)
static __inline void BITONIC_SORT_REVERSE_%d(SORT_TYPE *dst) {
%s}
""" % (n, n, n, swps[:-1])
return f
bitonic_sort_str = """
#ifndef SORT_CSWAP
#define SORT_CSWAP(x, y) { if(SORT_CMP((x),(y)) > 0) {SORT_SWAP((x),(y));}}
#endif
"""
bitonic_sort_str += "\n".join(generate_small_sort(i) for i in range(2,max_small_sort+1))
if small_sort_only:
bitonic_sort_case = "\n".join(" case %d:\n BITONIC_SORT_%d(dst);\n break;" % (n, n) for n in range(2, max_small_sort+1))
bitonic_sort_str += """
void BITONIC_SORT(SORT_TYPE *dst, const size_t size) {
switch(size) {
case 0:
case 1:
break;
%s
default:
BINARY_INSERTION_SORT(dst, size);
}
}
""" % (bitonic_sort_case)
print(bitonic_sort_str)
exit()
bitonic_sort_str += "\n".join(generate_small_sort(i, rev=True) for i in range(2,max_small_sort+1))
bitonic_sort_case = "\n".join(" case %d:\n BITONIC_SORT_%d(dst);\n break;" % (n, n) for n in range(2, max_small_sort+1))
bitonic_sort_case_rev = "\n".join(" case %d:\n BITONIC_SORT_REVERSE_%d(dst);\n break;" % (n, n) for n in range(2, max_small_sort+1))
bitonic_sort_str += """
#define BITONIC_SORT SORT_MAKE_STR(bitonic_sort)
void BITONIC_SORT(SORT_TYPE *dst, const size_t size);
#define BITONIC_SORT_REVERSE SORT_MAKE_STR(bitonic_sort_reverse)
void BITONIC_SORT_REVERSE(SORT_TYPE *dst, const size_t size);
"""
bitonic_sort_str += """
#define BITONIC_MERGE SORT_MAKE_STR(bitonic_merge)
void BITONIC_MERGE(SORT_TYPE *dst, const size_t size) {
size_t m, i, j;
if (size <= 1) {
return;
}
m = 1ULL<<(63 - CLZ(size-1));
j = m;
for (i = 0; i < size - m; ++i, ++j) {
SORT_CSWAP(dst[i], dst[j]);
}
BITONIC_MERGE(dst, m);
BITONIC_MERGE(dst + m, size - m);
}
#define BITONIC_MERGE_REVERSE SORT_MAKE_STR(bitonic_merge_reverse)
void BITONIC_MERGE_REVERSE(SORT_TYPE *dst, const size_t size) {
size_t m, i, j;
if (size <= 1) {
return;
}
m = 1ULL<<(63 - CLZ(size-1));
j = m;
for (i = 0; i < size - m; ++i, ++j) {
SORT_CSWAP(dst[j], dst[i]);
}
BITONIC_MERGE_REVERSE(dst, m);
BITONIC_MERGE_REVERSE(dst + m, size - m);
}
"""
bitonic_sort_str += """
void BITONIC_SORT(SORT_TYPE *dst, const size_t size) {
switch(size) {
case 0:
case 1:
break;
%s
default:
/*printf("Bitonic sort size = %%ld", size);*/
BITONIC_SORT_REVERSE(dst, (size>>1));
BITONIC_SORT(dst + (size>>1), size - (size>>1));
BITONIC_MERGE(dst, size);
}
}
""" % (bitonic_sort_case)
bitonic_sort_str += """
void BITONIC_SORT_REVERSE(SORT_TYPE *dst, const size_t size) {
switch(size) {
case 0:
case 1:
break;
%s
default:
/*printf("Bitonic sort reverse size = %%ld", size);*/
BITONIC_SORT(dst, (size>>1));
BITONIC_SORT_REVERSE(dst + (size>>1), size - (size>>1));
BITONIC_MERGE_REVERSE(dst, size);
}
}
""" % (bitonic_sort_case_rev)
import os
with open('bitonic_sort.h', 'w') as F:
F.write(bitonic_sort_str)
os.system('astyle --options=astyle.options bitonic_sort.h')