微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么Visual C自动矢量化器不能矢量化这个简单的循环?

我无法弄清楚为什么Visual C无法自动矢量化这个循环…任何想法?

我明白了:

testvec.cpp:12: info C5002: loop not vectorized due to reason '1200'

原因代码1200是:

Loop contains loop-carried data dependences that prevent vectorization. Different iterations of the loop interfere with each other such that vectorizing the loop would produce wrong answers,and the auto-vectorizer cannot prove to itself that there are no such data dependences.

但为什么?

#include <stdlib.h>

int main(int argc,char *argv[])
{
    int const n = argc;
    double
        *const p1 = (double *)malloc(n * n * sizeof(*p1)),*const p2 = (double *)malloc(n * n * sizeof(*p2));
    for (int j = 0; j < n; ++j)
    {
        double const sj = p1[n * j];
        for (int i = 0; i < n; ++i)
        {
            double const sum = p1[i] + sj,old = p1[i + n * j];
            p2[i + n * j] = sum < old ? sum : old;
        }
    }
}

解决方法

我终于找到了如何解决它…似乎n * j中的乘法是罪魁祸首.
将其作为int nj = n * j挂在外面;并在内循环中使用nj来修复问题.

我仍然不知道为什么会这样.

如果有人知道,请发布!

原文地址:https://www.jb51.cc/c/117980.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐