gpt4 book ai didi

c++ - Visual Studio 无法矢量化

转载 作者:搜寻专家 更新时间:2023-10-31 02:22:03 25 4
gpt4 key购买 nike

我尝试在 VS2013 中编译以下内容

template <class T>
void assignment(T* result, size_t sz, const T x)
{
for (size_t i = 0; i < sz; i++)
result[i] = x;
}

并且编译器无法使用以下消息对代码进行向量化。

info C5002: loop not vectorized due to reason '1104'

虽然下面的代码没问题

void discountFactor(double* result, const double* r, double t, size_t sz)
{
for (size_t i = 0; i < sz; i++)
result[i] = -r[i] * t;

for (size_t i = 0; i < sz; i++)
result[i] = exp(result[i]);
}

谁能给我解释一下11​​04的原因是什么?

最佳答案

正如其他人所说,这些类型的优化在 MSDN 上有很好的记录。 .事实上,那里有一个很好的例子。这是片段:

int code_1104(int *A, int *B)
{
// When it vectorizes a loop, the compiler must 'expand' scalar
// variables to a vector size such that they can fit in
// vector registers. Code 1104 is emitted when the compiler
// cannot 'expand' such scalars.

// In this example, we try to 'expand' x to be used in the
// vectorized loop. However, there is a use of 'x'
// beyond the loop body, which prohibits this expansion.

// To resolve this, try to limit scalars to be used only in
// the loop body and not beyond, and try to keep their types
// consistent with the loop types.

int x;
for (int i=0; i<1000; ++i)
{
x = B[i];
A[i] = A[i] + x;
}

return x;
}

我的解释(老实说,他们只是猜测):第二个示例之所以有效,是因为 result 是原始数据类型 (double *),即使它在循环范围之外也可以轻松修改。

该示例在其注释中说明...

// When it vectorizes a loop, the compiler must 'expand' scalar
// variables to a vector size such that they can fit in
// vector registers.

根据您调用模板化函数assignment 的方式,第一个示例中的T 可能是任何。包括一个不适合寄存器的类型。因此,我假设编译器无法自动矢量化您的 for 循环。

关于c++ - Visual Studio 无法矢量化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30736758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com