-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparseMatrixAddition.cpp
More file actions
276 lines (198 loc) · 5.26 KB
/
sparseMatrixAddition.cpp
File metadata and controls
276 lines (198 loc) · 5.26 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include<iostream>
#include<cmath>
#include<climits>
#include<vector>
#include<algorithm>
#include<time.h>
#include<fstream>
using namespace std;
//holds the matrix element by coordinates wise.
struct matrixElement{
int row;
int col;
int value;
};
//Functions declarations
bool checkElement(vector<matrixElement>, int, int);
int sparseMatrixAddition(vector <matrixElement>, vector<matrixElement>);
void noramlMatrixAddition(int, int, int);
int main(){
int m,n,p, size = 9;
float sp;
cout << "Enter the matrices dimensions m,n and p: ";
cin >> m >> n >> p;
ofstream file_writer("runtime.txt");
file_writer << "Run time for martix of dimensions " << m << "*" << n << endl;
file_writer << "(sparsity -> runtime)" << endl;
for(int i=0; i<size; i++){
cout << "Enter the matrix sparsity value [0-1]: ";
cin >> sp;
//calculating how many non zero element is required in matrix A
//calulated as per question
int total_non_zero_value;
total_non_zero_value = round((m*n) * sp);
// Triplet Representation(row, col and value)
//For matrix A
vector <matrixElement> matrixA;
//For matrix B
vector<matrixElement> matrixB;
int countA = 0;
matrixElement meA;
//Generating random row and column for matrix A
srand(time(0));
for(;;){
if(countA == total_non_zero_value){
break;
}
meA.row = rand()%m;
meA.col = rand()%n;
meA.value = rand()%100;
if(matrixA.size() == 0){
matrixA.push_back(meA);
countA += 1;
}else{
if(checkElement(matrixA, meA.row, meA.col) == false){
matrixA.push_back(meA);
countA += 1;
}
}
}
//sort the matrix by row wise
sort(matrixA.begin(), matrixA.end(), [](matrixElement a, matrixElement b){
return a.row < b.row;
});
//Generating random row and column for matrix B
int countB = 0;
matrixElement meB;
for(;;){
if(countB = total_non_zero_value){
break;
}
meB.row = rand()%n;
meB.col = rand()%p;
meB.value = rand()%100;
if(matrixB.size() == 0){
matrixB.push_back(meB);
countB += 1;
}else{
if(checkElement(matrixB, meB.row, meB.col) == false){
matrixB.push_back(meB);
countB += 1;
}
}
}
//sort the matrix by row wise
sort(matrixB.begin(), matrixB.end(), [](matrixElement a, matrixElement b){
return a.row < b.row;
});
reverse(matrixA.begin(), matrixA.end());
reverse(matrixB.begin(), matrixB.end());
clock_t start, stop;
start = clock();
sparseMatrixAddition(matrixA, matrixB);
stop = clock();
double runtime = double(stop - start)/CLOCKS_PER_SEC;
file_writer << "(" << sp << " -> " << runtime << " seconds)" << endl;
}
cout << "Run time for different sparsity value is computed \nOpen runtime.txt file for data" << endl;
//Calculating the run time for normal matrix addition
clock_t start1, stop1;
file_writer << "\n\nThe normal matrix addition of dimensions " << m << "*" << n << " takes" << endl;
start1 = clock();
noramlMatrixAddition(m,n,p);
stop1 = clock();
double runtime1 = double(stop1 - start1)/CLOCKS_PER_SEC;
file_writer << runtime1 << "seconds" << endl;
file_writer.close();
return 0;
}
int sparseMatrixAddition(vector<matrixElement> matrixA, vector<matrixElement> matrixB){
vector <matrixElement> matrixR;
int rA, cA, vA, rB, cB, vB;
matrixElement meR;
while(!matrixA.empty() || !matrixB.empty()){
if(matrixA.size() != 0){
rA = matrixA[matrixA.size()-1].row;
cA = matrixA[matrixA.size()-1].col;
vA = matrixA[matrixA.size()-1].value;
}else{
rA = INT_MAX;
cA = INT_MAX;
vA = INT_MAX;
}
if(matrixB.size() != 0){
rB = matrixB[matrixB.size()-1].row;
cB = matrixB[matrixB.size()-1].col;
vB = matrixB[matrixB.size()-1].value;
}else{
rB = INT_MAX;
cB = INT_MAX;
vB = INT_MAX;
}
if(rA < rB){
matrixR.push_back(matrixA[matrixA.size()-1]);
matrixA.pop_back();
}
else if(rB < rA){
matrixR.push_back(matrixB[matrixB.size()-1]);
matrixB.pop_back();
}
else{
if(cA < cB){
matrixR.push_back(matrixA[matrixA.size()-1]);
matrixA.pop_back();
}
else if(cB < cA){
matrixR.push_back(matrixB[matrixB.size()-1]);
matrixB.pop_back();
}
else{
meR.row = rA;
meR.col = cA;
meR.value = vA + vB;
matrixR.push_back(meR);
matrixA.pop_back();
matrixB.pop_back();
}
}
}
/*
//Accessing the resultant matrix R
cout << "Result Matrix: ";
for(int i=0; i<matrixR.size(); i++){
cout << "(" << matrixR[i].row << "," << matrixR[i].col << "," << matrixR[i].value << "), ";
}
cout << endl;
*/
return 0;
}
//Function to check that there is no overlap of the coordinates
bool checkElement(vector<matrixElement> matrix, int row, int col){
for(int i=0; i<matrix.size(); i++){
if(matrix[i].row == row && matrix[i].col == col){
return true;
}
}
return false;
}
void noramlMatrixAddition(int m, int n, int p){
int matrixA[m][n], matrixB[n][p], matrixR[m][p];
//Creating matrix of size m*n randomnly
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
matrixA[i][j] = rand()%100;
}
}
//Creating Matrix of size n*p randomnly
for(int i=0; i<n; i++){
for(int j=0; j<p; j++){
matrixB[i][j] = rand()%100;
}
}
//Doing normal matrix Multiplication
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
matrixR[i][j] = matrixA[i][j] + matrixB[i][j];
}
}
}