PaStiX Handbook  6.3.2
personal.c
Go to the documentation of this file.
1 /**
2  * @file personal.c
3  *
4  * @brief A step-by-step example with a personal ordering (identity).
5  *
6  * @copyright 2015-2023 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
7  * Univ. Bordeaux. All rights reserved.
8  *
9  * @version 6.3.2
10  * @author Pierre Ramet
11  * @author Mathieu Faverge
12  * @author Tony Delarue
13  * @author Alycia Lisito
14  * @date 2023-07-21
15  *
16  * @ingroup pastix_examples
17  * @code
18  *
19  */
20 #include <pastix.h>
21 #include <spm.h>
22 #include <pastix/order.h>
23 
24 int main (int argc, char **argv)
25 {
26  pastix_data_t *pastix_data = NULL; /*< Pointer to the storage structure required by pastix */
27  pastix_int_t iparm[IPARM_SIZE]; /*< Integer in/out parameters for pastix */
28  double dparm[DPARM_SIZE]; /*< Floating in/out parameters for pastix */
29  spm_driver_t driver;
30  char *filename = NULL;
31  spmatrix_t *spm, spm2;
32  void *x, *b, *x0 = NULL;
33  size_t size;
34  int scatter = 0;
35  int check = 1;
36  int nrhs = 1;
37  int rc = 0;
38  pastix_order_t ord;
39  pastix_int_t i;
40 
41  /**
42  * Initialize parameters to default values
43  */
44  pastixInitParam( iparm, dparm );
45 
46  /**
47  * Get options from command line
48  */
49  pastixGetOptions( argc, argv,
50  iparm, dparm,
51  &check, &scatter, &driver, &filename );
52 
53  /**
54  * Startup PaStiX
55  */
57  pastixInit( &pastix_data, MPI_COMM_WORLD, iparm, dparm );
58 
59  /**
60  * Read the sparse matrix with the driver
61  */
62  spm = malloc( sizeof( spmatrix_t ) );
63  if ( scatter ) {
64  rc = spmReadDriverDist( driver, filename, spm, MPI_COMM_WORLD );
65  }
66  else {
67  rc = spmReadDriver( driver, filename, spm );
68  }
69  free( filename );
70  if ( rc != SPM_SUCCESS ) {
71  pastixFinalize( &pastix_data );
72  return rc;
73  }
74 
75  spmPrintInfo( spm, stdout );
76 
77  rc = spmCheckAndCorrect( spm, &spm2 );
78  if ( rc != 0 ) {
79  spmExit( spm );
80  *spm = spm2;
81  rc = 0;
82  }
83 
84  /**
85  * Generate a Fake values array if needed for the numerical part
86  */
87  if ( spm->flttype == SpmPattern ) {
88  spmGenFakeValues( spm );
89  }
90 
91  /**
92  * Build personal ordering (identity)
93  */
94  pastixOrderAlloc( &ord, spm->gN, 0 );
95  for (i=0; i<ord.vertnbr; i++)
96  {
97  ord.permtab[i] = i;
98  ord.peritab[i] = i;
99  }
100 
101  /**
102  * Perform ordering, symbolic factorization, and analyze steps
103  */
104  pastix_subtask_order( pastix_data, spm, &ord );
105  pastix_subtask_symbfact( pastix_data );
106  pastix_subtask_reordering( pastix_data );
107  pastix_subtask_blend( pastix_data );
108 
109  /**
110  * Normalize A matrix (optional, but recommended for low-rank functionality)
111  */
112  double normA = spmNorm( SpmFrobeniusNorm, spm );
113  spmScal( 1./normA, spm );
114 
115  /**
116  * Perform the numerical factorization
117  */
118  pastix_task_numfact( pastix_data, spm );
119 
120  /**
121  * Generates the b and x vector such that A * x = b
122  * Compute the norms of the initial vectors if checking purpose.
123  */
124  size = pastix_size_of( spm->flttype ) * spm->nexp * nrhs;
125  x = malloc( size );
126  b = malloc( size );
127 
128  if ( check )
129  {
130  if ( check > 1 ) {
131  x0 = malloc( size );
132  }
133  spmGenRHS( SpmRhsRndX, nrhs, spm, x0, spm->nexp, b, spm->nexp );
134  memcpy( x, b, size );
135  }
136  else {
137  spmGenRHS( SpmRhsRndB, nrhs, spm, NULL, spm->nexp, x, spm->nexp );
138 
139  /* Apply also normalization to b vectors */
140  spmScalMat( 1./normA, spm, nrhs, b, spm->nexp );
141 
142  /* Save b for refinement */
143  memcpy( b, x, size );
144  }
145 
146  /**
147  * Solve the linear system (and perform the optional refinement)
148  */
149  pastix_task_solve( pastix_data, spm->nexp, nrhs, x, spm->nexp );
150  pastix_task_refine( pastix_data, spm->nexp, nrhs, b, spm->nexp, x, spm->nexp );
151 
152  if ( check )
153  {
154  rc = spmCheckAxb( dparm[DPARM_EPSILON_REFINEMENT], nrhs, spm, x0, spm->nexp, b, spm->nexp, x, spm->nexp );
155 
156  if ( x0 ) {
157  free( x0 );
158  }
159  }
160 
161  pastixOrderExit( &ord );
162  spmExit( spm );
163  free( spm );
164  free( x );
165  free( b );
166  pastixFinalize( &pastix_data );
167 
168  return rc;
169 }
170 
171 /**
172  * @endcode
173  */
BEGIN_C_DECLS typedef int pastix_int_t
Definition: datatypes.h:51
int pastix_subtask_symbfact(pastix_data_t *pastix_data)
Computes the symbolic factorization step.
int pastix_subtask_order(pastix_data_t *pastix_data, const spmatrix_t *spm, pastix_order_t *myorder)
Computes the ordering of the given graph in parameters.
int pastix_subtask_blend(pastix_data_t *pastix_data)
Compute the proportional mapping and the final solver structure.
int pastix_subtask_reordering(pastix_data_t *pastix_data)
Apply the reordering step to compact off-diagonal blocks.
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
@ IPARM_ORDERING
Definition: api.h:50
@ PastixOrderPersonal
Definition: api.h:347
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
pastix_int_t * permtab
Definition: order.h:51
pastix_int_t * peritab
Definition: order.h:52
pastix_int_t vertnbr
Definition: order.h:49
int pastixOrderAlloc(pastix_order_t *ordeptr, pastix_int_t vertnbr, pastix_int_t cblknbr)
Allocate the order structure.
Definition: order.c:55
void pastixOrderExit(pastix_order_t *ordeptr)
Free the arrays initialized in the order structure.
Definition: order.c:273
Order structure.
Definition: order.h:47
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_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