SIMD小测试
来源:互联网 发布:电脑音效软件 编辑:程序博客网 时间:2024/06/05 05:57
今天研究了一下SIMD指令,做了一个小测试,发现速度变快了5倍,但是在release模式下编译器会做同样的优化。所以很多时候我们可以不需要手动的写SIMD.当然编译器不一定会做这样的优化。
另外注意64位系统上new自动16位对齐,但是用int[]这样的办法就会导致内存越界,因为栈上的内存不一定是16位对齐的。除此之外发现了一个直接修改原来数据的办法,就是将原来的地址强制转换成__m128i,这样修改就直接进行了。注意simd的指令集里面没有直接从int*里面读取的,所以还是要用指针的办法来解决,或者用set指令也可以指定四个数,但是比较麻烦。
#include "iostream"#include "vector"#include "sstream"#include "string"#include "algorithm"#include "time.h"#include "nmmintrin.h"using namespace std;#define COMPUTE(x) do{clock_t start_time = clock(); \ x; \ clock_t end_time = clock(); \ cout << "Running time is: " << static_cast<double>\ (end_time - start_time) / CLOCKS_PER_SEC * 1000 << "ms" << endl; }while (0) //输出运行时间int test(const vector<int>& a, const vector<int>& b,vector<int>& d){ int n = a.size(); __m128i* dest = (__m128i*) (&d[0]); for (int i = 0; i < n; i += 4){ __m128i _a = *(__m128i*) (&a[i]); __m128i _b = *(__m128i*) (&b[i]); *dest = _mm_add_epi32(_a, _b); dest++; } return d[d.size()-1];}int test2(const vector<int>& a, const vector<int>& b, vector<int>& d){ int n = a.size(); for (int i = 0; i < n; ++i) d[i] = a[i] + b[i]; return d[d.size()-1];}int main(){ __declspec(align(16)) vector<int> a(30002400,50); __declspec(align(16)) vector<int> b(30002400,50); __declspec(align(16)) vector<int> d(30002400); __declspec(align(16)) vector<int> d2(30002400); int ret; COMPUTE(ret = test(a, b,d);); COMPUTE(ret = test2(a, b, d2);); cout << ret; return 0;}
0 0
- SIMD小测试
- SIMD
- SIMD
- SIMD
- SIMD
- SIMD
- SIMD
- SIMD介绍
- arm simd
- simd编程
- 理解“SIMD”
- SIMD&VLIW
- SIMD、MIMD
- DSP & SIMD
- SIMD简介
- SIMD&VLIW
- SIMD并行
- SIMD技术
- React 学习总结
- Android之关于点击多次的导致多个页面问题
- scrapy 爬取https网页时出现ssl错误
- 1618: [Usaco2008 Nov]Buying Hay 购买干草
- 进程间通信详解
- SIMD小测试
- 最小生成树Prim和kruskal
- 2072 分配房间 二分
- handle内存泄漏问题
- 数据结构概述
- Ubuntu 下 安装 ss
- Machine Learning第一讲[单变量线性回归] --(二)参数学习
- windows下批量修改文件后缀名
- 'target/WEB-INF/lib/org.springframework.spring-library-3.1.0.RELEASE.libd' in project 'mvcProject' c