-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_lcp_pattern_generator.h
239 lines (193 loc) · 8 KB
/
test_lcp_pattern_generator.h
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
#ifndef __TEST_LCP_PATTERN_GENERATOR_H__
#define __TEST_LCP_PATTERN_GENERATOR_H__
#include <string>
#include <fstream>
#include <algorithm>
#include "test_pattern_generation.h"
const std::string SAMPLE_DATA_32_MU02 = "sample_data_32_mu0.2.txt";
const std::string SAMPLE_DATA_64_MU02 = "sample_data_64_mu0.2.txt";
const std::string SAMPLE_DATA_128_MU02 = "sample_data_128_mu0.2.txt";
const std::string SAMPLE_DATA_256_MU02 = "sample_data_256_mu0.2.txt";
const std::string SAMPLE_DATA_512_MU02 = "sample_data_512_mu0.2.txt";
const std::string SAMPLE_DATA_1024_MU02 = "sample_data_1024_mu0.2.txt";
const std::string SAMPLE_DATA_32_MU08 = "sample_data_32_mu0.8.txt";
const std::string SAMPLE_DATA_64_MU08 = "sample_data_64_mu0.8.txt";
const std::string SAMPLE_DATA_128_MU08 = "sample_data_128_mu0.8.txt";
const std::string SAMPLE_DATA_256_MU08 = "sample_data_256_mu0.8.txt";
const std::string SAMPLE_DATA_512_MU08 = "sample_data_512_mu0.8.txt";
const std::string SAMPLE_DATA_1024_MU08 = "sample_data_1024_mu0.8.txt";
const std::string SAMPLE_DATA_32_SYM = "sample_data_32_sym.txt";
const std::string SAMPLE_DATA_64_SYM = "sample_data_64_sym.txt";
const std::string SAMPLE_DATA_128_SYM = "sample_data_128_sym.txt";
const std::string SAMPLE_DATA_256_SYM = "sample_data_256_sym.txt";
const std::string SAMPLE_DATA_512_SYM = "sample_data_512_sym.txt";
const std::string SAMPLE_DATA_1024_SYM = "sample_data_1024_sym.txt";
typedef enum _LCPTestPatternType {
LCP_RANDOM_DIAGONALLY_DOMINANT_SKEWSYMMETRIC,
LCP_RANDOM_DIAGONALLY_DOMINANT_SYMMETRIC,
LCP_REAL_NONSYMMETRIC_MU02,
LCP_REAL_NONSYMMETRIC_MU08,
LCP_REAL_SYMMETRIC
} LCPTestPatternType;
template <class T, bool IS_COL_MAJOR>
class LCPPatternGenerator {
protected:
default_random_engine m_e;
const T m_val_low_q;
const T m_val_high_q;
const std::string m_test_pattern_path;
public:
LCPPatternGenerator(const bool repeatable, const T val_low_q, const T val_high_q, const std::string test_pattern_path )
:m_e ( repeatable
? 0
: chrono::system_clock::now().time_since_epoch().count()
)
,m_val_low_q ( val_low_q )
,m_val_high_q ( val_high_q )
,m_test_pattern_path( test_pattern_path )
{}
~LCPPatternGenerator(){}
void generateTestPattern(
const int dim,
T* M,
T* q,
const LCPTestPatternType p_type,
const T condition_num
) {
if ( p_type == LCP_RANDOM_DIAGONALLY_DOMINANT_SKEWSYMMETRIC ) {
generateRandomCopositiveMat<T>( M, dim, condition_num, m_e );
fillArrayWithRandomValues<T>( m_e, q, dim, m_val_low_q, m_val_high_q );
}
else if ( p_type == LCP_RANDOM_DIAGONALLY_DOMINANT_SYMMETRIC ) {
generateRandomPDMat<T, IS_COL_MAJOR>( M, dim, condition_num, m_e );
fillArrayWithRandomValues<T>( m_e, q, dim, m_val_low_q, m_val_high_q );
}
else if ( p_type == LCP_REAL_NONSYMMETRIC_MU02 ) {
std::string file_name;
if ( dim <= 32 ) {
file_name = m_test_pattern_path + SAMPLE_DATA_32_MU02;
}
else if ( dim <= 64 ) {
file_name = m_test_pattern_path + SAMPLE_DATA_64_MU02;
}
else if ( dim <= 128 ) {
file_name = m_test_pattern_path + SAMPLE_DATA_128_MU02;
}
else if ( dim <= 256 ) {
file_name = m_test_pattern_path + SAMPLE_DATA_256_MU02;
}
else if ( dim <= 512 ) {
file_name = m_test_pattern_path + SAMPLE_DATA_512_MU02;
}
else if ( dim <= 1024 ) {
file_name = m_test_pattern_path + SAMPLE_DATA_1024_MU02;
}
else {
assert(true);
}
load_test_data( file_name, dim, M, q );
}
else if ( p_type == LCP_REAL_NONSYMMETRIC_MU08 ) {
std::string file_name;
if ( dim <= 32 ) {
file_name = m_test_pattern_path + SAMPLE_DATA_32_MU08;
}
else if ( dim <= 64 ) {
file_name = m_test_pattern_path + SAMPLE_DATA_64_MU08;
}
else if ( dim <= 128 ) {
file_name = m_test_pattern_path + SAMPLE_DATA_128_MU08;
}
else if ( dim <= 256 ) {
file_name = m_test_pattern_path + SAMPLE_DATA_256_MU08;
}
else if ( dim <= 512 ) {
file_name = m_test_pattern_path + SAMPLE_DATA_512_MU08;
}
else if ( dim <= 1024 ) {
file_name = m_test_pattern_path + SAMPLE_DATA_1024_MU08;
}
else {
assert(true);
}
load_test_data( file_name, dim, M, q );
}
else if ( p_type == LCP_REAL_SYMMETRIC ) {
std::string file_name;
if ( dim <= 32 ) {
file_name = m_test_pattern_path + SAMPLE_DATA_32_SYM;
}
else if ( dim <= 64 ) {
file_name = m_test_pattern_path + SAMPLE_DATA_64_SYM;
}
else if ( dim <= 128 ) {
file_name = m_test_pattern_path + SAMPLE_DATA_128_SYM;
}
else if ( dim <= 256 ) {
file_name = m_test_pattern_path + SAMPLE_DATA_256_SYM;
}
else if ( dim <= 512 ) {
file_name = m_test_pattern_path + SAMPLE_DATA_512_SYM;
}
else if ( dim <= 1024 ) {
file_name = m_test_pattern_path + SAMPLE_DATA_1024_SYM;
}
else {
assert(true);
}
load_test_data( file_name, dim, M, q );
}
}
void load_test_data( const std::string& file_name, const int dim, T* M, T* q ) {
std::ifstream file;
memset(M, 0, sizeof(T) * dim * dim );
memset(q, 0, sizeof(T) * dim );
file.open( file_name );
if ( file.is_open() ) {
std::string line;
while (std::getline(file, line)) {
line.erase(std::remove_if( line.begin(), line.end(), [](auto ch){ return (ch == '\n' || ch == '\r'); }),line.end() );
vector<std::string> tokens = tokenize( line, "\t" );
if ( tokens.size() == 3 ) {
if ( tokens[0][0] == 'q' ) {
const int i = atoi(tokens[1].c_str());
const T v = atof(tokens[2].c_str());
if ( 0 <= i && i < dim ) {
q[i] = v;
}
}
else {
const int row = atoi(tokens[0].c_str());
const int col = atoi(tokens[1].c_str());
const T v = atof(tokens[2].c_str());
if ( 0 <= row && row < dim && 0 <= col && col < dim ) {
M[ row*dim + col ] = v;
}
}
}
}
file.close();
}
// cerr << setprecision(2);
// for (int row = 0; row < dim ; row++ ) {
// cerr << "row[" << row << "]";
// for (int col = 0; col < dim ; col++ ) {
// cerr << "\t" << M[row*dim + col];
// }
// cerr << "\tq:" << q[row] << "\n";
// }
}
vector<std::string> tokenize( std::string line, const std::string delimiter ){
vector<std::string> tokens;
size_t pos = 0;
std::string token;
while ( ( pos = line.find( delimiter ) ) != std::string::npos ) {
token = line.substr( 0, pos );
tokens.push_back( token );
line.erase( 0, pos + delimiter.length() );
}
tokens.push_back( line );
return tokens;
}
};
#endif /*__TEST_LCP_PATTERN_GENERATOR_H__*/