NeoPZ
skylmat.cpp
Go to the documentation of this file.
1 
8 #ifdef HAVE_CONFIG_H
9 #include <pz_config.h>
10 #endif
11 
12 #include <iostream>
13 #include <cstdlib>
14 
15 #include "pzbfilestream.h" // TPZBFileStream, TPZFileStream
16 #include "pzmd5stream.h"
17 
18 #include <fstream>
19 #include <string>
20 
21 #ifdef USING_TBB
22 #include "tbb/task_scheduler_init.h"
23 using namespace tbb;
24 #endif
25 
26 #include "pzskylmat.h"
27 
28 #include "arglib.h"
29 #include "run_stats_table.h"
30 
31 using namespace std;
32 
33 void help(const char* prg)
34 {
35  cout << "Execute performance tests for skylmatrices" << endl;
36  cout << endl;
37  cout << "Usage: " << prg << "-op operation -[m|bm] matrixfn [-[m|bm]2 matrixfn] [-v level] [-perf_rdt rdt_file] [-h]";
38  cout << "[extra_arguments] [-h]" << endl << endl;
39  cout << "operation:" << endl;
40  cout << " 0: dump skyline matrix statistics" << endl;
41  cout << " 1: decompose matrix using Decompose_Cholesky()" << endl;
42  cout << " 2: decompose matrix using Decompose_LDLt()" << endl;
43  clarg::arguments_descriptions(cout, " ", "\n");
44 }
45 
46 clarg::argString m("-m", "input matrix file name (text format)", "matrix.txt");
47 clarg::argString bm("-bm", "input matrix file name (binary format)", "matrix.bin");
48 clarg::argString m2("-m2", "argument matrix file name (text format)", "matrix2.txt");
49 clarg::argString bm2("-bm2", "argument matrix file name (binary format)", "matrix2.bin");
50 
51 clarg::argInt verb_level("-v", "verbosity level", 0);
52 int verbose = 0;
53 /* Verbose macro. */
54 #define VERBOSE(level,...) if (level <= verbose) cout << __VA_ARGS__
55 
56 clarg::argInt mop("-op", "Matrix operation", 1);
57 clarg::argBool h("-h", "help message", false);
58 clarg::argString res_chk_t("-res_chk_t", "check the results using a reference data (text format)","");
59 clarg::argString res_chk_b("-res_chk_b", "check the results using a reference data (binary format)","");
60 clarg::argDouble res_chk_tol("-res_chk_tol", "error tolerance when checking results.", 1.e-12);
61 clarg::argString res_dump_t("-res_dump_t", "write final results to a text file.", "dump_matrix.txt");
62 clarg::argString res_dump_b("-res_dump_b", "write final results to a binary file.", "dump_matrix.bin");
63 
64 /* Run statistics. */
65 RunStatsTable operation_rst("-perf_rdt", "Raw data table file to add matrix operation performance statistics");
66 
68 int run_decompose_ldlt();
69 int dump_matrix_stats();
70 
71 int main(int argc, char *argv[])
72 {
73 #ifdef USING_TBB
74  task_scheduler_init init;
75 #endif
76 
77  /* Parse the arguments */
78  if (clarg::parse_arguments(argc, argv)) {
79  cerr << "Error when parsing the arguments!" << endl;
80  return 1;
81  }
82 
83  verbose = verb_level.get_value();
84  if (verbose >= 1) {
85  cout << "- Arguments -----------------------" << endl;
86  clarg::values(cout, false);
87  cout << "-----------------------------------" << endl;
88  }
89 
90  if (h.get_value() == true) {
91  help(argv[0]);
92  return 1;
93  }
94 
95  if (!mop.was_set()) {
96  cerr << "You must provide the operation -op." << endl;
97  help(argv[0]);
98  return 1;
99  }
100 
101  int status = 0; // Ok.
102 
103  switch (mop.get_value()) {
104  case 0:
105  status = dump_matrix_stats();
106  break;
107  case 1:
108  status = run_decompose_cholesky();
109  break;
110  case 2:
111  status = run_decompose_ldlt();
112  break;
113  default:
114  cerr << "ERROR: Invalid matrix operation type." << endl;
115  status = 1;
116  }
117 
118  if (status != 0) {
119  cerr << "ERROR when executing the experiment." << endl;
120  }
121 
122  return status;
123 }
124 
125 // -- Helper functions ---------------------------------------
126 
127 class FileStreamWrapper
128 {
129 public:
131  {}
133 
134  void OpenWrite(const string& fn)
135  {
136  if (binary)
137  bfs.OpenWrite(fn);
138  else
139  fs.OpenWrite(fn);
140  }
141 
142  void OpenRead(const string& fn)
143  {
144  if (binary)
145  bfs.OpenRead(fn);
146  else
147  fs.OpenRead(fn);
148  }
149 
150  operator TPZStream&()
151  {
152  if (binary)
153  return bfs;
154  else
155  return fs;
156  }
157 
158 protected:
159 
160  bool binary;
161  TPZFileStream fs;
162  TPZBFileStream bfs;
163 };
164 
166 {
167  string filename = 0;
168  bool binary = false;
169  if (res_chk_t.was_set()) {
170  if (res_chk_b.was_set()) {
171  cerr << "Both -res_chk_t and -res_chk_b were set. Use only one option." << endl;
172  return 1;
173  }
174  else {
175  filename = res_chk_t.get_value();
176  }
177  }
178  else {
179  binary = true;
180  filename = res_chk_b.get_value();
181  }
182 
183  VERBOSE(1,"Checking result using reference matrix : " << filename << endl);
184  FileStreamWrapper file(binary);
185  file.OpenRead(filename);
186  /* Reference matrix. */
187  TPZSkylMatrix<REAL> ref_matrix;
188  ref_matrix.Read(file,0);
189 
190  int max_j = matrix.Cols();
191  if (max_j != ref_matrix.Cols()) {
192  cerr << "Result matrix has " << max_j
193  << " cols while reference matrix has "
194  << ref_matrix.Cols() << endl;
195  VERBOSE(1,"Checking result using reference matrix : " << filename << "[FAILED]" << endl);
196  return 1;
197  }
198 
199  REAL error_tolerance = res_chk_tol.get_value();
200  REAL max_error = 0.0;
201  int ret = 0; // OK
202  for (int j=0; j<max_j; j++) {
203  int col_height = matrix.SkyHeight(j);
204  if (col_height != ref_matrix.SkyHeight(j)) {
205  cerr << "Column " << j << " of result matrix has " << col_height
206  << " non zero rows while reference matrix has "
207  << ref_matrix.SkyHeight(j) << endl;
208  VERBOSE(1,"Checking result using reference matrix : " << filename << "[FAILED]" << endl);
209  return 1;
210  }
211 
212  int min_i = (j+1) - col_height;
213  for (int i=min_i; i<=j; i++) {
214  REAL dm_ij = matrix.s(i,j);
215  REAL rm_ij = ref_matrix.s(i,j);
216  if (dm_ij != rm_ij) {
217  REAL diff = abs(dm_ij - rm_ij);
218  if (diff >= error_tolerance) {
219  VERBOSE(1, "diff(" << diff << ") tolerance (" << error_tolerance
220  << "). dm[" << i << "][" << j << "] (" << dm_ij
221  << ") != rm[" << i << "][" << j << "] (" << rm_ij
222  << ")." << endl);
223  ret = 1;
224  max_error = (max_error < diff)?diff:max_error;
225  }
226  }
227  }
228  }
229  if (ret != 0) {
230  cerr << "Error ("<< max_error <<") > error tolerance ("
231  << error_tolerance <<")" << endl;
232  VERBOSE(1,"Checking result using reference matrix : " << filename << "[FAILED]" << endl);
233  }
234  else {
235  VERBOSE(1,"Checking result using reference matrix : " << filename << "[OK]" << endl);
236  }
237  return ret;
238 }
239 
241 {
242  if (!res_dump_t.was_set() && !res_dump_b.was_set())
243  return 0; // nothing to dump
244 
245  string filename = 0;
246  bool binary = false;
247  if (res_dump_t.was_set()) {
248  if (res_dump_b.was_set()) {
249  cerr << "Both -res_dump_t and -res_dump_b were set. Use only one option." << endl;
250  return 1;
251  }
252  else {
253  filename = res_dump_t.get_value();
254  }
255  }
256  else {
257  binary = true;
258  filename = res_dump_b.get_value();
259  }
260 
261  VERBOSE(1,"Dumping result to : " << filename << endl);
262  FileStreamWrapper file(binary);
263  file.OpenWrite(filename);
264  matrix.Write(file,0);
265  VERBOSE(1,"Dumping result to : " << filename << "[DONE]" << endl);
266  return 0;
267 }
268 
270 {
271  string inputfn = 0;
272  bool binary = false;
273  if (!m.was_set() && !bm.was_set()) {
274  cerr << "Please provide an input matrix using -m or -bm." << endl;
275  return 1;
276  }
277  if (m.was_set()) {
278  if (bm.was_set()) {
279  cerr << "Both -m and -bm were set. Use only one option." << endl;
280  return 1;
281  }
282  else {
283  inputfn = m.get_value();
284  }
285  }
286  else {
287  binary = true;
288  inputfn = bm.get_value();
289  }
290 
291  VERBOSE(1,"Reading input file: " << inputfn << endl);
292  FileStreamWrapper input_file(binary);
293  input_file.OpenRead(inputfn);
294  matrix.Read(input_file,0);
295  VERBOSE(1,"Reading input file: " << inputfn << "[DONE]" << endl);
296  return 0;
297 }
298 
299 // -- dump_matrix_stats ---------------------------------------
301 {
303 
304  if (read_input_matrix(matrix))
305  return 1;
306 
307  unsigned n = matrix.Dim();
308  uint64_t n_sky_items = 0;
309  uint64_t max_height = 0;
310  for (unsigned i=0; i<n; i++) {
311  unsigned height = matrix.SkyHeight(i);
312  if (verbose > 2) {
313  cout << "col " << i << " height = " << height << endl;
314  }
315  n_sky_items += height;
316  if (height > max_height) max_height = height;
317  }
318  uint64_t n2 = n * n;
319  double av_height = (double) n_sky_items / (double) n;
320  cout << "N = " << n << endl;
321  cout << "N^2 = " << n2 << endl;
322  cout << "Sky items = " << n_sky_items << endl;
323  cout << "N^2 / Sky items = " << (double) n2 / (double) n_sky_items << endl;
324  cout << "Avg. Height = " << av_height << endl;
325  cout << "Max. Height = " << max_height << endl;
326 
327  return 0;
328 }
329 
330 
331 // -- run_decompose_cholesky ---------------------------------------
333 {
335 
336  if (read_input_matrix(matrix))
337  return 1;
338 
340  // int ret = matrix.Decompose_Cholesky();
342 
343  if (res_dump(matrix))
344  return 1;
345 
346  if (res_chk_t.was_set() || res_chk_b.was_set())
347  return res_check(matrix);
348  else
349  return 0;
350 }
351 
352 // -- run_decompose_ldlt ---------------------------------------
354 {
356 
357  if (read_input_matrix(matrix))
358  return 1;
359 
361  // int ret = matrix.Decompose_LDLt();
363 
364  if (res_dump(matrix))
365  return 1;
366 
367  if (res_chk_t.was_set() || res_chk_b.was_set())
368  return res_check(matrix);
369  else
370  return 0;
371 }
Contains a class to record running statistics on CSV tables.
clarg::argInt verb_level("-v", "verbosity level", 0)
clarg::argString m2("-m2", "argument matrix file name (text format)", "matrix2.txt")
filename
Definition: stats.py:82
int read_input_matrix(TPZSkylMatrix< REAL > &matrix)
Definition: skylmat.cpp:269
Contains declaration of the TPZMD5Stream class which implements the interface to write and check md5 ...
clarg::argString bm2("-bm2", "argument matrix file name (binary format)", "matrix2.bin")
void values(ostream &os, bool defined_only)
Definition: arglib.cpp:183
void OpenRead(const std::string &fn)
Definition: decompose.cpp:116
int main(int argc, char *argv[])
Definition: skylmat.cpp:71
void Write(TPZStream &buf, int withclassid) const override
Packs the object structure in a stream of bytes.
Definition: pzskylmat.cpp:3287
int dump_matrix_stats()
Definition: skylmat.cpp:300
int64_t SkyHeight(int64_t col)
return the height of the skyline for a given column
Definition: pzskylmat.h:422
TinyFad< 8, T > abs(const TinyFad< 8, T > &in)
Definition: tinyfadeight.h:846
void OpenWrite(const std::string &fn)
Definition: decompose.cpp:108
#define VERBOSE(level,...)
Definition: skylmat.cpp:54
Implements a skyline storage format. A Skyline matrix is symmetric so square. Matrix.
Definition: pzskylmat.h:394
fn
Definition: test.py:253
TPZSkylMatrix< REAL > matrix
Definition: numatst.cpp:255
virtual TVar & s(const int64_t row, const int64_t col) override
The operators check on the bounds if the DEBUG variable is defined.
Definition: pzskylmat.cpp:1871
int inputfn
Definition: rdt.py:95
int res_check(TPZSkylMatrix< REAL > &matrix)
Definition: skylmat.cpp:165
int run_decompose_ldlt()
Definition: skylmat.cpp:353
clarg::argString res_dump_b("-res_dump_b", "write final results to a binary file.", "dump_matrix.bin")
void help(const char *prg)
Definition: skylmat.cpp:33
Implements reading from and writing to an ascii file. Persistency.
Definition: TPZFileStream.h:15
int run_decompose_cholesky()
Definition: skylmat.cpp:332
int parse_arguments(int argc, char *argv[])
Definition: arglib.cpp:195
clarg::argString res_chk_b("-res_chk_b", "check the results using a reference data (binary format)","")
Contains TPZSkyline class which implements a skyline storage format.
void arguments_descriptions(ostream &os, string prefix, string suffix)
Definition: arglib.cpp:189
virtual int64_t Dim() const
Returns the dimension of the matrix if the matrix is square.
Definition: pzmatrix.h:892
clarg::argString bm("-bm", "input matrix file name (binary format)", "matrix.bin")
void Read(TPZStream &buf, void *context) override
Unpacks the object structure from a stream of bytes.
Definition: pzskylmat.cpp:3270
FileStreamWrapper(bool b)
Definition: skylmat.cpp:130
bool was_set() const
Definition: arglib.h:138
RunStatsTable operation_rst("-perf_rdt", "Raw data table file to add matrix operation performance statistics")
const T & get_value() const
Definition: arglib.h:177
clarg::argDouble res_chk_tol("-res_chk_tol", "error tolerance when checking results.", 1.e-12)
void OpenWrite(const string &fn)
Definition: skylmat.cpp:134
clarg::argString res_chk_t("-res_chk_t", "check the results using a reference data (text format)","")
int res_dump(TPZSkylMatrix< REAL > &matrix)
Definition: skylmat.cpp:240
int64_t Cols() const
Returns number of cols.
Definition: pzmatrix.h:809
clarg::argInt mop("-op", "Matrix operation", 1)
Defines the interface for saving and reading data. Persistency.
Definition: TPZStream.h:50
clarg::argString m("-m", "input matrix file name (text format)", "matrix.txt")
void OpenRead(const string &fn)
Definition: skylmat.cpp:142
clarg::argString res_dump_t("-res_dump_t", "write final results to a text file.", "dump_matrix.txt")
int verbose
Definition: skylmat.cpp:52
clarg::argBool h("-h", "help message", false)