-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix_opt.cpp
More file actions
190 lines (135 loc) · 4.23 KB
/
Matrix_opt.cpp
File metadata and controls
190 lines (135 loc) · 4.23 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Include Header files
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <assert.h>
#include "Timer.h"
#ifdef USE_LIKWID
extern "C"{
#include <likwid.h>
}
#endif
using namespace std;
// Main function passing argument
int main(int argc, char *argv[])
{
//Declaring the variables
double value_A;
double value_B;
vector<double> v_A;
vector<double> v_B;
// Declaring object of Fstream header file
ifstream file_A;
ifstream file_B;
ofstream file_C;
// Opening The files
file_A.open(argv[1]);
file_B.open(argv[2]);
file_C.open(argv[3]);
// Reading till untill end of file has reached
while(file_A>>value_A)
{
//file>> arr[n]; // putting it in array
v_A.push_back(value_A);
//++n;
}
// Reading the second file
while (file_B>> value_B)
{
//Reading the contents of the second file
v_B.push_back(value_B);
}
// Closing the files
file_A.close();
file_B.close();
// Fetching the number of rows and columns
int numRow_A= v_A[0];
int numCol_A = v_A[1];
//int numRow_B = v_B[0];
int numCol_B = v_B[1];
int numRow_C = numRow_A;
int numCol_C = numCol_B ;
int size_C = numRow_C*numCol_C;
int size_B = numCol_A*numCol_B;
//Declaring the vector
if(argc != 4)
cout<<"There is some error";
vector<double> v_C(size_C + 2);
vector<double> v_B_T(size_B + 2);
v_C[0]=numRow_C;
v_C[1] = numCol_C;
for(int i=0;i<numCol_A;++i)
{
for(int j=0;j<numCol_B;++j)
{
v_B_T[2+j*numCol_A + i ]= v_B[2+i*numCol_B+j];
}
}
assert(numCol_A == numRow_B);
siwir:: Timer timer;
double timeTaken;
// bLOCK Matrix Matrix multiplication O(n3)
#ifdef USE_LIKWID
likwid_markerInit();
likwid_markerStartRegion("vector");
#endif
int s = 16;
double temp=0.0;
// Correct version of block matrix multiplication
for(int jj=0;jj<numCol_B;jj+= s)
{
for(int kk=0;kk<numCol_A;kk+= s)
{
for(int i=0;i<numRow_A;i++)
for(int j = jj; j<((jj+s)>numCol_B?numCol_B:(jj+s)); j++)
{
temp = 0.0;
for(int k = kk; k<((kk+s)>numCol_A?numCol_A:(kk+s)); k++)
{
//temp += a[i][k]*b[k][j];
//temp += a[i*row_A + k]*b[k*col_B+ j];
//temp += v_A[2+i*numCol_A + k]*v_B[2+k*numCol_B+ j]; // Normal Logic
temp += v_A[2+i*numCol_A + k]*v_B_T[2+j*numCol_A+ k]; // Transpose
}
//c[i*numCol_C+ j] += temp;
v_C[2+i*numCol_C+ j]+= temp ;
}
}
}
/* Sagar Logic - Incorrect result
for(int jj=0;jj<numCol_B;jj+= s)
{
for(int kk=0;kk<numCol_A;kk+= s)
{
for(int i=0;i<numRow_A;i++)
{
for(int j = jj; j<(((jj+s)>numCol_B)?numCol_B:(jj+s)); j++)
{
temp = 0.0;
for(int k = kk; k<(((kk+s)>numCol_A)?numCol_A:(kk+s)); k++)
{
//temp += a[i][k]*b[k][j];
//temp += a[i*row_A + k]*b[k*col_B+ j];
temp += v_A[i*numCol_A + k]*v_B[k*numCol_B+ j];
//std::cout<< temp<<endl;
}
//c[i*numCol_C+ j] += temp;
v_C.push_back(temp);
}
}
}
}
*/
timeTaken = timer.elapsed();
#ifdef USE_LIKWID
likwid_markerStopRegion("vector");
likwid_markerClose();
#endif
file_C<< v_C[0]<<" "<<v_C[1]<<endl;
cout<<"\nTime taken in matrix multiplication is: " << timeTaken << endl;
for (int i=2;i<(size_C + 2);i++)
file_C <<v_C[i]<<endl;
file_C.close();
return 0;
}