I am trying to do Matrix multiplication using SSE. I have written a simple program for 4x4 matrices. Everything seems fine but when I print result , its some garbage values. please help to figure out problem/s. Secondly program stops working when I free memory, not a proper end of program.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <float.h>
#include <xmmintrin.h>
void main() {
float **a, **b, **c;
int a_r = 4, a_c = 4, b_c = 4, b_r = 4;
int i, j, k;
/* allocate memory for matrix one */
a = (float **)malloc(sizeof(float) * a_r);
for (i = 0; i < a_c; i++) {
a[i] = (float *)malloc(sizeof(float) * a_c);
}
/* allocate memory for matrix two */
b = (float **)malloc(sizeof(float) * b_r);
for (i = 0; i < b_c; i++) {
b[i] = (float *)malloc(sizeof(float) * b_c);
}
/* allocate memory for sum matrix */
c = (float **)malloc(sizeof(float) * a_r);
for (i = 0; i < b_c; i++) {
c[i] = (float *)malloc(sizeof(float) * b_c);
}
printf("Initializing matrices...\n");
//initializing first matrix
for (i = 0; i < a_r; i++) {
for (j = 0; j < a_c; j++) {
a[i][j] = 2;
}
}
// initializing second matrix
for (i = 0; i < b_r; i++) {
for (j = 0; j < b_c; j++) {
b[i][j] = 2;
}
}
/* initialize product matrix */
for (i = 0; i < a_r; i++) {
for (j = 0; j < b_c; j++) {
c[i][j] = 0;
}
}
int count = 0;
/* multiply matrix one and matrix two */
for (i = 0; i < a_r; i++) {
for (j = 0; j < a_c; j++) {
count = 0;
__m128 result = _mm_setzero_ps();
for (k = 0; k < 4; k += 4) {
__m128 row1 = _mm_loadu_ps(&a[i][k]);
__m128 row2 = _mm_loadu_ps(&b[k][j]);
result = _mm_mul_ps(row1, row2);
for (int t = 1; t < 4; t++) {
__m128 row3=_mm_loadu_ps(&a[t * 4]);
__m128 row4=_mm_loadu_ps(&b[i][t]);
__m128 row5 = _mm_mul_ps(row3,row4);
result = _mm_add_ps(row5, result);
}
_mm_storeu_ps(&c[i][j], result);
}
}
}
printf("******************************************************\n");
printf ("Done.\n");
for (i = 0; i < a_r ; i++) {
for (j = 0; j < b_c; j++) {
printf ("%f ", c[i][j]); // issue here when I print results.
}
printf("\n");
} // Here program stops working.
/*free memory*/
for (i = 0; i < a_r; i++) {
free(a[i]);
}
free(a);
for (i = 0; i < a_c; i++) {
free(b[i]);
}
free(b);
for (i = 0; i < b_c; i++) {
free(c[i]);
}
free(c);
}
please have look at address printed for output matrix. how to get aligned addresses, I have _aligned_malloc
, but still not aligned.
_mm_loadu_ps
and_mm_storeu_ps
. – Rotem