-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialSolution.cpp
130 lines (102 loc) · 2.54 KB
/
SerialSolution.cpp
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
// Hw1.cpp : Defines the entry point for the console application.
#include <iostream>
#include <fstream>
#include <chrono>
using namespace std;
using namespace std::chrono;
const int MAX_WIDTH = 720;
const int MAX_HEIGHT = 720;
void VectorMatrixMultiplication(int w, int h, double *i_matrix, double *i_vector, double *o_vector);
void GetMatrixFromFile(int &w, int &h, double *i_matrix);
void GetVectorFromFile(int &h, double *i_vector);
void WriteResultToFile(int h, double *o_vector);
int main()
{
int m_w = 0, m_h = 0, v_h;
double i_matrix[MAX_WIDTH * MAX_HEIGHT];
double i_vector[MAX_WIDTH];
double o_vector[MAX_HEIGHT];
// Get input matrix from file.
GetMatrixFromFile(m_w, m_h, i_matrix);
// Get input vector from file.
GetVectorFromFile(v_h, i_vector);
auto start = high_resolution_clock::now();
// Apply algorithm.
VectorMatrixMultiplication(m_w, m_h, i_matrix, i_vector, o_vector);
WriteResultToFile(m_h, o_vector);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Time taken by function: " << duration.count() << " microseconds" << endl;
return 0;
}
void GetVectorFromFile(int &h, double *i_vector)
{
ifstream infile;
infile.open("BigX.txt");
// First line consists width and height.
int w = 0;/*unused*/
infile >> h >> w;
if (h > MAX_HEIGHT)
{
h = MAX_HEIGHT;
}
// Else do nothing.
int index = 0;
while (!infile.eof() && index < MAX_HEIGHT)
{
infile >> i_vector[index];
index++;
}
// End of the loop.
infile.close();
}
void GetMatrixFromFile(int &w, int &h, double *i_matrix)
{
ifstream infile;
infile.open("BigA.txt");
// First line consists width and height.
infile >> h >> w;
if (h > MAX_HEIGHT)
{
h = MAX_HEIGHT;
}
// Else do nothing.
if (w > MAX_WIDTH)
{
w = MAX_WIDTH;
}
// Else do nothing.
long long index = 0;
while (!infile.eof() && index < (MAX_HEIGHT * MAX_WIDTH))
{
infile >> i_matrix[index];
index++;
}
// End of the loop.
infile.close();
}
void WriteResultToFile(int h, double *o_vector)
{
ofstream oVectorFile;
oVectorFile.open ("serialResult.txt");
oVectorFile<<h<<" "<<"1\n";
for (int i = 0; i < h; i++)
{
oVectorFile <<o_vector[i]<<"\n";
}
// End of the loop.
oVectorFile.close();
}
void VectorMatrixMultiplication(int w, int h, double *i_matrix, double *i_vector, double *o_vector)
{
for (int i = 0; i < h; i++)
{
o_vector[i] = 0.0;
for (int j = 0; j < w; j++)
{
o_vector[i] += i_vector[j] * i_matrix[(i * w) + j];
}
// End of the loop.
}
// End of the loop.
}