Skip to content

Commit ad00a00

Browse files
committed
Remove code duplication; reference C examples from cpp directory while
maintaining C++ compiler in CI.
1 parent b439876 commit ad00a00

10 files changed

+10
-879
lines changed

examples/cpp/benchmark_read.cpp

Lines changed: 1 addition & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -2,126 +2,4 @@
22
//
33
// SPDX-License-Identifier: BSD-3-Clause
44

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-
int main(int argc, char** argv) {
58-
if (argc < 2) {
59-
fprintf(stderr, "usage: ./benchmark_read [file_name.h5]\n");
60-
return 1;
61-
}
62-
63-
char* file_name = argv[1];
64-
65-
printf("Opening %s\n", file_name);
66-
67-
const int num_trials = 1;
68-
69-
double durations[num_trials];
70-
71-
size_t nbytes = 0;
72-
73-
// To flush the filesystem cache before each trial, change to `true`.
74-
bool cold_cache = false;
75-
76-
for (size_t i = 0; i < num_trials; i++) {
77-
if (cold_cache) {
78-
flush_cache();
79-
}
80-
double begin = gettime();
81-
bsp_matrix_t mat = bsp_read_matrix(file_name, NULL);
82-
double end = gettime();
83-
durations[i] = end - begin;
84-
nbytes = bsp_matrix_nbytes(mat);
85-
bsp_destroy_matrix_t(mat);
86-
}
87-
88-
printf("[");
89-
for (size_t i = 0; i < num_trials; i++) {
90-
printf("%lf", durations[i]);
91-
if (i + 1 < num_trials) {
92-
printf(", ");
93-
}
94-
}
95-
printf("]\n");
96-
97-
qsort(durations, num_trials, sizeof(double), compar);
98-
99-
double variance = compute_variance(durations, num_trials);
100-
101-
double median_time = durations[num_trials / 2];
102-
103-
printf("Read file in %lf seconds\n", median_time);
104-
105-
if (num_trials > 1) {
106-
printf("Variance is %lf seconds, standard devication is %lf seconds\n",
107-
variance, sqrt(variance));
108-
}
109-
110-
double gbytes = ((double) nbytes) / 1024 / 1024 / 1024;
111-
double gbytes_s = gbytes / median_time;
112-
113-
printf("Achieved %lf GiB/s\n", gbytes_s);
114-
115-
printf("[");
116-
for (size_t i = 0; i < num_trials; i++) {
117-
printf("%lf", durations[i]);
118-
if (i + 1 < num_trials) {
119-
printf(", ");
120-
}
121-
}
122-
printf("]\n");
123-
124-
printf("FORPARSER: %s,%lf,%lf\n", file_name, median_time, gbytes_s);
125-
126-
return 0;
127-
}
5+
#include "../benchmark_read.c"

examples/cpp/benchmark_write.cpp

Lines changed: 1 addition & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -2,173 +2,4 @@
22
//
33
// SPDX-License-Identifier: BSD-3-Clause
44

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

Comments
 (0)