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
|
// multi-threaded.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <omp.h>
#include "string-view.h"
#include "scanner.h"
#include "utils.h"
#include "card.h"
#include "user.h"
#include "transaction.h"
#include "transaction-heap.h"
#include "stb_ds.h"
unsigned int hash (register const char *str, register size_t len);
extern const char *wordlist[];
int max_threads;
void
read_filtered_cards_and_users (const char *filename,
struct card **cards, struct user **users)
{
MEASURE_SCOPE ();
ASSERT (filename);
ASSERT (cards);
ASSERT (users);
int fd = open (filename, O_RDONLY);
ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno));
struct string_view sv = mmap_whole (fd);
struct scanner sc;
scanner__init (&sc, sv);
// skip header: id, client_id, card_brand, card_type, card_number,
// expires, cvv, has_chip, num_cards_issued, credit_limit,
// acct_open_date, year_pin_last_changed, card_on_dark_web
scanner__skip_over (&sc, '\n');
while (!scanner__reached_end (&sc)) {
struct card card;
struct user user;
card.id = scanner__get_uint64 (&sc);
scanner__skip_n (&sc, 1);
user.id = scanner__get_uint64 (&sc);
scanner__skip_over (&sc, '$');
uint64_t credit_limit = scanner__get_uint64 (&sc);
scanner__skip_over (&sc, '\n');
if (credit_limit < 12000)
continue;
hmputs (*cards, card);
if (hmgetp_null (*users, user.key) == NULL)
hmputs (*users, user);
}
munmap ((char *) sv.begin, sv.end - sv.begin);
close (fd);
}
void
read_user_per_capita_incomes (const char *filename, struct user *users)
{
MEASURE_SCOPE ();
ASSERT (filename);
ASSERT (users);
int fd = open (filename, O_RDONLY);
ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno));
struct string_view sv = mmap_whole (fd);
struct scanner sc;
scanner__init (&sc, sv);
// skip header: id, current_age, retirement_age, birth_year,
// birth_month, gender, address, latitude, longitude,
// per_capita_income, yearly_income, total_debt, credit_score,
// num_credit_cards
scanner__skip_over (&sc, '\n');
while (!scanner__reached_end (&sc)) {
client_id_t id = scanner__get_uint64 (&sc);
struct user *user = hmgetp_null (users, id);
if (user == NULL) {
scanner__skip_over (&sc, '\n');
continue;
}
scanner__skip_over (&sc, '$');
user->normalizer = .12/scanner__get_uint64 (&sc);
scanner__skip_over (&sc, '\n');
}
munmap ((char *) sv.begin, sv.end - sv.begin);
close (fd);
}
void
scanner__skip_current_month (struct scanner *sc)
{
scanner__skip_over (sc, '\n');
uint16_t current_year_month;
scanner__skip_over (sc, ',');
current_year_month = scanner__get_uint64 (sc) << 4; scanner__skip_n (sc, 1);
current_year_month |= scanner__get_uint64 (sc);
const char *line;
uint16_t year_month;
do {
scanner__skip_over (sc, '\n');
line = sc->cursor;
scanner__skip_over (sc, ',');
year_month = scanner__get_uint64 (sc) << 4; scanner__skip_n (sc, 1);
year_month |= scanner__get_uint64 (sc);
} while (year_month == current_year_month);
sc->cursor = line;
}
void
split_transaction_scanner (struct scanner *sc, int block, int blocks)
{
struct scanner tmp = *sc;
size_t bytes_total = tmp.end - tmp.cursor;
size_t bytes_per_block = bytes_total/blocks;
if (block > 0) {
sc->cursor += block*bytes_per_block;
scanner__skip_current_month (sc);
} else {
// skip header: id, date, client_id, card_id, amount, use_chip,
// merchant_id, merchant_city, merchant_state, zip, mcc, errors
scanner__skip_over (sc, '\n');
}
if (block < blocks - 1) {
tmp.cursor += (block + 1)*bytes_per_block;
scanner__skip_current_month (&tmp);
sc->end = tmp.cursor;
}
}
void
scan_filtered_and_reduced_transactions (struct scanner *sc,
struct transaction **transactions,
struct card *cards)
{
while (!scanner__reached_end (sc)) {
union transaction_key transaction_key;
scanner__skip_over (sc, ',');
transaction_key.year_month = scanner__get_uint64 (sc) << 4;
scanner__skip_n (sc, 1);
transaction_key.year_month |= scanner__get_uint64 (sc);
scanner__skip_over (sc, ',');
transaction_key.client_id = scanner__get_uint64 (sc);
scanner__skip_n (sc, 1);
card_id_t card_id = scanner__get_uint64 (sc);
if (hmgetp_null (cards, card_id) == NULL) {
scanner__skip_over (sc, '\n');
continue;
}
scanner__skip_n (sc, 2);
int64_t amount = 100*scanner__get_int64 (sc);
scanner__skip_n (sc, 1);
int sign = amount >= 0 ? 1 : -1;
amount += sign*scanner__get_uint64 (sc);
scanner__skip_n (sc, 1);
scanner__skip_over (sc, ',');
scanner__skip_over (sc, ',');
struct string_view sv = scanner__get_until (sc, ',');
transaction_key.merchant_city_id = hash (sv.begin, STRING_VIEW__LENGTH (sv));
scanner__skip_over (sc, '\n');
struct transaction *transaction =
hmgetp_null (*transactions, transaction_key);
if (transaction == NULL)
hmput (*transactions, transaction_key, amount);
else transaction->sum += amount;
}
}
void
read_filtered_and_reduced_transactions (const char *filename,
struct transaction **transactions,
struct card *cards)
{
MEASURE_SCOPE ();
ASSERT (filename);
ASSERT (transactions);
ASSERT (cards);
int fd = open (filename, O_RDONLY);
ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno));
struct string_view sv = mmap_whole (fd);
#pragma omp parallel
{
int tid = omp_get_thread_num ();
struct scanner sc;
scanner__init (&sc, sv);
split_transaction_scanner (&sc, tid, max_threads);
scan_filtered_and_reduced_transactions (&sc, &transactions[tid], cards);
}
munmap ((char *) sv.begin, sv.end - sv.begin);
close (fd);
}
void
normalize_transaction_sums (struct transaction **transactions,
struct user *users)
{
MEASURE_SCOPE ();
ASSERT (transactions);
ASSERT (users);
#pragma omp parallel
{
int tid = omp_get_thread_num ();
ptrdiff_t tempvar;
for (size_t i = 0; i < hmlenu (transactions[tid]); ++i) {
struct transaction *transaction = &transactions[tid][i];
float normalizer = hmgetp_ts (users, transaction->key.client_id, tempvar)->normalizer;
transaction->normalized = transaction->sum*normalizer;
}
}
}
int
compare_transactions (const void *lhs, const void *rhs)
{
const struct transaction *lhs_transaction = lhs;
const struct transaction *rhs_transaction = rhs;
float lhs_normalized = lhs_transaction->normalized;
float rhs_normalized = rhs_transaction->normalized;
return (lhs_normalized < rhs_normalized) - (lhs_normalized > rhs_normalized);
}
void
extract_top_normalized_transaction_sums (struct transaction **transactions,
struct transaction_heap *heap)
{
MEASURE_SCOPE ();
ASSERT (transactions);
ASSERT (heap);
for (int i = 0; i < max_threads; ++i) {
for (size_t j = 0; j < hmlenu (transactions[i]); ++j)
transaction_heap__push (heap, transactions[i][j]);
}
qsort (heap->array, heap->count, sizeof (*heap->array), compare_transactions);
}
void
print_top_transactions (struct transaction_heap *heap)
{
MEASURE_SCOPE ();
printf (" i client_id normalized_spending year_month city\n");
for (int i = 0; i < heap->count; ++i) {
printf ("%2d %4hu %6.2f %hu-%02hu %s\n", i,
heap->array[i].key.client_id,
heap->array[i].normalized,
heap->array[i].key.year_month >> 4,
heap->array[i].key.year_month & ((1 << 4) - 1),
wordlist[heap->array[i].key.merchant_city_id]);
}
}
size_t
get_transaction_count (struct transaction **transactions)
{
size_t count = 0;
for (int i = 0; i < max_threads; ++i)
count += hmlenu (transactions[i]);
return count;
}
int
main (void)
{
MEASURE_SCOPE ();
max_threads = omp_get_max_threads ();
struct card *cards = NULL;
struct user *users = NULL;
struct transaction **transactions = calloc (max_threads, sizeof (*transactions));
struct transaction_heap top_transactions = {0};
read_filtered_cards_and_users ("res/cards_data.csv", &cards, &users);
read_filtered_and_reduced_transactions ("res/transactions_data.csv",
transactions, cards);
read_user_per_capita_incomes ("res/users_data.csv", users);
normalize_transaction_sums (transactions, users);
extract_top_normalized_transaction_sums (transactions, &top_transactions);
printf ("Top %d out of %lu:\n", top_transactions.count, get_transaction_count (transactions));
print_top_transactions (&top_transactions);
return EXIT_SUCCESS;
}
// multi-threaded.c ends here
|