-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.c
366 lines (310 loc) · 10.7 KB
/
find.c
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#define _DEFAULT_SOURCE
#define NB_THREADS 8
#define CACHE_LINE_SIZE 64
#include <immintrin.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
// Written by Charles MASSON
/*
count_ones_table
----------------
This is an array of integers such that count_ones_table[i] is the number of ones in the binary value of i.
count_ones_table is used in the vector computing implementation to improve the performances.
*/
int count_ones_table[256];
int count_ones(int n) {
int count = 0;
while (n != 0) {
if (n & 1)
count++;
n >>= 1;
}
return count;
}
void initialize_count_ones_table() {
for (int i = 0; i < 256; i++)
count_ones_table[i] = count_ones(i);
}
/*
get_time_ns()
-------------
Used to measure the execution time.
*/
long get_time_ns() {
struct timespec t;
clock_gettime(CLOCK_REALTIME, &t);
return t.tv_nsec + t.tv_sec * 1E9L;
}
/*
generate_U()
------------
Generates an array of random integers.
*/
int* generate_U(int nb_items, int min_val, int max_val) {
int *U = (int*) aligned_alloc(CACHE_LINE_SIZE, nb_items * sizeof(int));
for (int i = 0; i < nb_items; i++)
U[i] = rand() % (max_val - min_val + 1) + min_val;
return U;
}
/*
Scalar implementation
---------------------
This is the basic implementation, which looks for all occurrences of val within U between indices i_start and i_stop,
with step i_step, and writes the indices of the occurrences of val in ind_val.
*/
int find(int *U, int i_start, int i_end, int i_step, int val, int **ind_val) {
*ind_val = (int*) malloc(0);
int nb_find = 0;
for (int i = i_start; i <= i_end; i += i_step)
if (U[i] == val) {
nb_find++;
*ind_val = (int*) realloc(*ind_val, nb_find * sizeof(int));
(*ind_val)[nb_find - 1] = i;
}
return nb_find;
}
/*
Vector computing implementation
-------------------------------
This is the version that makes the most of vector computing.
i_step must be a multiple of 8.
*/
int vect_find(int *U, int i_start, int i_end, int i_step, int val, int **ind_val) {
if (i_step % 8 != 0)
return -1;
*ind_val = (int*) malloc(0);
int nb_find = 0;
__m256 vect_val = _mm256_castsi256_ps(_mm256_set1_epi32(val));
int i, j, mask;
for (i = i_start; i + 8 < i_end; i += i_step) {
mask = _mm256_movemask_ps(_mm256_cmp_ps(_mm256_loadu_ps((float*) (U + i)), vect_val, _CMP_EQ_OS));
if (mask) {
*ind_val = (int*) realloc(*ind_val, (nb_find + count_ones_table[mask]) * sizeof(int));
for (j = i; mask != 0; j++) {
if (mask & 1) {
(*ind_val)[nb_find] = j;
nb_find++;
}
mask >>= 1;
}
}
}
for (; i <= i_end; i++)
if (U[i] == val) {
nb_find++;
*ind_val = (int*) realloc(*ind_val, nb_find * sizeof(int));
(*ind_val)[nb_find - 1] = i;
}
return nb_find;
}
/*
Multithreaded implementation
----------------------------
*/
// To avoid global variables, we could incorporate them to thread_data and feed them to watch_nb_find_thread.
bool stop_threads = false;
int nb_find_thread[NB_THREADS];
int *U_threads;
int val_threads;
// Those variables are specific to each thread.
struct thread_data {
int i_start;
int i_end;
int i_step;
int **ind_val;
int *nb_find;
};
void *scalar_thread_function(void* thread_arg) {
struct thread_data *my_data;
my_data = (struct thread_data*) thread_arg;
int i_start = my_data->i_start;
int i_end = my_data->i_end;
int i_step = my_data->i_step;
int **ind_val = my_data->ind_val;
int *nb_find = my_data->nb_find;
*ind_val = (int*) malloc(0);
for (int i = i_start; i <= i_end; i += i_step) {
if (stop_threads)
pthread_exit(NULL);
if (U_threads[i] == val_threads) {
(*nb_find)++;
*ind_val = (int*) realloc(*ind_val, *nb_find * sizeof(int));
(*ind_val)[*nb_find - 1] = i;
}
}
pthread_exit(NULL);
}
void *vect_thread_function(void* thread_arg) {
struct thread_data *my_data;
my_data = (struct thread_data*) thread_arg;
int i_start = my_data->i_start;
int i_end = my_data->i_end;
int i_step = my_data->i_step;
int **ind_val = my_data->ind_val;
int *nb_find = my_data->nb_find;
*ind_val = (int*) malloc(0);
__m256 vect_val = _mm256_castsi256_ps(_mm256_set1_epi32(val_threads));
int i, j, mask;
for (i = i_start; i + 8 < i_end; i += i_step) {
if (stop_threads)
pthread_exit(NULL);
mask = _mm256_movemask_ps(_mm256_cmp_ps(_mm256_loadu_ps((float*) (U_threads + i)), vect_val, _CMP_EQ_OS));
if (mask) {
*ind_val = (int*) realloc(*ind_val, (*nb_find + count_ones_table[mask]) * sizeof(int));
for (j = i; mask != 0; j++) {
if (mask & 1) {
(*ind_val)[*nb_find] = j;
(*nb_find)++;
}
mask >>= 1;
}
}
}
for (; i <= i_end; i++)
if (U_threads[i] == val_threads) {
(*nb_find)++;
*ind_val = (int*) realloc(*ind_val, *nb_find * sizeof(int));
(*ind_val)[*nb_find - 1] = i;
}
pthread_exit(NULL);
}
void *watch_nb_find(void* thread_arg) {
int *k = (int*) thread_arg;
int nb_find;
while (true) {
usleep(1000);
if (stop_threads)
pthread_exit(NULL);
nb_find = 0;
for (int t = 0; t < NB_THREADS; t++)
nb_find += nb_find_thread[t];
if (nb_find > *k) {
stop_threads = true;
pthread_exit(NULL);
}
}
}
int thread_find(int *U, int i_start, int i_end, int i_step, int val, int **ind_val, int k, int ver) {
// Check the version to use (scalar of vector computing)
void *thread_function;
switch (ver) {
case 0:
thread_function = scalar_thread_function;
break;
case 1:
if (i_step % 8 != 0)
return -1;
thread_function = vect_thread_function;
break;
default:
printf("Invalid value for \"ver\".\n");
return -1;
}
// Initialize the variables
U_threads = U;
val_threads = val;
int **ind_val_thread[NB_THREADS];
for (int t = 0; t < NB_THREADS; t++) {
nb_find_thread[t] = 0;
ind_val_thread[t] = (int**) malloc(sizeof(int*));
}
// Start watch_nb_find_thread if nescessary
pthread_t watch_nb_find_thread;
if (k >= 0) {
pthread_create(&watch_nb_find_thread, NULL, watch_nb_find, &k);
}
// Initialize the variables that are specific to the threads and start the threads
pthread_t threads[NB_THREADS];
struct thread_data thread_data_array[NB_THREADS];
for (int t = 0; t < NB_THREADS; t++) {
thread_data_array[t].i_start = t * ((i_end - i_start) / i_step + 1) / NB_THREADS * i_step + i_start;
thread_data_array[t].i_end = (t + 1) * ((i_end - i_start) / i_step + 1) / NB_THREADS * i_step + i_start - 1;
thread_data_array[t].i_step = i_step;
thread_data_array[t].ind_val = ind_val_thread[t];
thread_data_array[t].nb_find = &nb_find_thread[t];
pthread_create(&threads[t], NULL, thread_function, (void*) &thread_data_array[t]);
}
// Wait for the threads to finish running
int nb_find = 0;
for (int t = 0; t < NB_THREADS; t++) {
pthread_join(threads[t], NULL);
nb_find += nb_find_thread[t];
}
// In case watch_nb_find_thread is still running
stop_threads = true;
// Concatenate the arrays that contain the indices of the found occurrences
*ind_val = (int*) malloc(nb_find * sizeof(int));
int *ind_val_current = *ind_val;
for (int t = 0; t < NB_THREADS; t++) {
memcpy(ind_val_current, *ind_val_thread[t], nb_find_thread[t] * sizeof(int));
ind_val_current += nb_find_thread[t];
}
// If necessary, ignore the extra indices
if (k >= 0) {
if (nb_find > k) {
*ind_val = (int*) realloc(*ind_val, k * sizeof(int));
nb_find = k;
}
pthread_join(watch_nb_find_thread, NULL);
}
return nb_find;
}
int main(int argc, char *argv[]){
srand((unsigned) time(NULL));
initialize_count_ones_table();
long t_start, t_end;
int **ind_val = (int**) malloc(sizeof(int*));
int nb_find;
bool print_ind = argc >= 2 ? atoi(argv[1]) != 0 : false;
int size = argc >= 3 ? atoi(argv[2]) : 1E9;
int min = argc >= 5 ? atoi(argv[3]) : 0;
int max = argc >= 5 ? atoi(argv[4]) : 100;
int val = argc >= 6 ? atoi(argv[5]) : rand() % (max - min + 1) + min;
printf("Creating a random input array with %i values between %i and %i...\n", size, min, max);
int *U = generate_U(size, min, max);
printf("Done.\nValue to seek: %i.\n", val);
// Scalar implementation
printf("\nRunning scalar version...\n");
t_start = get_time_ns();
nb_find = find(U, 0, size - 1, 1, val, ind_val);
t_end = get_time_ns();
printf("Done. Execution time: %liµs\n", (t_end - t_start) / 1000);
printf("Found %i valid indices.\n", nb_find);
if (print_ind) {
printf("Valid indices: ");
for (int i = 0; i < nb_find; i++)
printf("%i ", (*ind_val)[i]);
printf("\n");
}
// Vector computing implementation
printf("\nRunning vector version...\n");
t_start = get_time_ns();
nb_find = vect_find(U, 0, size - 1, 8, val, ind_val);
t_end = get_time_ns();
printf("Done. Execution time: %liµs\n", (t_end - t_start) / 1000);
printf("Found %i valid indices.\n", nb_find);
if (print_ind) {
printf("Valid indices: ");
for (int i = 0; i < nb_find; i++)
printf("%i ", (*ind_val)[i]);
printf("\n");
}
// multithreaded implementation (with vector computing)
printf("\nRunning multithreaded version...\n");
t_start = get_time_ns();
nb_find = thread_find(U, 0, size - 1, 8, val, ind_val, -1, 1);
t_end = get_time_ns();
printf("Done. Execution time: %liµs\n", (t_end - t_start) / 1000);
printf("Found %i valid indices.\n", nb_find);
if (print_ind) {
printf("Valid indices: ");
for (int i = 0; i < nb_find; i++)
printf("%i ", (*ind_val)[i]);
printf("\n");
}
}