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