XVector
xvec_context.h File Reference

Execution context management for the XVector library. More...

#include "xvec_types.h"
Include dependency graph for xvec_context.h:

Go to the source code of this file.

Functions

xvecStatus xvecGetNumDevices (uint32_t *outNumDevices)
 Get the number of available devices. More...
 
xvecStatus xvecCreateContext (xvecExecutionContextTag **outContext, uint32_t deviceId)
 Create an execution context. More...
 
xvecStatus xvecDestroyContext (xvecExecutionContextTag *context)
 Release a context. More...
 
xvecStatus xvecSetContextProperty (xvecExecutionContextTag *context, const char *key, const char *value)
 Set a property on the execution context. More...
 
xvecStatus xvecGetContextProperty (xvecExecutionContextTag *context, const char *key, char *value, size_t valueSize, size_t *outRequiredSize)
 Get a property from the execution context. More...
 
xvecStatus xvecResetVpeContext (xvecExecutionContextTag *context)
 Reset the VPE (Vector Process Engine) SRAM allocator state. More...
 

Detailed Description

Execution context management for the XVector library.

This header provides the API for creating, configuring, and destroying execution contexts. An execution context encapsulates the device handle, embedded production kernel, and runtime properties required for XVector operations.

Definition in file xvec_context.h.

Function Documentation

◆ xvecCreateContext()

xvecStatus xvecCreateContext ( xvecExecutionContextTag **  outContext,
uint32_t  deviceId 
)

Create an execution context.

Initializes a new execution context associated with the specified device using the production kernel embedded in libxvector. The created context manages resources required for buffer allocations and kernel execution.

This call does not automatically apply environment settings from xvec::host::ConfigManager. Pass the device ID explicitly and use xvecSetContextProperty() to configure execution properties.

The returned context handle (xvecExecutionContextTag) must be released using xvecDestroyContext when it is no longer needed.

Parameters
[out]outContextPointer to a variable that will receive the created context handle. Must not be NULL.
[in]deviceIdIdentifier of the target device on which the context will be created.
Returns
xvecStatus

◆ xvecDestroyContext()

xvecStatus xvecDestroyContext ( xvecExecutionContextTag *  context)

Release a context.

Releases the context and frees associated resources.

Index resource lifetime
Destroy all index and buffer handles before destroying the context. For KNN Exact, IVF-Flat, IVF-RaBitQ, and HNSW-Flat, destroying an index releases its handle and host data, but its device memory remains allocated until this call. Repeatedly loading and destroying indexes in one context therefore retains device memory. The same rules apply to C++ index wrappers: destroy them before their context, whose destruction releases the device memory.
Parameters
[in]contextPointer previously returned by xvecCreateContext. Must not be NULL.
Returns
xvecStatus

◆ xvecGetContextProperty()

xvecStatus xvecGetContextProperty ( xvecExecutionContextTag *  context,
const char *  key,
char *  value,
size_t  valueSize,
size_t *  outRequiredSize 
)

Get a property from the execution context.

Retrieves a named property value from the execution context as a string. If the property exists, its value will be copied to the provided buffer. The buffer must be large enough to hold the property value and null terminator.

To query the required buffer size, pass NULL for value and 0 for valueSize. The required size (including null terminator) will be written to outRequiredSize.

Parameters
[in]contextExecution context handle. Must not be NULL.
[in]keyProperty name. Must be a null-terminated string. Must not be NULL.
[out]valueBuffer to receive property value. May be NULL to query required size.
[in]valueSizeSize of the value buffer in bytes.
[out]outRequiredSizeOptional. If not NULL, receives the required buffer size including null terminator.
Returns
xvecStatus
  • XVEC_SUCCESS: Property retrieved successfully (or size query succeeded when value is NULL)
  • XVEC_ERROR_INVALID_PARAMETER: Invalid parameters (NULL context or key)
  • XVEC_ERROR_ENTRY_NOT_FOUND: Property with the given key does not exist
  • XVEC_ERROR_BUFFER_TOO_SMALL: Provided buffer is too small for the property value
  • XVEC_ERROR_UNKNOWN: Other errors

◆ xvecGetNumDevices()

xvecStatus xvecGetNumDevices ( uint32_t *  outNumDevices)

Get the number of available devices.

Returns the number of XCENA devices currently available on the system. This function can be called before creating any context.

Parameters
[out]outNumDevicesPointer to a variable that will receive the device count. Must not be NULL.
Returns
xvecStatus
  • XVEC_SUCCESS: Device count retrieved successfully
  • XVEC_ERROR_INVALID_PARAMETER: outNumDevices is NULL

◆ xvecResetVpeContext()

xvecStatus xvecResetVpeContext ( xvecExecutionContextTag *  context)

Reset the VPE (Vector Process Engine) SRAM allocator state.

Clears the SRAM buffer allocation state across all subsystems on the device. This must be called before executing kernels that use VpeContext to ensure a clean allocator state.

Sized like a search's Job: the "numSub" context property if set, else the device's Sub count.

Parameters
[in]contextExecution context handle. Must not be NULL.
Returns
xvecStatus
  • XVEC_SUCCESS: VPE state reset successfully
  • XVEC_ERROR_INVALID_PARAMETER: context is NULL
  • XVEC_ERROR_PXL_JOB_CREATE_FAILED: the device has fewer free Subs than the reset needs (another process or context holds them)
  • XVEC_ERROR_PXL_INVALID_MAP: PXL refused to build the reset Map
  • XVEC_ERROR_PXL_EXECUTION_FAILED: the reset kernel failed to launch
  • XVEC_ERROR_PXL_SYNCHRONIZATION_FAILED: the reset kernel did not complete
Warning
This function must NOT be called while device kernels are actively using VpeContext buffers. Doing so will corrupt allocator state.
xvecExecutionContextTag* context;
xvecCreateContext(&context, 0);
// Reset VPE before running kernels
xvecStatus xvecResetVpeContext(xvecExecutionContextTag *context)
Reset the VPE (Vector Process Engine) SRAM allocator state.
xvecStatus xvecCreateContext(xvecExecutionContextTag **outContext, uint32_t deviceId)
Create an execution context.

◆ xvecSetContextProperty()

xvecStatus xvecSetContextProperty ( xvecExecutionContextTag *  context,
const char *  key,
const char *  value 
)

Set a property on the execution context.

Sets a named property with a string value on the execution context. The caller is responsible for converting the value to the appropriate type when reading the property. Common properties include:

  • "numSub": Number of Subs to build the device Job from. Optional: unset means use however many the device reports. A larger value is clamped down, since PXL rejects an over-request instead of granting a smaller Job.
  • "taskCount": Task count for parallel execution. Optional: if the property is not set, each search kernel uses its own per-sub default. If set, the value is respected as-is (a value below numSub is raised to numSub for a device buffer-sizing invariant). Do not set this to request the default — leave it unset; an explicit value is always treated as a deliberate choice.
  • "ivfFlatL2NormTrick": See Optional IVF-Flat L2 optimization below.
  • "deviceLogLevel": Device log level ("0"=NONE, "1"=ERROR, "2"=WARN, "3"=INFO, "4"=DEBUG, "5"=TRACE)

Optional IVF-Flat L2 optimization
Set xvecSetContextProperty(ctx, "ivfFlatL2NormTrick", "1") to enable this optimization for IVF-Flat L2 searches in QUERY_FIXED mode. It is disabled by default. Accepted values are "1"/"true" and "0"/"false"; other values return XVEC_ERROR_INVALID_PARAMETER. It has no effect on inner-product searches or CLUSTER_LIST_FIXED mode.

The first enabled search caches squared vector norms (4 bytes of device memory per indexed vector); later enabled searches compute only query norms. Disabling the option stops norm processing but retains the cache until context destruction. Cache allocation failure during search returns XVEC_ERROR_OUT_OF_MEMORY_PXL. Default searches do not allocate this cache.

Close vectors use direct differences to limit cancellation. Scores can differ due to floating-point rounding, and equal-score ties may change. Benchmark representative data before enabling: performance depends on the workload, and highly similar vectors can make this optimization slower.

Parameters
[in]contextExecution context handle. Must not be NULL.
[in]keyProperty name. Must be a null-terminated string. Must not be NULL.
[in]valueProperty value as a string. Must be a null-terminated string. Must not be NULL.
Returns
xvecStatus