Created
July 12, 2020 21:18
-
-
Save karngyan/64d7acc05e459d092c407f31194e3aee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdlib.h> | |
#include<stdio.h> | |
#include<sys/time.h> | |
#define n 4096 | |
#define THRESHOLD 32 | |
double A[n][n]; | |
double B[n][n]; | |
double C[n][n]; | |
float time_difference(struct timeval *start, | |
struct timeval *end) { | |
return (end->tv_sec - start->tv_sec) | |
+ 1e-6*(end->tv_usec - start->tv_usec); | |
} | |
void mm_base(double *restrict C, int n_C, | |
double *restrict A, int n_A, | |
double *restrict B, int n_B, | |
int n) { | |
// C = A * B | |
for (int i = 0; i < n; ++i) | |
for (int k = 0; k < n; ++k) | |
for (int j = 0; j < n; ++j) | |
C[i*n_C + j] += A[i*n_A+k] * B[k*n_B+j]; | |
} | |
void mm_dac(double *restrict C, int n_C, | |
double *restrict A, int n_A, | |
double *restrict B, int n_B, | |
int n) { | |
// C += A * B | |
assert ((n & (-n)) == n); | |
if (n <= THRESHOLD) { | |
mm_base(C, n_C, A, n_A, B, n_B, n); | |
} else { | |
#define X(M,r,c) (M + (r*(n_##M) + c)*(n/2)) | |
cilk_spawn mm_dac(X(C,0,0), n_C, X(A,0,0), n_A, X(B,0,0), n_B, n/2); | |
cilk_spawn mm_dac(X(C,0,1), n_C, X(A,0,0), n_A, X(B,0,1), n_B, n/2); | |
cilk_spawn mm_dac(X(C,1,0), n_C, X(A,1,0), n_A, X(B,0,0), n_B, n/2); | |
mm_dac(X(C,1,1), n_C, X(A,1,0), n_A, X(B,0,1), n_B, n/2); | |
cilk_sync; | |
cilk_spawn mm_dac(X(C,0,0), n_C, X(A,0,1), n_A, X(B,1,0), n_B, n/2); | |
cilk_spawn mm_dac(X(C,0,1), n_C, X(A,0,1), n_A, X(B,1,1), n_B, n/2); | |
cilk_spawn mm_dac(X(C,1,0), n_C, X(A,1,1), n_A, X(B,1,0), n_B, n/2); | |
mm_dac(X(C,1,1), n_C, X(A,1,1), n_A, X(B,1,1), n_B, n/2); | |
cilk_sync; | |
} | |
} | |
int main(int argc, const char *argv[]) { | |
for (int i = 0; i < n; ++i) { | |
for (int j = 0; j < n; ++j) { | |
A[i][j] = (double)rand() / (double)RAND_MAX; | |
B[i][j] = (double)rand() / (double)RAND_MAX; | |
C[i][j] = 0; | |
} | |
} | |
struct timeval start, end; | |
gettimeofday(&start, NULL); | |
mm_dac(C, n, A, n, B, n, n); | |
gettimeofday(&end, NULL); | |
printf("%0.6f seconds\n", time_difference(&start, &end)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment