-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_serial.c
102 lines (81 loc) · 2.35 KB
/
matrix_serial.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#define NROW 1024
#define NCOL NROW
#define NUM_THREADS 8
#define TEST_RESULTS
//Matrix A
int matrixA [NROW][NCOL];
//Matrix B
int matrixB [NROW][NCOL];
//Matrix C
int matrixC [NROW][NCOL];
//Temp array
int tempMatrix [NROW][NCOL];
//Output Array C
int outputMatrix [NROW][NCOL];
struct timeval startTime;
struct timeval finishTime;
double timeIntervalLength;
void verifyMatrixSum();
int main(int argc, char* argv[])
{
int i,j,k;
// Matrix initialization. Just filling with arbitrary numbers.
for(i=0;i<NROW;i++)
{
for(j=0;j<NCOL;j++)
{
matrixA[i][j]= (i + j)/128;
matrixB[i][j]= (j + j)/128;
matrixC[i][j]= (i + j)/128;
tempMatrix[i][j] = 0;
outputMatrix[i][j]= 0;
}
}
//Get the start time
gettimeofday(&startTime, NULL); /* START TIME */
//A*B*C
for(i=0;i<NROW;i++){
for(j=0; j<NCOL; j++){
for(k=0;k<NROW;k++){
// K iterates rows on matrixA and columns in matrixB
tempMatrix[i][j] += matrixA[i][k] * matrixB[k][j];
}
}
for(j=0; j<NCOL; j++) {
for (k = 0; k < NROW; k++) {
// K iterates rows on matrixA and columns in matrixB
outputMatrix[i][j] += tempMatrix[i][k] * matrixC[k][j];
}
}
}
//Get the end time
gettimeofday(&finishTime, NULL); /* END TIME */
//Calculate the interval length
timeIntervalLength = (double)(finishTime.tv_sec-startTime.tv_sec) * 1000000
+ (double)(finishTime.tv_usec-startTime.tv_usec);
timeIntervalLength=timeIntervalLength/1000;
#ifdef TEST_RESULTS
//[Verifying the matrix summation]
verifyMatrixSum();
#endif
//Print the interval length
printf("Interval length: %g msec.\n", timeIntervalLength);
return 0;
}
// Helper function to verify if the sum from parallel and serial versions match
void verifyMatrixSum() {
int i, j;
double totalSum;
totalSum=0;
//
for(i=0;i<NROW;i++){
for(j=0;j<NCOL;j++)
{
totalSum+=(double)outputMatrix[i][j];
}
}
printf("\nTotal Sum = %g\n",totalSum);
}