array 类
表示用于将数据移动到加速器的数据容器。
语法
template <typename value_type, int _Rank>
friend class array;
参数
value_type
数据的元素类型。
_Rank
数组的秩。
成员
公共构造函数
名称 | 描述 |
---|---|
array 构造函数 | 初始化 array 类的新实例。 |
~array 构造函数 | 销毁 array 对象。 |
公共方法
名称 | 描述 |
---|---|
copy_to | 将数组的内容复制到另一个数组。 |
data | 返回一个指向数组原始数据的指针。 |
get_accelerator_view | 返回 accelerator_view 对象,该对象代表数组的分配位置。 只能在 CPU 上访问此属性。 |
get_associated_accelerator_view | 获取调用暂存构造函数以实例化 array 对象时,作为参数传递的第二个 accelerator_view 对象。 |
get_cpu_access_type | 返回数组的 access_type。 只能在 CPU 上访问此方法。 |
get_extent | 返回数组的 extent 对象。 |
reinterpret_as | 返回一个包含 array 对象中的所有元素的一维数组。 |
section | 返回位于指定原点以及(可选)具有指定盘区的 array 对象的一个子部分。 |
view_as | 返回从 array 对象构造的 array_view对象。 |
公共运算符
“属性” | 描述 |
---|---|
operator std::vector<value_type> |
使用 copy(*this, vector) 将数组隐式转换为 std::vector 对象。 |
operator() | 返回由参数指定的元素值。 |
operator[] | 返回位于指定索引处的元素。 |
operator= | 将指定 array 对象的内容复制到此对象中。 |
公共常量
“属性” | 描述 |
---|---|
rank 常量 | 存储数组的秩。 |
公共数据成员
“属性” | 描述 |
---|---|
accelerator_view | 获取 accelerator_view 对象,该对象代表数组的分配位置。 只能在 CPU 上访问此属性。 |
associated_accelerator_view | 获取调用暂存构造函数以实例化 array 对象时,作为参数传递的第二个 accelerator_view 对象。 |
cpu_access_type | 获取表示 CPU 如何访问数组存储的 access_type。 |
extent | 获取定义数组形状的盘区。 |
备注
类型 array<T,N>
表示位于特定位置(例如加速器或 CPU)的密集且常规(不交错)的 N 维数组。 数组中元素的数据类型为 T
,该类型必须与目标加速器兼容。 虽然数组的秩 N
是以静态方式确定的,并且是该类型的一部分,但数组的盘区由运行时确定,并通过使用类 extent<N>
表示。
尽管某些功能专用于秩 1、秩 2 和秩 3 的 array
对象,但数组可以具有任意数量的维度。 如果省略维度参数,则默认值为 1。
数组数据在内存中是连续排列的。 在最小有效维度上相差 1 的元素在内存中是相邻的。
数组在逻辑上被视为值类型,因为当数组被复制到另一个数组时,会执行一次深层复制。 两个数组永远不会指向相同的数据。
array<T,N>
类型用于多种场景:
作为可用于加速器计算的数据容器。
作为保留主机 CPU 内存的数据容器(可用于复制到其他数组或从其他数组复制)。
作为暂存对象,充当主机到设备复制的快速中介。
继承层次结构
array
要求
标头: amp.h
命名空间: 并发
~array
销毁 array
对象。
~array() restrict(cpu);
accelerator_view
获取 accelerator_view 对象,该对象代表数组的分配位置。 只能在 CPU 上访问此属性。
__declspec(property(get= get_accelerator_view)) Concurrency::accelerator_view accelerator_view;
array
初始化 array 类的新实例。 没有 array<T,N>
的默认构造函数。 所有构造函数仅在 CPU 上运行。 无法在 Direct3D 目标上执行这些构造函数。
explicit array(
const Concurrency::extent<_Rank>& _Extent) restrict(cpu);
explicit array(
int _E0) restrict(cpu);
explicit array(
int _E0,
int _E1) restrict(cpu);
explicit array(
int _E0,
int _E1,
int _E2) restrict(cpu);
array(
const Concurrency::extent<_Rank>& _Extent,
Concurrency::accelerator_view _Av
access_type _Cpu_access_type = access_type_auto) restrict(cpu);
array(
int _E0,
Concurrency::accelerator_view _Av
access_type _Cpu_access_type = access_type_auto) restrict(cpu);
array(
int _E0,
int _E1,
Concurrency::accelerator_view _Av
access_type _Cpu_access_type = access_type_auto) restrict(cpu);
array(
int _E0,
int _E1,
int _E2,
Concurrency::accelerator_view _Av
access_type _Cpu_access_type = access_type_auto) restrict(cpu);
array(
const Concurrency::extent<_Rank>& _Extent,
Concurrency::accelerator_view _Av,
Concurrency::accelerator_view _Associated_Av) restrict(cpu);
array(
int _E0,
accelerator_view _Av,
Concurrency::accelerator_view _Associated_Av) restrict(cpu);
array(
int _E0,
int _E1,
Concurrency::accelerator_view _Av,
Concurrency::accelerator_view _Associated_Av) restrict(cpu);
array(
int _E0,
int _E1,
int _E2,
Concurrency::accelerator_view _Av,
Concurrency::accelerator_view _Associated_Av) restrict(cpu);
template <typename _InputIterator>
array(
const Concurrency::extent<_Rank>& _Extent,
_InputIterator _Src_first,
_InputIterator _Src_last) restrict(cpu);
template <typename _InputIterator>
array(
const Concurrency::extent<_Rank>& _Extent,
_InputIterator _Src_first) restrict(cpu);
template <typename _InputIterator>
array(
int _E0,
_InputIterator _Src_first,
_InputIterator _Src_last) restrict(cpu);
template <typename _InputIterator>
array(
int _E0,
_InputIterator _Src_first) restrict(cpu);
template <typename _InputIterator>
array(
int _E0,
int _E1,
_InputIterator _Src_first,
_InputIterator _Src_last) restrict(cpu);
template <typename _InputIterator>
array(
int _E0,
int _E1,
_InputIterator _Src_first) restrict(cpu);
template <typename _InputIterator>
array(
int _E0,
int _E1,
int _E2,
_InputIterator _Src_first,
_InputIterator _Src_last) restrict(cpu);
template <typename _InputIterator>
array(
int _E0,
int _E1,
int _E2,
_InputIterator _Src_first) restrict(cpu);
template <typename _InputIterator>
array(
const Concurrency::extent<_Rank>& _Extent,
_InputIterator _Src_first,
_InputIterator _Src_last,
Concurrency::accelerator_view _Av
access_type _Cpu_access_type = access_type_auto) restrict(cpu);
template <typename _InputIterator>
array(
const Concurrency::extent<_Rank>& _Extent,
_InputIterator _Src_first,
Concurrency::accelerator_view _Av
access_type _Cpu_access_type = access_type_auto) restrict(cpu);
template <typename _InputIterator>
array(
int _E0,
_InputIterator _Src_first,
_InputIterator _Src_last,
Concurrency::accelerator_view _Av
access_type _Cpu_access_type = access_type_auto) restrict(cpu);
template <typename _InputIterator>
array(
int _E0,
_InputIterator _Src_first,
Concurrency::accelerator_view _Av
access_type _Cpu_access_type = access_type_auto) restrict(cpu);
template <typename _InputIterator>
array(
int _E0,
int _E1,
_InputIterator _Src_first,
_InputIterator _Src_last,
Concurrency::accelerator_view _Av
access_type _Cpu_access_type = access_type_auto) restrict(cpu);
template <typename _InputIterator>
array(
int _E0,
int _E1,
_InputIterator _Src_first,
Concurrency::accelerator_view _Av
access_type _Cpu_access_type = access_type_auto) restrict(cpu);
template <typename _InputIterator>
array(
int _E0,
int _E1,
int _E2,
_InputIterator _Src_first,
_InputIterator _Src_last,
Concurrency::accelerator_view _Av,
access_type _Cpu_access_type = access_type_auto) restrict(cpu);
template <typename _InputIterator>
array(
int _E0,
int _E1,
int _E2,
_InputIterator _Src_first,
Concurrency::accelerator_view _Av
access_type _Cpu_access_type = access_type_auto) restrict(cpu);
template <typename _InputIterator>
array(
const Concurrency::extent<_Rank>& _Extent,
_InputIterator _Src_first,
_InputIterator _Src_last,
Concurrency::accelerator_view _Av,
Concurrency::accelerator_view _Associated_Av) restrict(cpu);
template <typename _InputIterator>
array(
const Concurrency::extent<_Rank>& _Extent,
_InputIterator _Src_first,
Concurrency::accelerator_view _Av,
Concurrency::accelerator_view _Associated_Av) restrict(cpu);
template <typename _InputIterator>
array(
int _E0,
_InputIterator _Src_first,
_InputIterator _Src_last,
Concurrency::accelerator_view _Av,
Concurrency::accelerator_view _Associated_Av) restrict(cpu);
template <typename _InputIterator>
array(
int _E0, _InputIterator _Src_first,
Concurrency::accelerator_view _Av,
Concurrency::accelerator_view _Associated_Av) restrict(cpu);
template <typename _InputIterator>
array(
int _E0,
int _E1, _InputIterator _Src_first, _InputIterator _Src_last,
Concurrency::accelerator_view _Av,
Concurrency::accelerator_view _Associated_Av) restrict(cpu);
template <typename _InputIterator>
array(
int _E0,
int _E1, _InputIterator _Src_first,
Concurrency::accelerator_view _Av,
Concurrency::accelerator_view _Associated_Av) restrict(cpu);
template <typename _InputIterator>
array(
int _E0,
int _E1,
int _E2, _InputIterator _Src_first, _InputIterator _Src_last,
Concurrency::accelerator_view _Av,
Concurrency::accelerator_view _Associated_Av) restrict(cpu);
template <typename _InputIterator>
array(
int _E0,
int _E1,
int _E2, _InputIterator _Src_first,
Concurrency::accelerator_view _Av,
Concurrency::accelerator_view _Associated_Av) restrict(cpu);
explicit array(
const array_view<const value_type, _Rank>& _Src) restrict(cpu);
array(
const array_view<const value_type, _Rank>& _Src,
accelerator_view _Av
access_type _Cpu_access_type = access_type_auto) restrict(cpu);
array(
const array_view<const value_type, _Rank>& _Src,
accelerator_view _Av,
accelerator_view _Associated_Av) restrict(cpu);
array(const array& _Other) restrict(cpu);
array(array&& _Other) restrict(cpu);
参数
_Associated_Av
用于指定数组的首选目标位置的 accelerator_view。
_Av
用于指定数组位置的 accelerator_view 对象。
_Cpu_access_type
数组在 CPU 上所需的 access_type。 此参数的默认值是 access_type_auto
,由运行时确定 CPU 的 access_type
。 可以使用 get_cpu_access_type
方法查询数组的实际 CPU access_type
。
_Extent
数组的每个维度中的盘区。
_E0
本部分盘区中最重要的组成部分。
_E1
本部分盘区中次重要的组成部分。
_E2
本部分盘区中最不重要的组成部分。
_InputIterator
输入迭代器的类型。
_Src
要复制的对象。
_Src_first
源容器中的起始迭代器。
_Src_last
源容器中的结束迭代器。
_Other
其他数据源。
_Rank
节的排名。
value_type
复制的元素的数据类型。
associated_accelerator_view
获取调用暂存构造函数以实例化 array
对象时,作为参数传递的第二个 accelerator_view 对象。
__declspec(property(get= get_associated_accelerator_view)) Concurrency::accelerator_view associated_accelerator_view;
copy_to
将 array
的内容复制到另一个 array
。
void copy_to(
array<value_type, _Rank>& _Dest) const ;
void copy_to(
array_view<value_type, _Rank>& _Dest) const ;
参数
_Dest
要复制到的 array_view 对象。
cpu_access_type
获取此数组允许的 CPU access_type。
__declspec(property(get= get_cpu_access_type)) access_type cpu_access_type;
数据
返回一个指向 array
的原始数据的指针。
value_type* data() restrict(amp, cpu);
const value_type* data() const restrict(amp, cpu);
返回值
指向数组原始数据的指针。
extent
获取定义了 array
形状的 extent 对象。
__declspec(property(get= get_extent)) Concurrency::extent<_Rank> extent;
get_accelerator_view
返回 accelerator_view 对象,该对象代表 array
对象的分配位置。 只能在 CPU 上访问此属性。
Concurrency::accelerator_view get_accelerator_view() const;
返回值
表示 array
对象分配位置的 accelerator_view
对象。
get_associated_accelerator_view
获取调用暂存构造函数以实例化 array
对象时,作为参数传递的第二个 accelerator_view 对象。
Concurrency::accelerator_view get_associated_accelerator_view() const ;
返回值
传递给暂存构造函数的第二个 accelerator_view 对象。
get_cpu_access_type
返回此数组允许的 CPU access_type。
access_type get_cpu_access_type() const restrict(cpu);
返回值
get_extent
返回 array
的 extent 对象。
Concurrency::extent<_Rank> get_extent() const restrict(amp,cpu);
返回值
extent
的 array
对象。
operator std::vector<value_type>
使用 copy(*this, vector)
将数组隐式转换为 std::vector 对象。
operator std::vector<value_type>() const restrict(cpu);
参数
value_type
向量元素的数据类型。
返回值
一个类型为 vector<T>
的对象,其中包含数组中的数据的副本。
operator()
返回由参数指定的元素值。
value_type& operator() (const index<_Rank>& _Index) restrict(amp,cpu);
const value_type& operator() (const index<_Rank>& _Index) cons t restrict(amp,cpu);
value_type& operator() (int _I0, int _I1) restrict(amp,cpu);
const value_type& operator() (int _I0, int _I1) const restrict(amp,cpu) ;
value_type& operator() (int _I0, int _I1, int _I2) restrict(amp,cpu);
const value_type& operator() (int _I0, int _I1, int _I2) const restrict(amp,cpu);
typename details::_Projection_result_type<value_type,_Rank>::_Result_type operator()(int _I) restrict(amp,cpu);
typename details::_Projection_result_type<value_type,_Rank>::_Const_result_type operator()(int _I) const restrict(amp,cpu);
参数
_Index
元素的位置。
_I0
本部分原点的最重要的组成部分。
_I1
本部分原点的次重要的组成部分。
_I2
本部分原点的最不重要的组成部分。
_I
元素的位置。
返回值
由参数指定的元素值。
operator[]
返回位于指定索引处的元素。
value_type& operator[](const index<_Rank>& _Index) restrict(amp,cpu);
const value_type& operator[]
(const index<_Rank>& _Index) const restrict(amp,cpu);
typename details::_Projection_result_type<value_type,_Rank>::_Result_type operator[](int _i) restrict(amp,cpu);
typename details::_Projection_result_type<value_type,_Rank>::_Const_result_type operator[](int _i) const restrict(amp,cpu);
参数
_Index
索引。
_I
索引。
返回值
位于指定索引处的元素。
operator=
复制指定的 array
对象的内容。
array& operator= (const array& _Other) restrict(cpu);
array& operator= (array&& _Other) restrict(cpu);
array& operator= (
const array_view<const value_type, _Rank>& _Src) restrict(cpu);
参数
_Other
要从其复制的 array
对象。
_Src
要从其复制的 array
对象。
返回值
对此 array
对象的引用。
rank
存储 array
的秩。
static const int rank = _Rank;
reinterpret_as
通过一维 array_view 重新解释数组,它可以具有与源数组不同的值类型。
语法
template <typename _Value_type2>
array_view<_Value_type2,1> reinterpret_as() restrict(amp,cpu);
template <typename _Value_type2>
array_view<const _Value_type2, 1> reinterpret_as() const restrict(amp,cpu);
参数
_Value_type2
返回的数据的数据类型。
返回值
基于数组的 array_view 或 const array_view 对象,其中元素类型从 T 重新解释为 ElementType,秩从 N 减少到 1。
备注
有时,将多维数组视为线性的一维数组会很方便,它的值类型可能与源数组不同。 可以使用此方法来实现。 警告 使用不同的值类型重新解释 array 对象是一种潜在的不安全操作。 建议谨慎使用此功能。
以下代码是一个示例。
struct RGB { float r; float g; float b; };
array<RGB,3> a = ...;
array_view<float,1> v = a.reinterpret_as<float>();
assert(v.extent == 3*a.extent);
section
返回位于指定原点以及(可选)具有指定盘区的 array
对象的一个子部分。
array_view<value_type,_Rank> section(
const Concurrency::index<_Rank>& _Section_origin,
const Concurrency::extent<_Rank>& _Section_extent) restrict(amp,cpu);
array_view<const value_type,_Rank> section(
const Concurrency::index<_Rank>& _Section_origin,
const Concurrency::extent<_Rank>& _Section_extent) const restrict(amp,cpu);
array_view<value_type,_Rank> section(
const Concurrency::extent<_Rank>& _Ext) restrict(amp,cpu);
array_view<const value_type,_Rank> section(
const Concurrency::extent<_Rank>& _Ext) const restrict(amp,cpu);
array_view<value_type,_Rank> section(
const index<_Rank>& _Idx) restrict(amp,cpu);
array_view<const value_type,_Rank> section(
const index<_Rank>& _Idx) const restrict(amp,cpu);
array_view<value_type,1> section(
int _I0,
int _E0) restrict(amp,cpu);
array_view<const value_type,1> section(
int _I0,
int _E0) const restrict(amp,cpu);
array_view<value_type,2> section(
int _I0,
int _I1,
int _E0,
int _E1) restrict(amp,cpu);
array_view<const value_type,2> section(
int _I0,
int _I1,
int _E0,
int _E1) const restrict(amp,cpu);
array_view<value_type,3> section(
int _I0,
int _I1,
int _I2,
int _E0,
int _E1,
int _E2) restrict(amp,cpu);
array_view<const value_type,3> section(
int _I0,
int _I1,
int _I2,
int _E0,
int _E1,
int _E2) const restrict(amp,cpu);
参数
_E0
本部分盘区中最重要的组成部分。
_E1
本部分盘区中次重要的组成部分。
_E2
本部分盘区中最不重要的组成部分。
_Ext
指定该部分的盘区的 extent 对象。 原点为 0。
_Idx
指定原点位置的 index 对象。 子部分是盘区的其余部分。
_I0
本部分原点的最重要的组成部分。
_I1
本部分原点的次重要的组成部分。
_I2
本部分原点的最不重要的组成部分。
_Rank
节的排名。
_Section_extent
指定该部分的盘区的 extent 对象。
_Section_origin
指定原点位置的 index 对象。
value_type
复制的元素的数据类型。
返回值
返回位于指定原点以及(可选)具有指定盘区的 array
对象的一个子部分。 当仅指定 index
对象时,该子部分包含关联网格中的所有元素,其索引大于 index
对象中元素的索引。
view_as
将此数组重新解释为秩不同的 array_view。
template <int _New_rank>
array_view<value_type,_New_rank> view_as(
const Concurrency::extent<_New_rank>& _View_extent) restrict(amp,cpu);
template <int _New_rank>
array_view<const value_type,_New_rank> view_as(
const Concurrency::extent<_New_rank>& _View_extent) const restrict(amp,cpu);
参数
_New_rank
作为参数传递的 extent
对象的秩。
_View_extent
用于构造新的 array_view 对象的盘区。
value_type
原始 array
对象和返回的 array_view
对象中元素的数据类型。
返回值
构造的 array_view 对象。