-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_mult.cpp
More file actions
180 lines (126 loc) · 3.59 KB
/
matrix_mult.cpp
File metadata and controls
180 lines (126 loc) · 3.59 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
// Include Header files
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <assert.h>
#include "Timer.h"
using namespace std;
extern "C" {
#include <cblas.h>
}
#ifdef USE_LIKWID
extern "C"{
#include <likwid.h>
}
#endif
//using namespace std;
// Main function passing argumentg
int main(int argc, char *argv[])
{
std::vector<double> v_A;
std::vector<double> v_B;
std::vector<double> v_C;
//Declaring the variables
double value_A;
double value_B;
// Declaring object of Fstream header file
std::ifstream file_A;
std::ifstream file_B;
// Opening The files
file_A.open(argv[1]);
file_B.open(argv[2]);
// 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();
// Getting the size of vector s
int size_A;
int size_B;
size_A = v_A.size();
size_B = v_B.size();
// Printing the values stored in array
/*
for (int i =0; i < vsize ;++i)
cout<<"The value of vectors are "<<v[i]<<endl;
for (int j=0;j< vsize1;++j)
cout<<"The values of second files are "<<v1[j]<<endl;
*/
// 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;
v_C.push_back(numRow_C);
v_C.push_back(numCol_C);
assert(numCol_A == numRow_B);
/*for(int i=0;i< size_A;++i)
{
cout<<"v_A["<< i <<"]: " << v_A[i] << ", v_B["<< i <<"]: " << v_B[i] << endl;
}*/
//cout<<row<<" "<<row2<<endl;
//cout<<vsize<<" "<<vsize1<<endl;
//cout<<col<<" "<<col2<<endl;
#ifdef USE_LIKWID
likwid_markerInit();
likwid_markerStartRegion("vector");
#endif
//defining the timer
siwir:: Timer timer;
// Naive Matrix Matrix multiplication O(n3)
double timeTaken = 0.0;
double temp = 0.0;
int row = 0;
int col = 0;
for(int i=0; i< size_C ; ++i)
{
//cout<<"i is: "<<i <<endl;
temp = 0.0;
row = i/numCol_B;
col = i % numCol_B;
//cout<<"\nrow is: "<<row<<endl;
//cout<<"\ncol is:"<<col<<endl;
for(int j=0 ; j < numCol_A ; ++j)
{
//cout<<"j is: " << j << endl;
// cout<<"\ntemp: "<<temp<<endl;
temp += v_A[2+ row*numCol_A + j] * v_B[2 + col+ j*numCol_B];
}
//cout<<"Now the temp is : " << temp << endl;
v_C.push_back(temp);
//cout<<"the temp is : " << temp<< endl;
}
timeTaken = timer.elapsed();
#ifdef USE_LIKWID
likwid_markerInit();
likwid_markerStartRegion("vector");
#endif
cout<<"\nTime taken in matrix multiplication :" << timeTaken << " seconds \nDisplay of matrix C\n";
for(int i=0 ; i < numRow_C; ++i)
{
//cout<<"i is: "<<i<<endl;
for(int j=0 ; j < numCol_C; ++j)
{
//cout<<"j is: " <<j<<endl;
cout<< v_C[2+i*numCol_C + j]<<"\t";
}
cout<<endl;
}
// Storing the result of computation in another matrix
return 0;
}