Mic
Mic

Reputation: 41

crashing when I using AVX function

#include "stdio.h"
#include "math.h"
#include "stdlib.h"
#include "x86intrin.h"

void dd_m(double *clo, int m) 
{
int j;
__m256d *vclo = (__m256d *)clo;
__m256d al=_mm256_set_pd(0,0,0,0);
__m256d clo_n=_mm256_set_pd(0,0,0,0);
int i;
for (j = 0; j < m; j++) {
    for (i = 0; i < m; i++) {
        al = _mm256_add_pd(vclo[m/4*j+i] , clo_n);
    }
}
}
int main(int argc, const char * argv[]){

int m;
double* zlo;
int i;


m=(int)pow(2,8);

zlo=(double *)_mm_malloc(sizeof(double) * m*m,32);
for (i=0;i<m*m;i++) {
    zlo[i]=0.0;
}
dd_m(zlo, m);

_mm_free(zlo);

return 0;
}

Here's my code. It generate an error

"Thread 1: EXC_BAD_ACCESS (code=1, address=0x102900000)"

inside for loop.

I used latest xcode with clang.

What should I do?

Upvotes: 0

Views: 140

Answers (1)

noma
noma

Reputation: 1229

By casting your clo to point to 256-bit vectors as vclo, your row-length is divided by four, you changed it in the index computation, but not in the inner loop over i.

for (j = 0; j < m; j++) {
    for (i = 0; i < m/4; i++) { // in vclo, the rows are only m/4 long
        al = _mm256_add_pd(vclo[m/4*j+i] , clo_n);
    }
}

Upvotes: 3

Related Questions