-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNaiveMatrix.cc
executable file
·120 lines (100 loc) · 2.79 KB
/
NaiveMatrix.cc
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
#include <assert.h>
#include <iostream.h>
#include <math.h>
#include <stdlib.h>
#include "ipm-2.0a/IPM_timer.h"
void timer(double);
void matmultNaive(int);
int main(int argc, char* argv[]) {
int size = atoi(argv[1]);
matmultNaive(size);
return 0;
}
void timer(double bound) {
IPM_timer t;
// defalut = 200
char report[IPM_TIMER_REPORT_LEN+3];
int i;
float x = 0;
// initialize the IPM timer to 0
IPM_timer_clear(&t);
IPM_timer_start (&t);
for (i = 0; i < bound; i++) {
x=x*.99 + 1.0;
x=x*.99 + 1.0;
x=x*.99 + 1.0;
x=x*.99 + 1.0;
}
IPM_timer_stop(&t);
// format the report string
IPM_timer_report("timing computations", &t, report);
cout << report;
float time = IPM_timer_read(&t);
cout << "Elapsed time: " << time << endl;
cout << "MFlops: "<< (8.*bound/time)/1000000 << endl;
}
// Multiplies two square matrices of size n in naive 2n^3 operations
void matmultNaive(int n) {
IPM_timer t;
char report[IPM_TIMER_REPORT_LEN+3];
// create two square matrices
float matrix1 [n][n];
float matrix2 [n][n];
float result [n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// populate the matrices with data
matrix1 [i][j] = j*.99 + 1.0;
matrix2 [i][j] = j*.99 + 1.0;
}
}
// initialize the timer to 0
IPM_timer_clear(&t);
IPM_timer_start(&t);
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++) { // multiply them out
result[i][j] = 0;
for (int r = 0; r<n; r++) {
result[i][j] = result[i][j] + matrix1[i][r] * matrix2[r][j];
}
}
}
IPM_timer_stop(&t);
cout << "*************" << endl;
cout << "* Matrix 1 *" << endl;
cout << "*************\n" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << matrix1[i][j] << " ";
}
cout << endl;
}
cout << "*************" << endl;
cout << "* Matrix 2 *" << endl;
cout << "*************\n" << endl;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++) {
cout << matrix2[i][j] << " ";
}
cout << endl;
}
cout << "************" << endl;
cout << "* Product: *" << endl;
cout << "************\n" << endl;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++) {
cout << result[i][j] << " ";
}
cout << endl;
}
cout << endl;
// Format the report string for printing to stdout
IPM_timer_report("timing", &t, report);
cout << report;
// People often mean MFLOPS to mean different things, but a general
// definition would be the number of full word-size fp multiply operations
// that can be performed per second (the M stands for 'Million').
float time = IPM_timer_read(&t);
cout << "Elapsed time to compute: " << time *pow(10,6) << "u"<< endl;
cout << "MFlops: "<< ((2*pow(n, 3))/time)/1000000 << endl;
}