|
| 1 | +#include <binsparse/binsparse.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <time.h> |
| 4 | + |
| 5 | +double gettime() { |
| 6 | + struct timespec time; |
| 7 | + clock_gettime(CLOCK_MONOTONIC, &time); |
| 8 | + return ((double) time.tv_sec) + ((double) 1e-9) * time.tv_nsec; |
| 9 | +} |
| 10 | + |
| 11 | +int compar(const void* a, const void* b) { |
| 12 | + double x = *((const double*) a); |
| 13 | + double y = *((const double*) b); |
| 14 | + |
| 15 | + double diff = x - y; |
| 16 | + |
| 17 | + if (diff > 0) { |
| 18 | + return 1; |
| 19 | + } else if (diff < 0) { |
| 20 | + return -1; |
| 21 | + } else { |
| 22 | + return 0; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +double compute_variance(double* x, size_t n) { |
| 27 | + double sum = 0; |
| 28 | + |
| 29 | + for (size_t i = 0; i < n; i++) { |
| 30 | + sum += x[i]; |
| 31 | + } |
| 32 | + |
| 33 | + double mean = sum / n; |
| 34 | + |
| 35 | + double sum_of_squares = 0; |
| 36 | + for (size_t i = 0; i < n; i++) { |
| 37 | + sum_of_squares += (x[i] - mean) * (x[i] - mean); |
| 38 | + } |
| 39 | + |
| 40 | + return sum_of_squares / (n - 1); |
| 41 | +} |
| 42 | + |
| 43 | +void flush_cache() { |
| 44 | +#ifdef __APPLE__ |
| 45 | + system("bash -c \"sync && sudo purge\""); |
| 46 | +#elif __linux__ |
| 47 | + system("bash -c \"sync\" && sudo echo 3 > /proc/sys/vm/drop_caches"); |
| 48 | +#else |
| 49 | + static_assert(false); |
| 50 | +#endif |
| 51 | +} |
| 52 | + |
| 53 | +void flush_writes() { |
| 54 | +#ifdef __APPLE__ |
| 55 | + system("bash -c \"sync\""); |
| 56 | +#elif __linux__ |
| 57 | + system("bash -c \"sync\""); |
| 58 | +#else |
| 59 | + static_assert(false); |
| 60 | +#endif |
| 61 | +} |
| 62 | + |
| 63 | +void delete_file(const char* file_name) { |
| 64 | + char command[2048]; |
| 65 | + snprintf(command, 2047, "rm %s", file_name); |
| 66 | + system(command); |
| 67 | +} |
| 68 | + |
| 69 | +int main(int argc, char** argv) { |
| 70 | + if (argc < 2) { |
| 71 | + fprintf(stderr, |
| 72 | + "usage: ./benchmark_read [file_name.h5] [scratch_space] [optional: " |
| 73 | + "compression_level]\n"); |
| 74 | + return 1; |
| 75 | + } |
| 76 | + |
| 77 | + char* file_name = argv[1]; |
| 78 | + char* scratch_space = argv[2]; |
| 79 | + |
| 80 | + int compression_level = 0; |
| 81 | + |
| 82 | + if (argc >= 4) { |
| 83 | + compression_level = atoi(argv[3]); |
| 84 | + } |
| 85 | + |
| 86 | + printf("Opening %s\n", file_name); |
| 87 | + |
| 88 | + const int num_trials = 1; |
| 89 | + |
| 90 | + double durations[num_trials]; |
| 91 | + |
| 92 | + bsp_matrix_t mat = bsp_read_matrix(file_name, NULL); |
| 93 | + size_t nbytes = bsp_matrix_nbytes(mat); |
| 94 | + |
| 95 | + char output_filename[2048]; |
| 96 | + strncpy(output_filename, scratch_space, 2047); |
| 97 | + strncpy(output_filename + strlen(scratch_space), "/benchmark_write_file_n.h5", |
| 98 | + 2047 - strlen(scratch_space)); |
| 99 | + |
| 100 | + // Current output name logic does not do much. |
| 101 | + assert(num_trials <= 10); |
| 102 | + |
| 103 | + // To flush the filesystem cache before each trial, change to `true`. |
| 104 | + bool cold_cache = false; |
| 105 | + |
| 106 | + // To flush each write to the filesystem and include this in the timing, |
| 107 | + // change to `true`. |
| 108 | + bool flush_each_write = true; |
| 109 | + |
| 110 | + for (size_t i = 0; i < num_trials; i++) { |
| 111 | + if (cold_cache) { |
| 112 | + flush_cache(); |
| 113 | + } |
| 114 | + |
| 115 | + output_filename[strlen(scratch_space) + 21] = '0' + i; |
| 116 | + printf("Writing to file %s\n", output_filename); |
| 117 | + |
| 118 | + double begin = gettime(); |
| 119 | + bsp_write_matrix(output_filename, mat, NULL, NULL, compression_level); |
| 120 | + |
| 121 | + if (flush_each_write) { |
| 122 | + flush_writes(); |
| 123 | + } |
| 124 | + |
| 125 | + double end = gettime(); |
| 126 | + durations[i] = end - begin; |
| 127 | + |
| 128 | + delete_file(output_filename); |
| 129 | + } |
| 130 | + |
| 131 | + printf("["); |
| 132 | + for (size_t i = 0; i < num_trials; i++) { |
| 133 | + printf("%lf", durations[i]); |
| 134 | + if (i + 1 < num_trials) { |
| 135 | + printf(", "); |
| 136 | + } |
| 137 | + } |
| 138 | + printf("]\n"); |
| 139 | + |
| 140 | + qsort(durations, num_trials, sizeof(double), compar); |
| 141 | + |
| 142 | + double variance = compute_variance(durations, num_trials); |
| 143 | + |
| 144 | + double median_time = durations[num_trials / 2]; |
| 145 | + |
| 146 | + printf("Wrote file in %lf seconds\n", median_time); |
| 147 | + |
| 148 | + if (num_trials > 1) { |
| 149 | + printf("Variance is %lf seconds, standard devication is %lf seconds\n", |
| 150 | + variance, sqrt(variance)); |
| 151 | + } |
| 152 | + |
| 153 | + double gbytes = ((double) nbytes) / 1024 / 1024 / 1024; |
| 154 | + double gbytes_s = gbytes / median_time; |
| 155 | + |
| 156 | + printf("Achieved %lf GiB/s\n", gbytes_s); |
| 157 | + |
| 158 | + printf("["); |
| 159 | + for (size_t i = 0; i < num_trials; i++) { |
| 160 | + printf("%lf", durations[i]); |
| 161 | + if (i + 1 < num_trials) { |
| 162 | + printf(", "); |
| 163 | + } |
| 164 | + } |
| 165 | + printf("]\n"); |
| 166 | + |
| 167 | + printf("FORPARSER: %s,%lf,%lf\n", file_name, median_time, gbytes_s); |
| 168 | + |
| 169 | + return 0; |
| 170 | +} |
0 commit comments