-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiply.cpp
More file actions
92 lines (76 loc) · 2.86 KB
/
multiply.cpp
File metadata and controls
92 lines (76 loc) · 2.86 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <chrono>
#include <iomanip>
#include <filesystem>
#include <iomanip>
using namespace std;
using namespace chrono;
namespace fs = std::filesystem;
using Matrix = vector<vector<int>>;
Matrix readCSVMatrix(const string& filename, int size) {
ifstream file(filename);
Matrix mat(size, vector<int>(size));
string line;
for (int i = 0; i < size && getline(file, line); ++i) {
stringstream ss(line);
string value;
for (int j = 0; j < size && getline(ss, value, ','); ++j)
mat[i][j] = stoi(value);
}
return mat;
}
void writeCSVMatrix(const string& filename, const Matrix& mat) {
ofstream file(filename);
for (const auto& row : mat) {
for (size_t j = 0; j < row.size(); ++j) {
file << row[j];
if (j != row.size() - 1) file << ",";
}
file << "\n";
}
}
Matrix multiply(const Matrix& A, const Matrix& B, int size) {
Matrix C(size, vector<int>(size, 0));
for (int i = 0; i < size; ++i)
for (int j = 0; j < size; ++j)
for (int k = 0; k < size; ++k)
C[i][j] += A[i][k] * B[k][j];
return C;
}
int main() {
fs::create_directory("results");
ofstream benchmark("results/benchmark.csv");
benchmark << "Size,Time_ms\n";
Matrix fullA = readCSVMatrix("matrices/matrix_A.csv", 1000);
Matrix fullB = readCSVMatrix("matrices/matrix_B.csv", 1000);
for (int size = 100; size <= 1000; size += 100) {
Matrix A(size, vector<int>(size));
Matrix B(size, vector<int>(size));
for (int i = 0; i < size; ++i)
for (int j = 0; j < size; ++j) {
A[i][j] = fullA[i][j];
B[i][j] = fullB[i][j];
}
auto start = high_resolution_clock::now();
Matrix C = multiply(A, B, size);
auto end = high_resolution_clock::now();
double duration = duration_cast<microseconds>(end - start).count() / 1000.0;
benchmark << size << "," << fixed << setprecision(3) << duration << "\n";
string dir_name = "results/" + to_string(size) + "x" + to_string(size);
fs::create_directory(dir_name);
writeCSVMatrix(dir_name + "/result.csv", C);
ofstream timefile(dir_name + "/time.txt");
timefile << "Размер: " << size << "x" << size << "\n";
timefile << "Объём задачи: " << size * size << " элементов\n";
timefile << "Время выполнения: " << fixed << setprecision(3) << duration << " мс\n";
timefile.close();
cout << "📏 Размер: " << setw(9) << right << (to_string(size) + "x" + to_string(size))
<< " | ⏱ Время: " << setw(10) << fixed << setprecision(3) << duration << " мс"
<< endl;
}
benchmark.close();
return 0;
}