Skip to content

Commit

Permalink
removing _num_frozen_point from graph store
Browse files Browse the repository at this point in the history
  • Loading branch information
yashpatel007 committed Aug 3, 2023
1 parent d9647ce commit 3977719
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 53 deletions.
4 changes: 2 additions & 2 deletions include/abstract_graph_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class AbstractGraphStore
{
}

virtual int load(const std::string &index_path_prefix) = 0;
virtual int store(const std::string &index_path_prefix, const size_t num_points) = 0;
virtual int load(const std::string &index_path_prefix, const size_t num_points) = 0;
virtual int store(const std::string &index_path_prefix, const size_t num_points, const size_t num_fz_points) = 0;

// not synchronised, user should use lock when necvessary.
virtual std::vector<location_t> &get_neighbours(const location_t i) = 0;
Expand Down
10 changes: 5 additions & 5 deletions include/in_mem_graph_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ namespace diskann
class InMemGraphStore : public AbstractGraphStore
{
public:
InMemGraphStore(const size_t total_pts, const size_t num_frozen_points);
InMemGraphStore(const size_t total_pts);

int load(const std::string &index_path_prefix);
int store(const std::string &index_path_prefix, const size_t num_points);
int load(const std::string &index_path_prefix, const size_t num_points);
int store(const std::string &index_path_prefix, const size_t num_points, const size_t num_froxen_points);

virtual std::vector<location_t> &get_neighbours(const location_t i) override;
virtual void set_neighbours(const location_t i, std::vector<location_t> &neighbors) override;
Expand All @@ -36,13 +36,13 @@ class InMemGraphStore : public AbstractGraphStore
virtual location_t load_impl(AlignedFileReader &reader, size_t expected_num_points);
#endif

int save_graph(const std::string &index_path_prefix, const size_t active_points);
int save_graph(const std::string &index_path_prefix, const size_t active_points, const size_t num_frozen_points);

private:
size_t _max_range_of_graph = 0;
uint32_t _max_observed_degree = 0;
uint32_t _start = 0;
size_t _num_frozen_pts;

std::vector<std::vector<uint32_t>> _graph;
};

Expand Down
2 changes: 1 addition & 1 deletion include/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ template <typename T, typename TagT = uint32_t, typename LabelT = uint32_t> clas
// externally and won't be returned by search. At least 1 frozen point is
// needed for a dynamic index. The frozen points have consecutive locations.
// See also _start below.
size_t _num_frozen_pts;
size_t _num_frozen_pts = 0;
size_t _node_size;
size_t _data_len;
size_t _neighbor_len;
Expand Down
3 changes: 1 addition & 2 deletions include/index_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class IndexFactory
std::unique_ptr<AbstractDataStore<T>> construct_datastore(DataStoreStrategy stratagy, size_t num_points,
size_t dimension);

std::unique_ptr<AbstractGraphStore> construct_graphstore(GraphStoreStrategy stratagy, size_t size,
size_t frozen_points);
std::unique_ptr<AbstractGraphStore> construct_graphstore(GraphStoreStrategy stratagy, size_t size);

template <typename data_type, typename tag_type, typename label_type>
std::unique_ptr<AbstractIndex> create_instance();
Expand Down
49 changes: 13 additions & 36 deletions src/in_mem_graph_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
namespace diskann
{

InMemGraphStore::InMemGraphStore(const size_t total_pts, const size_t num_frozen_pts)
: AbstractGraphStore(total_pts), _num_frozen_pts(num_frozen_pts)
InMemGraphStore::InMemGraphStore(const size_t total_pts) : AbstractGraphStore(total_pts)
{
}

int InMemGraphStore::load(const std::string &index_path_prefix)
int InMemGraphStore::load(const std::string &index_path_prefix, const size_t num_points)
{
return load_impl(index_path_prefix, get_total_points());
return load_impl(index_path_prefix, num_points);
}
int InMemGraphStore::store(const std::string &index_path_prefix, const size_t num_points)
int InMemGraphStore::store(const std::string &index_path_prefix, const size_t num_points,
const size_t num_frozen_points)
{
return save_graph(index_path_prefix, num_points);
return save_graph(index_path_prefix, num_points, num_frozen_points);
}
std::vector<location_t> &InMemGraphStore::get_neighbours(const location_t i)
{
Expand Down Expand Up @@ -46,7 +46,7 @@ void InMemGraphStore::clear_graph()
location_t InMemGraphStore::load_impl(const std::string &filename, size_t expected_num_points)
{
size_t expected_file_size;
size_t file_frozen_pts;

auto max_points = get_max_points();
int header_size = 2 * sizeof(size_t) + 2 * sizeof(uint32_t);
std::unique_ptr<char[]> header = std::make_unique<char[]>(header_size);
Expand Down Expand Up @@ -146,38 +146,14 @@ location_t InMemGraphStore::load_impl(const std::string &filename, size_t expect
<< ", _max_observed_degree: " << _max_observed_degree << ", _start: " << _start
<< ", file_frozen_pts: " << file_frozen_pts << std::endl;

if (file_frozen_pts != _num_frozen_pts)
{
std::stringstream stream;
if (file_frozen_pts == 1)
{
stream << "ERROR: When loading graph, detected dynamic index, but "
"constructor asks for static index. Exitting."
<< std::endl;
}
else
{
stream << "ERROR: When loading index, detected static index, but "
"constructor asks for dynamic index. Exitting."
<< std::endl;
}
diskann::cerr << stream.str() << std::endl;
throw diskann::ANNException(stream.str(), -1, __FUNCSIG__, __FILE__, __LINE__);
}

diskann::cout << "Loading vamana graph " << filename << "..." << std::flush;

const size_t expected_max_points = expected_num_points - file_frozen_pts;

// If user provides more points than max_points
// resize the _graph to the larger size.
auto max_points = get_total_points() - _num_frozen_pts; // from parent class holding max_pts
if (max_points < expected_max_points)
if (get_total_points() < expected_num_points)
{
diskann::cout << "Number of points in data: " << expected_max_points
<< " is greater than max_points: " << max_points
<< " Setting max points to: " << expected_max_points << std::endl;
_graph.resize(expected_max_points + file_frozen_pts);
diskann::cout << "resizing graph to " << expected_num_points << std::endl;
_graph.resize(expected_num_points);
}

size_t bytes_read = vamana_metadata_size;
Expand Down Expand Up @@ -213,7 +189,8 @@ location_t InMemGraphStore::load_impl(const std::string &filename, size_t expect
return nodes_read;
}

int InMemGraphStore::save_graph(const std::string &index_path_prefix, const size_t num_points)
int InMemGraphStore::save_graph(const std::string &index_path_prefix, const size_t num_points,
const size_t num_frozen_points)
{
std::ofstream out;
open_file_to_write(out, index_path_prefix);
Expand All @@ -226,7 +203,7 @@ int InMemGraphStore::save_graph(const std::string &index_path_prefix, const size
out.write((char *)&_max_observed_degree, sizeof(uint32_t));
uint32_t ep_u32 = _start;
out.write((char *)&ep_u32, sizeof(uint32_t));
out.write((char *)&_num_frozen_pts, sizeof(size_t));
out.write((char *)&num_frozen_points, sizeof(size_t));

// Note: num_points = _nd + _num_frozen_points
for (uint32_t i = 0; i < num_points; i++)
Expand Down
6 changes: 3 additions & 3 deletions src/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Index<T, TagT, LabelT>::Index(Metric m, const size_t dim, const size_t max_point
_data_store =
std::make_unique<diskann::InMemDataStore<T>>((location_t)total_internal_points, _dim, this->_distance);
// init graph store => TODO : graph store should be injected
_graph_store = std::make_unique<diskann::InMemGraphStore>(total_internal_points, _num_frozen_pts);
_graph_store = std::make_unique<diskann::InMemGraphStore>(total_internal_points);
_graph_store->set_start((uint32_t)_max_points);
_graph_store->resize_graph(total_internal_points);
}
Expand Down Expand Up @@ -271,7 +271,7 @@ template <typename T, typename TagT, typename LabelT> size_t Index<T, TagT, Labe
// 4 byte uint32_t)
template <typename T, typename TagT, typename LabelT> size_t Index<T, TagT, LabelT>::save_graph(std::string graph_file)
{
return _graph_store->store(graph_file, _nd + _num_frozen_pts);
return _graph_store->store(graph_file, _nd + _num_frozen_pts, _num_frozen_pts);
}

template <typename T, typename TagT, typename LabelT>
Expand Down Expand Up @@ -686,7 +686,7 @@ size_t Index<T, TagT, LabelT>::load_graph(std::string filename, size_t expected_
{
#endif

return _graph_store->load(filename);
return _graph_store->load(filename, expected_num_points);
}

template <typename T, typename TagT, typename LabelT>
Expand Down
7 changes: 3 additions & 4 deletions src/index_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,12 @@ std::unique_ptr<AbstractDataStore<T>> IndexFactory::construct_datastore(DataStor
return nullptr;
}

std::unique_ptr<AbstractGraphStore> IndexFactory::construct_graphstore(GraphStoreStrategy strategy, size_t size,
size_t frozen_points)
std::unique_ptr<AbstractGraphStore> IndexFactory::construct_graphstore(GraphStoreStrategy strategy, size_t size)
{
switch (strategy)
{
case GraphStoreStrategy::MEMORY:
return std::make_unique<InMemGraphStore>(size, frozen_points);
return std::make_unique<InMemGraphStore>(size);
default:
throw ANNException("Error : Current GraphStoreStratagy is not supported.", -1);
}
Expand All @@ -93,7 +92,7 @@ std::unique_ptr<AbstractIndex> IndexFactory::create_instance()
size_t num_points = _config->max_points;
size_t dim = _config->dimension;
auto data_store = construct_datastore<data_type>(_config->data_strategy, num_points, dim);
auto graph_store = construct_graphstore(_config->graph_strategy, num_points, _config->num_frozen_pts);
auto graph_store = construct_graphstore(_config->graph_strategy, num_points + _config->num_frozen_pts);
return std::make_unique<diskann::Index<data_type, tag_type, label_type>>(*_config, std::move(data_store),
std::move(graph_store));
}
Expand Down

0 comments on commit 3977719

Please sign in to comment.