-
Notifications
You must be signed in to change notification settings - Fork 8
/
set_intersection.cpp
220 lines (198 loc) · 6.08 KB
/
set_intersection.cpp
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
210
211
212
213
214
215
216
217
218
219
220
#include <cstdio>
#include <chrono>
#include "projectconfig.h"
#include "constants.h"
#include "shuffle_dictionary.hpp"
#include "intersection/naive.hpp"
#include "intersection/stl.hpp"
#include "intersection/branchless.hpp"
#ifdef __SSE2__
# include "intersection/sse.hpp"
//# include "intersection/galloping_sse.hpp"
#endif
#ifdef __AVX__
# include "intersection/avx.hpp"
#endif
#ifdef __AVX2__
# include "intersection/avx2.hpp"
//# include "intersection/galloping_avx2.hpp"
//# include "intersection/galloping.hpp"
#endif
#if defined(__AVX512F__) && defined(__AVX512CD__) && defined(__AVX512DQ__)
# include "intersection/avx512.hpp"
#endif
void run(uint32_t **lists,
size_t (*func)(const uint32_t*,size_t,const uint32_t*,size_t,uint32_t*)=nullptr,
size_t (*func_count)(const uint32_t*,size_t,const uint32_t*,size_t)=nullptr,
size_t (*func_index)(const uint32_t*,size_t,const uint32_t*,size_t,uint32_t*)=nullptr
){
if(func){
auto t_start = std::chrono::high_resolution_clock::now();
size_t intersected=0;
for(size_t repeat=0; repeat<repeatCount; ++repeat){
for(size_t i=0; i<listCount; ++i){
uint32_t *intersected_list = new uint32_t[arraySize];
for(size_t j=i+1; j<listCount; ++j){
intersected += func(
lists[i], arraySize,
lists[j], arraySize,
intersected_list
);
}
delete[] intersected_list;
}
}
auto t_end = std::chrono::high_resolution_clock::now();
printf("Wall clock time passed: %10.2f ms - %lu\n",
std::chrono::duration<double, std::milli>(t_end-t_start).count(),
intersected
);
}
if(func_count){
auto t_start = std::chrono::high_resolution_clock::now();
size_t intersected=0;
for(size_t repeat=0; repeat<repeatCount; ++repeat){
for(size_t i=0; i<listCount; ++i){
for(size_t j=i+1; j<listCount; ++j){
intersected += func_count(
lists[i], arraySize,
lists[j], arraySize
);
}
}
}
auto t_end = std::chrono::high_resolution_clock::now();
printf("Wall clock time passed: %10.2f ms - %lu - just counting\n",
std::chrono::duration<double, std::milli>(t_end-t_start).count(),
intersected
);
}
if(func_index){
auto t_start = std::chrono::high_resolution_clock::now();
size_t intersected=0;
for(size_t repeat=0; repeat<repeatCount; ++repeat){
for(size_t i=0; i<listCount; ++i){
uint32_t *intersected_list = new uint32_t[arraySize];
for(size_t j=i+1; j<listCount; ++j){
intersected += func(
lists[i], arraySize,
lists[j], arraySize,
intersected_list
);
}
delete[] intersected_list;
}
}
auto t_end = std::chrono::high_resolution_clock::now();
printf("Wall clock time passed: %10.2f ms - %lu - index\n",
std::chrono::duration<double, std::milli>(t_end-t_start).count(),
intersected
);
}
}
int main(){
auto t_start = std::chrono::high_resolution_clock::now();
// load lists from file which was generated by genLists
FILE *fd = fopen("test.dat", "rb");
if(!fd){
puts("couldn't open test.dat");
return -1;
}
uint32_t **lists = new uint32_t*[listCount];
for(size_t i=0; i<listCount; ++i){
lists[i] = (uint32_t*)aligned_alloc(64, arraySize*sizeof(uint32_t));
fread(lists[i], 4, arraySize, fd);
}
fclose(fd);
auto t_end = std::chrono::high_resolution_clock::now();
printf("preparing lists done - %f ms\n",
std::chrono::duration<double, std::milli>(t_end-t_start).count()
);
//puts("scalar:");
//run(lists, intersect_scalar, intersect_scalar_count, intersect_scalar_index);
puts("stl set_intersection:");
run(lists, intersect_scalar_stl);
#if __GNUC__ >= 5
//puts("stl parallel set_intersection: uses more than one core, just for reference here");
//run(lists, intersect_scalar_stl_parallel);
#endif
puts("c branchless scalar:");
run(lists, intersect_scalar_branchless_c, intersect_scalar_branchless_c_count);
#ifndef DISABLE_ASM
puts("asm branchless scalar:");
run(lists, intersect_scalar_branchless, intersect_scalar_branchless_count);
#endif
#ifdef __SSE2__
puts("128bit SSE vector:");
run(lists, intersect_vector_sse, intersect_vector_sse_count);
#ifndef DISABLE_ASM
puts("128bit SSE vector - asm:");
run(lists, intersect_vector_sse_asm);
#endif
#endif
#ifdef __AVX__
puts("256bit AVX vector: (not AVX2)");
run(lists, intersect_vector_avx, intersect_vector_avx_count);
#ifndef DISABLE_ASM
puts("256bit AVX vector: (not AVX2) - asm");
//FIXME: normal intersection segfaults
//run(lists, intersect_vector_avx_asm, intersect_vector_avx_asm_count);
run(lists, nullptr, intersect_vector_avx_asm_count);
#endif
#endif
#ifdef __AVX2__
puts("256bit AVX2 vector");
run(lists, intersect_vector_avx2, intersect_vector_avx2_count);
#ifndef DISABLE_ASM
puts("256bit AVX2 vector - asm");
run(lists, intersect_vector_avx2_asm, intersect_vector_avx2_asm_count);
#endif
#endif
#if defined(__AVX512F__) && defined(__AVX512CD__) && defined(__AVX512DQ__)
puts("512bit AVX512 vector using vpconflictd");
run(lists, intersect_vector_avx512_conflict);
#ifndef DISABLE_ASM
puts("512bit AVX512 vector using vpconflictd - asm");
run(lists, intersect_vector_avx512_conflict_asm);
#endif
puts("512bit AVX512 vector using shuffling");
run(lists, intersect_vector_avx512);
#ifndef DISABLE_ASM
puts("512bit AVX512 vector using shuffling - asm");
run(lists, intersect_vector_avx512_asm);
#endif
#ifdef __AVX512VP2INTERSECT__
puts("512bit AVX512 vector using vp2intersect");
run(lists, intersect_vector_avx512_2intersect);
#endif
#endif
#if 0
puts("v1");
run(lists, v1);
puts("v3");
run(lists, v3);
puts("SIMD galloping");
run(lists, SIMDgalloping);
puts("v1_avx2");
run(lists, v1_avx2);
puts("v3_avx2");
run(lists, v3_avx2);
puts("SIMD galloping AVX2");
run(lists, SIMDgalloping_avx2);
#endif
#if 0
puts("galloping SSE");
run(lists, SIMDintersection);
puts("galloping AVX2");
run(lists, SIMDintersection_avx2);
#endif
// puts("SIMD Galloping V1: AVX");
// run(lists, intersect_galloping_V1_AVX, intersect_galloping_V1_AVX_count);
// puts("SIMD Galloping V1: SSE4.2"); //FIXME: broken
// run(lists, intersect_galloping_V1_SSE, intersect_galloping_V1_SSE_count);
for(size_t i=0; i<listCount; ++i){
free(lists[i]);
}
delete[] lists;
return 0;
}