为什么我的散点代码的性能比Vc SIMD更好

why the performance of my scatter code better than Vc SIMD?

本文关键字:性能比 Vc SIMD 更好 代码 我的 为什么      更新时间:2023-10-16

我是SIMD程序的新手

unsigned int Hash(unsigned int f);
uint_v Hash(uint_v vec);
int main()
{
        std::random_device rd;
        unsigned* mem1=new unsigned [_size]();
        for(int i=0;i<_size;++i)
                mem1[i]=rd();
        time_t t1=clock();
        uint_v mem;
        for(int i=0;i<_size;i+=uint_v::size())
        {
                mem.load(mem1+i,Vc::Unaligned);
                uint_v temp=Hash(mem);
        }
        t1=clock()-t1;
        std::cout<<"simd time:"<<(1.0*t1)/CLOCKS_PER_SEC<<"n";
        time_t t2=clock();
        for(int i=0;i<_size;++i)
                unsigned int temp=Hash(mem1[i]);
        t2=clock()-t2;
        std::cout<<"normal time:"<<(1.0*t2)/CLOCKS_PER_SEC<<"n";
        return 0;
}
unsigned int Hash(unsigned int f)
{
        return (f>>7)^(f>>13)^(f>>21)^f;
}
uint_v Hash(uint_v vec)
{
        uint_v mem=vec.apply([](unsigned f) ->unsigned{return (f>>7)^(f>>13)^(f>>21)^f;});
        return mem;
}

我的代码如上所述,时间结果是:
simd时间:0.127762正常时间:0.034841
结果和比较mem1和mem2中的日期(Vc-uint_v矢量(

相似

您没有测量您想要测量的内容。编译器将对您计算但从未使用过的所有内容进行死代码消除(好吧,编译器100%未使用过的内容(。编译器本应在两个循环上都执行DCE,但显然在Vc情况下未能执行。

想法:

  • 将结果存储到全局变量
  • 使用内联asm伪造对结果的使用