Skip to content

Commit

Permalink
Implement benchmark_write
Browse files Browse the repository at this point in the history
  • Loading branch information
BenBrock committed May 21, 2024
1 parent ba2fe4a commit 92f7c84
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ add_example(bsp2mtx)
add_example(check_equivalence)
add_example(bsp-ls)
add_example(benchmark_read)
add_example(benchmark_write)
8 changes: 8 additions & 0 deletions examples/benchmark_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ int main(int argc, char** argv) {

double durations[num_trials];

size_t nbytes = 0;

for (size_t i = 0; i < num_trials; i++) {
flush_cache();
double begin = gettime();
bsp_matrix_t mat = bsp_read_matrix(file_name, NULL);
double end = gettime();
durations[i] = end - begin;
nbytes = bsp_matrix_nbytes(mat);
bsp_destroy_matrix_t(mat);
}

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

double gbytes = ((double) nbytes) / 1024 / 1024 / 1024;
double gbytes_s = gbytes / durations[num_trials / 2];

printf("Achieved %lf GiB/s\n", gbytes_s);

printf("[");
for (size_t i = 0; i < num_trials; i++) {
printf("%lf", durations[i]);
Expand Down
138 changes: 138 additions & 0 deletions examples/benchmark_write.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#include <binsparse/binsparse.h>
#include <stdlib.h>
#include <time.h>

double gettime() {
struct timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
return ((double) time.tv_sec) + ((double) 1e-9) * time.tv_nsec;
}

int compar(const void* a, const void* b) {
double x = *((const double*) a);
double y = *((const double*) b);

double diff = x - y;

if (diff > 0) {
return 1;
} else if (diff < 0) {
return -1;
} else {
return 0;
}
}

double compute_variance(double* x, size_t n) {
double sum = 0;

for (size_t i = 0; i < n; i++) {
sum += x[i];
}

double mean = sum / n;

double sum_of_squares = 0;
for (size_t i = 0; i < n; i++) {
sum_of_squares += (x[i] - mean) * (x[i] - mean);
}

return sum_of_squares / (n - 1);
}

void flush_cache() {
#ifdef __APPLE__
system("bash -c \"sync && sudo purge\"");
#else
static_assert(false);
#endif
}

void flush_writes() {
#ifdef __APPLE__
system("bash -c \"sync\"");
#else
static_assert(false);
#endif
}

void delete_file(char* file_name) {
char command[2048];
snprintf(command, 2047, "rm %s", file_name);
system(command);
}

int main(int argc, char** argv) {
if (argc < 2) {
fprintf(stderr, "usage: ./benchmark_read [file_name.h5] [optional: "
"compression_level]\n");
return 1;
}

char* file_name = argv[1];

int compression_level = 0;

if (argc >= 3) {
compression_level = atoi(argv[2]);
}

printf("Opening %s\n", file_name);

const int num_trials = 10;

double durations[num_trials];

bsp_matrix_t mat = bsp_read_matrix(file_name, NULL);
size_t nbytes = bsp_matrix_nbytes(mat);

char output_filename[2048];
strncpy(output_filename, "benchmark_write_file_n.h5", 2047);

for (size_t i = 0; i < num_trials; i++) {
flush_cache();
output_filename[21] = '0' + i;
printf("Writing to file %s\n", output_filename);

double begin = gettime();
bsp_write_matrix(output_filename, mat, NULL, NULL, compression_level);
flush_writes();
double end = gettime();
durations[i] = end - begin;
delete_file(output_filename);
}

printf("[");
for (size_t i = 0; i < num_trials; i++) {
printf("%lf", durations[i]);
if (i + 1 < num_trials) {
printf(", ");
}
}
printf("]\n");

qsort(durations, num_trials, sizeof(double), compar);

double variance = compute_variance(durations, num_trials);

printf("Wrote file in %lf seconds\n", durations[num_trials / 2]);

printf("Variance is %lf seconds, standard devication is %lf seconds\n",
variance, sqrt(variance));

double gbytes = ((double) nbytes) / 1024 / 1024 / 1024;
double gbytes_s = gbytes / durations[num_trials / 2];

printf("Achieved %lf GiB/s\n", gbytes_s);

printf("[");
for (size_t i = 0; i < num_trials; i++) {
printf("%lf", durations[i]);
if (i + 1 < num_trials) {
printf(", ");
}
}
printf("]\n");

return 0;
}
21 changes: 21 additions & 0 deletions include/binsparse/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ void bsp_destroy_matrix_t(bsp_matrix_t matrix) {
bsp_destroy_array_t(matrix.pointers_to_1);
}

size_t bsp_matrix_nbytes(bsp_matrix_t mat) {
size_t nbytes = 0;
if (mat.values.size > 0) {
nbytes += mat.values.size * bsp_type_size(mat.values.type);
}

if (mat.indices_0.size > 0) {
nbytes += mat.indices_0.size * bsp_type_size(mat.indices_0.type);
}

if (mat.indices_1.size > 0) {
nbytes += mat.indices_1.size * bsp_type_size(mat.indices_1.type);
}

if (mat.pointers_to_1.size > 0) {
nbytes += mat.pointers_to_1.size * bsp_type_size(mat.pointers_to_1.type);
}

return nbytes;
}

void bsp_print_matrix_info(bsp_matrix_t matrix) {
printf("%lu x %lu matrix with %lu nnz.\n", matrix.nrows, matrix.ncols,
matrix.nnz);
Expand Down

0 comments on commit 92f7c84

Please sign in to comment.