forked from bu-cs547-20181s/s18a1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump_kec.cpp
177 lines (147 loc) · 5.79 KB
/
dump_kec.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include <fcntl.h>
#include <assert.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/mman.h>
#include <iomanip>
// Wrapper around a pointer, for reading values from byte sequence.
class Reader {
public:
Reader(const char *p) : ptr{p} {}
template <typename T>
Reader &operator>>(T &o) {
// Assert alignment.
assert(uintptr_t(ptr)%sizeof(T) == 0);
o = *(T *) ptr;
ptr += sizeof(T);
return *this;
}
private:
const char *ptr;
};
void dump(const std::string &fn);
int
main(int argc, char **argv) {
for (int i = 1; i < argc; i++) {
dump(argv[i]);
}
}
void
dump(const std::string &fn) {
std::cout << fn << std::endl;
/*
* Use mmap() for convenience.
*/
int fd = open(fn.c_str(), O_RDONLY);
if (fd < 0) {
int en = errno;
std::cerr << "Couldn't open " << fn << ": " << strerror(en) << "." << std::endl;
exit(2);
}
// Get the actual size of the file.
struct stat sb;
int rv = fstat(fd, &sb); assert(rv == 0);
// std::cout << sb.st_size << std::endl;
// Use some flags that will hopefully improve performance.
void *vp = mmap(nullptr, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (vp == MAP_FAILED) {
int en = errno;
fprintf(stderr, "mmap() failed: %s\n", strerror(en));
exit(3);
}
char *file_mem = (char *) vp;
// Tell the kernel that it should evict the pages as soon as possible.
rv = madvise(vp, sb.st_size, MADV_SEQUENTIAL|MADV_WILLNEED); assert(rv == 0);
rv = close(fd); assert(rv == 0);
// Prefix to print before every line, to improve readability.
std::string pref(" ");
/*
* Read file type string.
*/
int n = strnlen(file_mem, 8);
std::string file_type(file_mem, n);
std::cout << pref << "File type string: " << file_type << std::endl;
// Start to read data, skip the file type string.
Reader reader{file_mem + 8};
// TODO: Code below is repetitive, cleanup.
// TODO: Add io manip to print with commas.
if (file_type == "TRAINING") {
uint64_t id;
uint64_t n_points;
uint64_t n_dims;
reader >> id >> n_points >> n_dims;
std::cout << pref << "Training file ID: " << std::hex << std::setw(16) << std::setfill('0') << id << std::dec << std::endl;
std::cout << pref << "Number of points: " << n_points << std::endl;
std::cout << pref << "Number of dimensions: " << n_dims << std::endl;
for (std::uint64_t i = 0; i < n_points; i++) {
std::cout << pref << "Point " << i << ": ";
for (std::uint64_t j = 0; j < n_dims; j++) {
float f;
reader >> f;
std::cout << std::fixed << std::setprecision(6) << std::setw(15) << std::setfill(' ') << f;
// Add comma.
if (j < n_dims - 1) {
std::cout << ", ";
}
}
std::cout << std::endl;
}
} else if (file_type == "QUERY") {
uint64_t id;
uint64_t n_queries;
uint64_t n_dims;
uint64_t n_neighbors;
reader >> id >> n_queries >> n_dims >> n_neighbors;
std::cout << pref << "Query file ID: " << std::hex << std::setw(16) << std::setfill('0') << id << std::dec << std::endl;
std::cout << pref << "Number of queries: " << n_queries << std::endl;
std::cout << pref << "Number of dimensions: " << n_dims << std::endl;
std::cout << pref << "Number of neighbors to return for each point: " << n_neighbors << std::endl;
for (std::uint64_t i = 0; i < n_queries; i++) {
std::cout << pref << "Query " << i << ": ";
for (std::uint64_t j = 0; j < n_dims; j++) {
float f;
reader >> f;
std::cout << std::fixed << std::setprecision(6) << std::setw(15) << std::setfill(' ') << f;
// Add comma.
if (j < n_dims - 1) {
std::cout << ", ";
}
}
std::cout << std::endl;
}
} else if (file_type == "RESULT") {
uint64_t training_id;
uint64_t query_id;
uint64_t result_id;
uint64_t n_queries;
uint64_t n_dims;
uint64_t n_neighbors;
reader >> training_id >> query_id >> result_id >> n_queries >> n_dims >> n_neighbors;
std::cout << pref << "Training file ID: " << std::hex << std::setw(16) << std::setfill('0') << training_id << std::dec << std::endl;
std::cout << pref << "Query file ID: " << std::hex << std::setw(16) << std::setfill('0') << query_id << std::dec << std::endl;
std::cout << pref << "Result file ID: " << std::hex << std::setw(16) << std::setfill('0') << result_id << std::dec << std::endl;
std::cout << pref << "Number of queries: " << n_queries << std::endl;
std::cout << pref << "Number of dimensions: " << n_dims << std::endl;
std::cout << pref << "Number of neighbors returned for each query: " << n_neighbors << std::endl;
for (std::uint64_t i = 0; i < n_queries; i++) {
std::cout << pref << "Result " << i << ": ";
for (std::uint64_t j = 0; j < n_dims; j++) {
float f;
reader >> f;
std::cout << std::fixed << std::setprecision(6) << std::setw(15) << std::setfill(' ') << f;
// Add comma.
if (j < n_dims - 1) {
std::cout << ", ";
}
}
std::cout << std::endl;
}
} else {
std::cerr << "Unknown file type: " << file_type << std::endl;
exit(2);
}
rv = munmap(file_mem, sb.st_size); assert(rv == 0);
}