about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorArtyom Bologov2024-08-17 02:43:47 +0400
committerArtyom Bologov2024-08-17 02:43:47 +0400
commitda3c5ad5c6db46fb2198a5bf75648e71e2a17996 (patch)
treee6f0d97327b5c2f2c60402d4cfb9b92e40609155 /src
parent3f9296136abb54717c2ebd94252aafbf9ed09e33 (diff)
downloadpangemma-da3c5ad5c6db46fb2198a5bf75648e71e2a17996.tar.gz
Add -debug-dump CLI flag for dumping data to files.
Diffstat (limited to 'src')
-rw-r--r--src/debug.cpp75
-rw-r--r--src/debug.h2
-rw-r--r--src/gemma.cpp3
3 files changed, 57 insertions, 23 deletions
diff --git a/src/debug.cpp b/src/debug.cpp
index 8ef8797..0aa4bfc 100644
--- a/src/debug.cpp
+++ b/src/debug.cpp
@@ -41,7 +41,9 @@
 
 static bool debug_mode      = false;
 static bool debug_data_mode = false;
-static bool debug_check     = false;  // check data/algorithms
+static string debug_dump_path;
+static bool debug_dump_mode = false; // Dump all the pieces of data to files
+static bool debug_check     = false; // check data/algorithms
 static bool debug_fpe_check = true;  // check floating point errors (intel hardware)
 static bool debug_strict    = false; // fail on error, more rigorous checks
 static bool debug_quiet     = false;
@@ -50,6 +52,7 @@ static bool debug_legacy    = false; // legacy mode
 
 void debug_set_debug_mode(bool setting) { debug_mode = setting; }
 void debug_set_debug_data_mode(bool setting) { debug_data_mode = setting; }
+void debug_set_debug_dump_mode(bool setting, char *path) { debug_dump_mode = setting; debug_dump_path = path;}
 void debug_set_check_mode(bool setting) {debug_check = setting; }
 void debug_set_no_check_mode(bool setting) {debug_check = !setting; }
 void debug_set_no_fpe_check_mode(bool setting) {debug_fpe_check = !setting; }
@@ -60,6 +63,7 @@ void debug_set_legacy_mode(bool setting) { debug_legacy = setting; }
 
 bool is_debug_mode() { return debug_mode; };
 bool is_debug_data_mode() { return debug_data_mode; };
+bool is_debug_dump_mode() { return debug_dump_mode; };
 bool is_no_check_mode() { return !debug_check; };
 bool is_check_mode() { return debug_check; };
 bool is_fpe_check_mode() { return debug_fpe_check; };
@@ -157,29 +161,43 @@ void disable_segfpe() {
 }
 
 void write(const char *s, const char *msg) {
-  if (!is_debug_data_mode()) return;
-  cout << s << ": " << msg << endl;
+  if (!is_debug_data_mode() && !is_debug_dump_mode()) return;
+  ofstream out(debug_dump_path + "debug-dump-" + msg + ".txt");
+  (is_debug_dump_mode() ? out : cout)
+          << s << ": " << msg << endl;
 }
 
 void write(const double d, const char *msg) {
-  if (!is_debug_data_mode()) return;
-  cout << std::setprecision(6) << d << ": " << msg << endl;
+  if (!is_debug_data_mode() && !is_debug_dump_mode()) return;
+  ofstream out(debug_dump_path + "debug-dump-" + msg + ".txt");
+  (is_debug_dump_mode() ? out : cout)
+          << std::setprecision(6) << d << ": " << msg << endl;
 }
 
 void write(const gsl_vector *v, const char *msg) {
-  if (!is_debug_data_mode()) return;
-  if (msg) cout << "// " << msg << endl;
-  cout << "// vector size: " << v->size << endl;
-  cout << "double " << msg << "[] = {";
+  if (!is_debug_data_mode() && !is_debug_dump_mode()) return;
+  ofstream out(debug_dump_path + "debug-dump-" + msg + ".txt");
+  if (is_debug_data_mode()) {
+          if (msg)
+                  cout << "// " << msg << endl;
+          cout << "// vector size: " << v->size << endl;
+          cout << "double " << msg << "[] = {";
+  }
   for (size_t i=0; i < v->size; i++) {
-    cout << gsl_vector_get(v,i) << ",";
+          (is_debug_dump_mode() ? out : cout)
+                  << gsl_vector_get(v,i)
+                  << (is_debug_dump_mode() ? "\t" : ",");
   }
-  cout << "}" << endl;
+  if (is_debug_dump_mode())
+          out << endl;
+  else
+          cout << "}" << endl;
 }
 
 void write(const gsl_matrix *m, const char *msg) {
-  if (!is_debug_data_mode()) return;
-  if (msg) cout << "// " << msg << endl;
+  if (!is_debug_data_mode() && !is_debug_dump_mode()) return;
+  if (msg && is_debug_data_mode())
+          cout << "// " << msg << endl;
   // Matrices are stored in row-major order, meaning that each row of
   // elements forms a contiguous block in memory. This is the standard
   // “C-language ordering” of two-dimensional arrays. The number of
@@ -187,18 +205,29 @@ void write(const gsl_matrix *m, const char *msg) {
   auto rows = m->size1; // see https://www.gnu.org/software/gsl/manual/html_node/Matrices.html#Matrices
   auto cols = m->size2;
   auto tda = m->tda;
+  ofstream out(debug_dump_path + "debug-dump-" + msg + ".txt");
 
-  cout << "// matrix size: " << cols << " cols, " << rows << " rows," << tda << " tda" << endl;
-  cout << "double " << msg << "[] = {";
-  for (size_t row=0; row < rows; row++) {
-    for (size_t col=0; col < cols; col++) {
-      // cout << "(" << i << "," << j << ")";
-      cout << gsl_matrix_safe_get(m,row,col);
-      cout << ",";
-    }
-    cout << "// row " << row << endl;
+  if (is_debug_data_mode()) {
+          cout << "// matrix size: " << cols << " cols, " << rows << " rows," << tda << " tda" << endl;
+          cout << "double " << msg << "[] = {";
+  }
+  for (size_t row = 0; row < rows; row++) {
+          for (size_t col = 0; col < cols; col++) {
+                  // cout << "(" << i << "," << j << ")";
+                  (is_debug_dump_mode() ? out : cout)
+                          << gsl_matrix_safe_get(m, row, col);
+                  if (is_debug_dump_mode())
+                          out << "\t";
+                  else
+                          cout << ",";
+          }
+          if (is_debug_dump_mode())
+                  out << endl;
+          else
+                  cout << "// row " << row << endl;
   }
-  cout << "}" << endl;
+  if (is_debug_data_mode())
+          cout << "}" << endl;
 }
 
 /*
diff --git a/src/debug.h b/src/debug.h
index ff22756..0489a81 100644
--- a/src/debug.h
+++ b/src/debug.h
@@ -33,6 +33,7 @@ void gemma_gsl_error_handler (const char * reason,
 
 void debug_set_debug_mode(bool setting);
 void debug_set_debug_data_mode(bool setting);
+void debug_set_debug_dump_mode(bool setting, char *path);
 void debug_set_check_mode(bool setting);
 void debug_set_no_check_mode(bool setting);
 void debug_set_no_fpe_check_mode(bool setting);
@@ -43,6 +44,7 @@ void debug_set_legacy_mode(bool setting);
 
 bool is_debug_mode();
 bool is_debug_data_mode();
+bool is_debug_dump_mode();
 bool is_no_check_mode();
 bool is_check_mode();
 bool is_fpe_check_mode();
diff --git a/src/gemma.cpp b/src/gemma.cpp
index b3ca4c5..c73c174 100644
--- a/src/gemma.cpp
+++ b/src/gemma.cpp
@@ -1601,6 +1601,9 @@ void GEMMA::Assign(int argc, char **argv, PARAM &cPar) {
       // cPar.mode_debug = true;
       debug_set_debug_data_mode(true);
       debug_set_debug_mode(true);
+    } else if (strcmp(argv[i], "-debug-dump") == 0) {
+      debug_set_debug_dump_mode(true, (char *)cPar.path_out.c_str());
+      debug_set_debug_mode(true);
     } else if (strcmp(argv[i], "-debug") == 0) {
       // cPar.mode_debug = true;
       debug_set_debug_mode(true);