I am trying to add this two vectors using AVX2 SIMD instruction.
The code compiles with no error & warning, but crashes when run. Why?
It should print the result of SIMD addition with AVX2 no matter how large the array is which is initialized in the main method.
#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;
}