C++ · Refinement

Refinement

#include <usearch/types.hpp>#include <usearch/dense_index.hpp> 3.0.0-rc.1 · cabb6603

Ask your coding agent about this page, or add the docs server to it

curl -s 'https://www.unum.cloud/docs/usearch/cpp/refinement.md' | claude -p "How would this fit my code?"

refine re-runs the neighbor selection over entries already in the graph and answers a usearch_refine_report_t of the nodes re-selected, those whose neighbors changed, the islands found and reconnected, the neighborhoods re-stitched with the links added and dropped, the verdict, and the opening and reachability health checks around the round, so a batch built at a low expansion factor reaches the quality of a slower build afterwards. A verdict other than usearch_refine_improved_k is what settled looks like. On a RAM handle searches may run throughout, on a VRAM one they may not, and insertions never may.

us::dense_index_on<usearch_completion_blocking_k> const receiver = index.on(executor);
for (;;) {
    auto [report, status] = receiver.refine({.refine = {.expansion_factor = 128}});
    if (status.failed() || report.verdict != usearch_refine_improved_k) break;
}

On a loaded NVMe handle memory_limit bounds the bytes a round holds for the one part it rewrites: zero rounds the part in RAM, and a smaller budget streams its rounds from the drives.

A dense_refine_plan_t carries a round across calls: a stop leaves it live, the next call holding it continues where it stopped, phase reports the phase the call stopped in, and refine_plan_finish closes it.

us::dense_refine_plan_t plan;
while (receiver.refine({.progress = {before_the_deadline}, .plan = &plan}) == us::status_code_t::cancelled_k)
    std::printf("stopped in %s at %zu of %zu\n", usearch_phase_name(plan.phase().phase), plan.phase().completed,
                plan.phase().total);
auto [report, status] = receiver.refine_plan_finish(std::move(plan));

A deferred call reads and writes the plan it carries, which stays where it is until the call is final.

us::deferred<usearch_refine_report_t> call = index.deferred(executor).refine({.plan = &plan});
auto [answer, status] = co_await call.wait(loop); // or: call.wait()

§ Resharding

reshard moves the entries of its sources into its targets, keeping their links rather than re-inserting their entries, then runs a round on every target that took entries; the receiver supplies the executor. Several sources gather into the one target among them, one source cuts into several empty targets, and one handle alone recuts its own shards in place under shard_capacity. Every handle stays yours: a source the targets do not name ends empty and live. Readers of a RAM target may keep searching throughout, while a VRAM one admits no overlapping search.

usearch_dense_index_t const gathered[3] = {index.handle(), first_shard.handle(), second_shard.handle()};
us::status_t const gather = index.on(executor).reshard(gathered, {gathered, 1}).status;
usearch_dense_index_t const source[1] = {index.handle()}, parts[2] = {left.handle(), right.handle()};
us::status_t const cut = index.on(executor).reshard(source, parts).status; // two empty targets take a part each

A stopped reshard cuts only its closing rounds, every entry already moved, and reports status_code_t::cancelled_k; a dense_reshard_plan_t continues it as a refine plan does, and reshard_plan_finish closes it.

API

Types

refine_configThe knobs of refine and the plan_type_ it opens or continues.
types.hpp:807 ↗
template <typename plan_type_>
struct refine_config

Fields

refineusearch_refine_options_tThe walk breadth and the bytes a loaded NVMe handle holds per part; zero derives each.
progressprogress_tHears every barrier of every phase, and may cancel the round there.
statsusearch_traversal_stats_t *The traversal telemetry; nullptr skips reporting.
planplan_type_ *The plan a cancel leaves live; nullptr runs one round to its end.
reshard_configThe knobs of a modality's reshard, whose handles recut nothing in place, and the plan_type_ it opens or continues.
types.hpp:825 ↗
template <typename plan_type_>
struct reshard_config

Fields

refineusearch_refine_options_tThe walk breadth and the bytes a loaded NVMe handle holds per part; zero derives each.
seedstd::uint64_tPicks the part roots of a split: equal seeds over an unchanged source split equally.
progressprogress_tHears every barrier of the closing rounds, and may cancel them there, never a move.
statsusearch_traversal_stats_t *The traversal telemetry; nullptr skips reporting.
planplan_type_ *The plan a cancel leaves live; nullptr moves and runs to the end.
dense_reshard_config_tThe placement reshard recuts toward, the knobs of its closing rounds and its plan.
dense_index.hpp:144 ↗
struct dense_reshard_config_t

Fields

ramusearch_ram_options_t const *The placement of a RAM handle recut in place; nullptr keeps it.
vramusearch_vram_options_t const *The placement of a VRAM handle recut in place; nullptr keeps it.
nvmeusearch_nvme_options_t const *The placement of an NVMe handle recut in place; nullptr keeps it.
refineusearch_refine_options_tThe walk breadth and the bytes a loaded NVMe handle holds per part; zero derives each.
seedstd::uint64_tPicks the part roots of a split: equal seeds over an unchanged source split equally.
progressprogress_tHears every barrier of the closing rounds, and may cancel them there, never a move.
statsusearch_traversal_stats_t *The traversal telemetry; nullptr skips reporting.
plandense_reshard_plan_t *The plan a cancel leaves live; nullptr moves and runs to the end.