注意

RAFT 中的向量搜索和聚类算法正在迁移到一个专门用于向量搜索的新库 cuVS 中。在迁移过程中,我们将继续支持 RAFT 中的向量搜索算法,但在 RAPIDS 24.06(6 月)版本之后将不再更新。我们计划在 RAPIDS 24.10(10 月)版本之前完成迁移,并在 24.12(12 月)版本中将它们完全从 RAFT 中移除。

mdspan: 多维非拥有视图#

#include <raft/core/mdspan.hpp>

template<typename ElementType, typename Extents, typename LayoutPolicy = layout_c_contiguous, typename AccessorPolicy = std::experimental::default_accessor<ElementType>>
using raft::mdspan = std::experimental::mdspan<ElementType, Extents, LayoutPolicy, AccessorPolicy>#
template<typename ElementType, typename IndexType = std::uint32_t, typename LayoutPolicy = layout_c_contiguous, bool is_host_accessible = false, bool is_device_accessible = true, size_t... Extents>
auto raft::make_mdspan(ElementType *ptr, extents<IndexType, Extents...> exts)#

创建一个 raft::mdspan 对象。

模板参数:
  • ElementType – 矩阵元素的数据类型

  • IndexType – 范围的索引类型

  • LayoutPolicy – 用于步长和布局顺序的策略

  • is_host_accessible – 数据是否可在主机上访问

  • is_device_accessible – 数据是否可在设备上访问

参数:
  • ptr – 指向数据的指针

  • exts – 数组的维度(一系列整数)

返回:

raft::mdspan

template<typename IndexType, typename ...Extents, typename = ensure_integral_extents<Extents...>>
auto raft::make_extents(Extents... exts)#

创建 raft::extents 以指定维度。

模板参数:
  • IndexType – 范围中每个维度的类型

  • Extents – 维度(一系列整数)

参数:

exts – 所需的维度

返回:

raft::extents

template<typename Extents, typename Strides>
auto raft::make_strided_layout(Extents const &extents, Strides const &strides)#

从 extents 和 strides 创建一个 layout_stride 映射。

参数:
  • extents[in] 布局的维度

  • strides[in] 布局中元素之间的步长

返回:

raft::layout_stride::mapping<Extents>

template<typename Idx, typename IndexType, typename LayoutPolicy, size_t... Exts>
RAFT_INLINE_FUNCTION auto unravel_index(Idx idx, extents<IndexType, Exts...> shape, LayoutPolicy const &layout)#

将线性索引转换为坐标。类似于 numpy 的 unravel_index。

auto m = make_host_matrix<float>(7, 6);
auto m_v = m.view();
auto coord = unravel_index(2, m.extents(), typename decltype(m)::layout_type{});
std::apply(m_v, coord) = 2;
参数:
  • idx – 线性索引。

  • shape – 要使用的数组的形状。

  • layout – 在当前实现中必须是 layout_c_contiguous(行优先)。

返回:

一个表示坐标的 std::tuple。

template<typename Extents, typename Strides>
bool is_c_contiguous(Extents const &extents, Strides const &strides)#

步长是否意味着 c-contiguous(行优先)布局。

template<typename Extents, typename Strides>
bool is_f_contiguous(Extents const &extents, Strides const &strides)#

步长是否意味着 f-contiguous(列优先)布局。

template<class ElementType, class Extents, class Layout, class Accessor>
auto make_const_mdspan(mdspan<ElementType, Extents, Layout, Accessor> mds)#

创建一个带有 const 元素类型的给定 mdspan 的副本。

模板参数:
  • ElementType – mdspan 元素的 const 限定数据类型

  • Extents – 用于维度的 raft::extents

  • Layout – 用于步长和布局顺序的策略

  • Accessor – 输入和输出的访问器策略

参数:

mdsraft::mdspan 对象

返回:

raft::mdspan

设备相关术语#

#include <raft/core/device_mdspan.hpp>

template<typename ElementType, typename Extents, typename LayoutPolicy = layout_c_contiguous, typename AccessorPolicy = std::experimental::default_accessor<ElementType>>
using raft::device_mdspan = mdspan<ElementType, Extents, LayoutPolicy, device_accessor<AccessorPolicy>>#

带有设备标签的 std::experimental::mdspan,用于避免访问错误的内存位置。

template<typename T, bool B>
struct is_device_mdspan : public std::false_type#
template<typename T>
using raft::is_device_mdspan_t = is_device_mdspan<T, is_mdspan_v<T>>#

\brief 布尔值,用于确定模板类型 T 是 raft::device_mdspan 还是派生类型

template<typename T>
using raft::is_input_device_mdspan_t = is_device_mdspan<T, is_input_mdspan_v<T>>#
template<typename T>
using raft::is_output_device_mdspan_t = is_device_mdspan<T, is_output_mdspan_v<T>>#
template<typename ...Tn>
using raft::enable_if_device_mdspan = std::enable_if_t<is_device_mdspan_v<Tn...>>#
template<typename ...Tn>
using raft::enable_if_input_device_mdspan = std::enable_if_t<is_input_device_mdspan_v<Tn...>>#
template<typename ...Tn>
using raft::enable_if_output_device_mdspan = std::enable_if_t<is_output_device_mdspan_v<Tn...>>#
template<typename ElementType, typename IndexType = std::uint32_t, typename LayoutPolicy = layout_c_contiguous>
using raft::device_matrix_view = device_mdspan<ElementType, matrix_extent<IndexType>, LayoutPolicy>#

c-contiguous(行优先)设备矩阵视图的简写。

模板参数:
  • ElementType – 矩阵元素的数据类型

  • IndexType – 范围的索引类型

  • LayoutPolicy – 用于步长和布局顺序的策略

template<typename ElementType, typename IndexType = std::uint32_t, typename LayoutPolicy = layout_c_contiguous>
using raft::device_vector_view = device_mdspan<ElementType, vector_extent<IndexType>, LayoutPolicy>#

1 维设备 mdspan 的简写。

模板参数:
  • ElementType – 向量元素的数据类型

  • IndexType – 范围的索引类型

  • LayoutPolicy – 用于步长和布局顺序的策略

template<typename ElementType, typename IndexType = std::uint32_t>
using raft::device_scalar_view = device_mdspan<ElementType, scalar_extent<IndexType>>#

0 维主机 mdspan(标量)的简写。

模板参数:
  • ElementType – 标量元素的数据类型

  • IndexType – 范围的索引类型

设备工厂函数#

#include <raft/core/device_mdspan.hpp>

template<typename ElementType, typename IndexType = std::uint32_t, typename LayoutPolicy = layout_c_contiguous>
auto constexpr raft::make_device_matrix_view(ElementType *ptr, IndexType n_rows, IndexType n_cols)#

为设备指针创建一个 2 维 c-contiguous(行优先)mdspan 实例。期望给定的布局策略与底层指针的布局匹配。

模板参数:
  • ElementType – 矩阵元素的数据类型

  • LayoutPolicy – 用于步长和布局顺序的策略

  • IndexType – 范围的索引类型

参数:
  • ptr[in] 要包装的设备指针

  • n_rows[in] 指针中的行数

  • n_cols[in] 指针中的列数

template<typename ElementType, typename IndexType, typename LayoutPolicy = layout_c_contiguous>
auto constexpr raft::make_device_vector_view(ElementType *ptr, IndexType n)#

为设备指针创建一个 1 维 mdspan 实例。

模板参数:
  • ElementType – 向量元素的数据类型

  • IndexType – 范围的索引类型

  • LayoutPolicy – 用于步长和布局顺序的策略

参数:
  • ptr[in] 要包装的设备指针

  • n[in] 指针中的元素数量

返回:

raft::device_vector_view

template<typename ElementType, typename IndexType = std::uint32_t>
auto constexpr raft::make_device_scalar_view(ElementType *ptr)#

为设备值创建一个 0 维(标量)mdspan 实例。

模板参数:
  • ElementType – 矩阵元素的数据类型

  • IndexType – 范围的索引类型

参数:

ptr[in] 要包装的设备指针

托管相关术语#

#include <raft/core/managed_mdspan.hpp>

template<typename ElementType, typename Extents, typename LayoutPolicy = layout_c_contiguous, typename AccessorPolicy = std::experimental::default_accessor<ElementType>>
using raft::managed_mdspan = mdspan<ElementType, Extents, LayoutPolicy, managed_accessor<AccessorPolicy>>#

带有托管标签的 std::experimental::mdspan,用于指示主机/设备可访问性

template<typename T, bool B>
struct is_managed_mdspan : public std::false_type#
template<typename T>
using raft::is_managed_mdspan_t = is_managed_mdspan<T, is_mdspan_v<T>>#

\brief 布尔值,用于确定模板类型 T 是 raft::managed_mdspan 还是派生类型

template<typename T>
using raft::is_input_managed_mdspan_t = is_managed_mdspan<T, is_input_mdspan_v<T>>#
template<typename T>
using raft::is_output_managed_mdspan_t = is_managed_mdspan<T, is_output_mdspan_v<T>>#
template<typename ...Tn>
using raft::enable_if_managed_mdspan = std::enable_if_t<is_managed_mdspan_v<Tn...>>#
template<typename ...Tn>
using raft::enable_if_input_managed_mdspan = std::enable_if_t<is_input_managed_mdspan_v<Tn...>>#
template<typename ...Tn>
using raft::enable_if_output_managed_mdspan = std::enable_if_t<is_output_managed_mdspan_v<Tn...>>#

托管工厂函数#

#include <raft/core/managed_mdspan.hpp>

template<typename ElementType, typename IndexType = std::uint32_t, typename LayoutPolicy = layout_c_contiguous, size_t... Extents>
auto constexpr raft::make_managed_mdspan(ElementType *ptr, extents<IndexType, Extents...> exts)#

创建一个 raft::managed_mdspan 对象。

模板参数:
  • ElementType – 矩阵元素的数据类型

  • IndexType – 范围的索引类型

  • LayoutPolicy – 用于步长和布局顺序的策略

参数:
  • ptr – 指向数据的指针

  • exts – 数组的维度(一系列整数)

返回:

raft::managed_mdspan

主机相关术语#

#include <raft/core/host_mdspan.hpp>

template<typename ElementType, typename Extents, typename LayoutPolicy = layout_c_contiguous, typename AccessorPolicy = std::experimental::default_accessor<ElementType>>
using raft::host_mdspan = mdspan<ElementType, Extents, LayoutPolicy, host_accessor<AccessorPolicy>>#

带有主机标签的 std::experimental::mdspan,用于避免访问错误的内存位置。

template<typename T, bool B>
struct is_host_mdspan : public std::false_type#
template<typename T>
using raft::is_host_mdspan_t = is_host_mdspan<T, is_mdspan_v<T>>#

\brief 布尔值,用于确定模板类型 T 是 raft::host_mdspan 还是派生类型

template<typename T>
using raft::is_input_host_mdspan_t = is_host_mdspan<T, is_input_mdspan_v<T>>#
template<typename T>
using raft::is_output_host_mdspan_t = is_host_mdspan<T, is_output_mdspan_v<T>>#
template<typename ...Tn>
using raft::enable_if_host_mdspan = std::enable_if_t<is_input_mdspan_v<Tn...>>#
template<typename ...Tn>
using raft::enable_if_input_host_mdspan = std::enable_if_t<is_input_host_mdspan_v<Tn...>>#
template<typename ...Tn>
using raft::enable_if_output_host_mdspan = std::enable_if_t<is_output_host_mdspan_v<Tn...>>#
template<typename ElementType, typename IndexType = std::uint32_t, typename LayoutPolicy = layout_c_contiguous>
using raft::host_matrix_view = host_mdspan<ElementType, matrix_extent<IndexType>, LayoutPolicy>#

c-contiguous 主机矩阵视图的简写。

模板参数:
  • ElementType – 矩阵元素的数据类型

  • IndexType – 范围的索引类型

  • LayoutPolicy – 用于步长和布局顺序的策略

template<typename ElementType, typename IndexType = std::uint32_t, typename LayoutPolicy = layout_c_contiguous>
using raft::host_vector_view = host_mdspan<ElementType, vector_extent<IndexType>, LayoutPolicy>#

1维主机 mdspan 的简写。

模板参数:
  • ElementType – 向量元素的数据类型

  • IndexType – 范围的索引类型

template<typename ElementType, typename IndexType = std::uint32_t>
using raft::host_scalar_view = host_mdspan<ElementType, scalar_extent<IndexType>>#

0 维主机 mdspan(标量)的简写。

模板参数:
  • ElementType – 标量元素的数据类型

  • IndexType – 范围的索引类型

主机工厂#

#include <raft/core/host_mdspan.hpp>

template<typename ElementType, typename IndexType = std::uint32_t, typename LayoutPolicy = layout_c_contiguous>
auto constexpr raft::make_host_matrix_view(ElementType *ptr, IndexType n_rows, IndexType n_cols)#

为主机指针创建一个2维c-contiguous mdspan 实例。期望给定的布局策略与底层指针的布局相匹配。

模板参数:
  • ElementType – 矩阵元素的数据类型

  • IndexType – 范围的索引类型

  • LayoutPolicy – 用于步长和布局顺序的策略

参数:
  • **ptr** – **[in]** 要封装的主机上的指针

  • n_rows[in] 指针中的行数

  • n_cols[in] 指针中的列数

template<typename ElementType, typename IndexType = std::uint32_t, typename LayoutPolicy = layout_c_contiguous>
auto constexpr raft::make_host_vector_view(ElementType *ptr, IndexType n)#

为主机指针创建一个1维 mdspan 实例。

模板参数:
  • ElementType – 向量元素的数据类型

  • IndexType – 范围的索引类型

参数:
  • **ptr** – **[in]** 要封装的主机上的指针

  • n[in] 指针中的元素数量

返回:

raft::host_vector_view

template<typename ElementType, typename IndexType = std::uint32_t>
auto constexpr raft::make_host_scalar_view(ElementType *ptr)#

为主机值创建一个0维(标量)mdspan 实例。

模板参数:
  • ElementType – 矩阵元素的数据类型

  • IndexType – 范围的索引类型

参数:

ptr[in] 要包装的设备指针

固定内存词汇#

#include <raft/core/pinned_mdspan.hpp>

template<typename ElementType, typename Extents, typename LayoutPolicy = layout_c_contiguous, typename AccessorPolicy = std::experimental::default_accessor<ElementType>>
using raft::pinned_mdspan = mdspan<ElementType, Extents, LayoutPolicy, pinned_accessor<AccessorPolicy>>#

带有固定内存标记的 std::experimental::mdspan,用于指示主机/设备可访问性

template<typename ElementType, typename IndexType = std::uint32_t, typename LayoutPolicy = layout_c_contiguous>
using raft::pinned_matrix_view = pinned_mdspan<ElementType, matrix_extent<IndexType>, LayoutPolicy>#

c-contiguous 固定内存矩阵视图的简写。

模板参数:
  • ElementType – 矩阵元素的数据类型

  • IndexType – 范围的索引类型

  • LayoutPolicy – 用于步长和布局顺序的策略

template<typename ElementType, typename IndexType = std::uint32_t, typename LayoutPolicy = layout_c_contiguous>
using raft::pinned_vector_view = pinned_mdspan<ElementType, vector_extent<IndexType>, LayoutPolicy>#

1维固定内存 mdspan 的简写。

模板参数:
  • ElementType – 向量元素的数据类型

  • IndexType – 范围的索引类型

  • LayoutPolicy – 用于步长和布局顺序的策略

template<typename ElementType, typename IndexType = std::uint32_t>
using raft::pinned_scalar_view = pinned_mdspan<ElementType, scalar_extent<IndexType>>#

0维固定内存 mdspan(标量)的简写。

模板参数:
  • ElementType – 标量元素的数据类型

  • IndexType – 范围的索引类型

固定内存工厂#

#include <raft/core/pinned_mdspan.hpp>

template<typename ElementType, typename IndexType = std::uint32_t, typename LayoutPolicy = layout_c_contiguous>
auto constexpr raft::make_pinned_matrix_view(ElementType *ptr, IndexType n_rows, IndexType n_cols)#

为固定内存指针创建一个2维c-contiguous mdspan 实例。期望给定的布局策略与底层指针的布局相匹配。

模板参数:
  • ElementType – 矩阵元素的数据类型

  • LayoutPolicy – 用于步长和布局顺序的策略

  • IndexType – 范围的索引类型

参数:
  • **ptr** – **[in]** 要封装的固定内存指针

  • n_rows[in] 指针中的行数

  • n_cols[in] 指针中的列数

警告

doxygen函数:无法在目录 ../../cpp/doxygen/_xml/ 的项目“RAFT”的doxygen xml输出中解析参数为 None 的函数“raft::make_pinned_vector_view”。可能的匹配项

- template<typename ElementType, typename IndexType, typename LayoutPolicy = layout_c_contiguous> auto constexpr make_pinned_vector_view(ElementType *ptr, IndexType n)
- template<typename ElementType, typename IndexType, typename LayoutPolicy = layout_c_contiguous> auto constexpr make_pinned_vector_view(ElementType *ptr, const typename LayoutPolicy::template mapping<vector_extent<IndexType>> &mapping)
template<typename ElementType, typename IndexType = std::uint32_t>
auto constexpr raft::make_pinned_scalar_view(ElementType *ptr)#

为固定内存值创建一个0维(标量)mdspan 实例。

模板参数:
  • ElementType – 矩阵元素的数据类型

  • IndexType – 范围的索引类型

参数:

**ptr** – **[in]** 要封装的固定内存指针

验证例程#

#include <raft/core/mdspan.hpp>

template<typename T, typename = void>
struct is_mdspan : public std::false_type#
template<typename T>
using raft::is_mdspan_t = is_mdspan<std::remove_const_t<T>>#
template<typename T, typename = void>
struct is_input_mdspan : public std::false_type#
template<typename T>
using raft::is_input_mdspan_t = is_input_mdspan<T>#
template<typename T, typename = void>
struct is_output_mdspan : public std::false_type#
template<typename T>
using raft::is_output_mdspan_t = is_output_mdspan<T>#
template<typename ...Tn>
using raft::enable_if_mdspan = std::enable_if_t<is_mdspan_v<Tn...>>#
template<typename ...Tn>
using raft::enable_if_input_mdspan = std::enable_if_t<is_input_mdspan_v<Tn...>>#
template<typename ...Tn>
using raft::enable_if_output_mdspan = std::enable_if_t<is_output_mdspan_v<Tn...>>#