PaStiX Handbook  6.3.2
pastix_rhs.c
Go to the documentation of this file.
1 /**
2  *
3  * @file pastix_rhs.c
4  *
5  * @copyright 2004-2023 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
6  * Univ. Bordeaux. All rights reserved.
7  *
8  * @version 6.3.2
9  * @author Mathieu Faverge
10  * @author Pierre Ramet
11  * @author Vincent Bridonneau
12  * @author Alycia Lisito
13  * @date 2023-07-21
14  *
15  **/
16 #include "common.h"
17 #include "bcsc/bvec.h"
18 #include <lapacke.h>
19 
20 /**
21  *******************************************************************************
22  *
23  * @ingroup bcsc
24  *
25  * @brief Initialize an RHS data structure.
26  *
27  *******************************************************************************
28  *
29  * @param[inout] B_ptr
30  * On entry, an allocated pastix_rhs_t data structure.
31  * On exit, the data is initialized to be used by the pastix_subtask_*
32  * functions.
33  *
34  *******************************************************************************
35  *
36  * @retval PASTIX_SUCCESS on successful exit,
37  * @retval PASTIX_ERR_BADPARAMETER if one parameter is incorrect.
38  *
39  *******************************************************************************/
40 int
42 {
43  pastix_rhs_t B;
44 
45  if ( B_ptr == NULL ) {
46  pastix_print_error( "pastixRhsInit: wrong B parameter" );
48  }
49 
50  *B_ptr = malloc( sizeof(struct pastix_rhs_s) );
51  B = *B_ptr;
52 
53  B->allocated = -1;
54  B->flttype = PastixPattern;
55  B->m = -1;
56  B->n = -1;
57  B->ld = -1;
58  B->b = NULL;
59  B->cblkb = NULL;
60  B->rhs_comm = NULL;
61  B->Ploc2Pglob = NULL;
62 
63  return PASTIX_SUCCESS;
64 }
65 
66 /**
67  *******************************************************************************
68  *
69  * @ingroup bcsc
70  *
71  * @brief Cleanup an RHS data structure.
72  *
73  *******************************************************************************
74  *
75  * @param[inout] B
76  * On entry, the initialized pastix_rhs_t data structure.
77  * On exit, the structure is destroyed and should no longer be used.
78  *
79  *******************************************************************************
80  *
81  * @retval PASTIX_SUCCESS on successful exit,
82  * @retval PASTIX_ERR_BADPARAMETER if one parameter is incorrect.
83  *
84  *******************************************************************************/
85 int
87 {
88  if ( B == NULL ) {
89  pastix_print_error( "pastixRhsFinalize: wrong B parameter" );
91  }
92 
93  if ( B->b != NULL ) {
94  if ( B->allocated > 0 ) {
95  free( B->b );
96  }
97  else {
98  pastix_print_warning( "Calling pastixRhsFinalize before restoring the ordering of vector b.\n"
99  "Please call:\n"
100  " pastix_subtask_applyorder( pastix_data, flttype, PastixDirBackward, m, n,\n"
101  " b, ldb, Bp );\n"
102  "prior to this call to restore it.\n" );
103  }
104  }
105 
106  if ( B->cblkb != NULL ) {
107  memFree_null( B->cblkb );
108  }
109 
110  if ( B->Ploc2Pglob != NULL ) {
111  memFree_null( B->Ploc2Pglob );
112  }
113  if ( B->rhs_comm != NULL ) {
114  memFree_null( B->rhs_comm );
115  }
116  free( B );
117  return PASTIX_SUCCESS;
118 }
119 
120 /**
121  *******************************************************************************
122  *
123  * @ingroup pastix_solve
124  *
125  * @brief Reduces the precision of an RHS.
126  *
127  *******************************************************************************
128  *
129  * @param[in] dB
130  * The allocated pastix_rhs_t data structure to convert to lower
131  * precision.
132  *
133  * @param[out] sB
134  * On entry, an allocated pastix_rhs_t data structure.
135  * On exit, the reduced precision pastix_rhs_t of dB.
136  * If sB->allocated == -1 on entry, the internal b vector is
137  * automatically allocated by the function.
138  *
139  *******************************************************************************
140  *
141  * @retval PASTIX_SUCCESS on successful exit,
142  * @retval PASTIX_ERR_BADPARAMETER if one parameter is incorrect.
143  *
144  *******************************************************************************/
145 int
147  pastix_rhs_t sB )
148 {
149  int rc;
150  int tofree = 0;
151 
152  /* Generates halved-precision vector */
153  if ( ( dB->flttype != PastixComplex64 ) &&
154  ( dB->flttype != PastixDouble ) )
155  {
156  pastix_print_error( "bvecDoubletoSingle: Invalid float type for mixed-precision" );
158  }
159 
160  if ( sB->allocated == -1 ) {
161  size_t size = dB->ld * dB->n;
162 
163  memcpy( sB, dB, sizeof( struct pastix_rhs_s ) );
164 
165  sB->allocated = 1;
166  sB->flttype = dB->flttype - 1;
167  sB->b = malloc( size * pastix_size_of( sB->flttype ) );
168  sB->rhs_comm = NULL;
169  tofree = 1;
170  }
171  assert( sB->allocated >= 0 );
172  assert( sB->flttype == (dB->flttype - 1) );
173  assert( sB->b != NULL );
174  assert( sB->m == dB->m );
175  assert( sB->n == dB->n );
176 
177  switch( dB->flttype ) {
178  case PastixComplex64:
179  rc = LAPACKE_zlag2c_work( LAPACK_COL_MAJOR, dB->m, dB->n, dB->b, dB->ld, sB->b, sB->ld );
180  break;
181  case PastixDouble:
182  rc = LAPACKE_dlag2s_work( LAPACK_COL_MAJOR, dB->m, dB->n, dB->b, dB->ld, sB->b, sB->ld );
183  break;
184  default:
185  rc = 1;
186  pastix_print_error( "bvecDoubletoSingle: Invalid input float type for mixed-precision" );
187  }
188 
189  if ( rc ) {
190  if ( tofree ) {
191  free( dB->b );
192  dB->b = NULL;
193  }
194  return PASTIX_ERR_INTERNAL;
195  }
196 
197  return PASTIX_SUCCESS;
198 }
199 
200 /**
201  *******************************************************************************
202  *
203  * @ingroup pastix_solve
204  *
205  * @brief Increases the precision of an RHS.
206  *
207  *******************************************************************************
208  *
209  * @param[in] sB
210  * The allocated pastix_rhs_t data structure to convert to higher
211  * precision.
212  *
213  * @param[out] dB
214  * On entry, an allocated pastix_rhs_t data structure.
215  * On exit, the increased precision pastix_rhs_t of sB.
216  * If dB->allocated == -1 on entry, the internal b vector is
217  * automatically allocated by the function.
218  *
219  *******************************************************************************
220  *
221  * @retval PASTIX_SUCCESS on successful exit,
222  * @retval PASTIX_ERR_BADPARAMETER if one parameter is incorrect.
223  *
224  *******************************************************************************/
225 int
227  pastix_rhs_t dB )
228 {
229  int rc;
230  int tofree = 0;
231 
232  /* Frees halved-precision vector */
233  if ( ( sB->flttype != PastixComplex32 ) &&
234  ( sB->flttype != PastixFloat ) )
235  {
236  pastix_print_error( "bvecSingleToDouble: Invalid input float type for mixed-precision" );
238  }
239 
240  if ( dB->allocated == -1 ) {
241  size_t size = sB->ld * sB->n;
242 
243  memcpy( dB, sB, sizeof( struct pastix_rhs_s ) );
244 
245  dB->allocated = 1;
246  dB->flttype = sB->flttype + 1;
247  dB->b = malloc( size * pastix_size_of( dB->flttype ) );
248  dB->rhs_comm = NULL;
249  tofree = 1;
250  }
251  assert( dB->allocated >= 0 );
252  assert( dB->flttype == (sB->flttype + 1) );
253  assert( dB->b != NULL );
254  assert( dB->m == sB->m );
255  assert( dB->n == sB->n );
256 
257  switch( sB->flttype ) {
258  case PastixComplex32:
259  rc = LAPACKE_clag2z_work( LAPACK_COL_MAJOR, sB->m, sB->n, sB->b, sB->ld, dB->b, dB->ld );
260  break;
261  case PastixFloat:
262  rc = LAPACKE_slag2d_work( LAPACK_COL_MAJOR, sB->m, sB->n, sB->b, sB->ld, dB->b, dB->ld );
263  break;
264  default:
265  rc = 1;
266  pastix_print_error( "bvecSingleToDouble: Invalid float type for mixed-precision" );
267  }
268 
269  if ( rc ) {
270  if ( tofree ) {
271  free( sB->b );
272  sB->b = NULL;
273  }
274  return PASTIX_ERR_INTERNAL;
275  }
276 
277  return PASTIX_SUCCESS;
278 }
int pastixRhsInit(pastix_rhs_t *B_ptr)
Initialize an RHS data structure.
Definition: pastix_rhs.c:41
int pastixRhsFinalize(pastix_rhs_t B)
Cleanup an RHS data structure.
Definition: pastix_rhs.c:86
@ PASTIX_ERR_INTERNAL
Definition: api.h:373
@ PASTIX_SUCCESS
Definition: api.h:367
@ PASTIX_ERR_BADPARAMETER
Definition: api.h:374
int pastixRhsDoubletoSingle(const pastix_rhs_t dB, pastix_rhs_t sB)
Reduces the precision of an RHS.
Definition: pastix_rhs.c:146
int pastixRhsSingleToDouble(const pastix_rhs_t sB, pastix_rhs_t dB)
Increases the precision of an RHS.
Definition: pastix_rhs.c:226
void ** cblkb
Definition: pastixdata.h:157
pastix_int_t * Ploc2Pglob
Definition: pastixdata.h:159
pastix_int_t ld
Definition: pastixdata.h:155
bvec_handle_comm_t * rhs_comm
Definition: pastixdata.h:158
pastix_coeftype_t flttype
Definition: pastixdata.h:152
pastix_int_t m
Definition: pastixdata.h:153
pastix_int_t n
Definition: pastixdata.h:154
int8_t allocated
Definition: pastixdata.h:151
Main PaStiX RHS structure.
Definition: pastixdata.h:150