-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
101 lines (85 loc) · 3.34 KB
/
Copy pathmain.cpp
File metadata and controls
101 lines (85 loc) · 3.34 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
#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <iomanip>
using namespace std;
using namespace chrono;
// Function for block matrix multiplication
void blockMatrixMultiply(const vector<vector<double>>& A,
const vector<vector<double>>& B,
vector<vector<double>>& C, int blockSize) {
int n = A.size();
for (int i = 0; i < n; i += blockSize) {
for (int j = 0; j < n; j += blockSize) {
for (int k = 0; k < n; k += blockSize) {
for (int ii = i; ii < min(i + blockSize, n); ++ii) {
for (int jj = j; jj < min(j + blockSize, n); ++jj) {
double temp = 0.0;
for (int kk = k; kk < min(k + blockSize, n); ++kk) {
temp += A[ii][kk] * B[kk][jj];
}
C[ii][jj] += temp;
}
}
}
}
}
}
// Function for standard matrix multiplication
void regularMatrixMultiply(const vector<vector<double>>& A,
const vector<vector<double>>& B,
vector<vector<double>>& C) {
int n = A.size();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < n; ++k) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
}
// Function to generate a random matrix
vector<vector<double>> generateRandomMatrix(int size) {
vector<vector<double>> matrix(size, vector<double>(size));
random_device rd;
mt19937 gen(rd());
uniform_real_distribution<> dis(0.0, 1.0);
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
matrix[i][j] = dis(gen);
}
}
return matrix;
}
// Function to measure execution time
template <typename Func, typename... Args>
double measureExecutionTime(Func func, Args&&... args) {
auto start = high_resolution_clock::now();
func(forward<Args>(args)...);
auto end = high_resolution_clock::now();
return duration<double>(end - start).count();
}
// Main function
int main() {
vector<int> matrixSizes = {10, 100, 1000, 1500};
int blockSize = 64;
for (int size : matrixSizes) {
cout << "\nTesting for matrix size: " << size << "x" << size << endl;
try {
auto A = generateRandomMatrix(size);
auto B = generateRandomMatrix(size);
auto C_block = vector<vector<double>>(size, vector<double>(size, 0.0));
auto C_regular = vector<vector<double>>(size, vector<double>(size, 0.0));
double timeBlock = measureExecutionTime(blockMatrixMultiply, A, B, C_block, blockSize);
double timeRegular = measureExecutionTime(regularMatrixMultiply, A, B, C_regular);
cout << fixed << setprecision(6);
cout << "Block matrix multiplication time: " << timeBlock << " seconds" << endl;
cout << "Regular matrix multiplication time: " << timeRegular << " seconds" << endl;
cout << "Speed difference: " << timeRegular - timeBlock << " seconds" << endl;
} catch (const bad_alloc&) {
cout << "Matrix size " << size << "x" << size << " is too large to handle with available memory." << endl;
}
}
return 0;
}