-
Notifications
You must be signed in to change notification settings - Fork 14
/
Example_lazy_access.cpp
147 lines (105 loc) · 3.92 KB
/
Example_lazy_access.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
//
// Created by joel on 06.01.22.
//
const char* usage = R"(
Example showing iteration with lazy access to both particle coordinates and values
Usage:
(using *.apr output of Example_get_apr)
Example_lazy_access -i input_apr_file -d directory
Note: There is no output, this file is best utilized by looking at the source code for an example of how to lazily access APR files.
)";
#include "Example_lazy_access.hpp"
int main(int argc, char **argv) {
// INPUT PARSING
cmdLineOptions options = read_command_line_options(argc, argv);
std::string file_name = options.directory + options.input;
APRTimer timer(true);
timer.start_timer("initialization");
// open APR file for reading
APRFile aprFile;
aprFile.open(file_name, "READ");
// initialize lazy access and iterator for spatial information
LazyAccess access;
access.init(aprFile);
access.open();
LazyIterator lazy_it(access);
// initialize lazy particle data
LazyData<uint16_t> lazy_parts;
lazy_parts.init(aprFile);
lazy_parts.open();
timer.stop_timer();
timer.start_timer("lazy iteration (load by slice)");
// (optional) preallocate buffers for lazily loaded data
const uint64_t xy_num = lazy_it.x_num(lazy_it.level_max()) * lazy_it.y_num(lazy_it.level_max());
lazy_it.set_buffer_size(xy_num);
lazy_parts.set_buffer_size(xy_num);
// store the result in memory
ParticleData<uint16_t> result(lazy_it.total_number_particles());
for(int level = lazy_it.level_max(); level > lazy_it.level_min(); --level) {
for(int z = 0; z < lazy_it.z_num(level); ++z) {
// load slice data
auto slice_range = lazy_it.get_slice_range(level, z);
lazy_it.load_range(slice_range.begin, slice_range.end);
lazy_parts.load_range(slice_range.begin, slice_range.end);
for(int x = 0; x < lazy_it.x_num(level); ++x) {
for(lazy_it.begin(level, z, x); lazy_it < lazy_it.end(); ++lazy_it) {
result[lazy_it] = lazy_parts[lazy_it] + 3;
}
}
}
}
timer.stop_timer();
timer.start_timer("lazy iteration (load by row)");
for(int level = lazy_it.level_max(); level > lazy_it.level_min(); --level) {
for(int z = 0; z < lazy_it.z_num(level); ++z) {
for(int x = 0; x < lazy_it.x_num(level); ++x) {
// load row data
auto row_range = lazy_it.get_row_range(level, z, x);
lazy_it.load_range(row_range.begin, row_range.end);
lazy_parts.load_range(row_range.begin, row_range.end);
for(lazy_it.begin(level, z, x); lazy_it < lazy_it.end(); ++lazy_it) {
result[lazy_it] = lazy_parts[lazy_it] + 3;
}
}
}
}
timer.stop_timer();
// close files
access.close();
lazy_parts.close();
aprFile.close();
return 0;
}
bool command_option_exists(char **begin, char **end, const std::string &option)
{
return std::find(begin, end, option) != end;
}
char* get_command_option(char **begin, char **end, const std::string &option)
{
char ** itr = std::find(begin, end, option);
if (itr != end && ++itr != end)
{
return *itr;
}
return 0;
}
cmdLineOptions read_command_line_options(int argc, char **argv){
cmdLineOptions result;
if(argc == 1) {
std::cerr << "Usage: \"Example_lazy_access -i input_apr_file -d directory\"" << std::endl;
std::cerr << usage << std::endl;
exit(1);
}
if(command_option_exists(argv, argv + argc, "-i"))
{
result.input = std::string(get_command_option(argv, argv + argc, "-i"));
} else {
std::cout << "Input file required" << std::endl;
exit(2);
}
if(command_option_exists(argv, argv + argc, "-d"))
{
result.directory = std::string(get_command_option(argv, argv + argc, "-d"));
}
return result;
}