在 Accelerate Framework 中,对输入和输出使用相同的引用是否安全?

In the Accelerate Framework is it safe to use the same reference for both input and output?

我正在使用加速框架来创建一个快速向量和矩阵库。使用相同的浮点数组作为输入和输出是否安全?下面的代码安全吗?

Vec3f& Vec3f::operator+=(const Vec3f& rhs)
{
  // this->xyz is input and output.  Have I crossed the beams?
  vDSP_vadd(this->xyz, 1, rhs.xyz, 1, this->xyz, 1, 3);
  return *this;
}

使用 Accelerate Framework 的主要好处是利用其针对 ARM 架构进行优化的能力。

IMPORTANT: To achieve the best possible performance, the vDSP routines perform no error checking and return no completion codes. All arguments are assumed to be specified and passed correctly, and these responsibilities lie with the caller

要深入了解它是如何做到这一点的,WWDC 2013 Session 713 非常有用。

下面的代码安全吗? - 我不确定,但是为了获得最佳性能,我不推荐它。

这个问题的完整答案有点复杂,因为 "Accelerate framework" 由许多较小的库组成。从广义上讲,对于 "simple" vDSP 函数(那些在其输入缓冲区中线性工作的函数),如 vDSP_vadd,是的,使用函数 "in-place" 是安全的(即其中一个输入buffers 也是输出缓冲区)。

但是,我应该指出,您不想使用 vDSP_vadd 来添加长度为 3 的向量。这只需要三个标量运算,或一个向量运算;即使是最简单的标量实现也会在您甚至可以对 vDSP_vadd 进行函数调用之前完成。您实际上要查找的是 <simd/simd.h>,其中包含 iOS 和 OS X 的小型(2、3、4 维)向量和矩阵算法的 SIMD 实现。那里已经为您定义了运算符重载,因此您实际上可能不需要自己做很多事情:

#include <simd/simd.h>
using namespace simd;
float3 x = { 0, 1, 2 };
float3 y = { 1, 1, 1 };
float3 z = 2*x - y;
... etc