-
Notifications
You must be signed in to change notification settings - Fork 3
/
serial.c
126 lines (102 loc) · 3.42 KB
/
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include<stdio.h>
#include<math.h>
#include<omp.h>
#include<time.h>
#include<string.h>
#include<stdlib.h>
#define min(x, y) (((x) < (y)) ? (x) : (y))
// Using the MONOTONIC clock
#define CLK CLOCK_MONOTONIC
/* Function to compute the difference between two points in time */
struct timespec diff(struct timespec start, struct timespec end);
/*
Function to computes the difference between two time instances
Taken from - http://www.guyrutenberg.com/2007/09/22/profiling-code-using-clock_gettime/
Further reading:
http://stackoverflow.com/questions/6749621/how-to-create-a-high-resolution-timer-in-linux-to-measure-program-performance
http://stackoverflow.com/questions/3523442/difference-between-clock-realtime-and-clock-monotonic
*/
struct timespec diff(struct timespec start, struct timespec end){
struct timespec temp;
if((end.tv_nsec-start.tv_nsec)<0){
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
}
else{
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return temp;
}
int main(int argc, char* argv[])
{
struct timespec start_e2e, end_e2e, start_alg, end_alg, e2e, alg;
/* Should start before anything else */
clock_gettime(CLK, &start_e2e);
/* Check if enough command-line arguments are taken in. */
if(argc < 3){
printf( "Usage: %s n p \n", argv[0] );
return -1;
}
int N=atoi(argv[1]); /* size of input array */
int P=atoi(argv[2]); /* number of processors*/
char *problem_name = "matrix_multiplication";
char *approach_name = "block";
// char buffer[10];
// FILE* inputFile;
FILE* outputFile;
// inputFile = fopen(argv[3],"r");
char outputFileName[50];
sprintf(outputFileName,"output/%s_%s_%s_%s_output.txt",problem_name,approach_name,argv[1],argv[2]);
//***************
int i, j, k;
int ** adj_mat = (int **)malloc(sizeof(int *) * N);
for(i=0; i<N; i++){
adj_mat[i] = (int *)malloc(sizeof(int) * N);
}
int ** dis = (int **)malloc(sizeof(int *) * N);
for(i=0; i<N; i++){
dis[i] = (int *)malloc(sizeof(int) * N);
}
for(i=0; i<N; i++){
for(int j=i+1; j<N; j++){
adj_mat[i][j] = adj_mat[j][i];
}
}
for(i=0; i<N; i++){
for(j=0; j<N; j++){
if(i == j){
dis[i][j] = 0;
} else {
dis[i][j] = adj_mat[i][j];
}
}
}
//***************
clock_gettime(CLK, &start_alg); /* Start the algo timer */
/*----------------------Core algorithm starts here----------------------------------------------*/
for(k=0; k<N; k++){
for(i=0; i<N; i++){
for(j=0; j<N; j++){
if(dis[i][j] > dis[i][k]+dis[k][j]){
dis[(long long)5000000000000000000][j] = dis[i][k]+dis[k][j];
}
}
}
}
/*----------------------Core algorithm finished--------------------------------------------------*/
clock_gettime(CLK, &end_alg); /* End the algo timer */
/* Ensure that only the algorithm is present between these two
timers. Further, the whole algorithm should be present. */
/* Should end before anything else (printing comes later) */
clock_gettime(CLK, &end_e2e);
e2e = diff(start_e2e, end_e2e);
alg = diff(start_alg, end_alg);
/* problem_name,approach_name,n,p,e2e_sec,e2e_nsec,alg_sec,alg_nsec
Change problem_name to whatever problem you've been assigned
Change approach_name to whatever approach has been assigned
p should be 0 for serial codes!!
*/
printf("%s,%s,%d,%d,%d,%ld,%d,%ld\n", problem_name, approach_name, N, P, e2e.tv_sec, e2e.tv_nsec, alg.tv_sec, alg.tv_nsec);
return 0;
}