假设我有一个stl::array<float, 24> foo
这是列主格式 arrayfire 数组的线性化 STL 挂件,例如af::array bar = af::array(4,3,2, 1, f32);
。所以我有一个af::dim4
object dims
尺寸为bar
, 我最多有 4 个af::seq
-objects 和我有线性化数组foo
.
如何明确获得foo
(即线性化版本bar
) 代表例如第二行和第三行,即bar(af::seq(1,2), af::span, af::span, af::span)
?下面给出了一个小代码示例,它显示了我想要的内容。最后我也解释了为什么我想要这个。
af::dim4 bigDims = af::dim4(4,3,2);
stl::array<float, 24> foo; // Resides in RAM and is big
float* selBuffer_ptr; // Necessary for AF correct type autodetection
stl::vector<float> selBuffer;
// Load some data into foo
af::array selection; // Resides in VRAM and is small
af::seq selRows = af::seq(1,2);
af::seq selCols = af::seq(bigDims[1]); // Emulates af::span
af::seq selSlices = af::seq(bigDims[2]); // Emulates af::span
af::dim4 selDims = af::dim4(selRows.size, selCols.size, selSlices.size);
dim_t* linIndices;
// Magic functionality getting linear indices of the selection
// selRows x selCols x selSlices
// Assign all indexed elements to a consecutive memory region in selBuffer
// I know their positions within the full dataset, b/c I know the selection ranges.
selBuffer_ptr = static_cast<float> &(selBuffer[0]);
selection = af::array(selDims, selBuffer_ptr); // Copies just the selection to the device (e.g. GPU)
// Do sth. with selection and be happy
// I don't need to write back into the foo array.
Arrayfire 必须实现这样的逻辑才能访问元素,我发现了几个相关的类/函数,例如af::index, af::seqToDims, af::gen_indexing, af::array::operator()
- 然而我还想不出一个简单的出路。
我考虑过基本上重新实现operator()
,这样它的工作方式类似,但不需要引用数组对象。但如果 arrayfire 框架中有一个简单的方法,这可能会浪费精力。
背景:我想要这样做的原因是因为 arrayfire 不允许在与 GPU 后端链接时仅将数据存储在主内存(CPU 上下文)中。由于我有大量数据需要逐段处理,并且 VRAM 非常有限,所以我想实例化af::array
-来自始终驻留在主内存中的 stl 容器的临时对象。
当然,我知道我可以编写一些索引魔法来解决我的问题,但我想使用相当复杂的af::seq
对象,这可能会使索引逻辑的有效实现变得复杂。