Skip to content

Commit 92f7c84

Browse files
committed
Implement benchmark_write
1 parent ba2fe4a commit 92f7c84

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ add_example(bsp2mtx)
1212
add_example(check_equivalence)
1313
add_example(bsp-ls)
1414
add_example(benchmark_read)
15+
add_example(benchmark_write)

examples/benchmark_read.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,15 @@ int main(int argc, char** argv) {
6262

6363
double durations[num_trials];
6464

65+
size_t nbytes = 0;
66+
6567
for (size_t i = 0; i < num_trials; i++) {
6668
flush_cache();
6769
double begin = gettime();
6870
bsp_matrix_t mat = bsp_read_matrix(file_name, NULL);
6971
double end = gettime();
7072
durations[i] = end - begin;
73+
nbytes = bsp_matrix_nbytes(mat);
7174
bsp_destroy_matrix_t(mat);
7275
}
7376

@@ -89,6 +92,11 @@ int main(int argc, char** argv) {
8992
printf("Variance is %lf seconds, standard devication is %lf seconds\n",
9093
variance, sqrt(variance));
9194

95+
double gbytes = ((double) nbytes) / 1024 / 1024 / 1024;
96+
double gbytes_s = gbytes / durations[num_trials / 2];
97+
98+
printf("Achieved %lf GiB/s\n", gbytes_s);
99+
92100
printf("[");
93101
for (size_t i = 0; i < num_trials; i++) {
94102
printf("%lf", durations[i]);

examples/benchmark_write.c

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
#else
47+
static_assert(false);
48+
#endif
49+
}
50+
51+
void flush_writes() {
52+
#ifdef __APPLE__
53+
system("bash -c \"sync\"");
54+
#else
55+
static_assert(false);
56+
#endif
57+
}
58+
59+
void delete_file(char* file_name) {
60+
char command[2048];
61+
snprintf(command, 2047, "rm %s", file_name);
62+
system(command);
63+
}
64+
65+
int main(int argc, char** argv) {
66+
if (argc < 2) {
67+
fprintf(stderr, "usage: ./benchmark_read [file_name.h5] [optional: "
68+
"compression_level]\n");
69+
return 1;
70+
}
71+
72+
char* file_name = argv[1];
73+
74+
int compression_level = 0;
75+
76+
if (argc >= 3) {
77+
compression_level = atoi(argv[2]);
78+
}
79+
80+
printf("Opening %s\n", file_name);
81+
82+
const int num_trials = 10;
83+
84+
double durations[num_trials];
85+
86+
bsp_matrix_t mat = bsp_read_matrix(file_name, NULL);
87+
size_t nbytes = bsp_matrix_nbytes(mat);
88+
89+
char output_filename[2048];
90+
strncpy(output_filename, "benchmark_write_file_n.h5", 2047);
91+
92+
for (size_t i = 0; i < num_trials; i++) {
93+
flush_cache();
94+
output_filename[21] = '0' + i;
95+
printf("Writing to file %s\n", output_filename);
96+
97+
double begin = gettime();
98+
bsp_write_matrix(output_filename, mat, NULL, NULL, compression_level);
99+
flush_writes();
100+
double end = gettime();
101+
durations[i] = end - begin;
102+
delete_file(output_filename);
103+
}
104+
105+
printf("[");
106+
for (size_t i = 0; i < num_trials; i++) {
107+
printf("%lf", durations[i]);
108+
if (i + 1 < num_trials) {
109+
printf(", ");
110+
}
111+
}
112+
printf("]\n");
113+
114+
qsort(durations, num_trials, sizeof(double), compar);
115+
116+
double variance = compute_variance(durations, num_trials);
117+
118+
printf("Wrote file in %lf seconds\n", durations[num_trials / 2]);
119+
120+
printf("Variance is %lf seconds, standard devication is %lf seconds\n",
121+
variance, sqrt(variance));
122+
123+
double gbytes = ((double) nbytes) / 1024 / 1024 / 1024;
124+
double gbytes_s = gbytes / durations[num_trials / 2];
125+
126+
printf("Achieved %lf GiB/s\n", gbytes_s);
127+
128+
printf("[");
129+
for (size_t i = 0; i < num_trials; i++) {
130+
printf("%lf", durations[i]);
131+
if (i + 1 < num_trials) {
132+
printf(", ");
133+
}
134+
}
135+
printf("]\n");
136+
137+
return 0;
138+
}

include/binsparse/matrix.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ void bsp_destroy_matrix_t(bsp_matrix_t matrix) {
3939
bsp_destroy_array_t(matrix.pointers_to_1);
4040
}
4141

42+
size_t bsp_matrix_nbytes(bsp_matrix_t mat) {
43+
size_t nbytes = 0;
44+
if (mat.values.size > 0) {
45+
nbytes += mat.values.size * bsp_type_size(mat.values.type);
46+
}
47+
48+
if (mat.indices_0.size > 0) {
49+
nbytes += mat.indices_0.size * bsp_type_size(mat.indices_0.type);
50+
}
51+
52+
if (mat.indices_1.size > 0) {
53+
nbytes += mat.indices_1.size * bsp_type_size(mat.indices_1.type);
54+
}
55+
56+
if (mat.pointers_to_1.size > 0) {
57+
nbytes += mat.pointers_to_1.size * bsp_type_size(mat.pointers_to_1.type);
58+
}
59+
60+
return nbytes;
61+
}
62+
4263
void bsp_print_matrix_info(bsp_matrix_t matrix) {
4364
printf("%lu x %lu matrix with %lu nnz.\n", matrix.nrows, matrix.ncols,
4465
matrix.nnz);

0 commit comments

Comments
 (0)