/
c_parallel.c
351 lines (314 loc) · 10.7 KB
/
c_parallel.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
// SPDX-FileCopyrightText: Copyright 2024 Roland Csaszar
// SPDX-License-Identifier: MIT
//
// Project: 1-billion-row-challenge
// File: c_parallel.c
// Date: 23.Mar.2024
//
// =============================================================================
#include <errno.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Non-portable, Unix* specific.
#include <pthread.h>
#include <sys/mman.h>
struct StationTemperatures_t {
int64_t* temp_sum;
uint64_t* count;
int64_t* min;
int64_t* max;
};
typedef struct StationTemperatures_t StationTemperatures;
struct Chunk_t {
size_t start_idx;
size_t end_idx;
};
typedef struct Chunk_t Chunk;
struct MapStruct_t {
char name[100];
size_t idx;
};
typedef struct MapStruct_t MapStruct;
struct ThreadData_t {
char const* data;
size_t data_size;
};
typedef struct ThreadData_t ThreadData;
struct Result_t {
StationTemperatures temp;
MapStruct* idx_map;
};
typedef struct Result_t Result;
#define NUM_BITS ((uint32_t)16)
#define MASK ((uint32_t)((1 << NUM_BITS) - 1))
#define FNV_PRIME ((uint32_t)16777619)
#define FNV_OFFSET_BASIS ((uint32_t)2166136261)
uint32_t fnv_hash(char* s) {
uint32_t hash = FNV_OFFSET_BASIS;
while (*s) {
hash ^= (uint32_t)*s;
hash *= FNV_PRIME;
s++;
}
return hash & MASK;
}
Chunk const* generate_chunk_indices(size_t num_threads,
size_t data_size,
char const* data,
size_t chunk_size) {
Chunk* chunk_list = calloc(num_threads, sizeof *chunk_list);
chunk_list[0].start_idx = 0;
chunk_list[0].end_idx = data_size - 1;
size_t read_off = chunk_size;
for (size_t cpu_idx = 1; cpu_idx < num_threads; cpu_idx++) {
char const* newline_ptr =
memchr(&data[read_off], '\n',
150); // 150, as the station name is 100 bytes at most
if (newline_ptr == 0) {
chunk_list[cpu_idx].end_idx = data_size - 1;
break;
}
size_t newline_idx = newline_ptr - data;
chunk_list[cpu_idx - 1].end_idx = newline_idx;
chunk_list[cpu_idx].start_idx = newline_idx + 1;
chunk_list[cpu_idx].end_idx = data_size - 1;
read_off += chunk_size;
}
return chunk_list;
}
void* process_chunk(void* thread_data) {
char const* data = ((ThreadData*)thread_data)->data;
size_t data_size = ((ThreadData*)thread_data)->data_size;
Result* result = malloc(sizeof *result);
result->temp.count = calloc(10000, sizeof *result->temp.count);
result->temp.temp_sum = calloc(10000, sizeof *result->temp.temp_sum);
result->temp.min = calloc(10000, sizeof *result->temp.min);
result->temp.max = calloc(10000, sizeof *result->temp.max);
result->idx_map = calloc(MASK + 1, sizeof *result->idx_map);
size_t station_idx = 0;
char station_name[100];
char const* content = data;
char const* data_end = data + data_size;
while (data_end - content > 0) {
size_t semicolon_idx = 1;
station_name[0] = content[0];
char curr_byte = content[1];
uint32_t name_hash = FNV_OFFSET_BASIS;
while (curr_byte != ';') {
station_name[semicolon_idx] = curr_byte;
name_hash ^= (uint32_t)curr_byte;
name_hash *= FNV_PRIME;
semicolon_idx++;
curr_byte = content[semicolon_idx];
}
station_name[semicolon_idx] = '\0';
name_hash &= MASK;
int64_t temperature = 0;
int64_t negate = 1;
if (content[semicolon_idx + 1] == '-') {
negate = -1;
content += semicolon_idx + 2;
} else {
content += semicolon_idx + 1;
}
if (content[1] == '.') {
temperature =
negate * ((int64_t)content[0] * 10 + (int64_t)content[2] - 528);
content += 4;
} else {
temperature =
negate * ((int64_t)content[0] * 100 + (int64_t)content[1] * 10 +
(int64_t)content[3] - 5328);
content += 5;
}
for (size_t i = name_hash; i < MASK + 1; i++) {
if (result->idx_map[i].name[0] == 0) {
memcpy(result->idx_map[i].name, station_name, semicolon_idx + 1);
result->idx_map[i].idx = station_idx;
result->temp.temp_sum[station_idx] = temperature;
result->temp.count[station_idx] = 1;
result->temp.min[station_idx] = temperature;
result->temp.max[station_idx] = temperature;
station_idx++;
break;
}
if (strcmp(result->idx_map[i].name, station_name) == 0) {
size_t idx = result->idx_map[i].idx;
result->temp.temp_sum[idx] += temperature;
result->temp.count[idx]++;
result->temp.min[idx] = result->temp.min[idx] > temperature
? temperature
: result->temp.min[idx];
result->temp.max[idx] = result->temp.max[idx] < temperature
? temperature
: result->temp.max[idx];
break;
}
} // for i
} // while content < data_end
return result;
}
void sum_results(size_t num_threads,
pthread_t* thread,
MapStruct* sum_idx_map,
StationTemperatures* sum_data) {
size_t station_idx = 0;
for (size_t thread_id = 0; thread_id < num_threads; thread_id++) {
Result const* result = 0;
int ret_val = pthread_join(thread[thread_id], (void**)&result);
if (ret_val != 0) {
fprintf(stderr, "Error joining thread: %s\n", strerror(ret_val));
exit(EXIT_FAILURE);
}
for (size_t res_idx = 0; res_idx < MASK + 1; res_idx++) {
MapStruct station = result->idx_map[res_idx];
if (station.name[0] == 0) {
continue;
}
size_t idx = station.idx;
uint32_t name_hash = fnv_hash(station.name);
for (size_t i = name_hash; i < MASK + 1; i++) {
if (sum_idx_map[i].name[0] == 0) {
sum_idx_map[i].idx = station_idx;
memcpy(sum_idx_map[i].name, station.name, sizeof sum_idx_map[i].name);
sum_data->temp_sum[station_idx] = result->temp.temp_sum[idx];
sum_data->count[station_idx] = result->temp.count[idx];
sum_data->min[station_idx] = result->temp.min[idx];
sum_data->max[station_idx] = result->temp.max[idx];
station_idx++;
break;
}
if (strcmp(sum_idx_map[i].name, station.name) == 0) {
size_t st_idx = sum_idx_map[i].idx;
sum_data->temp_sum[st_idx] += result->temp.temp_sum[idx];
sum_data->count[st_idx] += result->temp.count[idx];
sum_data->min[st_idx] = sum_data->min[st_idx] > result->temp.min[idx]
? result->temp.min[idx]
: sum_data->min[st_idx];
sum_data->max[st_idx] = sum_data->max[st_idx] < result->temp.max[idx]
? result->temp.max[idx]
: sum_data->max[st_idx];
break;
}
}
}
free((void*)result->temp.temp_sum);
free((void*)result->temp.count);
free((void*)result->temp.min);
free((void*)result->temp.max);
free((void*)result->idx_map);
free((void*)result);
}
}
int compare(void const* a, void const* b) {
char const* str_a = ((MapStruct const*)a)->name;
char const* str_b = ((MapStruct const*)b)->name;
if (str_a == 0 && str_b == 0) {
return 0;
}
if (str_a == 0) {
return -1;
}
if (str_b == 0) {
return 1;
}
return strcmp(str_a, str_b);
}
double round_java(double x) {
double rounded = trunc(x);
if (x < 0.0 && rounded - x == 0.5) {
// return
} else if (fabs(x - rounded) >= 0.5) {
rounded += (x > 0) - (x < 0);
}
// oh, another hardcoded `-0.0` to `0.0` conversion.
if (rounded == 0) {
return 0.0;
}
return rounded / 10.0;
}
void print_results(MapStruct* sum_idx_map, StationTemperatures const sum_data) {
qsort(sum_idx_map, (MASK + 1), sizeof *sum_idx_map, compare);
printf("{");
bool first = true;
for (size_t idx = 0; idx < MASK + 1; idx++) {
if (sum_idx_map[idx].name[0] != 0) {
if (first) {
first = false;
} else {
printf(", ");
}
size_t station_idx = sum_idx_map[idx].idx;
double mean = (double)(sum_data.temp_sum[station_idx]) /
(double)(sum_data.count[station_idx]);
printf("%s=%.1f/%.1f/%.1f", sum_idx_map[idx].name,
round_java((double)sum_data.min[station_idx]), round_java(mean),
round_java((double)sum_data.max[station_idx]));
}
}
printf("}\n");
}
int main(int argc, char* argv[]) {
if (argc != 2) {
fprintf(stderr, "Error: no data file to process given! Exiting.\n");
exit(EXIT_FAILURE);
}
char* file_name = argv[1];
FILE* file = fopen(file_name, "r");
if (file == NULL) {
fprintf(stderr, "Error opening file '%s':\n%s\n", file_name,
strerror(errno));
exit(EXIT_FAILURE);
}
fseek(file, 0, SEEK_END);
long data_size = ftell(file);
rewind(file);
size_t num_threads = 10;
size_t chunk_size = data_size / num_threads;
char* data = mmap(NULL, data_size, PROT_READ, MAP_SHARED, fileno(file), 0);
if (data == MAP_FAILED) {
fprintf(stderr, "Error mapping file '%s':\n%s\n", file_name,
strerror(errno));
exit(EXIT_FAILURE);
}
Chunk const* chunk_list =
generate_chunk_indices(num_threads, data_size, data, chunk_size);
pthread_t thread[num_threads];
ThreadData thread_data[num_threads];
for (size_t thread_id = 0; thread_id < num_threads; thread_id++) {
thread_data[thread_id].data = &data[chunk_list[thread_id].start_idx];
thread_data[thread_id].data_size =
chunk_list[thread_id].end_idx - chunk_list[thread_id].start_idx + 1;
int ret_val = pthread_create(&thread[thread_id], NULL, process_chunk,
(void*)&thread_data[thread_id]);
if (ret_val != 0) {
fprintf(stderr, "Error creating thread: %s\n", strerror(ret_val));
exit(EXIT_FAILURE);
}
}
StationTemperatures sum_data;
sum_data.temp_sum = calloc(10000, sizeof *sum_data.temp_sum);
sum_data.count = calloc(10000, sizeof *sum_data.count);
sum_data.min = calloc(10000, sizeof *sum_data.min);
sum_data.max = calloc(10000, sizeof *sum_data.max);
MapStruct* sum_idx_map = calloc((MASK + 1), sizeof *sum_idx_map);
free((void*)chunk_list);
sum_results(num_threads, thread, sum_idx_map, &sum_data);
print_results(sum_idx_map, sum_data);
free((void*)sum_data.temp_sum);
free((void*)sum_data.count);
free((void*)sum_data.min);
free((void*)sum_data.max);
free((void*)sum_idx_map);
if (munmap(data, data_size) != 0) {
fprintf(stderr, "Error unmapping file '%s':\n%s\n", file_name,
strerror(errno));
exit(EXIT_FAILURE);
}
fclose(file);
exit(EXIT_SUCCESS);
}