---
name: "hwlocality_distances_get"
section: "3"
platform: "linux"
package: "hwloc"
version: "2.15.0-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/hwlocality_distances_get"
---

# hwlocality\_distances\_get(3)

Retrieve distances between objects

## NAME

hwlocality\_distances\_get - Retrieve distances between objects

## SYNOPSIS

### Data Structures

struct **hwloc\_distances\_s**\

### Enumerations

enum **hwloc\_distances\_kind\_e** { **HWLOC\_DISTANCES\_KIND\_FROM\_OS**, **HWLOC\_DISTANCES\_KIND\_FROM\_USER**, **HWLOC\_DISTANCES\_KIND\_MEANS\_LATENCY**, **HWLOC\_DISTANCES\_KIND\_MEANS\_BANDWIDTH**, **HWLOC\_DISTANCES\_KIND\_HETEROGENEOUS\_TYPES** }\
 enum **hwloc\_distances\_transform\_e** { **HWLOC\_DISTANCES\_TRANSFORM\_REMOVE\_NULL**, **HWLOC\_DISTANCES\_TRANSFORM\_LINKS**, **HWLOC\_DISTANCES\_TRANSFORM\_MERGE\_SWITCH\_PORTS**, **HWLOC\_DISTANCES\_TRANSFORM\_TRANSITIVE\_CLOSURE** }\

### Functions

int **hwloc\_distances\_get** (**hwloc\_topology\_t** topology, unsigned \*nr, struct **hwloc\_distances\_s** \*\*distances, unsigned long kind, unsigned long flags)\
 int **hwloc\_distances\_get\_by\_depth** (**hwloc\_topology\_t** topology, int depth, unsigned \*nr, struct **hwloc\_distances\_s** \*\*distances, unsigned long kind, unsigned long flags)\
 int **hwloc\_distances\_get\_by\_type** (**hwloc\_topology\_t** topology, **hwloc\_obj\_type\_t** type, unsigned \*nr, struct **hwloc\_distances\_s** \*\*distances, unsigned long kind, unsigned long flags)\
 int **hwloc\_distances\_get\_by\_name** (**hwloc\_topology\_t** topology, const char \*name, unsigned \*nr, struct **hwloc\_distances\_s** \*\*distances, unsigned long flags)\
 const char \* **hwloc\_distances\_get\_name** (**hwloc\_topology\_t** topology, struct **hwloc\_distances\_s** \*distances)\
 void **hwloc\_distances\_release** (**hwloc\_topology\_t** topology, struct **hwloc\_distances\_s** \*distances)\
 int **hwloc\_distances\_transform** (**hwloc\_topology\_t** topology, struct **hwloc\_distances\_s** \*distances, enum **hwloc\_distances\_transform\_e** transform, void \*transform\_attr, unsigned long flags)\

## Detailed Description

## Enumeration Type Documentation

### enum hwloc\_distances\_kind\_e

Kinds of distance matrices. The kind attribute of struct **hwloc\_distances\_s** is a OR'ed set of kinds.

Each distance matrix may have only one kind among HWLOC\_DISTANCES\_KIND\_FROM\_\* specifying where distance information comes from, and one kind among HWLOC\_DISTANCES\_KIND\_MEANS\_\* specifying whether values are latencies or bandwidths.

**Enumerator**

- ***HWLOC\_DISTANCES\_KIND\_FROM\_OS***

  These distances were obtained from the operating system or hardware.

- ***HWLOC\_DISTANCES\_KIND\_FROM\_USER***

  These distances were provided by the user.

- ***HWLOC\_DISTANCES\_KIND\_MEANS\_LATENCY***

  Distance values are similar to latencies between objects. Values are smaller for closer objects, hence minimal on the diagonal of the matrix (distance between an object and itself). It could also be the number of network hops between objects, etc.

- ***HWLOC\_DISTANCES\_KIND\_MEANS\_BANDWIDTH***

  Distance values are similar to bandwidths between objects. Values are higher for closer objects, hence maximal on the diagonal of the matrix (distance between an object and itself). Such values are currently ignored for distance-based grouping.

- ***HWLOC\_DISTANCES\_KIND\_HETEROGENEOUS\_TYPES***

  This distances structure covers objects of different types. This may apply to the "NVLinkBandwidth" structure in presence of a NVSwitch or POWER processor NVLink port.

### enum hwloc\_distances\_transform\_e

Transformations of distances structures.

**Enumerator**

- ***HWLOC\_DISTANCES\_TRANSFORM\_REMOVE\_NULL***

  Remove NULL objects from the distances structure. Every object that was replaced with NULL in the objs array is removed and the values array is updated accordingly.

At least 2 objects must remain, otherwise **hwloc\_distances\_transform()** will return -1 with errno set to EINVAL.

kind will be updated with or without **HWLOC\_DISTANCES\_KIND\_HETEROGENEOUS\_TYPES** according to the remaining objects.

- ***HWLOC\_DISTANCES\_TRANSFORM\_LINKS***

  Replace bandwidth values with a number of links. Usually all values will be either 0 (no link) or 1 (one link). However some matrices could get larger values if some pairs of peers are connected by different numbers of links.

Values on the diagonal are set to 0.

This transformation only applies to bandwidth matrices.

- ***HWLOC\_DISTANCES\_TRANSFORM\_MERGE\_SWITCH\_PORTS***

  Merge switches with multiple ports into a single object. This currently only applies to NVSwitches where GPUs seem connected to different switch ports. Switch ports must be objects with subtype "NVSwitch" as in the NVLinkBandwidth matrix.

This transformation will replace all ports with only the first one, now connected to all GPUs. Other ports are removed by applying **HWLOC\_DISTANCES\_TRANSFORM\_REMOVE\_NULL** internally.

- ***HWLOC\_DISTANCES\_TRANSFORM\_TRANSITIVE\_CLOSURE***

  Apply a transitive closure to the matrix to connect objects across switches. All pairs of GPUs will be reported as directly connected instead GPUs being only connected to switches.

Switch ports must be objects with subtype "NVSwitch" as in the NVLinkBandwidth matrix.

## Function Documentation

### int hwloc\_distances\_get (hwloc\_topology\_t topology, unsigned \* nr, struct hwloc\_distances\_s \*\* distances, unsigned long kind, unsigned long flags)

Retrieve distance matrices. Retrieve distance matrices from the topology into the distances array.

flags is currently unused, should be 0.

kind serves as a filter. If 0, all distance matrices are returned. If it contains some HWLOC\_DISTANCES\_KIND\_FROM\_\*, only distance matrices whose kind matches one of these are returned. If it contains some HWLOC\_DISTANCES\_KIND\_MEANS\_\*, only distance matrices whose kind matches one of these are returned.

On input, nr points to the number of distance matrices that may be stored in distances. On output, nr points to the number of distance matrices that were actually found, even if some of them couldn't be stored in distances. Distance matrices that couldn't be stored are ignored, but the function still returns success (0). The caller may find out by comparing the value pointed by nr before and after the function call.

Each distance matrix returned in the distances array should be released by the caller using **hwloc\_distances\_release()**.

**Returns**

0 on success, -1 on error.

### int hwloc\_distances\_get\_by\_depth (hwloc\_topology\_t topology, int depth, unsigned \* nr, struct hwloc\_distances\_s \*\* distances, unsigned long kind, unsigned long flags)

Retrieve distance matrices for object at a specific depth in the topology. Identical to **hwloc\_distances\_get()** with the additional depth filter.

**Returns**

0 on success, -1 on error.

### int hwloc\_distances\_get\_by\_name (hwloc\_topology\_t topology, const char \* name, unsigned \* nr, struct hwloc\_distances\_s \*\* distances, unsigned long flags)

Retrieve a distance matrix with the given name. Usually only one distances structure may match a given name.

The name of the most common structure is "NUMALatency". Others include "XGMIBandwidth", "XGMIHops", "XeLinkBandwidth", and "NVLinkBandwidth".

**Returns**

0 on success, -1 on error.

### int hwloc\_distances\_get\_by\_type (hwloc\_topology\_t topology, hwloc\_obj\_type\_t type, unsigned \* nr, struct hwloc\_distances\_s \*\* distances, unsigned long kind, unsigned long flags)

Retrieve distance matrices for object of a specific type. Identical to **hwloc\_distances\_get()** with the additional type filter.

**Returns**

0 on success, -1 on error.

### const char \* hwloc\_distances\_get\_name (hwloc\_topology\_t topology, struct hwloc\_distances\_s \* distances)

Get a description of what a distances structure contains. For instance "NUMALatency" for hardware-provided NUMA distances (ACPI SLIT), or NULL if unknown.

**Returns**

the constant string with the name of the distance structure.

**Note**

The returned name should not be freed by the caller, it belongs to the hwloc library.

### void hwloc\_distances\_release (hwloc\_topology\_t topology, struct hwloc\_distances\_s \* distances)

Release a distance matrix structure previously returned by **hwloc\_distances\_get()**.

**Note**

This function is not required if the structure is removed with **hwloc\_distances\_release\_remove()**.

### int hwloc\_distances\_transform (hwloc\_topology\_t topology, struct hwloc\_distances\_s \* distances, enum hwloc\_distances\_transform\_e transform, void \* transform\_attr, unsigned long flags)

Apply a transformation to a distances structure. Modify a distances structure that was previously obtained with **hwloc\_distances\_get()** or one of its variants.

This modifies the local copy of the distances structures but does not modify the distances information stored inside the topology (retrieved by another call to **hwloc\_distances\_get()** or exported to XML). To do so, one should add a new distances structure with same name, kind, objects and values (see **Add distances between objects**) and then remove this old one with **hwloc\_distances\_release\_remove()**.

transform must be one of the transformations listed in **hwloc\_distances\_transform\_e**.

These transformations may modify the contents of the objs or values arrays.

transform\_attr must be NULL for now.

flags must be 0 for now.

**Returns**

0 on success, -1 on error for instance if flags are invalid.

**Note**

Objects in distances array objs may be directly modified in place without using **hwloc\_distances\_transform()**. One may use **hwloc\_get\_obj\_with\_same\_locality()** to easily convert between similar objects of different types.

## Author

Generated automatically by Doxygen for Hardware Locality (hwloc) from the source code.
