-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmatrix_generator.cpp
More file actions
153 lines (139 loc) · 6.44 KB
/
matrix_generator.cpp
File metadata and controls
153 lines (139 loc) · 6.44 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
// matrix_generator.cpp
#include "matrix_generator.h"
#include <type_traits>
#include <limits>
#include <random>
#include <algorithm>
namespace mg {
template<typename T>
Matrix<T> generate_dense_block(int rows, int cols, std::mt19937 &rng) {
Matrix<T> block(rows, std::vector<T>(cols));
if constexpr (std::is_integral_v<T>) {
// avoid full int64 range for portability; clamp to -1000..1000 for ints
std::uniform_int_distribution<long long> dist(-1000, 1000);
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
block[i][j] = static_cast<T>(dist(rng));
} else {
std::uniform_real_distribution<double> dist(-1.0, 1.0);
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
block[i][j] = static_cast<T>(dist(rng));
}
return block;
}
template<typename T>
Matrix<T> generate_matrix(int num_rows,
int num_cols,
double sparsity,
const std::string &pattern,
int blocksize,
unsigned int seed) {
Matrix<T> mat(num_rows, std::vector<T>(num_cols));
std::mt19937 rng(seed);
auto fill_block = [&](int bi, int bj) {
int start_row = bi * blocksize;
int start_col = bj * blocksize;
int end_row = std::min(start_row + blocksize, num_rows);
int end_col = std::min(start_col + blocksize, num_cols);
auto block = generate_dense_block<T>(end_row - start_row, end_col - start_col, rng);
for (int i = start_row; i < end_row; ++i)
for (int j = start_col; j < end_col; ++j)
mat[i][j] = block[i - start_row][j - start_col];
};
if (pattern == "random") {
// Fill randomly with probability of being non-zero = 1 - sparsity
std::bernoulli_distribution keep(1.0 - sparsity);
for (int i = 0; i < num_rows; ++i) {
for (int j = 0; j < num_cols; ++j) {
if (keep(rng)) {
if constexpr (std::is_integral_v<T>) {
std::uniform_int_distribution<int> d(-1000, 1000);
mat[i][j] = static_cast<T>(d(rng));
} else {
std::uniform_real_distribution<double> d(-1.0, 1.0);
mat[i][j] = static_cast<T>(d(rng));
}
} else {
mat[i][j] = static_cast<T>(0);
}
}
}
} else if (pattern == "checkerboard") {
int nbi = (num_rows + blocksize - 1) / blocksize;
int nbj = (num_cols + blocksize - 1) / blocksize;
for (int i = 0; i < nbi; ++i)
for (int j = 0; j < nbj; ++j)
if (((i % 2) ^ (j % 2)) == 0)
fill_block(i, j);
} else if (pattern == "diagonal") {
int minrc = std::min(num_rows, num_cols);
for (int i = 0; i < minrc; ++i) {
auto block = generate_dense_block<T>(1, 1, rng);
mat[i][i] = block[0][0];
}
} else if (pattern == "blockdiagonal") {
int n = std::min(num_rows, num_cols) / blocksize + 1;
for (int i = 0; i < n; ++i) fill_block(i, i);
} else if (pattern == "blockrandom") {
int nbi = (num_rows + blocksize - 1) / blocksize;
int nbj = (num_cols + blocksize - 1) / blocksize;
std::bernoulli_distribution keep(1.0 - sparsity);
for (int i = 0; i < nbi; ++i)
for (int j = 0; j < nbj; ++j)
if (keep(rng)) fill_block(i, j);
} else if (pattern == "pattern64by16") {
// Rectangular block-random pattern with block height 64 and block width 16
const int brow = 64;
const int bcol = 16;
int nbi = (num_rows + brow - 1) / brow;
int nbj = (num_cols + bcol - 1) / bcol;
std::bernoulli_distribution keep(1.0 - sparsity);
for (int bi = 0; bi < nbi; ++bi) {
for (int bj = 0; bj < nbj; ++bj) {
if (!keep(rng)) continue;
int start_row = bi * brow;
int start_col = bj * bcol;
int end_row = std::min(start_row + brow, num_rows);
int end_col = std::min(start_col + bcol, num_cols);
auto block = generate_dense_block<T>(end_row - start_row, end_col - start_col, rng);
for (int i = start_row; i < end_row; ++i)
for (int j = start_col; j < end_col; ++j)
mat[i][j] = block[i - start_row][j - start_col];
}
}
} else if (pattern == "pattern32by16") {
// Rectangular block-random pattern with block height 32 and block width 16
const int brow = 32;
const int bcol = 16;
int nbi = (num_rows + brow - 1) / brow;
int nbj = (num_cols + bcol - 1) / bcol;
std::bernoulli_distribution keep(1.0 - sparsity);
for (int bi = 0; bi < nbi; ++bi) {
for (int bj = 0; bj < nbj; ++bj) {
if (!keep(rng)) continue;
int start_row = bi * brow;
int start_col = bj * bcol;
int end_row = std::min(start_row + brow, num_rows);
int end_col = std::min(start_col + bcol, num_cols);
auto block = generate_dense_block<T>(end_row - start_row, end_col - start_col, rng);
for (int i = start_row; i < end_row; ++i)
for (int j = start_col; j < end_col; ++j)
mat[i][j] = block[i - start_row][j - start_col];
}
}
} else {
throw std::invalid_argument("Unknown pattern: " + pattern);
}
return mat;
}
// Explicit template instantiations for common types (to avoid linker issues)
template Matrix<float> generate_matrix<float>(int, int, double, const std::string&, int, unsigned int);
template Matrix<double> generate_matrix<double>(int, int, double, const std::string&, int, unsigned int);
template Matrix<int> generate_matrix<int>(int, int, double, const std::string&, int, unsigned int);
template Matrix<long long> generate_matrix<long long>(int, int, double, const std::string&, int, unsigned int);
template Matrix<float> generate_dense_block<float>(int, int, std::mt19937 &);
template Matrix<double> generate_dense_block<double>(int, int, std::mt19937 &);
template Matrix<int> generate_dense_block<int>(int, int, std::mt19937 &);
template Matrix<long long> generate_dense_block<long long>(int, int, std::mt19937 &);
} // namespace mg