Created
December 13, 2020 03:28
-
-
Save gunavaran/e242d4fe05b6569a96cbbb50b8d2cf37 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
// | |
// Created by gunavaran on 12/8/20. | |
// | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <time.h> | |
#define n 1024 | |
#define s 512 | |
double A[n][n]; | |
double B[n][n]; | |
double C[n][n]; | |
int main() { | |
//populate the matrices with random values between 0.0 and 1.0 | |
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 timespec start, end; | |
double time_spent; | |
//matrix multiplication | |
// //i, k, j | |
clock_gettime(CLOCK_REALTIME, &start); | |
for (int ih = 0; ih < n; ih += s) { | |
for (int jh = 0; jh < n; jh += s) { | |
for (int kh = 0; kh < n; kh += s) { | |
for (int il = 0; il < s; il++) { | |
for (int kl = 0; kl < s; kl++) { | |
for (int jl = 0; jl < s; jl++) { | |
C[ih + il][jh + jl] += A[ih + il][kh + kl] * B[kh + kl][jh + jl]; | |
} | |
} | |
} | |
} | |
} | |
} | |
clock_gettime(CLOCK_REALTIME, &end); | |
time_spent = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.0; | |
printf("Elapsed time in seconds: %f \n", time_spent); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment