// INCLUDES --------------------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
// DEFINES ---------------------------------------------------------------------
#define MAXDIM 500
#define STEP 1
#define NUMTHREADS 8
#define RUNS 10
// VARIABLES -------------------------------------------------------------------
int matrix_a[MAXDIM][MAXDIM], matrix_b[MAXDIM][MAXDIM];
int matrix_c[MAXDIM][MAXDIM];
int dim;
int threads;
// MATRIX FUNCTIONS ------------------------------------------------------------
void print_matrix(int matrix[][MAXDIM]) {
for (int r = 0; r < dim; r++) {
for (int c = 0; c < dim; c++)
printf("%7d ", matrix[r][c]);
printf("\n");
}
printf("\n");
}
void random_matrix(int matrix[][MAXDIM]) {
for (int r = 0; r < dim; r++)
for (int c = 0; c < dim; c++)
matrix[r][c] = rand() % 100;
}
float run(void (*f)()) {
float time = 0;
for (int i = 0; i < RUNS; i++) {
struct timeval start, end;
gettimeofday(&start, NULL);
(*f)();
gettimeofday(&end, NULL);
long s = start.tv_sec * 1000 + start.tv_usec / 1000;
long e = end.tv_sec * 1000 + end.tv_usec / 1000;
time += e - s;
}
return time / RUNS;
}
// SEQ MULTIPLICATION ----------------------------------------------------------
void mult_seq() {
for (int r = 0; r < dim; r++) {
for (int c = 0; c < dim; c++) {
matrix_c[r][c] = 0;
for (int i = 0; i < dim; i++)
matrix_c[r][c] += matrix_a[r][i] * matrix_b[i][c];
}
}
}
// MAIN ------------------------------------------------------------------------
int main(int argc, char* argv[]) {
srand(time(NULL));
dim = 512;
random_matrix(matrix_a);
random_matrix(matrix_b);
printf("%d ", dim);
printf("%f \n", run(mult_seq));
return 0;
}