1#define _GNU_SOURCE
2#include <roaring/roaring.h>
3#include "benchmark.h"
4#include "numbersfromtextfiles.h"
5
6#define STARTBEST(numberoftests) \
7 { \
8 uint64_t min_diff = -1 ; \
9 uint64_t boguscyclesstart = 0; \
10 uint64_t boguscyclesend = 0; \
11 for(int bogustest = 0; bogustest < numberoftests; bogustest++ ) { \
12 uint64_t cycles_diff = 0;\
13 RDTSC_START(boguscyclesstart);
14
15#define ENDBEST(outputvar) \
16 RDTSC_FINAL(boguscyclesend); \
17 cycles_diff = (boguscyclesend - boguscyclesstart); \
18 if (cycles_diff < min_diff) min_diff = cycles_diff; \
19 } \
20 outputvar = min_diff;\
21 }
22/**
23 * Once you have collected all the integers, build the bitmaps.
24 */
25static roaring_bitmap_t **create_all_bitmaps(size_t *howmany,
26 uint32_t **numbers, size_t count,
27 bool runoptimize, bool copy_on_write) {
28 if (numbers == NULL) return NULL;
29 printf("Constructing %d bitmaps.\n", (int)count);
30 roaring_bitmap_t **answer = malloc(sizeof(roaring_bitmap_t *) * count);
31 for (size_t i = 0; i < count; i++) {
32 printf(".");
33 fflush(stdout);
34 answer[i] = roaring_bitmap_of_ptr(howmany[i], numbers[i]);
35 if(runoptimize) roaring_bitmap_run_optimize(answer[i]);
36 roaring_bitmap_shrink_to_fit(answer[i]);
37 roaring_bitmap_set_copy_on_write(answer[i], copy_on_write);
38 }
39 printf("\n");
40 return answer;
41}
42
43static void printusage(char *command) {
44 printf(
45 " Try %s directory \n where directory could be "
46 "benchmarks/realdata/weather_sept_85\n",
47 command);
48 ;
49}
50
51#define KNRM "\x1B[0m"
52#define KRED "\x1B[31m"
53#define KGRN "\x1B[32m"
54#define KYEL "\x1B[33m"
55#define KBLU "\x1B[34m"
56#define KMAG "\x1B[35m"
57#define KCYN "\x1B[36m"
58#define KWHT "\x1B[37m"
59
60int main(int argc, char **argv) {
61 int c;
62 const char *extension = ".txt";
63 bool copy_on_write = false;
64 bool runoptimize = true;
65 while ((c = getopt(argc, argv, "e:h")) != -1) switch (c) {
66 case 'e':
67 extension = optarg;
68 break;
69 case 'h':
70 printusage(argv[0]);
71 return 0;
72 default:
73 abort();
74 }
75 if (optind >= argc) {
76 printusage(argv[0]);
77 return -1;
78 }
79 char *dirname = argv[optind];
80 size_t count;
81
82 size_t *howmany = NULL;
83 uint32_t **numbers =
84 read_all_integer_files(dirname, extension, &howmany, &count);
85 if (numbers == NULL) {
86 printf(
87 "I could not find or load any data file with extension %s in "
88 "directory %s.\n",
89 extension, dirname);
90 return -1;
91 }
92
93 uint64_t cycles_start = 0, cycles_final = 0;
94
95 RDTSC_START(cycles_start);
96 roaring_bitmap_t **bitmaps =
97 create_all_bitmaps(howmany, numbers, count, runoptimize, copy_on_write);
98 RDTSC_FINAL(cycles_final);
99 if (bitmaps == NULL) return -1;
100 printf("Loaded %d bitmaps from directory %s \n", (int)count, dirname);
101
102 printf("Creating %zu bitmaps took %" PRIu64 " cycles\n", count,
103 cycles_final - cycles_start);
104 if(count == 0) return -1;
105 uint32_t maxvalue = roaring_bitmap_maximum(bitmaps[0]);
106 for (int i = 1; i < (int)count; i ++) {
107 uint32_t thismax = roaring_bitmap_maximum(bitmaps[0]);
108 if(thismax > maxvalue) maxvalue = thismax;
109 }
110 const int quartile_test_repetitions = 1000;
111
112
113 uint64_t quartcount;
114 uint64_t cycles;
115 STARTBEST(quartile_test_repetitions)
116 quartcount = 0;
117 for (size_t i = 0; i < count ; ++i) {
118 quartcount += roaring_bitmap_contains(bitmaps[i],maxvalue/4);
119 quartcount += roaring_bitmap_contains(bitmaps[i],maxvalue/2);
120 quartcount += roaring_bitmap_contains(bitmaps[i],3*maxvalue/4);
121 }
122 ENDBEST(cycles)
123
124 printf("Quartile queries on %zu bitmaps took %" PRIu64 " cycles\n", count,
125 cycles);
126
127
128 for (int i = 0; i < (int)count; ++i) {
129 free(numbers[i]);
130 numbers[i] = NULL; // paranoid
131 roaring_bitmap_free(bitmaps[i]);
132 bitmaps[i] = NULL; // paranoid
133 }
134 free(bitmaps);
135 free(howmany);
136 free(numbers);
137
138 return (int) quartcount;
139}
140