-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix-mul-naiive.c
85 lines (70 loc) · 1.95 KB
/
matrix-mul-naiive.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Experiement worth trying:
// matrix of size 1022,1023,1024,1025,1026
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
struct timeval time_stamp;
double timeStamp(){
double t;
gettimeofday(&time_stamp, NULL);
t = time_stamp.tv_sec + (time_stamp.tv_usec/1000000.0);
return t;
}
void multiply(int a[], int b[], int c[], int dimension){
for (int i = 0; i < dimension; i++)
for (int j = 0; j < dimension; j++)
for (int k = 0; k < dimension; k++)
c[dimension*i+j] += a[dimension*i+k] * b[dimension*k+j];
}
void print_square_matrix(int mat[], int dimension){
for (int i=0; i < dimension; i++){
for(int j=0; j<dimension; j++)
{
printf("%d ", mat[dimension*i + j]);
}
printf("\n");
}
}
int main(int argc, char** argv){
// Check to make sure we received a command line option
if(argc < 2) {
fprintf(stderr, "Usage: %s <list size>\n", argv[0]);
return 1;
}
// dimension of square matrix
int dim = atoi(argv[1]);
int *A, *B, *C;
double start, end;
// Allocation memory in the heap for matrices
A = (int *)malloc(dim*dim*sizeof(int));
B = (int *)malloc(dim*dim*sizeof(int));
C = (int *)malloc(dim*dim*sizeof(int));
// Use current time as seed for random generator
srand(time(0));
for (int i = 0; i < dim; i++){
for (int j = 0; j < dim; j++){
A[dim*i+j] = rand();
B[dim*i+j] = rand();
// A[dim*i+j] = rand() % (10 + 1 - 0) + 0;
// B[dim*i+j] = rand() % (10 + 1 - 0) + 0;
C[dim*i+j] = 0;
}
}
// printf("matrix A: \n");
// print_square_matrix(A, dim);
// printf("matrix B: \n");
// print_square_matrix(B, dim);
start = timeStamp();
printf("Start matrix multiplication:\n");
multiply(A, B, C, dim);
end = timeStamp();
printf("Duration (secs):%f\n", end-start);
// printf("Product matrix: \n");
// print_square_matrix(C, dim);
// Free memory
free(A);
free(B);
free(C);
return 0;
}