【问题标题】:AVX2 SIMD addition not workingAVX2 SIMD 添加不起作用
【发布时间】:2017-04-25 18:53:18
【问题描述】:

我正在尝试使用 AVX2 SIMD 指令添加这两个向量。

代码编译时没有错误和警告,但运行时崩溃。为什么?

无论在main方法中初始化的数组有多大,它都应该打印与AVX2相加的结果。

#include <iostream>
#include <immintrin.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;



void mul(float *a, float *b, float *c, int ARR_SIZE){

        for (int i=0; i < ARR_SIZE ; i+=8){

         __m256 vecA = _mm256_load_ps(&a[i]);

         __m256 vecB = _mm256_load_ps(&b[i]);

         __m256 res  = _mm256_add_ps(vecA,vecB);

         _mm256_store_ps(&c[i],res);

        float* f = (float*)&c[i];
        printf("%f %f %f %f %f %f %f %f\n", f[i + 0], f[i + 1], f[i + 2], f[i + 3], f[i + 4], f[i + 5], f[i + 6], f[i + 7]);

        }

}

int main(){

    float a[] = {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0};
    float b[] = {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0};

    int arrsize = sizeof(a) / sizeof (a[0]);

    float c[arrsize];

    mul((float*)&a, (float*)&b , (float*)&c, arrsize);

   return 0;

}

【问题讨论】:

  • 尝试未对齐的加载/存储(或充分对齐的数组)
  • 尝试删除 & 符号:mul((float*)a, (float*)b , (float*)c, arrsize);
  • 谢谢哈罗德。在尝试了您的建议后,该程序起作用了。
  • 您最好使用__attribute__(( aligned(32))) 左右来对齐数组,而不是加载未对齐的数组

标签: c++ sse simd avx avx2


【解决方案1】:

在尝试了未对齐的加载和存储后,程序运行良好。

【讨论】:

    猜你喜欢
    • 2013-09-29
    • 1970-01-01
    • 2020-02-15
    • 2012-09-22
    • 1970-01-01
    • 2021-05-07
    • 2019-09-25
    • 2015-03-11
    • 2015-08-17
    相关资源
    最近更新 更多