From 30aad95d04a3409f3393fa212283736241592a8b Mon Sep 17 00:00:00 2001
From: Mathieu Faverge <mathieu.faverge@inria.fr>
Date: Mon, 15 Jun 2020 15:01:59 +0200
Subject: [PATCH] Add a spmPrintRHS function to dump the righ hand side to help
 debug

---
 include/spm.h     |  4 +++-
 src/spm.c         | 55 +++++++++++++++++++++++++++++++++++++++++++++++
 src/z_spm.h       |  1 +
 src/z_spm_print.c | 44 +++++++++++++++++++++++++++++++++++++
 4 files changed, 103 insertions(+), 1 deletion(-)

diff --git a/include/spm.h b/include/spm.h
index 4cdf1cad..0f46e18f 100644
--- a/include/spm.h
+++ b/include/spm.h
@@ -11,7 +11,8 @@
  * @author Xavier Lacoste
  * @author Pierre Ramet
  * @author Mathieu Faverge
- * @date 2013-06-24
+ * @author Tony Delarue
+ * @date 2020-06-09
  *
  * @addtogroup spm
  * @{
@@ -206,6 +207,7 @@ int spmParseLaplacianInfo( const char *    filename,
  */
 void *      spm2Dense( const spmatrix_t *spm );
 void        spmPrint( const spmatrix_t *spm, FILE *f );
+void        spmPrintRHS( const spmatrix_t *spm, int n, const void *x, spm_int_t ldx, FILE *stream );
 void        spmPrintInfo( const spmatrix_t *spm, FILE *f );
 void        spmExpand( const spmatrix_t *spm_in, spmatrix_t *spm_out );
 spmatrix_t *spmDofExtend( const spmatrix_t *spm, const int type, const int dof );
diff --git a/src/spm.c b/src/spm.c
index 7db59d59..b492e714 100644
--- a/src/spm.c
+++ b/src/spm.c
@@ -993,6 +993,61 @@ spmPrint( const spmatrix_t *spm,
     }
 }
 
+/**
+ *******************************************************************************
+ *
+ * @brief Print a set of vector associated to an spm matrix.
+ *
+ *******************************************************************************
+ *
+ * @param[in] spm
+ *          The sparse matrix.
+ *
+ * @param[in] n
+ *          The number of columns of x.
+ *
+ * @param[in] x
+ *          The set of vectors associated to the spm of size n-by-ldx.
+ *
+ * @param[in] ldx
+ *          The local leading dimension of the set of vectors (ldx >= spm->n).
+ *
+ * @param[in] stream
+ *          File to print the spm matrix. stdout, if stream == NULL.
+ *
+ *******************************************************************************/
+void
+spmPrintRHS( const spmatrix_t *spm,
+             int               n,
+             const void       *x,
+             spm_int_t         ldx,
+             FILE             *stream )
+{
+    if (stream == NULL) {
+        stream = stdout;
+    }
+
+    switch(spm->flttype)
+    {
+    case SpmPattern:
+        /* Not handled for now */
+        break;
+    case SpmFloat:
+        s_spmPrintRHS( stream, spm, n, x, ldx );
+        break;
+    case SpmComplex32:
+        c_spmPrintRHS( stream, spm, n, x, ldx );
+        break;
+    case SpmComplex64:
+        z_spmPrintRHS( stream, spm, n, x, ldx );
+        break;
+    case SpmDouble:
+        d_spmPrintRHS( stream, spm, n, x, ldx );
+    }
+
+    return;
+}
+
 /**
  *******************************************************************************
  *
diff --git a/src/z_spm.h b/src/z_spm.h
index f7eb42fe..526985f3 100644
--- a/src/z_spm.h
+++ b/src/z_spm.h
@@ -81,6 +81,7 @@ int z_spmCheckAxb( spm_fixdbl_t eps, int nrhs, const spmatrix_t *spm, void *x0,
  */
 void z_spmDensePrint( FILE *f, spm_int_t m, spm_int_t n, const spm_complex64_t *A, spm_int_t lda );
 void z_spmPrint( FILE *f, const spmatrix_t *spm );
+void z_spmPrintRHS( FILE *f, const spmatrix_t *spm, int n, const void *x, spm_int_t ldx );
 
 void z_spmExpand( const spmatrix_t *spm_in, spmatrix_t *spm_out );
 void z_spmDofExtend( spmatrix_t *spm );
diff --git a/src/z_spm_print.c b/src/z_spm_print.c
index 12c647cb..7ba3c73e 100644
--- a/src/z_spm_print.c
+++ b/src/z_spm_print.c
@@ -432,3 +432,47 @@ z_spmPrint( FILE *f, const spmatrix_t *spm )
     }
     return;
 }
+
+/**
+ *******************************************************************************
+ *
+ * @ingroup spm_dev_print
+ *
+ * @brief Write into a file the vectors associated to a spm.
+ *
+ *******************************************************************************
+ *
+ * @param[inout] f
+ *          Output file
+ *
+ * @param[in] spm
+ *          The spm structure describing the matrix.
+ *
+ * @param[in] n
+ *          The number of columns of x.
+ *
+ * @param[in] x
+ *          The set of vectors associated to the spm of size n-by-ldx.
+ *
+ * @param[in] ldx
+ *          The local leading dimension of the set of vectors (ldx >= spm->n).
+ *
+ *******************************************************************************/
+void
+z_spmPrintRHS( FILE *f, const spmatrix_t *spm,
+               int n, const void *x, spm_int_t ldx )
+{
+    const spm_complex64_t *xptr = (const spm_complex64_t *)x;
+    spm_int_t i, j, ig, baseval;
+
+    baseval = spmFindBase( spm );
+
+    for( j=0; j<n; j++) {
+        for( i=0; i<spm->n; i++, xptr++ ) {
+            ig = (spm->loc2glob == NULL) ? i : spm->loc2glob[i] - baseval;
+
+            z_spmPrintElt( f, ig, j, *xptr );
+        }
+        xptr += ldx - i;
+    }
+}
-- 
GitLab