// multi-threaded.c #include #include #include #include #include #include #include #include #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