在 C++ 中将 ArrayFire 数组数据从 f64 转换为 f32

问题描述

我有一个 dtype=f64 的 ArrayFire 数组,我需要将其转换为 dtype=f32。除了转换为主机数组并通过执行 static_cast 进行迭代之外,我不知道如何执行此操作。我相信有更好的方法。我找到了 af_cast,但它似乎只是 C 接口的一部分。

// Example,double precision array
af::array A_double(100,1,f64); // f64 = double precision

// Need to cast this to an f32 array somehow...
af::array A_single = cast<f32>(A_double); // <-- something like this?

解决方法

事实证明答案非常简单。 ArrayFire 库有一个名为 as 的函数,我不知道并且在文档中忽略了该函数。

// Example,double precision array
af::array A_double(100,1,f64); // f64 = double precision

// Cast to an f32 array
af::array A_single = A_double.as(f32);

这是 documentation 的链接。