diff options
| author | 2026-11-10 08:29:58 +0000 | |
|---|---|---|
| committer | 2026-11-10 08:33:56 +0000 | |
| commit | 776276d5c7e67356a880f3e31076d32cdc1c1882 (patch) | |
| tree | cbde131f3d46338adee9c67ac0f988295736bdd4 /src/multi-threaded.c | |
| download | coursework-master.tar.gz | |
Diffstat (limited to 'src/multi-threaded.c')
| -rw-r--r-- | src/multi-threaded.c | 339 |
1 files changed, 339 insertions, 0 deletions
diff --git a/src/multi-threaded.c b/src/multi-threaded.c new file mode 100644 index 0000000..ecdda68 --- /dev/null +++ b/src/multi-threaded.c | |||
| @@ -0,0 +1,339 @@ | |||
| 1 | // multi-threaded.c | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | ||
| 5 | #include <errno.h> | ||
| 6 | #include <unistd.h> | ||
| 7 | #include <fcntl.h> | ||
| 8 | #include <sys/mman.h> | ||
| 9 | #include <omp.h> | ||
| 10 | |||
| 11 | #include "string-view.h" | ||
| 12 | #include "scanner.h" | ||
| 13 | #include "utils.h" | ||
| 14 | #include "card.h" | ||
| 15 | #include "user.h" | ||
| 16 | #include "transaction.h" | ||
| 17 | #include "transaction-heap.h" | ||
| 18 | #include "stb_ds.h" | ||
| 19 | |||
| 20 | unsigned int hash (register const char *str, register size_t len); | ||
| 21 | extern const char *wordlist[]; | ||
| 22 | |||
| 23 | int max_threads; | ||
| 24 | |||
| 25 | void | ||
| 26 | read_filtered_cards_and_users (const char *filename, | ||
| 27 | struct card **cards, struct user **users) | ||
| 28 | { | ||
| 29 | MEASURE_SCOPE (); | ||
| 30 | |||
| 31 | ASSERT (filename); | ||
| 32 | ASSERT (cards); | ||
| 33 | ASSERT (users); | ||
| 34 | |||
| 35 | int fd = open (filename, O_RDONLY); | ||
| 36 | ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno)); | ||
| 37 | struct string_view sv = mmap_whole (fd); | ||
| 38 | |||
| 39 | struct scanner sc; | ||
| 40 | scanner__init (&sc, sv); | ||
| 41 | |||
| 42 | // skip header: id, client_id, card_brand, card_type, card_number, | ||
| 43 | // expires, cvv, has_chip, num_cards_issued, credit_limit, | ||
| 44 | // acct_open_date, year_pin_last_changed, card_on_dark_web | ||
| 45 | scanner__skip_over (&sc, '\n'); | ||
| 46 | while (!scanner__reached_end (&sc)) { | ||
| 47 | struct card card; | ||
| 48 | struct user user; | ||
| 49 | |||
| 50 | card.id = scanner__get_uint64 (&sc); | ||
| 51 | |||
| 52 | scanner__skip_n (&sc, 1); | ||
| 53 | |||
| 54 | user.id = scanner__get_uint64 (&sc); | ||
| 55 | |||
| 56 | scanner__skip_over (&sc, '$'); | ||
| 57 | |||
| 58 | uint64_t credit_limit = scanner__get_uint64 (&sc); | ||
| 59 | scanner__skip_over (&sc, '\n'); | ||
| 60 | |||
| 61 | if (credit_limit < 12000) | ||
| 62 | continue; | ||
| 63 | |||
| 64 | hmputs (*cards, card); | ||
| 65 | |||
| 66 | if (hmgetp_null (*users, user.key) == NULL) | ||
| 67 | hmputs (*users, user); | ||
| 68 | } | ||
| 69 | |||
| 70 | munmap ((char *) sv.begin, sv.end - sv.begin); | ||
| 71 | close (fd); | ||
| 72 | } | ||
| 73 | |||
| 74 | void | ||
| 75 | read_user_per_capita_incomes (const char *filename, struct user *users) | ||
| 76 | { | ||
| 77 | MEASURE_SCOPE (); | ||
| 78 | |||
| 79 | ASSERT (filename); | ||
| 80 | ASSERT (users); | ||
| 81 | |||
| 82 | int fd = open (filename, O_RDONLY); | ||
| 83 | ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno)); | ||
| 84 | struct string_view sv = mmap_whole (fd); | ||
| 85 | |||
| 86 | struct scanner sc; | ||
| 87 | scanner__init (&sc, sv); | ||
| 88 | |||
| 89 | // skip header: id, current_age, retirement_age, birth_year, | ||
| 90 | // birth_month, gender, address, latitude, longitude, | ||
| 91 | // per_capita_income, yearly_income, total_debt, credit_score, | ||
| 92 | // num_credit_cards | ||
| 93 | scanner__skip_over (&sc, '\n'); | ||
| 94 | |||
| 95 | while (!scanner__reached_end (&sc)) { | ||
| 96 | client_id_t id = scanner__get_uint64 (&sc); | ||
| 97 | struct user *user = hmgetp_null (users, id); | ||
| 98 | if (user == NULL) { | ||
| 99 | scanner__skip_over (&sc, '\n'); | ||
| 100 | continue; | ||
| 101 | } | ||
| 102 | |||
| 103 | scanner__skip_over (&sc, '$'); | ||
| 104 | |||
| 105 | user->normalizer = .12/scanner__get_uint64 (&sc); | ||
| 106 | |||
| 107 | scanner__skip_over (&sc, '\n'); | ||
| 108 | } | ||
| 109 | |||
| 110 | munmap ((char *) sv.begin, sv.end - sv.begin); | ||
| 111 | close (fd); | ||
| 112 | } | ||
| 113 | |||
| 114 | void | ||
| 115 | scanner__skip_current_month (struct scanner *sc) | ||
| 116 | { | ||
| 117 | scanner__skip_over (sc, '\n'); | ||
| 118 | |||
| 119 | uint16_t current_year_month; | ||
| 120 | scanner__skip_over (sc, ','); | ||
| 121 | current_year_month = scanner__get_uint64 (sc) << 4; scanner__skip_n (sc, 1); | ||
| 122 | current_year_month |= scanner__get_uint64 (sc); | ||
| 123 | |||
| 124 | const char *line; | ||
| 125 | uint16_t year_month; | ||
| 126 | do { | ||
| 127 | scanner__skip_over (sc, '\n'); | ||
| 128 | line = sc->cursor; | ||
| 129 | scanner__skip_over (sc, ','); | ||
| 130 | year_month = scanner__get_uint64 (sc) << 4; scanner__skip_n (sc, 1); | ||
| 131 | year_month |= scanner__get_uint64 (sc); | ||
| 132 | } while (year_month == current_year_month); | ||
| 133 | |||
| 134 | sc->cursor = line; | ||
| 135 | } | ||
| 136 | |||
| 137 | void | ||
| 138 | split_transaction_scanner (struct scanner *sc, int block, int blocks) | ||
| 139 | { | ||
| 140 | struct scanner tmp = *sc; | ||
| 141 | |||
| 142 | size_t bytes_total = tmp.end - tmp.cursor; | ||
| 143 | size_t bytes_per_block = bytes_total/blocks; | ||
| 144 | |||
| 145 | if (block > 0) { | ||
| 146 | sc->cursor += block*bytes_per_block; | ||
| 147 | scanner__skip_current_month (sc); | ||
| 148 | } else { | ||
| 149 | // skip header: id, date, client_id, card_id, amount, use_chip, | ||
| 150 | // merchant_id, merchant_city, merchant_state, zip, mcc, errors | ||
| 151 | scanner__skip_over (sc, '\n'); | ||
| 152 | } | ||
| 153 | |||
| 154 | if (block < blocks - 1) { | ||
| 155 | tmp.cursor += (block + 1)*bytes_per_block; | ||
| 156 | scanner__skip_current_month (&tmp); | ||
| 157 | sc->end = tmp.cursor; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | void | ||
| 162 | scan_filtered_and_reduced_transactions (struct scanner *sc, | ||
| 163 | struct transaction **transactions, | ||
| 164 | struct card *cards) | ||
| 165 | { | ||
| 166 | while (!scanner__reached_end (sc)) { | ||
| 167 | union transaction_key transaction_key; | ||
| 168 | |||
| 169 | scanner__skip_over (sc, ','); | ||
| 170 | |||
| 171 | transaction_key.year_month = scanner__get_uint64 (sc) << 4; | ||
| 172 | scanner__skip_n (sc, 1); | ||
| 173 | transaction_key.year_month |= scanner__get_uint64 (sc); | ||
| 174 | |||
| 175 | scanner__skip_over (sc, ','); | ||
| 176 | |||
| 177 | transaction_key.client_id = scanner__get_uint64 (sc); | ||
| 178 | |||
| 179 | scanner__skip_n (sc, 1); | ||
| 180 | |||
| 181 | card_id_t card_id = scanner__get_uint64 (sc); | ||
| 182 | if (hmgetp_null (cards, card_id) == NULL) { | ||
| 183 | scanner__skip_over (sc, '\n'); | ||
| 184 | continue; | ||
| 185 | } | ||
| 186 | |||
| 187 | scanner__skip_n (sc, 2); | ||
| 188 | |||
| 189 | int64_t amount = 100*scanner__get_int64 (sc); | ||
| 190 | scanner__skip_n (sc, 1); | ||
| 191 | int sign = amount >= 0 ? 1 : -1; | ||
| 192 | amount += sign*scanner__get_uint64 (sc); | ||
| 193 | |||
| 194 | scanner__skip_n (sc, 1); | ||
| 195 | scanner__skip_over (sc, ','); | ||
| 196 | scanner__skip_over (sc, ','); | ||
| 197 | |||
| 198 | struct string_view sv = scanner__get_until (sc, ','); | ||
| 199 | transaction_key.merchant_city_id = hash (sv.begin, STRING_VIEW__LENGTH (sv)); | ||
| 200 | |||
| 201 | scanner__skip_over (sc, '\n'); | ||
| 202 | |||
| 203 | struct transaction *transaction = | ||
| 204 | hmgetp_null (*transactions, transaction_key); | ||
| 205 | if (transaction == NULL) | ||
| 206 | hmput (*transactions, transaction_key, amount); | ||
| 207 | else transaction->sum += amount; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | void | ||
| 212 | read_filtered_and_reduced_transactions (const char *filename, | ||
| 213 | struct transaction **transactions, | ||
| 214 | struct card *cards) | ||
| 215 | { | ||
| 216 | MEASURE_SCOPE (); | ||
| 217 | |||
| 218 | ASSERT (filename); | ||
| 219 | ASSERT (transactions); | ||
| 220 | ASSERT (cards); | ||
| 221 | |||
| 222 | int fd = open (filename, O_RDONLY); | ||
| 223 | ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno)); | ||
| 224 | struct string_view sv = mmap_whole (fd); | ||
| 225 | |||
| 226 | #pragma omp parallel | ||
| 227 | { | ||
| 228 | int tid = omp_get_thread_num (); | ||
| 229 | struct scanner sc; | ||
| 230 | scanner__init (&sc, sv); | ||
| 231 | split_transaction_scanner (&sc, tid, max_threads); | ||
| 232 | scan_filtered_and_reduced_transactions (&sc, &transactions[tid], cards); | ||
| 233 | } | ||
| 234 | |||
| 235 | munmap ((char *) sv.begin, sv.end - sv.begin); | ||
| 236 | close (fd); | ||
| 237 | } | ||
| 238 | |||
| 239 | void | ||
| 240 | normalize_transaction_sums (struct transaction **transactions, | ||
| 241 | struct user *users) | ||
| 242 | { | ||
| 243 | MEASURE_SCOPE (); | ||
| 244 | |||
| 245 | ASSERT (transactions); | ||
| 246 | ASSERT (users); | ||
| 247 | |||
| 248 | #pragma omp parallel | ||
| 249 | { | ||
| 250 | int tid = omp_get_thread_num (); | ||
| 251 | ptrdiff_t tempvar; | ||
| 252 | for (size_t i = 0; i < hmlenu (transactions[tid]); ++i) { | ||
| 253 | struct transaction *transaction = &transactions[tid][i]; | ||
| 254 | float normalizer = hmgetp_ts (users, transaction->key.client_id, tempvar)->normalizer; | ||
| 255 | transaction->normalized = transaction->sum*normalizer; | ||
| 256 | } | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | int | ||
| 261 | compare_transactions (const void *lhs, const void *rhs) | ||
| 262 | { | ||
| 263 | const struct transaction *lhs_transaction = lhs; | ||
| 264 | const struct transaction *rhs_transaction = rhs; | ||
| 265 | |||
| 266 | float lhs_normalized = lhs_transaction->normalized; | ||
| 267 | float rhs_normalized = rhs_transaction->normalized; | ||
| 268 | |||
| 269 | return (lhs_normalized < rhs_normalized) - (lhs_normalized > rhs_normalized); | ||
| 270 | } | ||
| 271 | |||
| 272 | void | ||
| 273 | extract_top_normalized_transaction_sums (struct transaction **transactions, | ||
| 274 | struct transaction_heap *heap) | ||
| 275 | { | ||
| 276 | MEASURE_SCOPE (); | ||
| 277 | |||
| 278 | ASSERT (transactions); | ||
| 279 | ASSERT (heap); | ||
| 280 | |||
| 281 | for (int i = 0; i < max_threads; ++i) { | ||
| 282 | for (size_t j = 0; j < hmlenu (transactions[i]); ++j) | ||
| 283 | transaction_heap__push (heap, transactions[i][j]); | ||
| 284 | } | ||
| 285 | |||
| 286 | qsort (heap->array, heap->count, sizeof (*heap->array), compare_transactions); | ||
| 287 | } | ||
| 288 | |||
| 289 | void | ||
| 290 | print_top_transactions (struct transaction_heap *heap) | ||
| 291 | { | ||
| 292 | MEASURE_SCOPE (); | ||
| 293 | |||
| 294 | printf (" i client_id normalized_spending year_month city\n"); | ||
| 295 | for (int i = 0; i < heap->count; ++i) { | ||
| 296 | printf ("%2d %4hu %6.2f %hu-%02hu %s\n", i, | ||
| 297 | heap->array[i].key.client_id, | ||
| 298 | heap->array[i].normalized, | ||
| 299 | heap->array[i].key.year_month >> 4, | ||
| 300 | heap->array[i].key.year_month & ((1 << 4) - 1), | ||
| 301 | wordlist[heap->array[i].key.merchant_city_id]); | ||
| 302 | } | ||
| 303 | } | ||
| 304 | |||
| 305 | size_t | ||
| 306 | get_transaction_count (struct transaction **transactions) | ||
| 307 | { | ||
| 308 | size_t count = 0; | ||
| 309 | |||
| 310 | for (int i = 0; i < max_threads; ++i) | ||
| 311 | count += hmlenu (transactions[i]); | ||
| 312 | |||
| 313 | return count; | ||
| 314 | } | ||
| 315 | |||
| 316 | int | ||
| 317 | main (void) | ||
| 318 | { | ||
| 319 | MEASURE_SCOPE (); | ||
| 320 | |||
| 321 | max_threads = omp_get_max_threads (); | ||
| 322 | |||
| 323 | struct card *cards = NULL; | ||
| 324 | struct user *users = NULL; | ||
| 325 | struct transaction **transactions = calloc (max_threads, sizeof (*transactions)); | ||
| 326 | struct transaction_heap top_transactions = {0}; | ||
| 327 | |||
| 328 | read_filtered_cards_and_users ("res/cards_data.csv", &cards, &users); | ||
| 329 | read_filtered_and_reduced_transactions ("res/transactions_data.csv", | ||
| 330 | transactions, cards); | ||
| 331 | read_user_per_capita_incomes ("res/users_data.csv", users); | ||
| 332 | normalize_transaction_sums (transactions, users); | ||
| 333 | extract_top_normalized_transaction_sums (transactions, &top_transactions); | ||
| 334 | printf ("Top %d out of %lu:\n", top_transactions.count, get_transaction_count (transactions)); | ||
| 335 | print_top_transactions (&top_transactions); | ||
| 336 | |||
| 337 | return EXIT_SUCCESS; | ||
| 338 | } | ||
| 339 | // multi-threaded.c ends here | ||
