-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdstrings.hpp
284 lines (243 loc) · 8.55 KB
/
dstrings.hpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Copyright 2018 Georgia Institute of Technology
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DSTRINGS_HPP
#define DSTRINGS_HPP
/**
* This file implements a simple distributed stringset, used mainly for
* loading query-patterns.
* In this representation, strings are split between processors, such
* that each stirng is fully contained within a processor.
*/
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <memory>
#include <mxx/comm.hpp>
#include <mxx/file.hpp>
#include <mxx/stream.hpp>
/**
* @brief A simple string representation that does not own its own storage.
*
* Not necessarily '\0' terminated. Just a pointer and length.
*
* Implements some simple string operations, so that it can be used
* interchangably with std::string in some functions. Namely implements:
*
* - size(), length()
* - operator[]
* - data()
*/
struct mystring {
/// pointer to string start
char* ptr;
/// string length
size_t len;
/// returns the size (length) of the string
size_t size() const {
return len;
}
/// returns the size (length) of the string
size_t length() const {
return len;
}
/// returns a reference to the `i`th character
char& operator[](size_t i) const {
return ptr[i];
}
/// returns a reference to the `i`th character
char& operator[](size_t i) {
return ptr[i];
}
/// returns a pointer to the first character
char* data() {
return ptr;
}
/// returns a pointer to the first character
const char* data() const {
return ptr;
}
};
/**
* @brief A simple stringset with shared storage.
*
* The data for all stirngs is stored in `data`.
* Each string `i` is represented by a pointer to its start
* `str_begins[i]` and its length `str_lens[i]`.
*/
struct strings {
/// raw data
std::vector<char> data;
/// number of strings
size_t nstrings;
/// pointer to first char for each string
std::vector<char*> str_begins;
/// length of each string
std::vector<size_t> str_lens;
static constexpr char sep = '\n';
// TODO: constructors??
// TODO: iterators through strings?
static strings from_vec(const std::vector<std::string>& v) {
strings ss;
ss.nstrings = v.size();
size_t total_size = 0;
for (size_t i = 0; i < v.size(); ++i) {
total_size += v[i].size() + 1;
}
ss.data.resize(total_size);
ss.str_begins.resize(ss.nstrings);
ss.str_lens.resize(ss.nstrings);
char* out = ss.data.data();
for (size_t i = 0; i < v.size(); ++i) {
ss.str_begins[i] = out;
ss.str_lens[i] = v[i].size();
memcpy(out, &v[i][0], v[i].size());
out += v[i].size();
if (i+1 < v.size()) {
*(out++) = '\n';
} else {
*(out++) = '\0';
}
}
// replace very last sep with 0
return ss;
}
void parse() {
// parse the strings by seperator into str_begins, and str_lens
// given that the data vector contains the string data
// now parse in mem data
nstrings = 0;
str_begins.clear();
str_lens.clear();
size_t pos = 0;
while (pos < data.size()) {
// skip seperators
while (pos < data.size() && data[pos] == '\n') {
++pos;
}
size_t str_beg = pos;
while (pos < data.size() && data[pos] != '\n') {
++pos;
}
if (pos > str_beg && pos < data.size()) {
// save string info
nstrings++;
str_begins.emplace_back(data.data() + str_beg);
str_lens.emplace_back(pos - str_beg);
}
}
}
static strings from_string(const std::string& s) {
strings ss;
// copy into ss
ss.data.resize(s.size()+1);
memcpy(ss.data.data(), &s[0], s.size()+1);
ss.parse();
return ss;
}
static strings from_dfile(const std::string& filename, const mxx::comm& comm) {
size_t size = mxx::get_filesize(filename.c_str());
mxx::blk_dist dist(size, comm.size(), comm.rank());
// open file and seek to my pos
std::ifstream t(filename);
t.seekg(dist.eprefix_size());
// scan for first newline (sep?)
size_t my_offset = 0;
if (comm.rank() == 0) {
my_offset = 0;
} else {
my_offset = dist.eprefix_size();
// find first '\n'
char c;
while (t.get(c) && c != '\n') {
++my_offset;
}
if (my_offset < size) {
++my_offset; // advance one further
}
}
size_t my_end = mxx::left_shift(my_offset, comm);
if (comm.rank() + 1 == comm.size()) {
my_end = size;
}
// create rangebuf
mxx::rangebuf rb(my_offset, my_end-my_offset, t.rdbuf());
// read file (range) buffer into string stream
std::stringstream sstr;
sstr << &rb;
std::string local_str(sstr.str());
return strings::from_string(local_str);
}
};
std::ostream& operator<<(std::ostream& os, const strings& ss) {
os << "{nstrings=" << ss.nstrings << ", nbytes=" << ss.data.size() << ", [";
for (size_t i = 0; i < ss.nstrings; ++i) {
std::string s(ss.str_begins[i], ss.str_begins[i] + ss.str_lens[i]);
os << "\"" << s << "\"";
if (i+1 < ss.nstrings)
os << ", ";
}
os << "]}";
return os;
}
strings all2all_strings(const strings& ss, std::vector<int>& target, std::vector<size_t>& send_counts, const mxx::comm& comm) {
std::vector<size_t> offset = mxx::local_exscan(send_counts);
size_t send_num = offset.back() + send_counts.back();
// string lengths to send
std::vector<size_t> send_lens(send_num); // str-lens in bucketed order of strings
std::vector<size_t> send_data_sizes(comm.size()); // total data size per target proc
for (size_t i = 0; i < ss.nstrings; ++i) {
if (target[i] >= 0 && target[i] != comm.rank()) {
send_lens[offset[target[i]]++] = ss.str_lens[i];
send_data_sizes[target[i]] += ss.str_lens[i];
}
}
std::vector<size_t> data_offsets = mxx::local_exscan(send_data_sizes);
size_t send_data_size = data_offsets.back() + send_data_sizes.back();
std::vector<char> send_data(send_data_size); // buffer for sending
// create a "sorted" send buffer
for (size_t i = 0; i < ss.nstrings; ++i) {
if (target[i] >= 0 && target[i] != comm.rank()) {
memcpy(&send_data[data_offsets[target[i]]], ss.str_begins[i], ss.str_lens[i]);
data_offsets[target[i]] += ss.str_lens[i];
// TODO: just copy this extra char from `ss`?
//send_data[data_offsets[sprocs[i]]] = '\n';
//data_offsets[sprocs[i]] += 1;
// keep string lengths
//send_lens[lens_offset[target[i]]++] = ss.str_lens[i];
}
}
// send/recv the sequence lengths
std::vector<size_t> recv_counts = mxx::all2all(send_counts, comm);
std::vector<size_t> recv_lens = mxx::all2allv(send_lens, send_counts, recv_counts, comm);
// send/recv the string data of the patterns
std::vector<size_t> recv_data_sizes = mxx::all2all(send_data_sizes, comm);
std::vector<char> recv_data = mxx::all2allv(send_data, send_data_sizes, recv_data_sizes, comm);
// create `strings` data structure from received data
strings recv_ss;
recv_ss.data.swap(recv_data); // FIXME allocate memory for data
recv_ss.nstrings = std::accumulate(recv_counts.begin(), recv_counts.end(), static_cast<size_t>(0));
recv_ss.str_lens = recv_lens;
recv_ss.str_begins.resize(recv_ss.nstrings);
// init str_begins
size_t beg = 0;
for (size_t i = 0; i < recv_ss.nstrings; ++i) {
recv_ss.str_begins[i] = recv_ss.data.data() + beg;
beg += recv_ss.str_lens[i];
}
return recv_ss;
}
#endif // DSTRINGS_HPP