diff options
| author | 2026-11-10 08:29:58 +0000 | |
|---|---|---|
| committer | 2026-11-10 08:33:56 +0000 | |
| commit | 776276d5c7e67356a880f3e31076d32cdc1c1882 (patch) | |
| tree | cbde131f3d46338adee9c67ac0f988295736bdd4 /src/super-computer.c | |
| download | coursework-master.tar.gz | |
Diffstat (limited to 'src/super-computer.c')
| -rw-r--r-- | src/super-computer.c | 672 |
1 files changed, 672 insertions, 0 deletions
diff --git a/src/super-computer.c b/src/super-computer.c new file mode 100644 index 0000000..78453b6 --- /dev/null +++ b/src/super-computer.c | |||
| @@ -0,0 +1,672 @@ | |||
| 1 | // super-computer.c | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <stdlib.h> | ||
| 4 | #include <stdint.h> | ||
| 5 | #include <stdarg.h> | ||
| 6 | #include <string.h> | ||
| 7 | #include <errno.h> | ||
| 8 | #include <unistd.h> | ||
| 9 | #include <sys/mman.h> | ||
| 10 | #include <sys/stat.h> | ||
| 11 | #include <mpi.h> | ||
| 12 | #include <omp.h> | ||
| 13 | #include <dlfcn.h> | ||
| 14 | #include <fcntl.h> | ||
| 15 | |||
| 16 | #include "utils.h" | ||
| 17 | #include "string-view.h" | ||
| 18 | #include "scanner.h" | ||
| 19 | #include "stb_ds.h" | ||
| 20 | #include "card.h" | ||
| 21 | #include "user.h" | ||
| 22 | #include "transaction.h" | ||
| 23 | #include "transaction-heap.h" | ||
| 24 | |||
| 25 | #define RATIO__CPU_GPU (70/30.) | ||
| 26 | |||
| 27 | unsigned int hash (register const char *str, register size_t len); | ||
| 28 | extern const char *wordlist[]; | ||
| 29 | const char *mpi_strerror (int errorcode) | ||
| 30 | { | ||
| 31 | static char string[MPI_MAX_ERROR_STRING]; | ||
| 32 | MPI_Error_string (errorcode, string, NULL); | ||
| 33 | return string; | ||
| 34 | } | ||
| 35 | |||
| 36 | void | ||
| 37 | die (const char *fmt, ...) | ||
| 38 | { | ||
| 39 | va_list ap; | ||
| 40 | |||
| 41 | va_start (ap, fmt); | ||
| 42 | vfprintf (stderr, fmt, ap); | ||
| 43 | va_end (ap); | ||
| 44 | |||
| 45 | exit (EXIT_FAILURE); | ||
| 46 | } | ||
| 47 | |||
| 48 | #define DIE(FMT, ...) \ | ||
| 49 | die (__FILE__ ":" TO_STRING (__LINE__) ": %s: die: " FMT, \ | ||
| 50 | __func__ __VA_OPT__ (,) __VA_ARGS__) | ||
| 51 | |||
| 52 | int node_count__cpu; | ||
| 53 | int node_count__gpu; | ||
| 54 | int world_size; | ||
| 55 | int world_rank; | ||
| 56 | char processor_name[MPI_MAX_PROCESSOR_NAME]; | ||
| 57 | int name_len; | ||
| 58 | int max_threads; | ||
| 59 | bool has_gpu; | ||
| 60 | |||
| 61 | void | ||
| 62 | mpi_init (int *argc, char ***argv) | ||
| 63 | { | ||
| 64 | int err = MPI_Init (argc, argv); | ||
| 65 | if (err) DIE ("MPI_Init: %s", mpi_strerror (err)); | ||
| 66 | } | ||
| 67 | |||
| 68 | int | ||
| 69 | mpi_world_size (void) | ||
| 70 | { | ||
| 71 | int world_size; | ||
| 72 | int err = MPI_Comm_size (MPI_COMM_WORLD, &world_size); | ||
| 73 | if (err) DIE ("MPI_Comm_size: %s", mpi_strerror (err)); | ||
| 74 | return world_size; | ||
| 75 | } | ||
| 76 | |||
| 77 | int | ||
| 78 | mpi_world_rank (void) | ||
| 79 | { | ||
| 80 | int world_rank; | ||
| 81 | int err = MPI_Comm_rank (MPI_COMM_WORLD, &world_rank); | ||
| 82 | if (err) DIE ("MPI_Comm_rank: %s", mpi_strerror (err)); | ||
| 83 | return world_rank; | ||
| 84 | } | ||
| 85 | |||
| 86 | void | ||
| 87 | mpi_get_processor_name (char processor_name[MPI_MAX_PROCESSOR_NAME], | ||
| 88 | int *name_len) | ||
| 89 | { | ||
| 90 | int err = MPI_Get_processor_name (processor_name, name_len); | ||
| 91 | if (err) DIE ("MPI_Get_processor_name: %s", mpi_strerror (err)); | ||
| 92 | } | ||
| 93 | |||
| 94 | void | ||
| 95 | mpi_send (const void *buf, int count, MPI_Datatype datatype, int dest) | ||
| 96 | { | ||
| 97 | int err = MPI_Send (buf, count, datatype, dest, 0, MPI_COMM_WORLD); | ||
| 98 | if (err) DIE ("MPI_Send: %s", mpi_strerror (err)); | ||
| 99 | } | ||
| 100 | |||
| 101 | void | ||
| 102 | mpi_recv (void *buf, int count, MPI_Datatype datatype, int source) | ||
| 103 | { | ||
| 104 | int err = MPI_Recv (buf, count, datatype, source, 0, | ||
| 105 | MPI_COMM_WORLD, MPI_STATUS_IGNORE); | ||
| 106 | if (err) DIE ("MPI_Recv: %s", mpi_strerror (err)); | ||
| 107 | } | ||
| 108 | |||
| 109 | void | ||
| 110 | mpi_finalize (void) | ||
| 111 | { | ||
| 112 | int err = MPI_Finalize (); | ||
| 113 | if (err) DIE ("MPI_Finalize: %s", mpi_strerror (err)); | ||
| 114 | } | ||
| 115 | |||
| 116 | void | ||
| 117 | send_top (struct transaction_heap *top, int dest) | ||
| 118 | { | ||
| 119 | mpi_send (&top->count, 1, MPI_UINT8_T, dest); | ||
| 120 | mpi_send (top->array, 2*top->count, MPI_UINT64_T, dest); | ||
| 121 | } | ||
| 122 | |||
| 123 | void | ||
| 124 | recv_top (struct transaction_heap *top, int source) | ||
| 125 | { | ||
| 126 | struct transaction_heap other; | ||
| 127 | |||
| 128 | mpi_recv (&other.count, 1, MPI_UINT8_T, source); | ||
| 129 | mpi_recv (other.array, 2*other.count, MPI_UINT64_T, source); | ||
| 130 | |||
| 131 | for (int i = 0; i < other.count; ++i) | ||
| 132 | transaction_heap__push (top, other.array[i]); | ||
| 133 | } | ||
| 134 | |||
| 135 | void | ||
| 136 | process_cards (const char *filename, struct card **cards, struct user **users) | ||
| 137 | { | ||
| 138 | MEASURE_SCOPE ("[%d/%d]", world_rank + 1, world_size); | ||
| 139 | |||
| 140 | ASSERT (filename); | ||
| 141 | ASSERT (cards); | ||
| 142 | ASSERT (users); | ||
| 143 | |||
| 144 | int fd = open (filename, O_RDONLY); | ||
| 145 | ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno)); | ||
| 146 | struct string_view sv = mmap_whole (fd); | ||
| 147 | |||
| 148 | struct scanner sc; | ||
| 149 | scanner__init (&sc, sv); | ||
| 150 | |||
| 151 | // skip header: id, client_id, card_brand, card_type, card_number, | ||
| 152 | // expires, cvv, has_chip, num_cards_issued, credit_limit, | ||
| 153 | // acct_open_date, year_pin_last_changed, card_on_dark_web | ||
| 154 | scanner__skip_over (&sc, '\n'); | ||
| 155 | while (!scanner__reached_end (&sc)) { | ||
| 156 | struct card card; | ||
| 157 | struct user user; | ||
| 158 | |||
| 159 | card.id = scanner__get_uint64 (&sc); | ||
| 160 | |||
| 161 | scanner__skip_n (&sc, 1); | ||
| 162 | |||
| 163 | user.id = scanner__get_uint64 (&sc); | ||
| 164 | |||
| 165 | scanner__skip_over (&sc, '$'); | ||
| 166 | |||
| 167 | uint64_t credit_limit = scanner__get_uint64 (&sc); | ||
| 168 | scanner__skip_over (&sc, '\n'); | ||
| 169 | |||
| 170 | if (credit_limit < 12000) | ||
| 171 | continue; | ||
| 172 | |||
| 173 | hmputs (*cards, card); | ||
| 174 | |||
| 175 | if (hmgetp_null (*users, user.key) == NULL) | ||
| 176 | hmputs (*users, user); | ||
| 177 | } | ||
| 178 | |||
| 179 | munmap ((char *) sv.begin, sv.end - sv.begin); | ||
| 180 | close (fd); | ||
| 181 | } | ||
| 182 | |||
| 183 | void | ||
| 184 | process_users (const char *filename, struct user *users) | ||
| 185 | { | ||
| 186 | MEASURE_SCOPE ("[%d/%d]", world_rank + 1, world_size); | ||
| 187 | |||
| 188 | ASSERT (filename); | ||
| 189 | ASSERT (users); | ||
| 190 | |||
| 191 | int fd = open (filename, O_RDONLY); | ||
| 192 | ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno)); | ||
| 193 | struct string_view sv = mmap_whole (fd); | ||
| 194 | |||
| 195 | struct scanner sc; | ||
| 196 | scanner__init (&sc, sv); | ||
| 197 | |||
| 198 | // skip header: id, current_age, retirement_age, birth_year, | ||
| 199 | // birth_month, gender, address, latitude, longitude, | ||
| 200 | // per_capita_income, yearly_income, total_debt, credit_score, | ||
| 201 | // num_credit_cards | ||
| 202 | scanner__skip_over (&sc, '\n'); | ||
| 203 | |||
| 204 | while (!scanner__reached_end (&sc)) { | ||
| 205 | client_id_t id = scanner__get_uint64 (&sc); | ||
| 206 | struct user *user = hmgetp_null (users, id); | ||
| 207 | if (user == NULL) { | ||
| 208 | scanner__skip_over (&sc, '\n'); | ||
| 209 | continue; | ||
| 210 | } | ||
| 211 | |||
| 212 | scanner__skip_over (&sc, '$'); | ||
| 213 | |||
| 214 | user->normalizer = 0.12/scanner__get_uint64 (&sc); // 12 months, cents->dollars | ||
| 215 | scanner__skip_over (&sc, '\n'); | ||
| 216 | } | ||
| 217 | |||
| 218 | munmap ((char *) sv.begin, sv.end - sv.begin); | ||
| 219 | close (fd); | ||
| 220 | } | ||
| 221 | |||
| 222 | void | ||
| 223 | scanner__skip_current_month (struct scanner *sc) | ||
| 224 | { | ||
| 225 | scanner__skip_over (sc, '\n'); | ||
| 226 | if (sc->cursor == sc->end) return; | ||
| 227 | |||
| 228 | uint16_t current_year_month; | ||
| 229 | scanner__skip_over (sc, ','); | ||
| 230 | current_year_month = scanner__get_uint64 (sc) << 4; scanner__skip_n (sc, 1); | ||
| 231 | current_year_month |= scanner__get_uint64 (sc); | ||
| 232 | |||
| 233 | const char *line; | ||
| 234 | uint16_t year_month; | ||
| 235 | do { | ||
| 236 | scanner__skip_over (sc, '\n'); | ||
| 237 | line = sc->cursor; | ||
| 238 | scanner__skip_over (sc, ','); | ||
| 239 | year_month = scanner__get_uint64 (sc) << 4; scanner__skip_n (sc, 1); | ||
| 240 | year_month |= scanner__get_uint64 (sc); | ||
| 241 | } while (year_month == current_year_month); | ||
| 242 | |||
| 243 | sc->cursor = line; | ||
| 244 | } | ||
| 245 | |||
| 246 | void | ||
| 247 | split_transaction_scanner (struct scanner *sc, int block, int blocks) | ||
| 248 | { | ||
| 249 | struct scanner tmp = *sc; | ||
| 250 | |||
| 251 | size_t bytes_total = tmp.end - tmp.cursor; | ||
| 252 | size_t bytes_per_block = bytes_total/blocks; | ||
| 253 | |||
| 254 | if (block > 0) { | ||
| 255 | sc->cursor += block*bytes_per_block; | ||
| 256 | scanner__skip_current_month (sc); | ||
| 257 | } else { | ||
| 258 | // skip header: id, date, client_id, card_id, amount, use_chip, | ||
| 259 | // merchant_id, merchant_city, merchant_state, zip, mcc, errors | ||
| 260 | scanner__skip_over (sc, '\n'); | ||
| 261 | } | ||
| 262 | |||
| 263 | if (block < blocks - 1) { | ||
| 264 | tmp.cursor += (block + 1)*bytes_per_block; | ||
| 265 | scanner__skip_current_month (&tmp); | ||
| 266 | sc->end = tmp.cursor; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | void | ||
| 271 | process_transactions__cpu (struct scanner *sc, | ||
| 272 | struct transaction_heap *top_transactions, | ||
| 273 | struct card *cards, struct user *users) | ||
| 274 | { | ||
| 275 | MEASURE_SCOPE ("[%d/%d]", world_rank + 1, world_size); | ||
| 276 | |||
| 277 | ASSERT (sc != NULL); | ||
| 278 | ASSERT (top_transactions != NULL); | ||
| 279 | ASSERT (cards != NULL); | ||
| 280 | ASSERT (users != NULL); | ||
| 281 | |||
| 282 | struct transaction *transactions = NULL; | ||
| 283 | |||
| 284 | // Parse, aggregate, reduce | ||
| 285 | while (!scanner__reached_end (sc)) { | ||
| 286 | union transaction_key key; | ||
| 287 | |||
| 288 | scanner__skip_over (sc, ','); | ||
| 289 | |||
| 290 | key.year_month = scanner__get_uint64 (sc) << 4; | ||
| 291 | scanner__skip_n (sc, 1); | ||
| 292 | key.year_month |= scanner__get_uint64 (sc); | ||
| 293 | |||
| 294 | scanner__skip_over (sc, ','); | ||
| 295 | |||
| 296 | key.client_id = scanner__get_uint64 (sc); | ||
| 297 | |||
| 298 | scanner__skip_n (sc, 1); | ||
| 299 | |||
| 300 | card_id_t card_id = scanner__get_uint64 (sc); | ||
| 301 | if (hmgetp_null (cards, card_id) == NULL) { | ||
| 302 | scanner__skip_over (sc, '\n'); | ||
| 303 | continue; | ||
| 304 | } | ||
| 305 | |||
| 306 | scanner__skip_n (sc, 2); | ||
| 307 | |||
| 308 | int64_t amount = 100*scanner__get_int64 (sc); | ||
| 309 | scanner__skip_n (sc, 1); | ||
| 310 | int sign = amount >= 0 ? 1 : -1; | ||
| 311 | amount += sign*scanner__get_uint64 (sc); | ||
| 312 | |||
| 313 | scanner__skip_n (sc, 1); | ||
| 314 | scanner__skip_over (sc, ','); | ||
| 315 | scanner__skip_over (sc, ','); | ||
| 316 | |||
| 317 | struct string_view sv = scanner__get_until (sc, ','); | ||
| 318 | key.merchant_city_id = hash (sv.begin, STRING_VIEW__LENGTH (sv)); | ||
| 319 | |||
| 320 | scanner__skip_over (sc, '\n'); | ||
| 321 | |||
| 322 | struct transaction *transaction = | ||
| 323 | hmgetp_null (transactions, key); | ||
| 324 | if (transaction == NULL) | ||
| 325 | hmput (transactions, key, amount); | ||
| 326 | else transaction->sum += amount; | ||
| 327 | } | ||
| 328 | |||
| 329 | // Normalize | ||
| 330 | ptrdiff_t tempvar; | ||
| 331 | for (size_t i = 0; i < hmlenu (transactions); ++i) { | ||
| 332 | struct transaction *transaction = &transactions[i]; | ||
| 333 | float normalizer = hmgetp_ts (users, transaction->key.client_id, tempvar)->normalizer; | ||
| 334 | transaction->normalized = transaction->sum*normalizer; | ||
| 335 | } | ||
| 336 | |||
| 337 | // Exctract top | ||
| 338 | for (size_t i = 0; i < hmlenu (transactions); ++i) | ||
| 339 | transaction_heap__push (top_transactions, transactions[i]); | ||
| 340 | |||
| 341 | hmfree (transactions); | ||
| 342 | } | ||
| 343 | |||
| 344 | void | ||
| 345 | process_transactions__gpu (struct scanner *sc, | ||
| 346 | struct transaction_heap *top_transactions, | ||
| 347 | struct card *cards, struct user *users) | ||
| 348 | { | ||
| 349 | MEASURE_SCOPE ("[%d/%d]", world_rank + 1, world_size); | ||
| 350 | |||
| 351 | ASSERT (sc != NULL); | ||
| 352 | ASSERT (top_transactions != NULL); | ||
| 353 | ASSERT (cards != NULL); | ||
| 354 | ASSERT (users != NULL); | ||
| 355 | |||
| 356 | uint64_t *keys = NULL; | ||
| 357 | float *vals = NULL; | ||
| 358 | ptrdiff_t tempvar; | ||
| 359 | while (!scanner__reached_end (sc)) { | ||
| 360 | union transaction_key key; | ||
| 361 | |||
| 362 | scanner__skip_over (sc, ','); | ||
| 363 | |||
| 364 | key.year_month = scanner__get_uint64 (sc) << 4; | ||
| 365 | scanner__skip_n (sc, 1); | ||
| 366 | key.year_month |= scanner__get_uint64 (sc); | ||
| 367 | |||
| 368 | scanner__skip_over (sc, ','); | ||
| 369 | |||
| 370 | key.client_id = scanner__get_uint64 (sc); | ||
| 371 | |||
| 372 | scanner__skip_n (sc, 1); | ||
| 373 | |||
| 374 | card_id_t card_id = scanner__get_uint64 (sc); | ||
| 375 | if (hmgetp_null (cards, card_id) == NULL) { | ||
| 376 | scanner__skip_over (sc, '\n'); | ||
| 377 | continue; | ||
| 378 | } | ||
| 379 | |||
| 380 | scanner__skip_n (sc, 2); | ||
| 381 | |||
| 382 | int64_t amount = 100*scanner__get_int64 (sc); | ||
| 383 | scanner__skip_n (sc, 1); | ||
| 384 | int sign = amount >= 0 ? 1 : -1; | ||
| 385 | amount += sign*scanner__get_uint64 (sc); | ||
| 386 | |||
| 387 | scanner__skip_n (sc, 1); | ||
| 388 | scanner__skip_over (sc, ','); | ||
| 389 | scanner__skip_over (sc, ','); | ||
| 390 | |||
| 391 | struct string_view sv = scanner__get_until (sc, ','); | ||
| 392 | key.merchant_city_id = hash (sv.begin, STRING_VIEW__LENGTH (sv)); | ||
| 393 | |||
| 394 | scanner__skip_over (sc, '\n'); | ||
| 395 | |||
| 396 | float normalizer = hmgetp_ts (users, key.client_id, tempvar)->normalizer; | ||
| 397 | arrput (keys, key.packed); | ||
| 398 | arrput (vals, amount*normalizer); | ||
| 399 | } | ||
| 400 | |||
| 401 | void *handle = dlopen ("./cuda.so", RTLD_LAZY); | ||
| 402 | if (handle == NULL) | ||
| 403 | DIE ("dlopen: %s", dlerror ()); | ||
| 404 | |||
| 405 | int (*cuda_process_transactions) (uint64_t *keys, float *vals, size_t) = | ||
| 406 | dlsym (handle, "process_transactions"); | ||
| 407 | if (cuda_process_transactions == NULL) | ||
| 408 | DIE ("dlsym: %s", dlerror ()); | ||
| 409 | |||
| 410 | int count = cuda_process_transactions (keys, vals, arrlenu (keys)); | ||
| 411 | |||
| 412 | if (dlclose (handle)) | ||
| 413 | DIE ("dlclose: %s", dlerror ()); | ||
| 414 | |||
| 415 | for (int i = 0; i < count; ++i) { | ||
| 416 | transaction_heap__push (top_transactions, (struct transaction) { | ||
| 417 | .key.packed = keys[i], | ||
| 418 | .normalized = vals[i], | ||
| 419 | }); | ||
| 420 | } | ||
| 421 | |||
| 422 | arrfree (keys); | ||
| 423 | arrfree (vals); | ||
| 424 | } | ||
| 425 | |||
| 426 | struct sizes { | ||
| 427 | size_t cpu; | ||
| 428 | size_t gpu; | ||
| 429 | size_t tail; | ||
| 430 | }; | ||
| 431 | |||
| 432 | struct partition { | ||
| 433 | size_t offset; | ||
| 434 | size_t size; | ||
| 435 | }; | ||
| 436 | |||
| 437 | struct sizes | ||
| 438 | get_sizes (size_t size) | ||
| 439 | { | ||
| 440 | int thread_count__cpu = ((max_threads - 0)*node_count__cpu + | ||
| 441 | (max_threads - 1)*node_count__gpu); | ||
| 442 | int thread_count__gpu = node_count__gpu; | ||
| 443 | |||
| 444 | double weight__cpu = thread_count__cpu*RATIO__CPU_GPU; | ||
| 445 | double weight__gpu = thread_count__gpu; | ||
| 446 | |||
| 447 | double ratio__cpu = weight__cpu/(weight__cpu + weight__gpu); | ||
| 448 | |||
| 449 | struct sizes sizes; | ||
| 450 | |||
| 451 | size_t size__cpus = size * ratio__cpu; | ||
| 452 | size_t size__gpus = size - size__cpus; | ||
| 453 | sizes.cpu = (thread_count__cpu > 0) ? size__cpus/thread_count__cpu : 0; | ||
| 454 | sizes.gpu = (thread_count__gpu > 0) ? size__gpus/thread_count__gpu : 0; | ||
| 455 | sizes.tail = size - (thread_count__cpu*sizes.cpu + | ||
| 456 | thread_count__gpu*sizes.gpu); | ||
| 457 | |||
| 458 | return sizes; | ||
| 459 | } | ||
| 460 | |||
| 461 | struct partition | ||
| 462 | partition_node (struct sizes sizes, int node_rank) | ||
| 463 | { | ||
| 464 | struct partition partition; | ||
| 465 | |||
| 466 | if (node_rank < node_count__cpu) { // CPU NODE | ||
| 467 | partition.size = max_threads*sizes.cpu; | ||
| 468 | partition.offset = node_rank*partition.size; | ||
| 469 | } else { // GPU NODE | ||
| 470 | partition.size = (max_threads - 1)*sizes.cpu + sizes.gpu; | ||
| 471 | partition.offset = (node_count__cpu*max_threads*sizes.cpu + | ||
| 472 | (node_rank - node_count__cpu)*partition.size); | ||
| 473 | } | ||
| 474 | |||
| 475 | if (node_rank + 1 == node_count__cpu + node_count__gpu) | ||
| 476 | partition.size += sizes.tail; | ||
| 477 | |||
| 478 | return partition; | ||
| 479 | } | ||
| 480 | |||
| 481 | struct partition | ||
| 482 | partition_thread (struct partition partition, struct sizes sizes, | ||
| 483 | int node_rank, int thread) | ||
| 484 | { | ||
| 485 | if (node_rank < node_count__cpu) { | ||
| 486 | partition.size = sizes.cpu; | ||
| 487 | partition.offset += thread*partition.size; | ||
| 488 | } else { | ||
| 489 | if (thread == 0) { | ||
| 490 | partition.size = sizes.gpu; | ||
| 491 | } else { | ||
| 492 | partition.size = sizes.cpu; | ||
| 493 | partition.offset += sizes.gpu + (thread - 1)*partition.size; | ||
| 494 | } | ||
| 495 | } | ||
| 496 | |||
| 497 | if (node_rank + 1 == node_count__cpu + node_count__gpu && thread + 1 == max_threads) | ||
| 498 | partition.size += sizes.tail; | ||
| 499 | |||
| 500 | return partition; | ||
| 501 | } | ||
| 502 | |||
| 503 | void | ||
| 504 | align_scanner (struct scanner *sc, const char *base, | ||
| 505 | struct partition partition, bool last) | ||
| 506 | { | ||
| 507 | sc->cursor = base + partition.offset; | ||
| 508 | |||
| 509 | struct scanner tmp = *sc; | ||
| 510 | |||
| 511 | if (partition.offset > 0) | ||
| 512 | scanner__skip_current_month (sc); | ||
| 513 | |||
| 514 | if (!last) { | ||
| 515 | tmp.cursor += partition.size; | ||
| 516 | scanner__skip_current_month (&tmp); | ||
| 517 | sc->end = tmp.cursor; | ||
| 518 | } | ||
| 519 | } | ||
| 520 | |||
| 521 | void | ||
| 522 | process_transactions (const char *filename, | ||
| 523 | struct transaction_heap *top_transactions, | ||
| 524 | struct card *cards, struct user *users) | ||
| 525 | { | ||
| 526 | MEASURE_SCOPE ("[%d/%d]", world_rank + 1, world_size); | ||
| 527 | |||
| 528 | ASSERT (filename); | ||
| 529 | ASSERT (top_transactions); | ||
| 530 | ASSERT (cards); | ||
| 531 | ASSERT (users); | ||
| 532 | |||
| 533 | int fd = open (filename, O_RDONLY); | ||
| 534 | ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno)); | ||
| 535 | |||
| 536 | struct stat st; | ||
| 537 | int ret = fstat (fd, &st); | ||
| 538 | ASSERT_MSG (ret != -1, "fstat: %s", strerror (errno)); | ||
| 539 | ASSERT (S_ISREG (st.st_mode)); | ||
| 540 | |||
| 541 | struct sizes sizes = get_sizes (st.st_size); | ||
| 542 | |||
| 543 | struct partition node_partition = partition_node (sizes, world_rank); | ||
| 544 | |||
| 545 | off_t page_align = node_partition.offset % 4096; | ||
| 546 | struct string_view sv = | ||
| 547 | mmap_n_from (fd, // mmap till end | ||
| 548 | st.st_size - node_partition.offset + page_align, | ||
| 549 | 0 + node_partition.offset - page_align); | ||
| 550 | |||
| 551 | struct transaction_heap top_transactions_other = {0}; | ||
| 552 | #pragma omp parallel | ||
| 553 | { | ||
| 554 | int tid = omp_get_thread_num (); | ||
| 555 | struct partition thread_partition = | ||
| 556 | partition_thread (node_partition, sizes, world_rank, tid); | ||
| 557 | struct scanner sc; | ||
| 558 | scanner__init (&sc, sv); | ||
| 559 | sc.cursor += page_align; | ||
| 560 | align_scanner (&sc, sc.cursor - node_partition.offset, thread_partition, | ||
| 561 | world_rank == world_size - 1 && tid == max_threads - 1); | ||
| 562 | |||
| 563 | if (world_rank == 0 && tid == 0) | ||
| 564 | scanner__skip_over (&sc, '\n'); | ||
| 565 | |||
| 566 | printf ("[%d/%d] [%d/%d] Data split: 0x%06lx 0x%06lx %7luK %p %p %p\n", | ||
| 567 | world_rank + 1, world_size, tid + 1, max_threads, | ||
| 568 | (sc.cursor - (sv.begin + page_align) + node_partition.offset) >> 10, | ||
| 569 | (sc.end - (sv.begin + page_align) + node_partition.offset) >> 10, | ||
| 570 | (sc.end - sc.cursor) >> 10, | ||
| 571 | sc.cursor, sc.end, sv.end); | ||
| 572 | |||
| 573 | if (tid == 0) { | ||
| 574 | if (has_gpu) process_transactions__gpu (&sc, top_transactions, cards, users); | ||
| 575 | else process_transactions__cpu (&sc, top_transactions, cards, users); | ||
| 576 | } else process_transactions__cpu (&sc, &top_transactions_other, cards, users); | ||
| 577 | } | ||
| 578 | |||
| 579 | munmap ((char *) sv.begin, sv.end - sv.begin); | ||
| 580 | close (fd); | ||
| 581 | |||
| 582 | // Aggregate | ||
| 583 | for (int i = 0; i < top_transactions_other.count; ++i) | ||
| 584 | transaction_heap__push (top_transactions, top_transactions_other.array[i]); | ||
| 585 | } | ||
| 586 | |||
| 587 | int | ||
| 588 | compare_transactions (const void *lhs, const void *rhs) | ||
| 589 | { | ||
| 590 | const struct transaction *lhs_transaction = lhs; | ||
| 591 | const struct transaction *rhs_transaction = rhs; | ||
| 592 | |||
| 593 | float lhs_normalized = lhs_transaction->normalized; | ||
| 594 | float rhs_normalized = rhs_transaction->normalized; | ||
| 595 | |||
| 596 | return (lhs_normalized < rhs_normalized) - (lhs_normalized > rhs_normalized); | ||
| 597 | } | ||
| 598 | |||
| 599 | void | ||
| 600 | sort_top (struct transaction_heap *top) | ||
| 601 | { | ||
| 602 | MEASURE_SCOPE (); | ||
| 603 | ASSERT (top != NULL); | ||
| 604 | |||
| 605 | qsort (top->array, top->count, sizeof (*top->array), compare_transactions); | ||
| 606 | } | ||
| 607 | |||
| 608 | void | ||
| 609 | print_top (const struct transaction_heap *top) | ||
| 610 | { | ||
| 611 | MEASURE_SCOPE (); | ||
| 612 | ASSERT (top != NULL); | ||
| 613 | |||
| 614 | printf (" i client_id normalized_spending year_month city\n"); | ||
| 615 | for (int i = 0; i < top->count; ++i) { | ||
| 616 | printf ("%2d %4hu %6.2f %hu-%02hu %s\n", i, | ||
| 617 | top->array[i].key.client_id, | ||
| 618 | top->array[i].normalized, | ||
| 619 | top->array[i].key.year_month >> 4, | ||
| 620 | top->array[i].key.year_month & ((1 << 4) - 1), | ||
| 621 | wordlist[top->array[i].key.merchant_city_id]); | ||
| 622 | } | ||
| 623 | } | ||
| 624 | |||
| 625 | int | ||
| 626 | main (int argc, char *argv[]) | ||
| 627 | { | ||
| 628 | mpi_init (&argc, &argv); | ||
| 629 | |||
| 630 | if (argc != 3) | ||
| 631 | return 1; | ||
| 632 | |||
| 633 | const char *progname = *argv++; | ||
| 634 | node_count__cpu = atoi (*argv++); | ||
| 635 | node_count__gpu = atoi (*argv++); | ||
| 636 | |||
| 637 | UNUSED (progname); | ||
| 638 | |||
| 639 | world_size = mpi_world_size (); | ||
| 640 | world_rank = mpi_world_rank (); | ||
| 641 | mpi_get_processor_name (processor_name, &name_len); | ||
| 642 | max_threads = omp_get_max_threads (); | ||
| 643 | has_gpu = world_rank >= node_count__cpu; | ||
| 644 | |||
| 645 | MEASURE_SCOPE ("[%d/%d]", world_rank + 1, world_size); | ||
| 646 | |||
| 647 | printf ("Hello from processor %s, rank %d out of %d processors (%d threads, %s GPU)\n", | ||
| 648 | processor_name, world_rank, world_size, | ||
| 649 | max_threads, has_gpu ? "Has" : "Doesn't have"); | ||
| 650 | |||
| 651 | struct card *cards = NULL; | ||
| 652 | struct user *users = NULL; | ||
| 653 | struct transaction_heap top_transactions = {0}; | ||
| 654 | |||
| 655 | // TODO(cmmm): all threads read their part, then the host shares with others | ||
| 656 | process_cards ("res/cards_data.csv", &cards, &users); | ||
| 657 | process_users ("res/users_data.csv", users); | ||
| 658 | |||
| 659 | process_transactions ("res/transactions_data.csv", | ||
| 660 | &top_transactions, cards, users); | ||
| 661 | if (world_rank > 0) { | ||
| 662 | send_top (&top_transactions, 0); | ||
| 663 | } else { | ||
| 664 | for (int i = 1; i < world_size; ++i) | ||
| 665 | recv_top (&top_transactions, i); | ||
| 666 | sort_top (&top_transactions); | ||
| 667 | print_top (&top_transactions); | ||
| 668 | } | ||
| 669 | |||
| 670 | mpi_finalize (); | ||
| 671 | } | ||
| 672 | // super-computer.c ends here | ||
