-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
61 lines (53 loc) · 1.58 KB
/
utils.h
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
#include <fstream>
#include <iostream>
#include <thread>
void preview_dataset(std::vector<float> xb) {
for (int64_t i = 0; i < 5; i++) {
for (int64_t j = 0; j < 10; j++) {
std::cout << xb[i * 10 + j] << " ";
}
std::cout << std::endl;
}
}
void write_vector(const char *filename, int64_t *data, int64_t size) {
FILE *f = fopen(filename, "w");
if (!f) {
fprintf(stderr, "Could not open %s\n", filename);
perror("");
abort();
}
int64_t e = fwrite(data, sizeof(int64_t), size, f);
fclose(f);
}
std::vector<int64_t> read_vector(const char *filename, int64_t size) {
FILE *f = fopen(filename, "r");
if (!f) {
fprintf(stderr, "Could not open %s\n", filename);
perror("");
abort();
}
std::vector<int64_t> data(size);
int64_t e = fread(data.data(), sizeof(int64_t), size, f);
fclose(f);
return data;
}
std::vector<float> read_bin_dataset(std::string fname, int64_t *n, int64_t *d,
int64_t limit) {
// Read datafile in
std::ifstream datafile(fname, std::ifstream::binary);
uint32_t N_uint32;
uint32_t dim_uint32;
datafile.read((char *)&N_uint32, sizeof(uint32_t));
datafile.read((char *)&dim_uint32, sizeof(uint32_t));
int64_t N = (int64_t)std::min((int64_t)N_uint32, limit);
int64_t dim = (int64_t)dim_uint32;
*n = N;
*d = dim;
printf("Read in file - N:%li, dim:%li\n", N, dim);
std::vector<float> data;
data.resize((size_t)N * (size_t)dim);
datafile.read(reinterpret_cast<char *>(data.data()),
(size_t)N * (size_t)dim * sizeof(float));
datafile.close();
return data;
}