PaStiX Handbook  6.3.2
compress.c
Go to the documentation of this file.
1 /**
2  * @file compress.c
3  *
4  * @brief A compression example that factorizes the matrix with the Just-In-Time
5  * strategy and Rank-Revealing kernels.
6  *
7  * @copyright 2015-2023 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
8  * Univ. Bordeaux. All rights reserved.
9  *
10  * @version 6.3.2
11  * @author Gregoire Pichon
12  * @author Mathieu Faverge
13  * @author Pierre Ramet
14  * @author Tony Delarue
15  * @author Alycia Lisito
16  * @date 2023-07-21
17  *
18  * @ingroup pastix_examples
19  * @code
20  *
21  */
22 #include <pastix.h>
23 #include <spm.h>
24 
25 int main (int argc, char **argv)
26 {
27  pastix_data_t *pastix_data = NULL; /*< Pointer to the storage structure required by pastix */
28  pastix_int_t iparm[IPARM_SIZE]; /*< Integer in/out parameters for pastix */
29  double dparm[DPARM_SIZE]; /*< Floating in/out parameters for pastix */
30  spm_driver_t driver;
31  char *filename = NULL;
32  spmatrix_t *spm, spm2;
33  void *x, *b, *x0 = NULL;
34  size_t size;
35  int scatter = 0;
36  int check = 1;
37  int nrhs = 1;
38  int rc = 0;
39 
40  /**
41  * Initialize parameters to default values
42  */
43  pastixInitParam( iparm, dparm );
44 
45  /**
46  * Get options from command line
47  */
48  pastixGetOptions( argc, argv,
49  iparm, dparm,
50  &check, &scatter, &driver, &filename );
51 
52  /**
53  * Set low-rank parameters
54  */
57  iparm[IPARM_COMPRESS_MIN_WIDTH] = 128;
58  iparm[IPARM_COMPRESS_MIN_HEIGHT] = 25;
59  dparm[DPARM_COMPRESS_TOLERANCE] = 1e-8;
60 
61  /**
62  * Startup PaStiX
63  */
64  pastixInit( &pastix_data, MPI_COMM_WORLD, iparm, dparm );
65 
66  /**
67  * Read the sparse matrix with the driver
68  */
69  spm = malloc( sizeof( spmatrix_t ) );
70  if ( scatter ) {
71  rc = spmReadDriverDist( driver, filename, spm, MPI_COMM_WORLD );
72  }
73  else {
74  rc = spmReadDriver( driver, filename, spm );
75  }
76  free( filename );
77  if ( rc != SPM_SUCCESS ) {
78  pastixFinalize( &pastix_data );
79  return rc;
80  }
81 
82  spmPrintInfo( spm, stdout );
83 
84  rc = spmCheckAndCorrect( spm, &spm2 );
85  if ( rc != 0 ) {
86  spmExit( spm );
87  *spm = spm2;
88  rc = 0;
89  }
90 
91  /**
92  * Generate a Fake values array if needed for the numerical part
93  */
94  if ( spm->flttype == SpmPattern ) {
95  spmGenFakeValues( spm );
96  }
97 
98  /**
99  * Perform ordering, symbolic factorization, and analyze steps
100  */
101  pastix_task_analyze( pastix_data, spm );
102 
103  /**
104  * Normalize A matrix (optional, but recommended for low-rank functionality)
105  */
106  double normA = spmNorm( SpmFrobeniusNorm, spm );
107  spmScal( 1./normA, spm );
108 
109  /**
110  * Perform the numerical factorization
111  */
112  pastix_task_numfact( pastix_data, spm );
113 
114  /**
115  * Generates the b and x vector such that A * x = b
116  * Compute the norms of the initial vectors if checking purpose.
117  */
118  size = pastix_size_of( spm->flttype ) * spm->nexp * nrhs;
119  x = malloc( size );
120  b = malloc( size );
121 
122  if ( check )
123  {
124  if ( check > 1 ) {
125  x0 = malloc( size );
126  }
127  spmGenRHS( SpmRhsRndX, nrhs, spm, x0, spm->nexp, b, spm->nexp );
128  memcpy( x, b, size );
129  }
130  else {
131  spmGenRHS( SpmRhsRndB, nrhs, spm, NULL, spm->nexp, x, spm->nexp );
132 
133  /* Apply also normalization to b vectors */
134  spmScalMat( 1./normA, spm, nrhs, b, spm->nexp );
135 
136  /* Save b for refinement */
137  memcpy( b, x, size );
138  }
139 
140  /**
141  * Solve the linear system (and perform the optional refinement)
142  */
143  pastix_task_solve( pastix_data, spm->nexp, nrhs, x, spm->nexp );
144  pastix_task_refine( pastix_data, spm->nexp, nrhs, b, spm->nexp, x, spm->nexp );
145 
146  if ( check )
147  {
148  rc = spmCheckAxb( dparm[DPARM_EPSILON_REFINEMENT], nrhs, spm, x0, spm->nexp, b, spm->nexp, x, spm->nexp );
149 
150  if ( x0 ) {
151  free( x0 );
152  }
153  }
154 
155  spmExit( spm );
156  free( spm );
157  free( x );
158  free( b );
159  pastixFinalize( &pastix_data );
160 
161  return rc;
162 }
163 
164 /**
165  * @endcode
166  */
BEGIN_C_DECLS typedef int pastix_int_t
Definition: datatypes.h:51
void pastixFinalize(pastix_data_t **pastix_data)
Finalize the solver instance.
Definition: api.c:919
void pastixInitParam(pastix_int_t *iparm, double *dparm)
Initialize the iparm and dparm arrays to their default values.
Definition: api.c:411
void pastixInit(pastix_data_t **pastix_data, PASTIX_Comm pastix_comm, pastix_int_t *iparm, double *dparm)
Initialize the solver instance.
Definition: api.c:896
@ DPARM_EPSILON_REFINEMENT
Definition: api.h:161
@ DPARM_COMPRESS_TOLERANCE
Definition: api.h:184
@ PastixCompressWhenEnd
Definition: api.h:387
@ IPARM_COMPRESS_WHEN
Definition: api.h:131
@ IPARM_COMPRESS_MIN_WIDTH
Definition: api.h:129
@ IPARM_COMPRESS_MIN_HEIGHT
Definition: api.h:130
@ IPARM_COMPRESS_METHOD
Definition: api.h:132
@ PastixCompressMethodPQRCP
Definition: api.h:396
void pastixGetOptions(int argc, char **argv, pastix_int_t *iparm, double *dparm, int *check, int *scatter, spm_driver_t *driver, char **filename)
PaStiX helper function to read command line options in examples.
Definition: get_options.c:149
int pastix_task_refine(pastix_data_t *pastix_data, pastix_int_t n, pastix_int_t nrhs, void *B, pastix_int_t ldb, void *X, pastix_int_t ldx)
Perform iterative refinement.
int pastix_task_analyze(pastix_data_t *pastix_data, const spmatrix_t *spm)
Perform all the preprocessing steps: ordering, symbolic factorization, reordering,...
int pastix_task_solve(pastix_data_t *pastix_data, pastix_int_t m, pastix_int_t nrhs, void *B, pastix_int_t ldb)
Solve the given problem.
int pastix_task_numfact(pastix_data_t *pastix_data, spmatrix_t *spm)
Perform all the numerical factorization steps: fill the internal block CSC and the solver matrix stru...
Main PaStiX data structure.
Definition: pastixdata.h:67