diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/card.h | 15 | ||||
| -rw-r--r-- | src/cuda.cu | 157 | ||||
| -rw-r--r-- | src/file-scanner.c | 301 | ||||
| -rw-r--r-- | src/file-scanner.h | 42 | ||||
| -rw-r--r-- | src/multi-threaded.c | 339 | ||||
| -rw-r--r-- | src/partition.c | 134 | ||||
| -rw-r--r-- | src/sandbox.c | 55 | ||||
| -rw-r--r-- | src/scanner.c | 137 | ||||
| -rw-r--r-- | src/scanner.h | 29 | ||||
| -rw-r--r-- | src/single-threaded.c | 262 | ||||
| -rw-r--r-- | src/string-view.h | 16 | ||||
| -rw-r--r-- | src/super-computer.c | 672 | ||||
| -rw-r--r-- | src/transaction-heap.c | 60 | ||||
| -rw-r--r-- | src/transaction-heap.h | 18 | ||||
| -rw-r--r-- | src/transaction.h | 26 | ||||
| -rw-r--r-- | src/user.h | 22 | ||||
| -rw-r--r-- | src/utils.c | 91 | ||||
| -rw-r--r-- | src/utils.h | 95 |
18 files changed, 2471 insertions, 0 deletions
diff --git a/src/card.h b/src/card.h new file mode 100644 index 0000000..b92add5 --- /dev/null +++ b/src/card.h | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | // card.h | ||
| 2 | #ifndef INCLUDED_CARD_H | ||
| 3 | #define INCLUDED_CARD_H | ||
| 4 | |||
| 5 | typedef uint16_t card_id_t; | ||
| 6 | |||
| 7 | struct card { | ||
| 8 | union { | ||
| 9 | card_id_t key; | ||
| 10 | card_id_t id; | ||
| 11 | }; | ||
| 12 | }; | ||
| 13 | |||
| 14 | #endif // INCLUDED_CARD_H | ||
| 15 | // card.h ends here | ||
diff --git a/src/cuda.cu b/src/cuda.cu new file mode 100644 index 0000000..fb59c57 --- /dev/null +++ b/src/cuda.cu | |||
| @@ -0,0 +1,157 @@ | |||
| 1 | // cuda.cu -*- C++ -*- | ||
| 2 | #include <cuda_runtime.h> | ||
| 3 | #include <cuda.h> | ||
| 4 | #include <cub/device/device_radix_sort.cuh> | ||
| 5 | #include <cub/device/device_reduce.cuh> | ||
| 6 | #include <stdint.h> | ||
| 7 | #include "utils.h" | ||
| 8 | |||
| 9 | // #define K 100 | ||
| 10 | |||
| 11 | extern "C" int | ||
| 12 | process_transactions (uint64_t *h_keys, float *h_vals, size_t count) | ||
| 13 | { | ||
| 14 | // CUresult cuerr = cuInit(0); | ||
| 15 | // if (cuerr != CUDA_SUCCESS) { | ||
| 16 | // DEBUG ("cuInit: %d\n", cuerr); | ||
| 17 | // return 0; | ||
| 18 | // } | ||
| 19 | |||
| 20 | cudaError_t err; | ||
| 21 | // int device_count = 0; | ||
| 22 | // err = cudaGetDeviceCount (&device_count); | ||
| 23 | // if (err != cudaSuccess) { | ||
| 24 | // DEBUG ("cudaGetDeviceCount: %s", cudaGetErrorString (err)); | ||
| 25 | // return 0; | ||
| 26 | // } | ||
| 27 | // printf ("CUDA Device Count: %d\n", device_count); | ||
| 28 | |||
| 29 | // if (device_count == 0) { | ||
| 30 | // printf("ERROR: No CUDA devices found!\n"); | ||
| 31 | // return 0; | ||
| 32 | // } | ||
| 33 | |||
| 34 | // cudaSetDevice (0); | ||
| 35 | |||
| 36 | uint64_t *d_in_keys = nullptr, *d_out_keys = nullptr; | ||
| 37 | float *d_in_vals = nullptr, *d_out_vals = nullptr; | ||
| 38 | |||
| 39 | cudaMalloc (&d_in_keys, count*sizeof (*d_in_keys)); | ||
| 40 | cudaMalloc (&d_in_vals, count*sizeof (*d_in_vals)); | ||
| 41 | |||
| 42 | cudaMemcpy (d_in_keys, h_keys, count*sizeof (*h_keys), cudaMemcpyHostToDevice); | ||
| 43 | cudaMemcpy (d_in_vals, h_vals, count*sizeof (*h_vals), cudaMemcpyHostToDevice); | ||
| 44 | |||
| 45 | cudaMalloc (&d_out_keys, count*sizeof (*d_out_keys)); | ||
| 46 | cudaMalloc (&d_out_vals, count*sizeof (*d_out_vals)); | ||
| 47 | |||
| 48 | void *d_temp = nullptr; | ||
| 49 | size_t temp_size = 0; | ||
| 50 | err = cub::DeviceRadixSort::SortPairs (d_temp, temp_size, | ||
| 51 | d_in_keys, d_out_keys, | ||
| 52 | d_in_vals, d_out_vals, count); | ||
| 53 | // if (err != cudaSuccess) { | ||
| 54 | // DEBUG ("SortPairs: %s", cudaGetErrorString (err)); | ||
| 55 | // return 0; | ||
| 56 | // } | ||
| 57 | cudaMalloc (&d_temp, temp_size); | ||
| 58 | err = cub::DeviceRadixSort::SortPairs (d_temp, temp_size, | ||
| 59 | d_in_keys, d_out_keys, | ||
| 60 | d_in_vals, d_out_vals, count); | ||
| 61 | // if (err != cudaSuccess) { | ||
| 62 | // DEBUG ("SortPairs: %s", cudaGetErrorString (err)); | ||
| 63 | // return 0; | ||
| 64 | // } | ||
| 65 | cudaFree (d_in_keys); | ||
| 66 | cudaFree (d_in_vals); | ||
| 67 | cudaFree (d_temp); | ||
| 68 | |||
| 69 | uint64_t *d_unique_keys = nullptr; | ||
| 70 | float *d_unique_vals = nullptr; | ||
| 71 | int *d_unique_count = nullptr; | ||
| 72 | cudaMalloc (&d_unique_keys, count*sizeof (*d_unique_keys)); | ||
| 73 | cudaMalloc (&d_unique_vals, count*sizeof (*d_unique_vals)); | ||
| 74 | cudaMalloc (&d_unique_count, sizeof (*d_unique_count)); | ||
| 75 | d_temp = nullptr; | ||
| 76 | temp_size = 0; | ||
| 77 | err = cub::DeviceReduce::ReduceByKey (d_temp, temp_size, | ||
| 78 | d_out_keys, | ||
| 79 | d_unique_keys, | ||
| 80 | d_out_vals, | ||
| 81 | d_unique_vals, | ||
| 82 | d_unique_count, cub::Sum (), count); | ||
| 83 | // if (err != cudaSuccess) { | ||
| 84 | // DEBUG ("SortPairs: %s", cudaGetErrorString (err)); | ||
| 85 | // return 0; | ||
| 86 | // } | ||
| 87 | cudaMalloc (&d_temp, temp_size); | ||
| 88 | err = cub::DeviceReduce::ReduceByKey (d_temp, temp_size, | ||
| 89 | d_out_keys, | ||
| 90 | d_unique_keys, | ||
| 91 | d_out_vals, | ||
| 92 | d_unique_vals, | ||
| 93 | d_unique_count, cub::Sum (), count); | ||
| 94 | // if (err != cudaSuccess) { | ||
| 95 | // DEBUG ("SortPairs: %s", cudaGetErrorString (err)); | ||
| 96 | // return 0; | ||
| 97 | // } | ||
| 98 | cudaFree (d_out_keys); | ||
| 99 | cudaFree (d_out_vals); | ||
| 100 | cudaFree (d_temp); | ||
| 101 | |||
| 102 | int h_unique_count; | ||
| 103 | cudaMemcpy (&h_unique_count, d_unique_count, | ||
| 104 | sizeof (h_unique_count), cudaMemcpyDeviceToHost); | ||
| 105 | cudaFree (d_unique_count); | ||
| 106 | |||
| 107 | cudaMemcpy (h_keys, d_unique_keys, h_unique_count*sizeof (*h_keys), cudaMemcpyDeviceToHost); | ||
| 108 | cudaMemcpy (h_vals, d_unique_vals, h_unique_count*sizeof (*h_vals), cudaMemcpyDeviceToHost); | ||
| 109 | |||
| 110 | cudaFree (d_unique_keys); | ||
| 111 | cudaFree (d_unique_vals); | ||
| 112 | |||
| 113 | return h_unique_count; | ||
| 114 | |||
| 115 | // requires cuda 13... | ||
| 116 | #if 0 | ||
| 117 | if (h_unique_count <= K) { | ||
| 118 | cudaMemcpy (h_keys, d_unique_keys, | ||
| 119 | h_unique_count*sizeof (*h_keys), cudaMemcpyDeviceToHost); | ||
| 120 | cudaFree (d_unique_keys); | ||
| 121 | |||
| 122 | cudaMemcpy (h_vals, d_unique_vals, | ||
| 123 | h_unique_count*sizeof (*h_vals), cudaMemcpyDeviceToHost); | ||
| 124 | cudaFree (d_unique_vals); | ||
| 125 | |||
| 126 | return h_unique_count; | ||
| 127 | } | ||
| 128 | |||
| 129 | uint64_t *d_top_keys = nullptr; | ||
| 130 | float *d_top_vals = nullptr; | ||
| 131 | cudaMalloc (&d_top_keys, K*sizeof (*d_unique_keys)); | ||
| 132 | cudaMalloc (&d_top_vals, K*sizeof (*d_unique_vals)); | ||
| 133 | d_temp = nullptr; | ||
| 134 | temp_size = 0; | ||
| 135 | cub::DeviceTopK::MaxPairs (d_temp, temp_size, | ||
| 136 | d_unique_vals, d_top_vals, | ||
| 137 | d_unique_keys, d_top_keys, | ||
| 138 | h_unique_count, K, env); | ||
| 139 | cudaMalloc (&d_temp, temp_size); | ||
| 140 | cub::DeviceTopK::MaxPairs (d_temp, temp_size, | ||
| 141 | d_unique_vals, d_top_vals, | ||
| 142 | d_unique_keys, d_top_keys, | ||
| 143 | h_unique_count, K); | ||
| 144 | cudaFree (d_unique_keys); | ||
| 145 | cudaFree (d_unique_vals); | ||
| 146 | cudaFree (d_temp); | ||
| 147 | |||
| 148 | cudaMemcpy (h_keys, d_top_keys, K*sizeof (*h_keys), cudaMemcpyDeviceToHost); | ||
| 149 | cudaMemcpy (h_vals, d_top_vals, K*sizeof (*h_vals), cudaMemcpyDeviceToHost); | ||
| 150 | |||
| 151 | cudaFree (d_top_keys); | ||
| 152 | cudaFree (d_top_vals); | ||
| 153 | |||
| 154 | return K; | ||
| 155 | #endif | ||
| 156 | } | ||
| 157 | // cuda.cu ends here | ||
diff --git a/src/file-scanner.c b/src/file-scanner.c new file mode 100644 index 0000000..6ae217a --- /dev/null +++ b/src/file-scanner.c | |||
| @@ -0,0 +1,301 @@ | |||
| 1 | #define _GNU_SOURCE | ||
| 2 | #include <sys/mman.h> | ||
| 3 | #include <unistd.h> | ||
| 4 | |||
| 5 | #include "file-scanner.h" | ||
| 6 | #include "utils.h" | ||
| 7 | |||
| 8 | void | ||
| 9 | file_scanner__init (struct file_scanner *fs, int fd) | ||
| 10 | { | ||
| 11 | union { | ||
| 12 | int i; | ||
| 13 | void *p; | ||
| 14 | } ret; | ||
| 15 | |||
| 16 | ASSERT (fs != NULL); | ||
| 17 | ASSERT (fd >= 0); | ||
| 18 | |||
| 19 | fs->to_mark_as_scanned = 0; | ||
| 20 | |||
| 21 | fs->input_fd = fd; // Open with O_DIRECT? | ||
| 22 | ret.i = posix_fadvise (fs->input_fd, 0, 0, POSIX_FADV_SEQUENTIAL); | ||
| 23 | ASSERT (ret.i == 0); | ||
| 24 | |||
| 25 | struct stat st; | ||
| 26 | ret.i = fstat (fs->input_fd, &st); | ||
| 27 | ASSERT (ret.i == 0); | ||
| 28 | ASSERT (st.st_size > 0); | ||
| 29 | fs->input_size = st.st_size; | ||
| 30 | |||
| 31 | // Create backing memory (can't be virtual in order to double map) | ||
| 32 | fs->memfd = memfd_create ("file-scanner", 0); // TODO(cmmm): gen UID from fs | ||
| 33 | ASSERT (fs->memfd != -1); | ||
| 34 | ret.i = ftruncate (fs->memfd, RING_SIZE*READ_SIZE); | ||
| 35 | ASSERT (ret.i == 0); | ||
| 36 | |||
| 37 | // Reserve contiguous virtual memory region for double mapping memfd | ||
| 38 | fs->buffer = mmap (NULL, 2*RING_SIZE*READ_SIZE, PROT_NONE, | ||
| 39 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | ||
| 40 | ASSERT (fs->buffer != MAP_FAILED); | ||
| 41 | |||
| 42 | // Double map buffer (to cleanly handle wraparound) | ||
| 43 | ret.p = mmap (fs->buffer, RING_SIZE*READ_SIZE, | ||
| 44 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fs->memfd, 0); | ||
| 45 | ASSERT (ret.p != MAP_FAILED); | ||
| 46 | ret.p = mmap (fs->buffer + RING_SIZE*READ_SIZE, RING_SIZE*READ_SIZE, | ||
| 47 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fs->memfd, 0); | ||
| 48 | ASSERT (ret.p != MAP_FAILED); | ||
| 49 | |||
| 50 | ret.i = io_uring_queue_init (IO_URING_SIZE, &fs->io_uring, 0); | ||
| 51 | ASSERT (ret.i == 0); | ||
| 52 | |||
| 53 | struct iovec iov[RING_SIZE]; | ||
| 54 | for (int i = 0; i < RING_SIZE; ++i) { | ||
| 55 | iov[i].iov_base = fs->buffer + i*READ_SIZE; | ||
| 56 | iov[i].iov_len = READ_SIZE; | ||
| 57 | } | ||
| 58 | io_uring_register_buffers (&fs->io_uring, iov, RING_SIZE); | ||
| 59 | io_uring_register_files (&fs->io_uring, &fs->input_fd, 1); | ||
| 60 | |||
| 61 | fs->scanner.cursor = fs->buffer; | ||
| 62 | fs->scanner.end = fs->buffer; // NOTE(cmmm): haven't read anything yet | ||
| 63 | |||
| 64 | // Fill io_uring queue | ||
| 65 | size_t read_size = MIN (4 << 10, // Start with reading a single page | ||
| 66 | fs->input_size - fs->input_submitted); | ||
| 67 | fs->input_scanned = 0; | ||
| 68 | fs->input_submitted = 0; | ||
| 69 | int submitted; | ||
| 70 | for (submitted = 0; submitted < IO_URING_SIZE;) { | ||
| 71 | // Getting a submission queue entry (SQE) | ||
| 72 | struct io_uring_sqe *sqe = io_uring_get_sqe (&fs->io_uring); | ||
| 73 | ASSERT (sqe != NULL); | ||
| 74 | submitted += 1; | ||
| 75 | |||
| 76 | // Prepare a read | ||
| 77 | // printf ("prepared: %lu @ %lu\n", read_size, fs->input_submitted); | ||
| 78 | io_uring_prep_read_fixed (sqe, 0, fs->buffer + fs->input_submitted, | ||
| 79 | read_size, fs->input_submitted, | ||
| 80 | fs->input_submitted/READ_SIZE); | ||
| 81 | sqe->flags |= IOSQE_FIXED_FILE; | ||
| 82 | fs->input_submitted += read_size; | ||
| 83 | io_uring_sqe_set_data64 (sqe, fs->input_submitted); | ||
| 84 | |||
| 85 | if (fs->input_submitted == RING_SIZE*READ_SIZE || | ||
| 86 | fs->input_submitted == fs->input_size) | ||
| 87 | break; | ||
| 88 | if (read_size > fs->input_size - fs->input_submitted) | ||
| 89 | read_size = fs->input_size - fs->input_submitted; // For last read | ||
| 90 | else if (read_size < READ_SIZE && fs->input_submitted != read_size) | ||
| 91 | read_size *= 2; // Ramp-up | ||
| 92 | } | ||
| 93 | |||
| 94 | // Submit SQEs to the kernel | ||
| 95 | ret.i = io_uring_submit (&fs->io_uring); | ||
| 96 | ASSERT_MSG (ret.i == submitted, "io_uring_submit: %s", strerror (-ret.i)); | ||
| 97 | } | ||
| 98 | |||
| 99 | // void | ||
| 100 | // file_scanner__reinit (struct file_scanner *fs, int fd) | ||
| 101 | // { | ||
| 102 | // union { | ||
| 103 | // int i; | ||
| 104 | // void *p; | ||
| 105 | // } ret; | ||
| 106 | |||
| 107 | // ASSERT (fs); | ||
| 108 | // ASSERT (fd >= 0); | ||
| 109 | |||
| 110 | // fs->input_fd = fd; // Open with O_DIRECT? | ||
| 111 | // posix_fadvise (fs->input_fd, 0, 0, POSIX_FADV_SEQUENTIAL); | ||
| 112 | |||
| 113 | // struct stat st; | ||
| 114 | // ret = fstat (fs->input_fd, &st); | ||
| 115 | // ASSERT (ret.i != -1); | ||
| 116 | // fs->input_bytes_to_submit = st.st_size; | ||
| 117 | |||
| 118 | // fs->scanner.begin = fs->buffer; | ||
| 119 | // fs->scanner.end = fs->buffer; // NOTE(cmmm): Haven't read anything yet | ||
| 120 | |||
| 121 | // // Reset io_uring queues | ||
| 122 | // memset (&fs->io_uring.sq, 0, sizeof (fs->io_uring.sq)); | ||
| 123 | // memset (&fs->io_uring.cq, 0, sizeof (fs->io_uring.cq)); | ||
| 124 | // io_uring_cq_advance (&fs->io_uring, fs->io_uring.cq.ring_tail); | ||
| 125 | |||
| 126 | // // Fill io_uring submission queue | ||
| 127 | // size_t read_size = 4 << 10; // Start with reading a single page | ||
| 128 | // size_t bytes_submitted = 0; | ||
| 129 | // for (int enqueued = 0; enqueued < IO_URING_SIZE; ++enqueued) { | ||
| 130 | // // Getting a submission queue entry (SQE) | ||
| 131 | // struct io_uring_sqe *sqe = io_uring_get_sqe (&fs->io_uring); | ||
| 132 | // ASSERT (sqe != NULL); | ||
| 133 | |||
| 134 | // // Prepare a read | ||
| 135 | // io_uring_prep_read ( | ||
| 136 | // sqe, fs->input_fd, fs->buffer + bytes_submitted, read_size, bytes_submitted); | ||
| 137 | // io_uring_sqe_set_data (sqe, bytes_submitted); | ||
| 138 | // bytes_submitted += read_size; | ||
| 139 | // fs->input_bytes_to_submit -= read_size; | ||
| 140 | // if (fs->input_bytes_to_submit == 0) | ||
| 141 | // break; | ||
| 142 | // if (fs->input_bytes_to_submit < read_size) | ||
| 143 | // read_size = fs->input_bytes_to_submit; // For last read | ||
| 144 | // else if (read_size < RING_SIZE && bytes_submitted != read_size) | ||
| 145 | // read_size *= 2; // Ramp-up | ||
| 146 | // } | ||
| 147 | |||
| 148 | // // Submit SQEs to the kernel | ||
| 149 | // ret = io_uring_submit (&fs->io_uring); | ||
| 150 | // ASSERT (ret == 0); | ||
| 151 | // } | ||
| 152 | |||
| 153 | void | ||
| 154 | file_scanner__deinit (struct file_scanner *fs) | ||
| 155 | { | ||
| 156 | ASSERT (fs != NULL); | ||
| 157 | |||
| 158 | io_uring_queue_exit (&fs->io_uring); | ||
| 159 | |||
| 160 | munmap (fs->buffer + RING_SIZE*READ_SIZE, RING_SIZE*READ_SIZE); | ||
| 161 | munmap (fs->buffer, RING_SIZE*READ_SIZE); | ||
| 162 | |||
| 163 | close (fs->memfd); | ||
| 164 | } | ||
| 165 | |||
| 166 | static void | ||
| 167 | mark_scanned (struct file_scanner *fs, size_t offset) | ||
| 168 | { | ||
| 169 | if (offset == 0) return; | ||
| 170 | |||
| 171 | ASSERT (fs != NULL); | ||
| 172 | ASSERT (fs->input_scanned + offset <= fs->input_submitted); | ||
| 173 | |||
| 174 | int buffers_to_read = (fs->input_scanned + offset)/READ_SIZE - | ||
| 175 | (fs->input_scanned )/READ_SIZE; | ||
| 176 | |||
| 177 | fs->input_scanned += offset; | ||
| 178 | |||
| 179 | // printf ("Reading %d buffers (scanned %lu)\n", buffers_to_read, offset); | ||
| 180 | if (!buffers_to_read) return; | ||
| 181 | for (int i = 0; i < buffers_to_read; ++i) { | ||
| 182 | // Getting a submission queue entry (SQE) | ||
| 183 | struct io_uring_sqe *sqe = io_uring_get_sqe (&fs->io_uring); | ||
| 184 | ASSERT (sqe != NULL); | ||
| 185 | |||
| 186 | // Prepare a read | ||
| 187 | size_t read_size = MIN (READ_SIZE, fs->input_size - fs->input_submitted); | ||
| 188 | io_uring_prep_read_fixed (sqe, 0, &fs->buffer[fs->input_submitted % | ||
| 189 | (RING_SIZE*READ_SIZE)], | ||
| 190 | read_size, fs->input_submitted, | ||
| 191 | fs->input_submitted/READ_SIZE % RING_SIZE); | ||
| 192 | sqe->flags |= IOSQE_FIXED_FILE; | ||
| 193 | fs->input_submitted += read_size; | ||
| 194 | io_uring_sqe_set_data64 (sqe, fs->input_submitted); | ||
| 195 | } | ||
| 196 | |||
| 197 | // submit SQEs to the kernel | ||
| 198 | int ret = io_uring_submit (&fs->io_uring); | ||
| 199 | ASSERT_MSG (ret == buffers_to_read, "io_uring_submit: %s", strerror (-ret)); | ||
| 200 | } | ||
| 201 | |||
| 202 | void | ||
| 203 | wrap_scanner (struct file_scanner *fs) | ||
| 204 | { | ||
| 205 | if (fs->scanner.cursor - fs->buffer < RING_SIZE*READ_SIZE) | ||
| 206 | return; | ||
| 207 | |||
| 208 | fs->scanner.end = fs->scanner.cursor; | ||
| 209 | fs->scanner.cursor -= RING_SIZE*READ_SIZE; | ||
| 210 | } | ||
| 211 | |||
| 212 | void | ||
| 213 | file_scanner__skip_until (struct file_scanner *fs, char c) | ||
| 214 | { | ||
| 215 | ASSERT (fs != NULL); | ||
| 216 | |||
| 217 | mark_scanned (fs, fs->to_mark_as_scanned); | ||
| 218 | fs->to_mark_as_scanned = 0; | ||
| 219 | |||
| 220 | do { | ||
| 221 | wrap_scanner (fs); | ||
| 222 | const char *begin = fs->scanner.cursor; | ||
| 223 | scanner__skip_until (&fs->scanner, c); | ||
| 224 | // printf ("%s %p %p %p\n", __func__, fs->buffer, fs->scanner.cursor, fs->scanner.end); | ||
| 225 | mark_scanned (fs, fs->scanner.cursor - begin); | ||
| 226 | } while ( scanner__reached_end (&fs->scanner) && | ||
| 227 | !file_scanner__reached_end (fs)); | ||
| 228 | } | ||
| 229 | |||
| 230 | struct string_view | ||
| 231 | file_scanner__get_until (struct file_scanner *fs, char c) | ||
| 232 | { | ||
| 233 | ASSERT (fs != NULL); | ||
| 234 | |||
| 235 | mark_scanned (fs, fs->to_mark_as_scanned); | ||
| 236 | fs->to_mark_as_scanned = 0; | ||
| 237 | wrap_scanner (fs); | ||
| 238 | |||
| 239 | struct string_view sv = { .begin = fs->scanner.cursor }; | ||
| 240 | do { | ||
| 241 | scanner__skip_until (&fs->scanner, c); | ||
| 242 | // printf ("%s %p %p %p\n", __func__, fs->buffer, fs->scanner.cursor, fs->scanner.end); | ||
| 243 | } while ( scanner__reached_end (&fs->scanner) && | ||
| 244 | !file_scanner__reached_end (fs)); | ||
| 245 | |||
| 246 | sv.end = fs->scanner.cursor; | ||
| 247 | fs->to_mark_as_scanned = sv.end - sv.begin; | ||
| 248 | |||
| 249 | return sv; | ||
| 250 | } | ||
| 251 | |||
| 252 | // TODO(cmmm): Could potentially be used to skip reads, has to be | ||
| 253 | // implemented differently | ||
| 254 | void | ||
| 255 | file_scanner__skip_n (struct file_scanner *fs, size_t n) | ||
| 256 | { | ||
| 257 | ASSERT (fs != NULL); | ||
| 258 | |||
| 259 | mark_scanned (fs, fs->to_mark_as_scanned); | ||
| 260 | fs->to_mark_as_scanned = 0; | ||
| 261 | |||
| 262 | do { | ||
| 263 | wrap_scanner (fs); | ||
| 264 | const char *begin = fs->scanner.cursor; | ||
| 265 | scanner__skip_n (&fs->scanner, n); | ||
| 266 | // printf ("%s %p %p %p\n", __func__, fs->buffer, fs->scanner.cursor, fs->scanner.end); | ||
| 267 | size_t skipped = fs->scanner.cursor - begin; | ||
| 268 | n -= skipped; | ||
| 269 | mark_scanned (fs, skipped); | ||
| 270 | if (n == 0) break; | ||
| 271 | } while ( scanner__reached_end (&fs->scanner) && | ||
| 272 | !file_scanner__reached_end (fs)); | ||
| 273 | } | ||
| 274 | |||
| 275 | bool | ||
| 276 | file_scanner__reached_end (struct file_scanner *fs) | ||
| 277 | { | ||
| 278 | ASSERT (fs != NULL); | ||
| 279 | |||
| 280 | mark_scanned (fs, fs->to_mark_as_scanned); | ||
| 281 | |||
| 282 | if (!scanner__reached_end (&fs->scanner)) return false; | ||
| 283 | if (fs->input_scanned == fs->input_size) return true; | ||
| 284 | |||
| 285 | // Wait for completion | ||
| 286 | struct io_uring_cqe *cqe; | ||
| 287 | int ret = io_uring_wait_cqe (&fs->io_uring, &cqe); | ||
| 288 | ASSERT (ret == 0); | ||
| 289 | ASSERT (cqe->res != -1); | ||
| 290 | ASSERT (cqe->res != 0); | ||
| 291 | |||
| 292 | int res = cqe->res; | ||
| 293 | size_t end = io_uring_cqe_get_data64 (cqe); | ||
| 294 | io_uring_cqe_seen (&fs->io_uring, cqe); | ||
| 295 | |||
| 296 | // printf ("res: %d, end: %lu\n", res, end); | ||
| 297 | fs->scanner.end = fs->buffer + end % (RING_SIZE*READ_SIZE) + (RING_SIZE*READ_SIZE); | ||
| 298 | // printf ("%s %p %p %p\n", __func__, fs->buffer, fs->scanner.cursor, fs->scanner.end); | ||
| 299 | |||
| 300 | return false; | ||
| 301 | } | ||
diff --git a/src/file-scanner.h b/src/file-scanner.h new file mode 100644 index 0000000..494bff2 --- /dev/null +++ b/src/file-scanner.h | |||
| @@ -0,0 +1,42 @@ | |||
| 1 | #ifndef INCLUDED_FILE_SCANNER_H | ||
| 2 | #define INCLUDED_FILE_SCANNER_H | ||
| 3 | |||
| 4 | #include <liburing.h> | ||
| 5 | |||
| 6 | #include "scanner.h" | ||
| 7 | |||
| 8 | #define READ_SIZE (128 << 10) | ||
| 9 | #define RING_SIZE 4 | ||
| 10 | #define IO_URING_SIZE 16 // 4K 4K 8K 16K 32K 64K 128K 128K 128K | ||
| 11 | |||
| 12 | struct file_scanner { | ||
| 13 | struct scanner scanner; | ||
| 14 | size_t to_mark_as_scanned; | ||
| 15 | size_t input_size; | ||
| 16 | size_t input_submitted; | ||
| 17 | size_t input_scanned; | ||
| 18 | int input_fd; | ||
| 19 | int memfd; | ||
| 20 | enum file_scanner__state { | ||
| 21 | FILE_SCANNER__STATE__NO_END, | ||
| 22 | FILE_SCANNER__STATE__READ_END, | ||
| 23 | FILE_SCANNER__STATE__REACHED_END, | ||
| 24 | } state; | ||
| 25 | struct io_uring io_uring; | ||
| 26 | char *buffer; | ||
| 27 | }; | ||
| 28 | |||
| 29 | void file_scanner__init (struct file_scanner *, int fd); | ||
| 30 | void file_scanner__deinit (struct file_scanner *); | ||
| 31 | bool file_scanner__reached_end (struct file_scanner *); | ||
| 32 | void file_scanner__skip_until (struct file_scanner *, char); | ||
| 33 | void file_scanner__skip_over (struct file_scanner *, char); | ||
| 34 | void file_scanner__skip_until_one_of (struct file_scanner *, char *); | ||
| 35 | void file_scanner__skip_n (struct file_scanner *, size_t); | ||
| 36 | struct string_view file_scanner__get_until (struct file_scanner *, char); | ||
| 37 | struct string_view file_scanner__get_until_one_of (struct file_scanner *, char *); | ||
| 38 | struct string_view file_scanner__get_n (struct file_scanner *, size_t); | ||
| 39 | uint64_t file_scanner__get_uint64 (struct file_scanner *); | ||
| 40 | int64_t file_scanner__get_int64 (struct file_scanner *); | ||
| 41 | |||
| 42 | #endif // INCLUDED_FILE_SCANNER_H | ||
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 | ||
diff --git a/src/partition.c b/src/partition.c new file mode 100644 index 0000000..529f1ac --- /dev/null +++ b/src/partition.c | |||
| @@ -0,0 +1,134 @@ | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | |||
| 4 | #define RATIO (66/33.) | ||
| 5 | |||
| 6 | struct sizes { | ||
| 7 | size_t cpu; | ||
| 8 | size_t gpu; | ||
| 9 | size_t tail; | ||
| 10 | }; | ||
| 11 | |||
| 12 | struct partition { | ||
| 13 | size_t offset; | ||
| 14 | size_t size; | ||
| 15 | }; | ||
| 16 | |||
| 17 | struct sizes | ||
| 18 | get_sizes (size_t size, int node_count__cpu, int node_count__gpu, int thread_count) | ||
| 19 | { | ||
| 20 | int thread_count__cpu = thread_count*node_count__cpu + (thread_count - 1)*node_count__gpu; | ||
| 21 | int thread_count__gpu = node_count__gpu; | ||
| 22 | |||
| 23 | double weight__cpu = thread_count__cpu*RATIO; | ||
| 24 | double weight__gpu = thread_count__gpu; | ||
| 25 | |||
| 26 | double ratio__cpu = weight__cpu/(weight__cpu + weight__gpu); | ||
| 27 | |||
| 28 | struct sizes sizes; | ||
| 29 | |||
| 30 | size_t size__cpus = size * ratio__cpu; | ||
| 31 | size_t size__gpus = size - size__cpus; | ||
| 32 | sizes.cpu = (thread_count__cpu > 0) ? size__cpus/thread_count__cpu : 0; | ||
| 33 | sizes.gpu = (thread_count__gpu > 0) ? size__gpus/thread_count__gpu : 0; | ||
| 34 | sizes.tail = size - (thread_count__cpu*sizes.cpu + thread_count__gpu*sizes.gpu); | ||
| 35 | |||
| 36 | return sizes; | ||
| 37 | } | ||
| 38 | |||
| 39 | struct partition | ||
| 40 | partition_node (struct sizes sizes, int node_count__cpu, int node_count__gpu, | ||
| 41 | int thread_count, int node_rank) | ||
| 42 | { | ||
| 43 | struct partition partition; | ||
| 44 | |||
| 45 | if (node_rank < node_count__cpu) { // CPU NODE | ||
| 46 | partition.size = thread_count*sizes.cpu; | ||
| 47 | partition.offset = node_rank*partition.size; | ||
| 48 | } else { // GPU NODE | ||
| 49 | partition.size = (thread_count - 1)*sizes.cpu + sizes.gpu; | ||
| 50 | partition.offset = node_count__cpu*thread_count*sizes.cpu + (node_rank - node_count__cpu)*partition.size; | ||
| 51 | } | ||
| 52 | |||
| 53 | if (node_rank + 1 == node_count__cpu + node_count__gpu) | ||
| 54 | partition.size += sizes.tail; | ||
| 55 | |||
| 56 | return partition; | ||
| 57 | } | ||
| 58 | |||
| 59 | struct partition | ||
| 60 | partition_thread (struct partition partition, struct sizes sizes, bool cpu, bool tail, | ||
| 61 | int thread_count, int thread) | ||
| 62 | { | ||
| 63 | if (cpu) { | ||
| 64 | partition.size = sizes.cpu; | ||
| 65 | partition.offset += thread*partition.size; | ||
| 66 | } else { | ||
| 67 | if (thread == 0) { | ||
| 68 | partition.size = sizes.gpu; | ||
| 69 | } else { | ||
| 70 | partition.size = sizes.cpu; | ||
| 71 | partition.offset += sizes.gpu + (thread - 1)*partition.size; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | if (tail) | ||
| 76 | partition.size += sizes.tail; | ||
| 77 | |||
| 78 | return partition; | ||
| 79 | } | ||
| 80 | |||
| 81 | void | ||
| 82 | test (size_t size, int node_count__cpu, int node_count__gpu, int thread_count) | ||
| 83 | { | ||
| 84 | printf ("%lu %d %d %d\n", size, node_count__cpu, node_count__gpu, thread_count); | ||
| 85 | |||
| 86 | int node_count = node_count__cpu + node_count__gpu; | ||
| 87 | for (int n = 0; n < node_count; ++n) { | ||
| 88 | struct sizes sizes = get_sizes (size, node_count__cpu, node_count__gpu, thread_count); | ||
| 89 | struct partition pn = partition_node (sizes, node_count__cpu, node_count__gpu, thread_count, n); | ||
| 90 | printf ("[%d/%d] %10lu %10lu %10lu\n", n + 1, node_count, | ||
| 91 | pn.offset, pn.offset + pn.size, pn.size); | ||
| 92 | for (int t = 0; t < thread_count; ++t) { | ||
| 93 | struct partition pt = partition_thread (pn, sizes, n < node_count__cpu, | ||
| 94 | n + 1 == node_count__cpu + node_count__gpu && | ||
| 95 | t + 1 == thread_count, thread_count, t); | ||
| 96 | printf ("[%d/%d] [%d/%d] %10lu %10lu %10lu\n", n + 1, node_count, t + 1, thread_count, | ||
| 97 | pt.offset, pt.offset + pt.size, pt.size); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | int | ||
| 103 | main (int argc, char *argv[]) | ||
| 104 | { | ||
| 105 | test (1024, 2, 0, 4); putchar ('\n'); | ||
| 106 | test (1024, 0, 2, 4); putchar ('\n'); | ||
| 107 | |||
| 108 | test (1024, 1, 1, 1); putchar ('\n'); | ||
| 109 | test (1024, 1, 1, 2); putchar ('\n'); | ||
| 110 | test (1024, 1, 1, 8); putchar ('\n'); | ||
| 111 | |||
| 112 | test (1024, 2, 2, 1); putchar ('\n'); | ||
| 113 | test (1024, 2, 2, 2); putchar ('\n'); | ||
| 114 | test (1024, 2, 2, 3); putchar ('\n'); | ||
| 115 | test (1024, 2, 2, 8); putchar ('\n'); | ||
| 116 | |||
| 117 | test (1024, 4, 1, 8); putchar ('\n'); | ||
| 118 | test (1024, 1, 4, 8); putchar ('\n'); | ||
| 119 | |||
| 120 | test (1000, 2, 2, 3); putchar ('\n'); | ||
| 121 | test (1000, 3, 1, 4); putchar ('\n'); | ||
| 122 | test (1000, 1, 3, 4); putchar ('\n'); | ||
| 123 | |||
| 124 | test (7, 2, 2, 4); putchar ('\n'); | ||
| 125 | test (3, 2, 2, 4); putchar ('\n'); | ||
| 126 | test (1, 2, 2, 4); putchar ('\n'); | ||
| 127 | |||
| 128 | test (0, 2, 2, 4); putchar ('\n'); | ||
| 129 | |||
| 130 | test (1ULL<<20, 2, 2, 2); putchar ('\n'); | ||
| 131 | test (1ULL<<32, 2, 2, 4); putchar ('\n'); | ||
| 132 | |||
| 133 | return EXIT_SUCCESS; | ||
| 134 | } | ||
diff --git a/src/sandbox.c b/src/sandbox.c new file mode 100644 index 0000000..957d318 --- /dev/null +++ b/src/sandbox.c | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <fcntl.h> | ||
| 4 | #include <unistd.h> | ||
| 5 | #include <sys/mman.h> | ||
| 6 | |||
| 7 | #include "utils.h" | ||
| 8 | #include "file-scanner.h" | ||
| 9 | |||
| 10 | int | ||
| 11 | main (void) | ||
| 12 | { | ||
| 13 | // const char *filename = "./res/users_data.csv"; | ||
| 14 | const char *filename = "./res/transactions_data.csv"; | ||
| 15 | int fd = open (filename, O_RDONLY); | ||
| 16 | |||
| 17 | if (0) { | ||
| 18 | MEASURE_SCOPE ("mmap"); | ||
| 19 | struct string_view sv = mmap_whole (fd); | ||
| 20 | struct scanner sc; | ||
| 21 | scanner__init (&sc, sv); | ||
| 22 | while (!scanner__reached_end (&sc)) { | ||
| 23 | static size_t i = 0; | ||
| 24 | struct string_view sv = scanner__get_until (&sc, '\n'); | ||
| 25 | sc.cursor += 1; | ||
| 26 | // if (i > 13300000) | ||
| 27 | // printf ("%s:%lu: " STRING_VIEW__FMT "\n", filename, ++i, STRING_VIEW__FMTA (sv)); | ||
| 28 | i += 1; | ||
| 29 | if (i > 13300000) break; | ||
| 30 | } | ||
| 31 | munmap (&sv.begin, STRING_VIEW__LENGTH (sv)); | ||
| 32 | } | ||
| 33 | |||
| 34 | if (1) { | ||
| 35 | MEASURE_SCOPE ("io_uring"); | ||
| 36 | struct file_scanner fs; | ||
| 37 | file_scanner__init (&fs, fd); | ||
| 38 | while (!file_scanner__reached_end (&fs)) { | ||
| 39 | static size_t i = 0; | ||
| 40 | struct string_view sv = file_scanner__get_until (&fs, '\n'); | ||
| 41 | file_scanner__skip_n (&fs, 1); | ||
| 42 | // if (i > 13300000) | ||
| 43 | // printf ("%s:%lu: %lX/%lX/%lX " STRING_VIEW__FMT "\n", filename, i, | ||
| 44 | // fs.input_scanned, fs.input_submitted, fs.input_size, | ||
| 45 | // STRING_VIEW__FMTA (sv)); | ||
| 46 | i += 1; | ||
| 47 | if (i > 13300000) break; | ||
| 48 | } | ||
| 49 | file_scanner__deinit (&fs); | ||
| 50 | } | ||
| 51 | |||
| 52 | close (fd); | ||
| 53 | |||
| 54 | return EXIT_SUCCESS; | ||
| 55 | } | ||
diff --git a/src/scanner.c b/src/scanner.c new file mode 100644 index 0000000..8326eee --- /dev/null +++ b/src/scanner.c | |||
| @@ -0,0 +1,137 @@ | |||
| 1 | // scanner.c | ||
| 2 | #include <string.h> | ||
| 3 | #include <stdlib.h> | ||
| 4 | |||
| 5 | #include "utils.h" | ||
| 6 | #include "scanner.h" | ||
| 7 | |||
| 8 | void | ||
| 9 | scanner__init (struct scanner *self, struct string_view sv) | ||
| 10 | { | ||
| 11 | ASSERT (sv.begin <= sv.end); | ||
| 12 | |||
| 13 | self->cursor = sv.begin; | ||
| 14 | self->end = sv.end; | ||
| 15 | } | ||
| 16 | |||
| 17 | void | ||
| 18 | mmap_wholescanner__skip_until (struct scanner *self, char c) | ||
| 19 | { | ||
| 20 | ASSERT (self->cursor <= self->end); | ||
| 21 | |||
| 22 | char *p = memchr (self->cursor, c, self->end - self->cursor); | ||
| 23 | self->cursor = p ? p : self->end; | ||
| 24 | } | ||
| 25 | |||
| 26 | void | ||
| 27 | scanner__skip_over (struct scanner *self, char c) | ||
| 28 | { | ||
| 29 | scanner__skip_until (self, c); | ||
| 30 | if (!scanner__reached_end (self)) | ||
| 31 | self->cursor += 1; | ||
| 32 | } | ||
| 33 | |||
| 34 | struct string_view | ||
| 35 | scanner__get_until (struct scanner *self, char c) | ||
| 36 | { | ||
| 37 | ASSERT (self->cursor <= self->end); | ||
| 38 | |||
| 39 | struct string_view sv; | ||
| 40 | |||
| 41 | sv.begin = self->cursor; | ||
| 42 | scanner__skip_until (self, c); | ||
| 43 | sv.end = self->cursor; | ||
| 44 | |||
| 45 | return sv; | ||
| 46 | } | ||
| 47 | |||
| 48 | void | ||
| 49 | scanner__skip_until_one_of (struct scanner *self, char *str) | ||
| 50 | { | ||
| 51 | ASSERT (self->cursor <= self->end); | ||
| 52 | |||
| 53 | while (self->cursor != self->end && strchr (str, *self->cursor) == NULL) | ||
| 54 | self->cursor += 1; | ||
| 55 | } | ||
| 56 | |||
| 57 | struct string_view | ||
| 58 | scanner__get_until_one_of (struct scanner *self, char *str) | ||
| 59 | { | ||
| 60 | ASSERT (self->cursor <= self->end); | ||
| 61 | |||
| 62 | struct string_view sv; | ||
| 63 | |||
| 64 | sv.begin = self->cursor; | ||
| 65 | scanner__skip_until_one_of (self, str); | ||
| 66 | sv.end = self->cursor; | ||
| 67 | |||
| 68 | return sv; | ||
| 69 | } | ||
| 70 | |||
| 71 | void | ||
| 72 | scanner__skip_n (struct scanner *self, size_t n) | ||
| 73 | { | ||
| 74 | ASSERT (self->cursor <= self->end); | ||
| 75 | |||
| 76 | self->cursor += n; | ||
| 77 | if (self->cursor > self->end) | ||
| 78 | self->cursor = self->end; | ||
| 79 | } | ||
| 80 | |||
| 81 | struct string_view | ||
| 82 | scanner__get_n (struct scanner *self, size_t n) | ||
| 83 | { | ||
| 84 | ASSERT (self->cursor <= self->end); | ||
| 85 | |||
| 86 | struct string_view sv; | ||
| 87 | |||
| 88 | sv.begin = self->cursor; | ||
| 89 | scanner__skip_n (self, n); | ||
| 90 | sv.end = self->cursor; | ||
| 91 | |||
| 92 | return sv; | ||
| 93 | } | ||
| 94 | |||
| 95 | uint64_t | ||
| 96 | scanner__get_uint64 (struct scanner *self) | ||
| 97 | { | ||
| 98 | ASSERT (self->cursor <= self->end); | ||
| 99 | |||
| 100 | uint64_t val = 0; | ||
| 101 | while ((unsigned) *self->cursor - '0' < 10) { | ||
| 102 | val *= 10; | ||
| 103 | val += *self->cursor - '0'; | ||
| 104 | self->cursor += 1; | ||
| 105 | ASSERT (self->cursor < self->end); | ||
| 106 | } | ||
| 107 | |||
| 108 | ASSERT (self->cursor <= self->end); | ||
| 109 | |||
| 110 | return val; | ||
| 111 | } | ||
| 112 | |||
| 113 | int64_t | ||
| 114 | scanner__get_int64 (struct scanner *self) | ||
| 115 | { | ||
| 116 | ASSERT (self->cursor <= self->end); | ||
| 117 | |||
| 118 | int64_t negative = 1; | ||
| 119 | if (*self->cursor == '-') { | ||
| 120 | negative = -1; | ||
| 121 | self->cursor += 1; | ||
| 122 | } | ||
| 123 | int64_t val = scanner__get_uint64 (self); | ||
| 124 | |||
| 125 | ASSERT (self->cursor <= self->end); | ||
| 126 | |||
| 127 | return negative*val; | ||
| 128 | } | ||
| 129 | |||
| 130 | bool | ||
| 131 | scanner__reached_end (struct scanner *self) | ||
| 132 | { | ||
| 133 | ASSERT (self->cursor <= self->end); | ||
| 134 | |||
| 135 | return self->cursor == self->end; | ||
| 136 | } | ||
| 137 | // scanner.c ends here | ||
diff --git a/src/scanner.h b/src/scanner.h new file mode 100644 index 0000000..22c2c41 --- /dev/null +++ b/src/scanner.h | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | // scanner.h | ||
| 2 | #ifndef INCLUDED_SCANNER_H | ||
| 3 | #define INCLUDED_SCANNER_H | ||
| 4 | |||
| 5 | #include <stdbool.h> | ||
| 6 | #include <stddef.h> | ||
| 7 | #include <stdint.h> | ||
| 8 | |||
| 9 | #include "string-view.h" | ||
| 10 | |||
| 11 | struct scanner { | ||
| 12 | const char *cursor; | ||
| 13 | const char *end; | ||
| 14 | }; | ||
| 15 | |||
| 16 | void scanner__init (struct scanner *, struct string_view); | ||
| 17 | void scanner__skip_until (struct scanner *, char); | ||
| 18 | void scanner__skip_over (struct scanner *, char); | ||
| 19 | void scanner__skip_until_one_of (struct scanner *, char *); | ||
| 20 | void scanner__skip_n (struct scanner *, size_t); | ||
| 21 | struct string_view scanner__get_until (struct scanner *, char); | ||
| 22 | struct string_view scanner__get_until_one_of (struct scanner *, char *); | ||
| 23 | struct string_view scanner__get_n (struct scanner *, size_t); | ||
| 24 | uint64_t scanner__get_uint64 (struct scanner *); | ||
| 25 | int64_t scanner__get_int64 (struct scanner *); | ||
| 26 | bool scanner__reached_end (struct scanner *); | ||
| 27 | |||
| 28 | #endif // INCLUDED_SCANNER_H | ||
| 29 | // scanner.h ends here | ||
diff --git a/src/single-threaded.c b/src/single-threaded.c new file mode 100644 index 0000000..6f2b159 --- /dev/null +++ b/src/single-threaded.c | |||
| @@ -0,0 +1,262 @@ | |||
| 1 | // single-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 | |||
| 10 | #include "string-view.h" | ||
| 11 | #include "scanner.h" | ||
| 12 | #include "utils.h" | ||
| 13 | #include "card.h" | ||
| 14 | #include "user.h" | ||
| 15 | #include "transaction.h" | ||
| 16 | #include "transaction-heap.h" | ||
| 17 | #include "stb_ds.h" | ||
| 18 | |||
| 19 | unsigned int hash (register const char *str, register size_t len); | ||
| 20 | extern const char *wordlist[]; | ||
| 21 | |||
| 22 | void | ||
| 23 | read_filtered_cards_and_users (const char *filename, | ||
| 24 | struct card **cards, struct user **users) | ||
| 25 | { | ||
| 26 | MEASURE_SCOPE (); | ||
| 27 | |||
| 28 | ASSERT (filename); | ||
| 29 | ASSERT (cards); | ||
| 30 | ASSERT (users); | ||
| 31 | |||
| 32 | int fd = open (filename, O_RDONLY); | ||
| 33 | ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno)); | ||
| 34 | struct string_view sv = mmap_whole (fd); | ||
| 35 | |||
| 36 | struct scanner sc; | ||
| 37 | scanner__init (&sc, sv); | ||
| 38 | |||
| 39 | // skip header: id, client_id, card_brand, card_type, card_number, | ||
| 40 | // expires, cvv, has_chip, num_cards_issued, credit_limit, | ||
| 41 | // acct_open_date, year_pin_last_changed, card_on_dark_web | ||
| 42 | scanner__skip_over (&sc, '\n'); | ||
| 43 | while (!scanner__reached_end (&sc)) { | ||
| 44 | struct card card; | ||
| 45 | struct user user; | ||
| 46 | |||
| 47 | card.id = scanner__get_uint64 (&sc); | ||
| 48 | |||
| 49 | scanner__skip_n (&sc, 1); | ||
| 50 | |||
| 51 | user.id = scanner__get_uint64 (&sc); | ||
| 52 | |||
| 53 | scanner__skip_over (&sc, '$'); | ||
| 54 | |||
| 55 | uint64_t credit_limit = scanner__get_uint64 (&sc); | ||
| 56 | scanner__skip_over (&sc, '\n'); | ||
| 57 | |||
| 58 | if (credit_limit < 12000) | ||
| 59 | continue; | ||
| 60 | |||
| 61 | hmputs (*cards, card); | ||
| 62 | |||
| 63 | if (hmgetp_null (*users, user.key) == NULL) | ||
| 64 | hmputs (*users, user); | ||
| 65 | } | ||
| 66 | |||
| 67 | munmap ((char *) sv.begin, sv.end - sv.begin); | ||
| 68 | close (fd); | ||
| 69 | } | ||
| 70 | |||
| 71 | void | ||
| 72 | read_user_per_capita_incomes (const char *filename, struct user *users) | ||
| 73 | { | ||
| 74 | MEASURE_SCOPE (); | ||
| 75 | |||
| 76 | ASSERT (filename); | ||
| 77 | ASSERT (users); | ||
| 78 | |||
| 79 | int fd = open (filename, O_RDONLY); | ||
| 80 | ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno)); | ||
| 81 | struct string_view sv = mmap_whole (fd); | ||
| 82 | |||
| 83 | struct scanner sc; | ||
| 84 | scanner__init (&sc, sv); | ||
| 85 | |||
| 86 | // skip header: id, current_age, retirement_age, birth_year, | ||
| 87 | // birth_month, gender, address, latitude, longitude, | ||
| 88 | // per_capita_income, yearly_income, total_debt, credit_score, | ||
| 89 | // num_credit_cards | ||
| 90 | scanner__skip_over (&sc, '\n'); | ||
| 91 | |||
| 92 | while (!scanner__reached_end (&sc)) { | ||
| 93 | client_id_t id = scanner__get_uint64 (&sc); | ||
| 94 | struct user *user = hmgetp_null (users, id); | ||
| 95 | if (user == NULL) { | ||
| 96 | scanner__skip_over (&sc, '\n'); | ||
| 97 | continue; | ||
| 98 | } | ||
| 99 | |||
| 100 | scanner__skip_over (&sc, '$'); | ||
| 101 | |||
| 102 | user->normalizer = 0.12/scanner__get_uint64 (&sc); | ||
| 103 | |||
| 104 | scanner__skip_over (&sc, '\n'); | ||
| 105 | } | ||
| 106 | |||
| 107 | munmap ((char *) sv.begin, sv.end - sv.begin); | ||
| 108 | close (fd); | ||
| 109 | } | ||
| 110 | |||
| 111 | void | ||
| 112 | read_filtered_and_reduced_transactions (const char *filename, | ||
| 113 | struct transaction **transactions, | ||
| 114 | struct card *cards) | ||
| 115 | { | ||
| 116 | MEASURE_SCOPE (); | ||
| 117 | |||
| 118 | ASSERT (filename); | ||
| 119 | ASSERT (transactions); | ||
| 120 | ASSERT (cards); | ||
| 121 | |||
| 122 | int fd = open (filename, O_RDONLY); | ||
| 123 | ASSERT_MSG (fd != -1, "open %s: %s", filename, strerror (errno)); | ||
| 124 | struct string_view sv = mmap_whole (fd); | ||
| 125 | |||
| 126 | struct scanner sc; | ||
| 127 | scanner__init (&sc, sv); | ||
| 128 | |||
| 129 | // skip header: id, date, client_id, card_id, amount, use_chip, | ||
| 130 | // merchant_id, merchant_city, merchant_state, zip, mcc, errors | ||
| 131 | scanner__skip_over (&sc, '\n'); | ||
| 132 | while (!scanner__reached_end (&sc)) { | ||
| 133 | union transaction_key transaction_key; | ||
| 134 | |||
| 135 | scanner__skip_over (&sc, ','); | ||
| 136 | |||
| 137 | transaction_key.year_month = scanner__get_uint64 (&sc) << 4; | ||
| 138 | scanner__skip_n (&sc, 1); | ||
| 139 | transaction_key.year_month |= scanner__get_uint64 (&sc); | ||
| 140 | |||
| 141 | scanner__skip_over (&sc, ','); | ||
| 142 | |||
| 143 | transaction_key.client_id = scanner__get_uint64 (&sc); | ||
| 144 | |||
| 145 | scanner__skip_n (&sc, 1); | ||
| 146 | |||
| 147 | card_id_t card_id = scanner__get_uint64 (&sc); | ||
| 148 | if (hmgetp_null (cards, card_id) == NULL) { | ||
| 149 | scanner__skip_over (&sc, '\n'); | ||
| 150 | continue; | ||
| 151 | } | ||
| 152 | |||
| 153 | scanner__skip_n (&sc, 2); | ||
| 154 | |||
| 155 | int64_t amount = 100*scanner__get_int64 (&sc); | ||
| 156 | scanner__skip_n (&sc, 1); | ||
| 157 | int sign = amount >= 0 ? 1 : -1; | ||
| 158 | amount += sign*scanner__get_uint64 (&sc); | ||
| 159 | |||
| 160 | scanner__skip_n (&sc, 1); | ||
| 161 | scanner__skip_over (&sc, ','); | ||
| 162 | scanner__skip_over (&sc, ','); | ||
| 163 | |||
| 164 | struct string_view sv = scanner__get_until (&sc, ','); | ||
| 165 | transaction_key.merchant_city_id = hash (sv.begin, STRING_VIEW__LENGTH (sv)); | ||
| 166 | |||
| 167 | scanner__skip_over (&sc, '\n'); | ||
| 168 | |||
| 169 | struct transaction *transaction = hmgetp_null (*transactions, | ||
| 170 | transaction_key); | ||
| 171 | if (transaction == NULL) | ||
| 172 | hmput (*transactions, transaction_key, amount); | ||
| 173 | else transaction->sum += amount; | ||
| 174 | } | ||
| 175 | |||
| 176 | munmap ((char *) sv.begin, sv.end - sv.begin); | ||
| 177 | close (fd); | ||
| 178 | } | ||
| 179 | |||
| 180 | void | ||
| 181 | normalize_transaction_sums (struct transaction *transactions, | ||
| 182 | struct user *users, size_t count) | ||
| 183 | { | ||
| 184 | MEASURE_SCOPE ("%lu transactions", count); | ||
| 185 | |||
| 186 | ASSERT (transactions); | ||
| 187 | ASSERT (users); | ||
| 188 | |||
| 189 | for (size_t i = 0; i < count; ++i) { | ||
| 190 | struct transaction *transaction = &transactions[i]; | ||
| 191 | float normalizer = hmgets (users, transaction->key.client_id).normalizer; | ||
| 192 | transaction->normalized = transaction->sum*normalizer; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | int | ||
| 197 | compare_transactions (const void *lhs, const void *rhs) | ||
| 198 | { | ||
| 199 | const struct transaction *lhs_transaction = lhs; | ||
| 200 | const struct transaction *rhs_transaction = rhs; | ||
| 201 | |||
| 202 | float lhs_normalized = lhs_transaction->normalized; | ||
| 203 | float rhs_normalized = rhs_transaction->normalized; | ||
| 204 | |||
| 205 | return (lhs_normalized < rhs_normalized) - (lhs_normalized > rhs_normalized); | ||
| 206 | } | ||
| 207 | |||
| 208 | void | ||
| 209 | extract_top_normalized_transaction_sums (struct transaction *transactions, | ||
| 210 | size_t count, struct transaction_heap *heap) | ||
| 211 | { | ||
| 212 | MEASURE_SCOPE ("(~10x faster then full sort)"); | ||
| 213 | |||
| 214 | ASSERT (transactions); | ||
| 215 | ASSERT (heap); | ||
| 216 | |||
| 217 | for (size_t i = 0; i < count; ++i) | ||
| 218 | transaction_heap__push (heap, transactions[i]); | ||
| 219 | |||
| 220 | qsort (heap->array, heap->count, sizeof (*heap->array), compare_transactions); | ||
| 221 | } | ||
| 222 | |||
| 223 | void | ||
| 224 | print_top_transactions (struct transaction_heap *heap) | ||
| 225 | { | ||
| 226 | MEASURE_SCOPE (); | ||
| 227 | |||
| 228 | printf (" i client_id normalized_spending year_month city\n"); | ||
| 229 | for (int i = 0; i < heap->count; ++i) { | ||
| 230 | printf ("%2d %4hu %6.2f %hu-%02hu %s\n", i, | ||
| 231 | heap->array[i].key.client_id, | ||
| 232 | heap->array[i].normalized, | ||
| 233 | heap->array[i].key.year_month >> 4, | ||
| 234 | heap->array[i].key.year_month & ((1 << 4) - 1), | ||
| 235 | wordlist[heap->array[i].key.merchant_city_id]); | ||
| 236 | } | ||
| 237 | } | ||
| 238 | |||
| 239 | int | ||
| 240 | main (void) | ||
| 241 | { | ||
| 242 | MEASURE_SCOPE (); | ||
| 243 | |||
| 244 | struct card *cards = NULL; | ||
| 245 | struct user *users = NULL; | ||
| 246 | struct transaction *transactions = NULL; | ||
| 247 | struct transaction_heap top_transactions = {0}; | ||
| 248 | |||
| 249 | read_filtered_cards_and_users ("res/cards_data.csv", &cards, &users); | ||
| 250 | read_filtered_and_reduced_transactions ("res/transactions_data.csv", | ||
| 251 | &transactions, cards); | ||
| 252 | size_t transaction_count = hmlenu (transactions); | ||
| 253 | read_user_per_capita_incomes ("res/users_data.csv", users); | ||
| 254 | normalize_transaction_sums (transactions, users, transaction_count); | ||
| 255 | extract_top_normalized_transaction_sums (transactions, transaction_count, | ||
| 256 | &top_transactions); | ||
| 257 | printf ("Top %d out of %lu:\n", top_transactions.count, transaction_count); | ||
| 258 | print_top_transactions (&top_transactions); | ||
| 259 | |||
| 260 | return EXIT_SUCCESS; | ||
| 261 | } | ||
| 262 | // single-threaded.c ends here | ||
diff --git a/src/string-view.h b/src/string-view.h new file mode 100644 index 0000000..3ebebc0 --- /dev/null +++ b/src/string-view.h | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | // string-view.h | ||
| 2 | #ifndef INCLUDED_STRING_VIEW_H | ||
| 3 | #define INCLUDED_STRING_VIEW_H | ||
| 4 | |||
| 5 | #define STRING_VIEW__LENGTH(SV) ((SV).end - (SV).begin) | ||
| 6 | |||
| 7 | #define STRING_VIEW__FMT "%.*s" | ||
| 8 | #define STRING_VIEW__FMTA(SV) (int) STRING_VIEW__LENGTH (SV), (SV).begin | ||
| 9 | |||
| 10 | struct string_view { | ||
| 11 | const char *begin; | ||
| 12 | const char *end; | ||
| 13 | }; | ||
| 14 | |||
| 15 | #endif // INCLUDED_STRING_VIEW_H | ||
| 16 | // string-view.h ends here | ||
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 | ||
diff --git a/src/transaction-heap.c b/src/transaction-heap.c new file mode 100644 index 0000000..038b634 --- /dev/null +++ b/src/transaction-heap.c | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | // transaction-heap.c | ||
| 2 | #include "transaction-heap.h" | ||
| 3 | #include "utils.h" | ||
| 4 | |||
| 5 | void | ||
| 6 | transaction_heap__pop (struct transaction_heap *heap) | ||
| 7 | { | ||
| 8 | ASSERT (heap != NULL); | ||
| 9 | ASSERT (heap->count > 0); | ||
| 10 | |||
| 11 | heap->array[0] = heap->array[--heap->count]; | ||
| 12 | |||
| 13 | int current = 0; | ||
| 14 | loop: { | ||
| 15 | int left = 2*current + 1; | ||
| 16 | int right = 2*current + 2; | ||
| 17 | int smallest = current; | ||
| 18 | |||
| 19 | if (left < heap->count && | ||
| 20 | heap->array[left].normalized < heap->array[smallest].normalized) { | ||
| 21 | smallest = left; | ||
| 22 | } | ||
| 23 | if (right < heap->count && | ||
| 24 | heap->array[right].normalized < heap->array[smallest].normalized) { | ||
| 25 | smallest = right; | ||
| 26 | } | ||
| 27 | |||
| 28 | if (smallest != current) { | ||
| 29 | struct transaction temp = heap->array[current]; | ||
| 30 | heap->array[current] = heap->array[smallest]; | ||
| 31 | heap->array[smallest] = temp; | ||
| 32 | current = smallest; | ||
| 33 | goto loop; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | void | ||
| 39 | transaction_heap__push (struct transaction_heap *heap, struct transaction value) | ||
| 40 | { | ||
| 41 | ASSERT (heap != NULL); | ||
| 42 | |||
| 43 | if (heap->count > 0 && value.normalized < heap->array[0].normalized) | ||
| 44 | return; | ||
| 45 | |||
| 46 | if (heap->count == ARRAY_LENGTH (heap->array)) | ||
| 47 | transaction_heap__pop (heap); | ||
| 48 | |||
| 49 | heap->array[heap->count++] = value; | ||
| 50 | // bottom up, parent = (current - 1)/2 | ||
| 51 | for (int current = heap->count - 1; | ||
| 52 | current > 0 && | ||
| 53 | heap->array[(current - 1)/2].normalized > heap->array[current].normalized; | ||
| 54 | current = (current - 1)/2) { | ||
| 55 | struct transaction temp = heap->array[(current - 1)/2]; | ||
| 56 | heap->array[(current - 1)/2] = heap->array[current]; | ||
| 57 | heap->array[current] = temp; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | // transaction-heap.c ends here | ||
diff --git a/src/transaction-heap.h b/src/transaction-heap.h new file mode 100644 index 0000000..43eeea3 --- /dev/null +++ b/src/transaction-heap.h | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | // transaction-heap.h | ||
| 2 | #ifndef INCLUDED_TRANSACTION_HEAP_H | ||
| 3 | #define INCLUDED_TRANSACTION_HEAP_H | ||
| 4 | |||
| 5 | #include <stdint.h> | ||
| 6 | |||
| 7 | #include "transaction.h" | ||
| 8 | |||
| 9 | struct transaction_heap { | ||
| 10 | struct transaction array[100]; | ||
| 11 | int count; | ||
| 12 | }; | ||
| 13 | |||
| 14 | void transaction_heap__pop (struct transaction_heap *); | ||
| 15 | void transaction_heap__push (struct transaction_heap *, struct transaction); | ||
| 16 | |||
| 17 | #endif // INCLUDED_TRANSACTION_HEAP_H | ||
| 18 | // transaction-heap.h ends here | ||
diff --git a/src/transaction.h b/src/transaction.h new file mode 100644 index 0000000..709b242 --- /dev/null +++ b/src/transaction.h | |||
| @@ -0,0 +1,26 @@ | |||
| 1 | // transaction.h | ||
| 2 | #ifndef INCLUDED_TRANSACTION_H | ||
| 3 | #define INCLUDED_TRANSACTION_H | ||
| 4 | |||
| 5 | #include <stdint.h> | ||
| 6 | |||
| 7 | typedef uint32_t merchant_city_id_t; | ||
| 8 | |||
| 9 | struct transaction { | ||
| 10 | union transaction_key { | ||
| 11 | uint64_t packed; | ||
| 12 | struct { | ||
| 13 | uint32_t merchant_city_id; | ||
| 14 | uint16_t client_id; | ||
| 15 | uint16_t year_month; // year << 4 + month | ||
| 16 | }; | ||
| 17 | } key; | ||
| 18 | union { | ||
| 19 | int32_t value; | ||
| 20 | int32_t sum; | ||
| 21 | float normalized; | ||
| 22 | }; | ||
| 23 | }; | ||
| 24 | |||
| 25 | #endif // INCLUDED_TRANSACTION_H | ||
| 26 | // transaction.h ends here | ||
diff --git a/src/user.h b/src/user.h new file mode 100644 index 0000000..d52b763 --- /dev/null +++ b/src/user.h | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | // user.h | ||
| 2 | #ifndef INCLUDED_USER_H | ||
| 3 | #define INCLUDED_USER_H | ||
| 4 | |||
| 5 | #include <stdint.h> | ||
| 6 | |||
| 7 | typedef uint16_t client_id_t; | ||
| 8 | |||
| 9 | struct user { | ||
| 10 | union { | ||
| 11 | client_id_t key; | ||
| 12 | client_id_t id; | ||
| 13 | }; | ||
| 14 | union { | ||
| 15 | uint32_t value; | ||
| 16 | uint32_t per_capita_income; | ||
| 17 | float normalizer; | ||
| 18 | }; | ||
| 19 | }; | ||
| 20 | |||
| 21 | #endif // INCLUDED_USER_H | ||
| 22 | // user.h ends here | ||
diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..ea4b936 --- /dev/null +++ b/src/utils.c | |||
| @@ -0,0 +1,91 @@ | |||
| 1 | // utils.c | ||
| 2 | #include <stddef.h> | ||
| 3 | #include <stdarg.h> | ||
| 4 | #include <sys/stat.h> | ||
| 5 | #include <sys/mman.h> | ||
| 6 | #include <string.h> | ||
| 7 | #include <errno.h> | ||
| 8 | #include <fcntl.h> | ||
| 9 | |||
| 10 | #include "utils.h" | ||
| 11 | |||
| 12 | struct string_view | ||
| 13 | mmap_whole (int fd) | ||
| 14 | { | ||
| 15 | struct string_view sv; | ||
| 16 | |||
| 17 | struct stat st; | ||
| 18 | int ret = fstat (fd, &st); | ||
| 19 | UNUSED (ret); | ||
| 20 | ASSERT_MSG (ret != -1, "fstat: %s", strerror (errno)); | ||
| 21 | ASSERT (S_ISREG (st.st_mode)); | ||
| 22 | |||
| 23 | posix_fadvise (fd, 0, st.st_size, POSIX_FADV_SEQUENTIAL | | ||
| 24 | POSIX_FADV_WILLNEED | | ||
| 25 | POSIX_FADV_NOREUSE); | ||
| 26 | |||
| 27 | sv.begin = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE | | ||
| 28 | MAP_POPULATE | | ||
| 29 | MAP_NONBLOCK, fd, 0); | ||
| 30 | ASSERT_MSG (sv.begin != MAP_FAILED, "mmap: %s", strerror (errno)); | ||
| 31 | posix_madvise ((void *) sv.begin, st.st_size, MADV_HUGEPAGE | MADV_WILLNEED); | ||
| 32 | |||
| 33 | sv.end = sv.begin + st.st_size; | ||
| 34 | |||
| 35 | return sv; | ||
| 36 | } | ||
| 37 | |||
| 38 | struct string_view | ||
| 39 | mmap_n_from (int fd, size_t n, off_t offset) | ||
| 40 | { | ||
| 41 | struct string_view sv; | ||
| 42 | |||
| 43 | posix_fadvise (fd, offset, n, POSIX_FADV_SEQUENTIAL | | ||
| 44 | POSIX_FADV_WILLNEED | | ||
| 45 | POSIX_FADV_NOREUSE); | ||
| 46 | |||
| 47 | sv.begin = mmap (NULL, n, PROT_READ, MAP_PRIVATE | | ||
| 48 | MAP_POPULATE | | ||
| 49 | MAP_NONBLOCK, fd, offset); | ||
| 50 | ASSERT_MSG (sv.begin != MAP_FAILED, "mmap: %s", strerror (errno)); | ||
| 51 | posix_madvise ((void *) sv.begin, n, MADV_HUGEPAGE | MADV_WILLNEED); | ||
| 52 | |||
| 53 | sv.end = sv.begin + n; | ||
| 54 | |||
| 55 | return sv; | ||
| 56 | } | ||
| 57 | |||
| 58 | int | ||
| 59 | vasprintf (char **str, const char *fmt, va_list args) | ||
| 60 | { | ||
| 61 | int size = 0; | ||
| 62 | va_list tmpa; | ||
| 63 | |||
| 64 | va_copy (tmpa, args); | ||
| 65 | size = vsnprintf (NULL, 0, fmt, tmpa); | ||
| 66 | va_end (tmpa); | ||
| 67 | |||
| 68 | if (size < 0) | ||
| 69 | return -1; | ||
| 70 | |||
| 71 | *str = (char *) malloc (size + 1); | ||
| 72 | if (NULL == *str) | ||
| 73 | return -1; | ||
| 74 | |||
| 75 | size = vsprintf (*str, fmt, args); | ||
| 76 | return size; | ||
| 77 | } | ||
| 78 | |||
| 79 | int | ||
| 80 | asprintf (char **str, const char *fmt, ...) | ||
| 81 | { | ||
| 82 | int size = 0; | ||
| 83 | va_list args; | ||
| 84 | |||
| 85 | va_start (args, fmt); | ||
| 86 | size = vasprintf (str, fmt, args); | ||
| 87 | va_end (args); | ||
| 88 | |||
| 89 | return size; | ||
| 90 | } | ||
| 91 | // utils.c ends here | ||
diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..d8e7dda --- /dev/null +++ b/src/utils.h | |||
| @@ -0,0 +1,95 @@ | |||
| 1 | // utils.h | ||
| 2 | #ifndef INCLUDED_UTILS_H | ||
| 3 | #define INCLUDED_UTILS_H | ||
| 4 | |||
| 5 | #include <stdio.h> | ||
| 6 | #include <stdlib.h> | ||
| 7 | #include <stdint.h> | ||
| 8 | #include <string.h> | ||
| 9 | #include <time.h> | ||
| 10 | |||
| 11 | #include "string-view.h" | ||
| 12 | |||
| 13 | #define NOP() do {} while (0) | ||
| 14 | |||
| 15 | #define ARRAY_LENGTH(ARR) (sizeof (ARR)/sizeof (*(ARR))) | ||
| 16 | |||
| 17 | #ifndef NDEBUG | ||
| 18 | #include <assert.h> | ||
| 19 | #define ASSERT assert | ||
| 20 | #else | ||
| 21 | #define ASSERT(...) NOP () | ||
| 22 | #endif | ||
| 23 | |||
| 24 | #define UNUSED(EXP) ((void) EXP) | ||
| 25 | |||
| 26 | #define FALLTHROUGH __attribute__ ((__fallthrough__)) | ||
| 27 | |||
| 28 | #define STRINGIFY(ARG) #ARG | ||
| 29 | #define TO_STRING(ARG) STRINGIFY (ARG) | ||
| 30 | |||
| 31 | #ifndef NDEBUG | ||
| 32 | #define DEBUG(FMT, ...) \ | ||
| 33 | fprintf (stderr, __FILE__ ":" TO_STRING (__LINE__) ": %s: " FMT "\n", \ | ||
| 34 | __func__ __VA_OPT__ (,) __VA_ARGS__) | ||
| 35 | #else | ||
| 36 | #define DEBUG(...) NOP () | ||
| 37 | #endif | ||
| 38 | |||
| 39 | #ifndef NDEBUG | ||
| 40 | #define ASSERT_MSG(EXP, FMT, ...) do if (!(EXP)) { \ | ||
| 41 | DEBUG (FMT __VA_OPT__ (,) __VA_ARGS__); \ | ||
| 42 | exit (EXIT_FAILURE); \ | ||
| 43 | } while (0) | ||
| 44 | #else | ||
| 45 | #define ASSERT_MSG(EXP, FMT, ...) NOP () | ||
| 46 | #endif | ||
| 47 | |||
| 48 | #define MEASURE(FMT, ...) \ | ||
| 49 | for (struct timespec __ts0, __ts1, *__once = NULL; \ | ||
| 50 | clock_gettime (CLOCK_MONOTONIC, &__ts0), !__once; \ | ||
| 51 | clock_gettime (CLOCK_MONOTONIC, &__ts1), \ | ||
| 52 | fprintf (stderr, __FILE__ ":" TO_STRING (__LINE__) \ | ||
| 53 | ": %s: " FMT " %.6fs\n", __func__, \ | ||
| 54 | __VA_ARGS__ __VA_OPT__ (,) \ | ||
| 55 | (__ts1.tv_sec - __ts0.tv_sec ) + \ | ||
| 56 | (__ts1.tv_nsec - __ts0.tv_nsec) / 1e9), \ | ||
| 57 | __once += 1) | ||
| 58 | |||
| 59 | #define MIN(A, B) ((A) <= (B) ? (A) : (B)) | ||
| 60 | |||
| 61 | struct timer_scope { | ||
| 62 | char *fmt; | ||
| 63 | struct timespec start; | ||
| 64 | }; | ||
| 65 | |||
| 66 | static void | ||
| 67 | timer_end (struct timer_scope *t) | ||
| 68 | { | ||
| 69 | struct timespec end; | ||
| 70 | clock_gettime (CLOCK_MONOTONIC, &end); | ||
| 71 | |||
| 72 | double s = ((end.tv_sec - t->start.tv_sec) + | ||
| 73 | (end.tv_nsec - t->start.tv_nsec)/1e9); | ||
| 74 | |||
| 75 | fprintf (stderr, t->fmt, s); | ||
| 76 | if (t->fmt[strlen (t->fmt) - 1] != ' ') | ||
| 77 | putc (' ', stderr); | ||
| 78 | fprintf (stderr, "took %.6fs\n", s); | ||
| 79 | free (t->fmt); | ||
| 80 | } | ||
| 81 | |||
| 82 | #define MEASURE_SCOPE(FMT, ...) \ | ||
| 83 | __attribute__ ((cleanup (timer_end))) struct timer_scope __ts; \ | ||
| 84 | asprintf (&__ts.fmt, \ | ||
| 85 | __FILE__ ":" TO_STRING (__LINE__) ": %s: " FMT, \ | ||
| 86 | __func__ __VA_OPT__ (,) __VA_ARGS__); \ | ||
| 87 | clock_gettime (CLOCK_MONOTONIC, &__ts.start) | ||
| 88 | |||
| 89 | struct string_view mmap_whole (int fd); | ||
| 90 | struct string_view mmap_n_from (int fd, size_t n, off_t offset); | ||
| 91 | |||
| 92 | int asprintf (char **str, const char *fmt, ...); | ||
| 93 | |||
| 94 | #endif // INCLUDED_UTILS_H | ||
| 95 | // utils.h ends here | ||
