C API reference¶
Vesin’s C API is defined in the vesin.h header. The main function is
vesin_neighbors(), which runs a neighbors list calculation.
-
int32_t vesin_neighbors(const double (*points)[3], size_t n_points, const double box[3][3], const bool periodic[3], VesinDevice device, void *stream, struct VesinOptions options, struct VesinNeighborList *neighbors, const char **error_message)¶
Compute a neighbor list.
The data is returned in a
VesinNeighborList. For an initial call, theVesinNeighborListshould be zero-initialized (or default-initalized in C++). TheVesinNeighborListcan be re-used across calls to this functions to re-use memory allocations, and once it is no longer needed, users should callvesin_freeto release the corresponding memory.streamis the CUDA stream on which to run the calculation. It must be set toNULLfor CPU devices. For CUDA devices,NULLis the same ascudaStreamLegacy. All the device work (kernel launches, memory copies, …) happens in order on this stream: the inputs only need to be ready on this stream when calling this function, and the outputs are ready on this stream when it returns. This function might still synchronize and block on this stream at some point during the calculation.- Parameters:
points – positions of all points in the system;
n_points – number of elements in the
pointsarraybox – bounding box for the system. If the system is non-periodic, this is ignored. This should contain the three vectors of the bounding box, one vector per row of the matrix.
periodic – is the system using periodic boundary conditions? This
device – device where the
pointsandboxdata is allocated.stream – stream on which to run the calculation
options – options for the calculation
neighbors – non-NULL pointer to
VesinNeighborListthat will be used to store the computed list of neighbors.error_message – Pointer to a
char*that wil be set to the error message if this function fails. This does not need to be freed when no longer needed.
- Returns:
VESIN_SUCCESSif the calculation succeeded,VESIN_OUTPUT_TOO_SMALLif the arrays pre-allocated by the caller are too small (seeVesinOptions::pre_allocated_outputs), andVESIN_FAILUREfor all other errors.
-
void vesin_free(struct VesinNeighborList *neighbors)¶
Free all allocated memory inside a
VesinNeighborList, according the it’sdevice.
-
struct VesinNeighborList¶
The actual neighbor list
This is organized as a list of pairs, where each pair can contain the following data:
indices of the points in the pair;
distance between points in the pair, accounting for periodic boundary conditions;
vector between points in the pair, accounting for periodic boundary conditions;
periodic shift that created the pair. This is only relevant when using periodic boundary conditions, and contains the number of bounding box we need to cross to create the pair. If the positions of the points are
r_iandr_j, the bounding box is described by a matrix of three vectorsH, and the periodic shift isS, the distance vector for a given pair will be given byr_ij = r_j - r_i + S @ H.
Under periodic boundary conditions, two atoms can be part of multiple pairs, each pair having a different periodic shift.
Public Members
-
size_t length¶
Number of pairs in this neighbor list.
When using
VesinOptions::pre_allocated_outputs, this must be set before each call to the number of pairs the pre-allocated arrays can hold.If the call returns
VESIN_OUTPUT_TOO_SMALL, this contains the number of pairs the arrays must be able to hold for a calculation to succeed.
-
VesinDevice device¶
Device used for the data allocations.
-
size_t (*pairs)[2]¶
Array of pairs (storing the indices of the first and second point in the pair), containing
lengthelements.
-
int32_t (*shifts)[3]¶
Array of box shifts, one for each
pair. This is only set ifoptions.return_pairswastrueduring the calculation.
-
double *distances¶
Array of pair distance (i.e. distance between the two points), one for each pair. This is only set if
options.return_distanceswastrueduring the calculation.
-
double (*vectors)[3]¶
Array of pair vector (i.e. vector between the two points), one for each pair. This is only set if
options.return_vectorwastrueduring the calculation.
-
void *opaque = nullptr¶
Private pointer used to hold additional internal data.
-
struct VesinOptions¶
Options for a neighbor list calculation.
Public Members
-
double cutoff¶
Spherical cutoff, only pairs below this cutoff will be included.
-
bool full¶
Should the returned neighbor list be a full list (include both
i -> jandj -> ipairs) or a half list (include onlyi -> j)?
-
bool sorted¶
Should the neighbor list be sorted? If
true, the returned pairs will be sorted by the first point index (i). The order of the second point index (j) and shifts in the list of pairs is unspecified.
-
size_t padded_to¶
Number of outgoing pairs to pad each point to in full lists.
If this is set to zero (default), no padding is applied.
If this is non-zero,
fullmust betrue. The neighbor list will contain exactlyn_points * padded_topairs. Missing pairs are padded with pseudo-pairs(i, SIZE_MAX)with zero shifts/distances/vectors. If any point has more neighbors thanpadded_to, the calculation will fail.
-
VesinAlgorithm algorithm¶
Which algorithm to use for the calculation.
-
double skin¶
Skin size for Verlet caching. A positive value enables caching: vesin builds the cached topology with
cutoff + skinand reuses it until an atom moves more thanskin / 2from the cached reference positions. A negative value sets the skin to an unspecified fraction of the cutoff that is optimal for the current implementation. A value of zero disables caching.
-
int32_t n_threads¶
Number of CPU threads to use. This must be zero or positive. A value of zero means using the
OMP_NUM_THREADSenvironment variable when set to a positive value, or otherwise defaulting to the number of available CPU cores.
-
bool return_shifts¶
Should the returned
VesinNeighborListcontainshifts?
-
bool return_distances¶
Should the returned
VesinNeighborListcontaindistances?
-
bool return_vectors¶
Should the returned
VesinNeighborListcontainvector?
-
bool pre_allocated_outputs¶
Should vesin write the output in arrays pre-allocated by the caller?
If this is
true, all the arrays requested by the options above (pairs, andshifts,distancesorvectors) must be set inVesinNeighborListbefore the call, and itslengthmust be set to the number of pairs these arrays can hold. vesin writes the pairs directly in these arrays. These arrays stay owned by the caller, and are never released by vesin.If the arrays are too small (including when a point has more neighbors than
padded_to),vesin_neighborsreturnsVESIN_OUTPUT_TOO_SMALLand setsVesinNeighborList::lengthto the number of pairs needed. The content of the arrays is then unspecified.If this is
false(the default), vesin allocates the output arrays itself, and re-uses them across calls. The pointers to these arrays must not be modified by the caller.
-
double cutoff¶
-
struct VesinDevice¶
Represents a device on which data can be allocated.
This structure combines the device type (CPU or CUDA) with an optional device index. For CPU allocations,
device_idis always 0. For CUDA allocations,device_idspecifies which GPU to use (e.g., 0, 1, 2).Example usage:
VesinDevice cpu { VesinCPU, 0 }; VesinDevice gpu0 { VesinCUDA, 0 }; VesinDevice gpu1 { VesinCUDA, 1 };
Public Members
-
VesinDeviceKind type¶
Type of the device.
-
int32_t device_id = 0¶
Device index (0 for CPU, GPU index for CUDA)
-
VesinDeviceKind type¶
-
enum VesinDeviceKind¶
Device on which the data can be.
Values:
-
enumerator VesinUnknownDevice¶
Unknown device, used for default initialization and to indicate no allocated data.
-
enumerator VesinCPU¶
CPU device.
-
enumerator VesinCUDA¶
-
enumerator VesinUnknownDevice¶
-
enum VesinAlgorithm¶
Algorithm to use for neighbor list construction.
Values:
-
enumerator VesinAutoAlgorithm¶
Automatically select algorithm based on system characteristics (number of points, size of the box, …), this is the default and recommended option.
-
enumerator VesinBruteForce¶
Brute-force O(n^2) algorithm, this requires minimum image convention in CUDA, and is not available on CPU.
-
enumerator VesinCellList¶
Cell list algorithm with O(n) scaling.
-
enumerator VesinAutoAlgorithm¶