-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathraw_file_group_reader.cpp
More file actions
175 lines (147 loc) · 5.01 KB
/
Copy pathraw_file_group_reader.cpp
File metadata and controls
175 lines (147 loc) · 5.01 KB
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
#include "raw_file_group_reader.h"
#include <assert.h>
#include <fmt/core.h>
#include <iostream>
#include <sys/sysinfo.h>
#include "thread_util.h"
#include "util.h"
using namespace std;
/*
Reads [first_band, last_band], inclusive, out of num_bands total.
*/
RawFileGroupReader::RawFileGroupReader(RawFileGroup& file_group, int num_bands,
int first_band, int last_band,
int num_batches, int blocks_per_batch)
: file_group(file_group), num_bands(num_bands), first_band(first_band),
last_band(last_band), num_batches(num_batches), blocks_per_batch(blocks_per_batch),
coarse_channels_per_band(file_group.num_coarse_channels / num_bands),
stopped(false) {
// Limit queue size depending on total memory.
struct sysinfo info;
sysinfo(&info);
size_t mb = 1024 * 1024;
size_t gb = mb * 1024;
if ((size_t) info.totalram > 50 * gb) {
// Looks like a production machine.
size_t buffer_size = rawBufferSize(blocks_per_batch, file_group.nants,
coarse_channels_per_band,
file_group.timesteps_per_block,
file_group.npol);
buffer_queue_max_size = (int) (0.25 * info.totalram / buffer_size);
cout << fmt::format("limiting raw file input buffer memory to {:.1f} GB\n",
1.0 * buffer_queue_max_size * buffer_size / gb);
} else {
// Looks like a dev machine.
buffer_queue_max_size = 4;
}
io_thread = thread(&RawFileGroupReader::runInputThread, this);
device_raw_buffer = makeDeviceBuffer();
cout << "raw buffer memory: " << prettyBytes(device_raw_buffer->size) << endl;
}
void RawFileGroupReader::stop() {
unique_lock<mutex> lock(m);
stopped = true;
lock.unlock();
cv.notify_all();
}
RawFileGroupReader::~RawFileGroupReader() {
stop();
if (io_thread.joinable()) {
io_thread.join();
}
}
unique_ptr<RawBuffer> RawFileGroupReader::makeBuffer() {
unique_lock<mutex> lock(m);
if (!extra_buffers.empty()) {
auto buffer = move(extra_buffers.front());
extra_buffers.pop();
return buffer;
}
lock.unlock();
return make_unique<RawBuffer>(blocks_per_batch,
file_group.nants,
coarse_channels_per_band,
file_group.timesteps_per_block,
file_group.npol);
}
shared_ptr<DeviceRawBuffer> RawFileGroupReader::makeDeviceBuffer() {
return make_shared<DeviceRawBuffer>(blocks_per_batch,
file_group.nants,
coarse_channels_per_band,
file_group.timesteps_per_block,
file_group.npol);
}
unique_ptr<RawBuffer> RawFileGroupReader::readToHost() {
unique_lock<mutex> lock(m);
while (!stopped && buffer_queue.empty()) {
cv.wait(lock);
}
if (stopped) {
fatal("RawFileGroupReader stopped");
}
auto buffer = move(buffer_queue.front());
buffer_queue.pop();
lock.unlock();
cv.notify_one();
return buffer;
}
void RawFileGroupReader::returnBuffer(unique_ptr<RawBuffer> buffer) {
// We might want to check it's the right size
if (buffer.get() == nullptr) {
return;
}
unique_lock<mutex> lock(m);
extra_buffers.push(move(buffer));
}
// Returns false if the reader gets stopped before a new item is pushed
bool RawFileGroupReader::push(unique_ptr<RawBuffer> buffer) {
unique_lock<mutex> lock(m);
while (!stopped && (int) buffer_queue.size() >= buffer_queue_max_size) {
cv.wait(lock);
}
if (stopped) {
return false;
}
buffer_queue.push(move(buffer));
lock.unlock();
cv.notify_one();
return true;
}
// Reads all the input and passes it to the buffer_queue
void RawFileGroupReader::runInputThread() {
setThreadName("input");
for (int band = first_band; band <= last_band; ++band) {
file_group.resetBand(band, num_bands);
for (int batch = 0; batch < num_batches; ++batch) {
if (stopped) {
return;
}
auto buffer = makeBuffer();
vector<function<bool()> > tasks;
for (int block = 0; block < buffer->num_blocks; ++block) {
file_group.readTasks(buffer->blockPointer(block), &tasks);
}
// Testing on meerkat, any more than 4 threads doesn't help
int num_threads = 4;
if (!runInParallel(move(tasks), num_threads)) {
stop();
return;
}
if (!push(move(buffer))) {
return;
}
}
}
}
shared_ptr<DeviceRawBuffer> RawFileGroupReader::readToDevice() {
// Client code could still be using the raw buffers.
// Wait for it to finish.
device_raw_buffer->waitUntilUnused();
returnBuffer(move(read_buffer));
read_buffer = readToHost();
// Useful for debugging the output of raw file reading
// cerr << "read_buffer[0] = " << (int) read_buffer->data[0] << endl;
device_raw_buffer->copyFromAsync(*read_buffer);
device_raw_buffer->waitUntilReady();
return device_raw_buffer;
}