Skip to content

Instantly share code, notes, and snippets.

@IonianIronist
Created March 5, 2021 16:16
Show Gist options
  • Save IonianIronist/79d5bb04c4aaf4be109187e76dfde5b0 to your computer and use it in GitHub Desktop.
Save IonianIronist/79d5bb04c4aaf4be109187e76dfde5b0 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <sys/timeb.h>
#define N 1000
int main() {
double *a, *b, *c;
int i, j, k;
double mflop, sum;
struct timeb ts, te;
a = (double *)malloc(N*N*sizeof(double));
if (a==NULL) { exit(1); }
b = (double *)malloc(N*N*sizeof(double));
if (b == NULL){free(a); exit(1);}
c = (double *)malloc(N*N*sizeof(double));
if (c == NULL){free(a); free(b); exit(1);}
//b = &a[N*N];
for (i=0;i<N*N;i++) {
a[i] = 2.0;
b[i] = 3.0;
c[i] = b[i];
}
double b_cell = 3.0*2.0*N;
ftime(&ts);
//i*N + j
//i + j*Ν
// loop unrolling
for (i=0;i<N;i+=4) {
for (j=0;j<N;j++) {
c[i*N + j] = b[j*N + i];
c[i*(N + 1) + j ] = b[j*N + i + 1];
c[i*(N + 2) + j ] = b[j*N + i + 2];
c[i*(N + 3) + j ] = b[j*N + i + 3];
}
}
// c is now b^T
for(i = 0; i<N; i++){ // grammes tou a
for(j = 0; j<N; j++){ // grammes toy c
sum = 0.0; // b[i,j]
for(k = 0; k<N; k++){
sum += a[i*N + k]*c[j*N + k];
}
b[i*N + j] = sum;
}
}
ftime(&te);
for (i=0;i<N;i++) {
for (j = 0; j<0; j++){
if(b[i*N+j] != b_cell ) {
printf("error\n");
break;
}
}
}
free(a);
free(b);
free(c);
int diff = (int) (1000.0 * (te.time - ts.time)
+ (te.millitm - ts.millitm));
printf("Time in milliseconds:%d\n", diff);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment