From 66e8f85f7893b37b3fdd28a22176f0b2fe0828b4 Mon Sep 17 00:00:00 2001
From: Gregoire Pichon <gregoire.pichon@inria.fr>
Date: Tue, 29 Nov 2016 14:14:37 +0100
Subject: [PATCH] add a function in spm to scal a matrix and obtain a norm of 1

---
 CMakeLists.txt |  1 +
 spm.c          | 35 ++++++++++++++++++++++++++++++++
 spm.h          |  2 ++
 z_spm.h        |  3 ++-
 z_spm_scal.c   | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 95 insertions(+), 1 deletion(-)
 create mode 100644 z_spm_scal.c

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 223b370a..2e0cf877 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -20,6 +20,7 @@ set(SOURCES
   z_spm_2dense.c
   z_spm_dof_extend.c
   z_spm_norm.c
+  z_spm_scal.c
 
   z_spm_convert_to_csc.c
   z_spm_convert_to_csr.c
diff --git a/spm.c b/spm.c
index 129b2cc5..dc4a2a8e 100644
--- a/spm.c
+++ b/spm.c
@@ -1079,3 +1079,38 @@ spmCheckAxb( int nrhs,
         return ptrfunc[id](nrhs, spm, x0, ldx0, b, ldb, x, ldx );
     }
 }
+
+/**
+ *******************************************************************************
+ *
+ * @ingroup pastix_spm
+ *
+ * @brief Scal a matrix with ||A||_2
+ *
+ *******************************************************************************
+ *
+ * @param[in,out] spm
+ *          The sparse matrix to scal.
+ *
+ *******************************************************************************/
+void
+spmScal(pastix_spm_t* spm)
+{
+    switch(spm->flttype)
+    {
+    case PastixPattern:
+        break;
+    case PastixFloat:
+        s_spmScal(spm);
+        break;
+    case PastixComplex32:
+        c_spmScal(spm);
+        break;
+    case PastixComplex64:
+        z_spmScal(spm);
+        break;
+    case PastixDouble:
+    default:
+        d_spmScal(spm);
+    }
+}
diff --git a/spm.h b/spm.h
index 4851b549..df5a54ec 100644
--- a/spm.h
+++ b/spm.h
@@ -150,6 +150,7 @@ void *        spm2Dense( const pastix_spm_t *spm );
 pastix_int_t  spmFindBase( const pastix_spm_t *spm );
 double        spmNorm( int ntype, const pastix_spm_t *spm );
 int           spmMatVec(const pastix_trans_t trans, const void *alpha, const pastix_spm_t *spm, const void *x, const void *beta, void *y );
+void          spmScal( pastix_spm_t* spm );
 
 int           spmSort( pastix_spm_t *spm );
 pastix_int_t  spmMergeDuplicate( pastix_spm_t *spm );
@@ -171,4 +172,5 @@ int spmReadDriver( pastix_driver_t  driver,
                    pastix_spm_t    *spm,
                    MPI_Comm         pastix_comm );
 
+
 #endif /* _SPM_H_ */
diff --git a/z_spm.h b/z_spm.h
index af179942..db2b8a30 100644
--- a/z_spm.h
+++ b/z_spm.h
@@ -64,8 +64,9 @@ int z_spmCheckAxb( int nrhs, const pastix_spm_t *spm, void *x0, int ldx0, void *
 void z_spmDensePrint( FILE *f, pastix_int_t m, pastix_int_t n, pastix_complex64_t *A, pastix_int_t lda );
 void z_spmPrint( FILE *f, const pastix_spm_t *spm );
 
-
 pastix_spm_t *z_spmExpand(const pastix_spm_t *spm);
 void          z_spmDofExtend(pastix_spm_t *spm);
+void          z_spmScal( pastix_spm_t *spm );
+
 
 #endif /* _z_spm_H_ */
diff --git a/z_spm_scal.c b/z_spm_scal.c
new file mode 100644
index 00000000..2d253eba
--- /dev/null
+++ b/z_spm_scal.c
@@ -0,0 +1,55 @@
+/**
+ * @file z_spm_scal.c
+ *
+ *  PaStiX spm computational routines.
+ *
+ *  PaStiX is a software package provided by Inria Bordeaux - Sud-Ouest,
+ *  LaBRI, University of Bordeaux 1 and IPB.
+ *
+ * @version 1.0.0
+ * @author Mathieu Faverge
+ * @author Pierre Ramet
+ * @author Xavier Lacoste
+ * @author Theophile Terraz
+ * @date 2015-06-01
+ * @precisions normal z -> c d s
+ *
+ **/
+#include "common.h"
+#include "spm.h"
+#include "z_spm.h"
+
+/**
+ *******************************************************************************
+ *
+ * @ingroup pastix_spm_internal
+ *
+ * z_spmScal - Scal the matrix with ||A||_2
+ *
+ *******************************************************************************
+ *
+ * @param[in,out] spm
+ *           The spm which needs to be scaled.
+ *
+ *******************************************************************************/
+void
+z_spmScal( pastix_spm_t *spm )
+{
+    double              norm;
+    pastix_int_t        nnz, i;
+    pastix_complex64_t *values;
+
+    nnz    = spm->nnz;
+    values = spm->values;
+
+    norm   = z_spmNorm( PastixFrobeniusNorm, spm );
+    printf("NORM IN %.3g\n", norm);
+
+    for (i=0; i<nnz; i++){
+        values[i] /= norm;
+    }
+
+    norm = z_spmNorm( PastixFrobeniusNorm, spm );
+
+    printf("NORM OUT %.3g\n", norm);
+}
-- 
GitLab