forked from microsoft/DiskANN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
in_mem_data_store.cpp
401 lines (351 loc) · 14 KB
/
in_mem_data_store.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include <memory>
#include "abstract_scratch.h"
#include "in_mem_data_store.h"
#include "utils.h"
namespace diskann
{
template <typename data_t>
InMemDataStore<data_t>::InMemDataStore(const location_t num_points, const size_t dim,
std::unique_ptr<Distance<data_t>> distance_fn)
: AbstractDataStore<data_t>(num_points, dim), _distance_fn(std::move(distance_fn))
{
_aligned_dim = ROUND_UP(dim, _distance_fn->get_required_alignment());
alloc_aligned(((void **)&_data), this->_capacity * _aligned_dim * sizeof(data_t), 8 * sizeof(data_t));
std::memset(_data, 0, this->_capacity * _aligned_dim * sizeof(data_t));
}
template <typename data_t> InMemDataStore<data_t>::~InMemDataStore()
{
if (_data != nullptr)
{
aligned_free(this->_data);
}
}
template <typename data_t> size_t InMemDataStore<data_t>::get_aligned_dim() const
{
return _aligned_dim;
}
template <typename data_t> size_t InMemDataStore<data_t>::get_alignment_factor() const
{
return _distance_fn->get_required_alignment();
}
template <typename data_t> location_t InMemDataStore<data_t>::load(const std::string &filename)
{
return load_impl(filename);
}
#ifdef EXEC_ENV_OLS
template <typename data_t> location_t InMemDataStore<data_t>::load_impl(AlignedFileReader &reader)
{
size_t file_dim, file_num_points;
diskann::get_bin_metadata(reader, file_num_points, file_dim);
if (file_dim != this->_dim)
{
std::stringstream stream;
stream << "ERROR: Driver requests loading " << this->_dim << " dimension,"
<< "but file has " << file_dim << " dimension." << std::endl;
diskann::cerr << stream.str() << std::endl;
aligned_free(_data);
throw diskann::ANNException(stream.str(), -1, __FUNCSIG__, __FILE__, __LINE__);
}
if (file_num_points > this->capacity())
{
this->resize((location_t)file_num_points);
}
copy_aligned_data_from_file<data_t>(reader, _data, file_num_points, file_dim, _aligned_dim);
return (location_t)file_num_points;
}
#endif
template <typename data_t> location_t InMemDataStore<data_t>::load_impl(const std::string &filename)
{
size_t file_dim, file_num_points;
if (!file_exists(filename))
{
std::stringstream stream;
stream << "ERROR: data file " << filename << " does not exist." << std::endl;
diskann::cerr << stream.str() << std::endl;
aligned_free(_data);
throw diskann::ANNException(stream.str(), -1, __FUNCSIG__, __FILE__, __LINE__);
}
diskann::get_bin_metadata(filename, file_num_points, file_dim);
if (file_dim != this->_dim)
{
std::stringstream stream;
stream << "ERROR: Driver requests loading " << this->_dim << " dimension,"
<< "but file has " << file_dim << " dimension." << std::endl;
diskann::cerr << stream.str() << std::endl;
aligned_free(_data);
throw diskann::ANNException(stream.str(), -1, __FUNCSIG__, __FILE__, __LINE__);
}
if (file_num_points > this->capacity())
{
this->resize((location_t)file_num_points);
}
copy_aligned_data_from_file<data_t>(filename.c_str(), _data, file_num_points, file_dim, _aligned_dim);
return (location_t)file_num_points;
}
template <typename data_t> size_t InMemDataStore<data_t>::save(const std::string &filename, const location_t num_points)
{
return save_data_in_base_dimensions(filename, _data, num_points, this->get_dims(), this->get_aligned_dim(), 0U);
}
template <typename data_t> void InMemDataStore<data_t>::populate_data(const data_t *vectors, const location_t num_pts)
{
memset(_data, 0, _aligned_dim * sizeof(data_t) * num_pts);
for (location_t i = 0; i < num_pts; i++)
{
std::memmove(_data + i * _aligned_dim, vectors + i * this->_dim, this->_dim * sizeof(data_t));
}
if (_distance_fn->preprocessing_required())
{
_distance_fn->preprocess_base_points(_data, this->_aligned_dim, num_pts);
}
}
template <typename data_t> void InMemDataStore<data_t>::populate_data(const std::string &filename, const size_t offset)
{
size_t npts, ndim;
copy_aligned_data_from_file(filename.c_str(), _data, npts, ndim, _aligned_dim, offset);
if ((location_t)npts > this->capacity())
{
std::stringstream ss;
ss << "Number of points in the file: " << filename
<< " is greater than the capacity of data store: " << this->capacity()
<< ". Must invoke resize before calling populate_data()" << std::endl;
throw diskann::ANNException(ss.str(), -1);
}
if ((location_t)ndim != this->get_dims())
{
std::stringstream ss;
ss << "Number of dimensions of a point in the file: " << filename
<< " is not equal to dimensions of data store: " << this->capacity() << "." << std::endl;
throw diskann::ANNException(ss.str(), -1);
}
if (_distance_fn->preprocessing_required())
{
_distance_fn->preprocess_base_points(_data, this->_aligned_dim, this->capacity());
}
}
template <typename data_t>
void InMemDataStore<data_t>::extract_data_to_bin(const std::string &filename, const location_t num_points)
{
save_data_in_base_dimensions(filename, _data, num_points, this->get_dims(), this->get_aligned_dim(), 0U);
}
template <typename data_t> void InMemDataStore<data_t>::get_vector(const location_t i, data_t *dest) const
{
// REFACTOR TODO: Should we denormalize and return values?
memcpy(dest, _data + i * _aligned_dim, this->_dim * sizeof(data_t));
}
template <typename data_t> void InMemDataStore<data_t>::set_vector(const location_t loc, const data_t *const vector)
{
size_t offset_in_data = loc * _aligned_dim;
memset(_data + offset_in_data, 0, _aligned_dim * sizeof(data_t));
memcpy(_data + offset_in_data, vector, this->_dim * sizeof(data_t));
if (_distance_fn->preprocessing_required())
{
_distance_fn->preprocess_base_points(_data + offset_in_data, _aligned_dim, 1);
}
}
template <typename data_t> void InMemDataStore<data_t>::prefetch_vector(const location_t loc)
{
diskann::prefetch_vector((const char *)_data + _aligned_dim * (size_t)loc * sizeof(data_t),
sizeof(data_t) * _aligned_dim);
}
template <typename data_t>
void InMemDataStore<data_t>::preprocess_query(const data_t *query, AbstractScratch<data_t> *query_scratch) const
{
if (query_scratch != nullptr)
{
memcpy(query_scratch->aligned_query_T(), query, sizeof(data_t) * this->get_dims());
}
else
{
std::stringstream ss;
ss << "In InMemDataStore::preprocess_query: Query scratch is null";
diskann::cerr << ss.str() << std::endl;
throw diskann::ANNException(ss.str(), -1);
}
}
template <typename data_t> float InMemDataStore<data_t>::get_distance(const data_t *query, const location_t loc) const
{
return _distance_fn->compare(query, _data + _aligned_dim * loc, (uint32_t)_aligned_dim);
}
template <typename data_t>
void InMemDataStore<data_t>::get_distance(const data_t *query, const location_t *locations,
const uint32_t location_count, float *distances,
AbstractScratch<data_t> *scratch_space) const
{
for (location_t i = 0; i < location_count; i++)
{
distances[i] = _distance_fn->compare(query, _data + locations[i] * _aligned_dim, (uint32_t)this->_aligned_dim);
}
}
template <typename data_t>
float InMemDataStore<data_t>::get_distance(const location_t loc1, const location_t loc2) const
{
return _distance_fn->compare(_data + loc1 * _aligned_dim, _data + loc2 * _aligned_dim,
(uint32_t)this->_aligned_dim);
}
template <typename data_t>
void InMemDataStore<data_t>::get_distance(const data_t *preprocessed_query, const std::vector<location_t> &ids,
std::vector<float> &distances, AbstractScratch<data_t> *scratch_space) const
{
for (int i = 0; i < ids.size(); i++)
{
distances[i] =
_distance_fn->compare(preprocessed_query, _data + ids[i] * _aligned_dim, (uint32_t)this->_aligned_dim);
}
}
template <typename data_t> location_t InMemDataStore<data_t>::expand(const location_t new_size)
{
if (new_size == this->capacity())
{
return this->capacity();
}
else if (new_size < this->capacity())
{
std::stringstream ss;
ss << "Cannot 'expand' datastore when new capacity (" << new_size << ") < existing capacity("
<< this->capacity() << ")" << std::endl;
throw diskann::ANNException(ss.str(), -1);
}
#ifndef _WINDOWS
data_t *new_data;
alloc_aligned((void **)&new_data, new_size * _aligned_dim * sizeof(data_t), 8 * sizeof(data_t));
memcpy(new_data, _data, this->capacity() * _aligned_dim * sizeof(data_t));
aligned_free(_data);
_data = new_data;
#else
realloc_aligned((void **)&_data, new_size * _aligned_dim * sizeof(data_t), 8 * sizeof(data_t));
#endif
this->_capacity = new_size;
return this->_capacity;
}
template <typename data_t> location_t InMemDataStore<data_t>::shrink(const location_t new_size)
{
if (new_size == this->capacity())
{
return this->capacity();
}
else if (new_size > this->capacity())
{
std::stringstream ss;
ss << "Cannot 'shrink' datastore when new capacity (" << new_size << ") > existing capacity("
<< this->capacity() << ")" << std::endl;
throw diskann::ANNException(ss.str(), -1);
}
#ifndef _WINDOWS
data_t *new_data;
alloc_aligned((void **)&new_data, new_size * _aligned_dim * sizeof(data_t), 8 * sizeof(data_t));
memcpy(new_data, _data, new_size * _aligned_dim * sizeof(data_t));
aligned_free(_data);
_data = new_data;
#else
realloc_aligned((void **)&_data, new_size * _aligned_dim * sizeof(data_t), 8 * sizeof(data_t));
#endif
this->_capacity = new_size;
return this->_capacity;
}
template <typename data_t>
void InMemDataStore<data_t>::move_vectors(const location_t old_location_start, const location_t new_location_start,
const location_t num_locations)
{
if (num_locations == 0 || old_location_start == new_location_start)
{
return;
}
/* // Update pointers to the moved nodes. Note: the computation is correct
even
// when new_location_start < old_location_start given the C++ uint32_t
// integer arithmetic rules.
const uint32_t location_delta = new_location_start - old_location_start;
*/
// The [start, end) interval which will contain obsolete points to be
// cleared.
uint32_t mem_clear_loc_start = old_location_start;
uint32_t mem_clear_loc_end_limit = old_location_start + num_locations;
if (new_location_start < old_location_start)
{
// If ranges are overlapping, make sure not to clear the newly copied
// data.
if (mem_clear_loc_start < new_location_start + num_locations)
{
// Clear only after the end of the new range.
mem_clear_loc_start = new_location_start + num_locations;
}
}
else
{
// If ranges are overlapping, make sure not to clear the newly copied
// data.
if (mem_clear_loc_end_limit > new_location_start)
{
// Clear only up to the beginning of the new range.
mem_clear_loc_end_limit = new_location_start;
}
}
// Use memmove to handle overlapping ranges.
copy_vectors(old_location_start, new_location_start, num_locations);
memset(_data + _aligned_dim * mem_clear_loc_start, 0,
sizeof(data_t) * _aligned_dim * (mem_clear_loc_end_limit - mem_clear_loc_start));
}
template <typename data_t>
void InMemDataStore<data_t>::copy_vectors(const location_t from_loc, const location_t to_loc,
const location_t num_points)
{
assert(from_loc < this->_capacity);
assert(to_loc < this->_capacity);
assert(num_points < this->_capacity);
memmove(_data + _aligned_dim * to_loc, _data + _aligned_dim * from_loc, num_points * _aligned_dim * sizeof(data_t));
}
template <typename data_t> location_t InMemDataStore<data_t>::calculate_medoid() const
{
// allocate and init centroid
float *center = new float[_aligned_dim];
for (size_t j = 0; j < _aligned_dim; j++)
center[j] = 0;
for (size_t i = 0; i < this->capacity(); i++)
for (size_t j = 0; j < _aligned_dim; j++)
center[j] += (float)_data[i * _aligned_dim + j];
for (size_t j = 0; j < _aligned_dim; j++)
center[j] /= (float)this->capacity();
// compute all to one distance
float *distances = new float[this->capacity()];
// TODO: REFACTOR. Removing pragma might make this slow. Must revisit.
// Problem is that we need to pass num_threads here, it is not clear
// if data store must be aware of threads!
// #pragma omp parallel for schedule(static, 65536)
for (int64_t i = 0; i < (int64_t)this->capacity(); i++)
{
// extract point and distance reference
float &dist = distances[i];
const data_t *cur_vec = _data + (i * (size_t)_aligned_dim);
dist = 0;
float diff = 0;
for (size_t j = 0; j < _aligned_dim; j++)
{
diff = (center[j] - (float)cur_vec[j]) * (center[j] - (float)cur_vec[j]);
dist += diff;
}
}
// find imin
uint32_t min_idx = 0;
float min_dist = distances[0];
for (uint32_t i = 1; i < this->capacity(); i++)
{
if (distances[i] < min_dist)
{
min_idx = i;
min_dist = distances[i];
}
}
delete[] distances;
delete[] center;
return min_idx;
}
template <typename data_t> Distance<data_t> *InMemDataStore<data_t>::get_dist_fn() const
{
return this->_distance_fn.get();
}
template DISKANN_DLLEXPORT class InMemDataStore<float>;
template DISKANN_DLLEXPORT class InMemDataStore<int8_t>;
template DISKANN_DLLEXPORT class InMemDataStore<uint8_t>;
} // namespace diskann