[英]I have a problem with C++ because it refuses to display the answer and I don't know where the problem is in code
[英]I don't understand where I have a problem in code using sse
我是 sse 编程的新手。 我想编写代码,在其中对向量 v 中的 4 个连续数字求和,并将该求和的结果写入 ans 向量中。 我想使用 sse 编写优化的代码。 但是当我设置大小等于 4 时,我的程序正在运行。 但是当我将大小设置为 8 时,我的程序不起作用,并且出现以下错误消息:“抛出异常:读取访问冲突。
ans 是 0x1110112。
如果有这个异常的处理程序,程序可能会安全地继续。”我不明白我哪里有问题。我分配内存正确,我在哪个地方有问题。有人可以帮助我,我会真的很感谢。
#include <iostream>
#include <immintrin.h>
#include <pmmintrin.h>
#include <vector>
#include <math.h>
using namespace std;
arith_t = double
void init(arith_t *&v, size_t size) {
for (int i = 0; i < size; ++i) {
v[i] = i / 10.0;
}
}
//accumulate with sse
void sub_func_sse(arith_t *v, size_t size, int start_idx, arith_t *ans, size_t start_idx_ans) {
__m128d first_part = _mm_loadu_pd(v + start_idx);
__m128d second_part = _mm_loadu_pd(v + start_idx + 2);
__m128d sum = _mm_add_pd(first_part, second_part);
sum = _mm_hadd_pd(sum, sum);
_mm_store_pd(ans + start_idx_ans, sum);
}
int main() {
const size_t size = 8;
arith_t *v = new arith_t[size];
arith_t *ans_sse = new arith_t[size / 4];
init(v, size);
init(ans_sse, size / 4);
int num_repeat = 1;
arith_t total_time_sse = 0;
for (int p = 0; p < num_repeat; ++p) {
for (int idx = 0, ans_idx = 0; idx < size; idx += 4, ans_idx++) {
sub_func_sse(v, size, idx, ans_sse, ans_idx);
}
}
for (size_t i = 0; i < size / 4; ++i) {
cout << *(ans_sse + i) << endl;
}
delete[] ans_sse;
delete[] v;
}
您正在使用需要特殊版本的加载和存储功能的未对齐内存。 您正确使用了_mm_loadu_pd
但_mm_store_pd
不适用于未对齐的内存,因此您应该将其更改为_mm_storeu_pd
。 还要考虑使用对齐的内存,这将导致更好的性能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.