PaStiX Handbook  6.2.1
core_clr2xx.c
Go to the documentation of this file.
1 /**
2  *
3  * @file core_clr2xx.c
4  *
5  * PaStiX low-rank kernel routines that perform the addition of AB into C.
6  *
7  * @copyright 2016-2021 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
8  * Univ. Bordeaux. All rights reserved.
9  *
10  * @version 6.0.3
11  * @author Mathieu Faverge
12  * @author Gregoire Pichon
13  * @author Pierre Ramet
14  * @date 2019-11-12
15  * @generated from /builds/solverstack/pastix/kernels/core_zlr2xx.c, normal z -> c, Tue Apr 12 09:38:38 2022
16  *
17  **/
18 #include "common.h"
19 #include <cblas.h>
20 #include "kernels_trace.h"
21 #include "blend/solver.h"
22 #include "pastix_ccores.h"
23 #include "pastix_clrcores.h"
24 
25 #ifndef DOXYGEN_SHOULD_SKIP_THIS
26 static pastix_complex32_t cone = 1.0;
27 static pastix_complex32_t czero = 0.0;
28 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
29 
30 /**
31  *******************************************************************************
32  *
33  * @brief Perform the addition of the low-rank matrix AB and the full-rank
34  * matrix C.
35  *
36  *******************************************************************************
37  *
38  * @param[inout] params
39  * The LRMM structure that stores all the parameters used in the LRMM
40  * functions family.
41  * On exit, the C matrix is udpated with the addition of AB.
42  * @sa core_clrmm_t
43  *
44  * @param[in] AB
45  * The low-rank structure of the AB matrix to apply to C.
46  *
47  * @param[in] transV
48  * Specify if AB->v is stored normally or transposed.
49  * - If PastixNoTrans, AB->v is stored normally for low-rank format.
50  * - If PastixTrans, AB->v is stored transposed.
51  * - If PastixConjTrans, AB->v is stored transposed, and conjf() must be
52  * applied to the matrix.
53  *
54  *******************************************************************************
55  *
56  * @return The number of flops required to perform the operation.
57  *
58  *******************************************************************************/
59 static inline pastix_fixdbl_t
61  const pastix_lrblock_t *AB,
62  pastix_trans_t transV )
63 {
64  PASTE_CORE_CLRMM_PARAMS( params );
65  pastix_int_t ldabu = M;
66  pastix_int_t ldabv = (transV == PastixNoTrans) ? AB->rkmax : N;
67  pastix_fixdbl_t flops = 0.;
68  pastix_complex32_t *Cfr = C->u;
69  Cfr += Cm * offy + offx;
70 
71  assert( C->rk == -1 );
72 
73  /* TODO: find a suitable name to trace this kind of kernel. */
74  if ( AB->rk == -1 ) {
75  flops = 2 * M * N;
76  kernel_trace_start_lvl2( PastixKernelLvl2_LR_add2C_updateCfr );
78  alpha, AB->u, ldabu,
79  beta, Cfr, Cm );
80  kernel_trace_stop_lvl2( flops );
81  }
82  else {
83  flops = FLOPS_CGEMM( M, N, AB->rk );
84  kernel_trace_start_lvl2( PastixKernelLvl2_LR_add2C_updateCfr );
85  cblas_cgemm( CblasColMajor, CblasNoTrans, (CBLAS_TRANSPOSE)transV,
86  M, N, AB->rk,
87  CBLAS_SADDR(alpha), AB->u, ldabu,
88  AB->v, ldabv,
89  CBLAS_SADDR(beta), Cfr, Cm );
90  kernel_trace_stop_lvl2( flops );
91  }
92 
94  return flops;
95 }
96 
97 /**
98  *******************************************************************************
99  *
100  * @brief Perform the addition of the low-rank matrix AB and the low-rank
101  * matrix C.
102  *
103  *******************************************************************************
104  *
105  * @param[inout] params
106  * The LRMM structure that stores all the parameters used in the LRMM
107  * functions family.
108  * On exit, the C matrix is udpated with the addition of AB.
109  * @sa core_clrmm_t
110  *
111  * @param[in] AB
112  * The low-rank structure of the AB matrix to apply to C.
113  *
114  * @param[in] transV
115  * Specify if AB->v is stored normally or transposed.
116  * - If PastixNoTrans, AB->v is stored normally for low-rank format.
117  * - If PastixTrans, AB->v is stored transposed.
118  * - If PastixConjTrans, AB->v is stored transposed, and conjf() must be
119  * applied to the matrix.
120  *
121  *******************************************************************************
122  *
123  * @return The number of flops required to perform the operation.
124  *
125  *******************************************************************************/
126 static inline pastix_fixdbl_t
128  const pastix_lrblock_t *AB,
129  pastix_trans_t transV )
130 {
131  PASTE_CORE_CLRMM_PARAMS( params );
132  pastix_int_t rklimit = core_get_rklimit( Cm, Cn );
133  pastix_int_t rAB = ( AB->rk == -1 ) ? pastix_imin( M, N ) : AB->rk;
134  pastix_int_t ldabu = M;
135  pastix_int_t ldabv = (transV == PastixNoTrans) ? AB->rkmax : N;
136  pastix_fixdbl_t total_flops = 0.;
137  pastix_fixdbl_t flops = 0.;
138 
139  assert( (C->rk >= 0) && (C->rk <= C->rkmax) );
140 
141  /*
142  * The rank is too big, we need to uncompress/compress C
143  */
144  if ( (C->rk + rAB) > rklimit )
145  {
146  pastix_complex32_t *Cfr, *Coff;
147  int allocated = 0;
148  if ( (Cfr = core_clrmm_getws( params, Cm * Cn )) == NULL ) {
149  Cfr = malloc( Cm * Cn * sizeof(pastix_complex32_t) );
150  allocated = 1;
151  }
152  Coff = Cfr + Cm * offy + offx;
153 
154  kernel_trace_start_lvl2( PastixKernelLvl2_LR_add2C_uncompress );
155  cblas_cgemm( CblasColMajor, CblasNoTrans, CblasNoTrans,
156  Cm, Cn, C->rk,
157  CBLAS_SADDR(cone), C->u, Cm,
158  C->v, C->rkmax,
159  CBLAS_SADDR(czero), Cfr, Cm );
160  flops = FLOPS_CGEMM( Cm, Cn, C->rk );
161 
162  /* Add A*B */
163  if ( AB->rk == -1 ) {
164  core_cgeadd( PastixNoTrans, M, N,
165  alpha, AB->u, M,
166  beta, Coff, Cm );
167  flops += (2. * M * N);
168  }
169  else {
170  cblas_cgemm( CblasColMajor, CblasNoTrans, (CBLAS_TRANSPOSE)transV,
171  M, N, AB->rk,
172  CBLAS_SADDR(alpha), AB->u, ldabu,
173  AB->v, ldabv,
174  CBLAS_SADDR(beta), Coff, Cm );
175  flops += FLOPS_CGEMM( M, N, AB->rk );
176  }
177  kernel_trace_stop_lvl2( flops );
178  total_flops += flops;
179 
180  /* Try to recompress */
181  kernel_trace_start_lvl2( PastixKernelLvl2_LR_add2C_recompress );
182  core_clrfree(C); // TODO: Can we give it directly to ge2lr as this
183  flops = lowrank->core_ge2lr( lowrank->use_reltol, lowrank->tolerance, -1, Cm, Cn, Cfr, Cm, C );
184  kernel_trace_stop_lvl2_rank( flops, C->rk );
185  total_flops += flops;
186 
187  if (allocated) {
188  free(Cfr);
189  }
190  }
191  /*
192  * The rank is not too large, we perform a low-rank update
193  */
194  else {
195  total_flops += lowrank->core_rradd( lowrank, transV, &alpha,
196  M, N, AB,
197  Cm, Cn, C,
198  offx, offy );
199  }
200 
202  return total_flops;
203 }
204 
205 /**
206  *******************************************************************************
207  *
208  * @brief Perform the addition of the low-rank matrix AB into the null matrix C.
209  *
210  *******************************************************************************
211  *
212  * @param[inout] params
213  * The LRMM structure that stores all the parameters used in the LRMM
214  * functions family.
215  * On exit, the C matrix contains the product AB aligned with its own
216  * dimensions.
217  * @sa core_clrmm_t
218  *
219  * @param[in] AB
220  * The low-rank structure of the AB matrix to apply to C.
221  *
222  * @param[in] transV
223  * Specify if AB->v is stored normally or transposed.
224  * - If PastixNoTrans, AB->v is stored normally for low-rank format.
225  * - If PastixTrans, AB->v is stored transposed.
226  * - If PastixConjTrans, AB->v is stored transposed, and conjf() must be
227  * applied to the matrix.
228  *
229  * @param[in] infomask
230  * Mask of informations returned by the core_cxx2lr() functions.
231  * If CORE_LRMM_ORTHOU is set, then AB.u is orthogonal, otherwise an
232  * orthogonalization step is added before adding it to C.
233  *
234  *******************************************************************************
235  *
236  * @return The number of flops required to perform the operation.
237  *
238  *******************************************************************************/
239 static inline pastix_fixdbl_t
241  const pastix_lrblock_t *AB,
242  pastix_trans_t transV,
243  int infomask )
244 {
245  PASTE_CORE_CLRMM_PARAMS( params );
246  pastix_int_t rklimit = core_get_rklimit( Cm, Cn );
247  pastix_int_t ldabu = M;
248  pastix_int_t ldabv = (transV == PastixNoTrans) ? AB->rkmax : N;
249  pastix_fixdbl_t total_flops = 0.;
250  pastix_fixdbl_t flops;
251  int allocated = 0;
252 
253  assert( C->rk == 0 );
254 
255  if ( AB->rk > rklimit ) {
256  pastix_complex32_t *Cfr, *Coff;
257  if ( (Cfr = core_clrmm_getws( params, Cm * Cn )) == NULL ) {
258  Cfr = malloc( Cm * Cn * sizeof(pastix_complex32_t) );
259  allocated = 1;
260  }
261  Coff = Cfr + Cm * offy + offx;
262 
263  /* Set to 0 if contribution smaller than C */
264  if ( (M != Cm) || (N != Cn) ) {
265  memset( Cfr, 0, Cm * Cn * sizeof(pastix_complex32_t) );
266  }
267 
268  /* Uncompress the AB product into C */
269  flops = FLOPS_CGEMM( M, N, AB->rk );
270  kernel_trace_start_lvl2( PastixKernelLvl2_LR_add2C_uncompress );
271  cblas_cgemm( CblasColMajor, CblasNoTrans, (CBLAS_TRANSPOSE)transV,
272  M, N, AB->rk,
273  CBLAS_SADDR(alpha), AB->u, ldabu,
274  AB->v, ldabv,
275  CBLAS_SADDR(beta), Coff, Cm );
276  kernel_trace_stop_lvl2( flops );
277  total_flops += flops;
278 
279  /* Try to recompress C */
280  kernel_trace_start_lvl2( PastixKernelLvl2_LR_add2C_recompress );
281  flops = lowrank->core_ge2lr( lowrank->use_reltol, lowrank->tolerance, -1, Cm, Cn, Cfr, Cm, C );
282  kernel_trace_stop_lvl2_rank( flops, C->rk );
283  total_flops += flops;
284 
285  if ( allocated ) {
286  free( work );
287  }
288  }
289  else {
290  /*
291  * Let's chech that AB->u is orthogonal before copying it to C.u
292  */
293  int orthou = infomask & PASTIX_LRM3_ORTHOU;
294  if ( !orthou ) {
295  pastix_complex32_t *ABfr;
296  pastix_lrblock_t backup;
297  int allocated = 0;
298 
299  kernel_trace_start_lvl2( PastixKernelLvl2_LR_add2C_orthou );
300 
301  if ( AB->rk > 0 ) {
302  if ( (ABfr = core_clrmm_getws( params, M * N )) == NULL ) {
303  ABfr = malloc( M * N * sizeof(pastix_complex32_t) );
304  allocated = 1;
305  }
306 
307  cblas_cgemm( CblasColMajor, CblasNoTrans, (CBLAS_TRANSPOSE)transV,
308  M, N, AB->rk,
309  CBLAS_SADDR(cone), AB->u, ldabu,
310  AB->v, ldabv,
311  CBLAS_SADDR(czero), ABfr, M );
312  flops = FLOPS_CGEMM( M, N, AB->rk );
313  }
314  else {
315  ABfr = AB->u;
316  flops = 0.0;
317  }
318 
319  flops += lowrank->core_ge2lr( lowrank->use_reltol, lowrank->tolerance, rklimit,
320  M, N, ABfr, M, &backup );
321 
322  core_clrcpy( lowrank, PastixNoTrans, alpha,
323  M, N, &backup, Cm, Cn, C,
324  offx, offy );
325 
326  kernel_trace_stop_lvl2( flops );
327  core_clrfree( &backup );
328  total_flops += flops;
329 
330  if ( allocated ) {
331  free( ABfr );
332  }
333  }
334  /*
335  * AB->u is orthogonal, we directly copy AB->u into C
336  */
337  else {
338  core_clrcpy( lowrank, transV, alpha,
339  M, N, AB, Cm, Cn, C,
340  offx, offy );
341  }
342  }
343 
345  return total_flops;
346 }
347 
348 /**
349  *******************************************************************************
350  *
351  * @brief Perform the addition of two low-rank matrices.
352  *
353  *******************************************************************************
354  *
355  * @param[inout] params
356  * The LRMM structure that stores all the parameters used in the LRMM
357  * functions family.
358  * On exit, the C matrix contains the addition of C and A aligned with its own
359  * dimensions.
360  * @sa core_clrmm_t
361  *
362  * @param[in] A
363  * The low-rank structure of the A matrix to add to C.
364  *
365  * @param[in] transV
366  * Specify if A->v is stored normally or transposed.
367  * - If PastixNoTrans, AB->v is stored normally for low-rank format.
368  * - If PastixTrans, AB->v is stored transposed.
369  * - If PastixConjTrans, AB->v is stored transposed, and conjf() must be
370  * applied to the matrix.
371  *
372  * @param[in] infomask
373  * Mask of informations returned by the core_cxx2lr() functions.
374  * If CORE_LRMM_ORTHOU is set, then A.u is orthogonal, otherwise an
375  * orthogonalization step is added before adding it to C.
376  *
377  *******************************************************************************
378  *
379  * @return The number of flops required to perform the operation.
380  *
381  *******************************************************************************/
382 pastix_fixdbl_t
384  const pastix_lrblock_t *A,
385  pastix_trans_t transV,
386  int infomask )
387 {
388  pastix_lrblock_t *C = params->C;
389  pastix_fixdbl_t flops = 0.;
390 
391  if ( A->rk != 0 ) {
392  pastix_atomic_lock( params->lock );
393  switch ( C->rk ) {
394  case -1:
395  /*
396  * C became full rank
397  */
398  flops = core_clr2fr( params, A, transV );
399  break;
400 
401  case 0:
402  /*
403  * C is still null
404  */
405  flops = core_clr2null( params, A, transV, infomask );
406  break;
407 
408  default:
409  /*
410  * C is low-rank of rank k
411  */
412  flops = core_clr2lr( params, A, transV );
413  }
414  assert( C->rk <= C->rkmax);
415  pastix_atomic_unlock( params->lock );
416  }
417 
418  return flops;
419 }
core_clrcpy
void core_clrcpy(const pastix_lr_t *lowrank, pastix_trans_t transA, pastix_complex32_t alpha, pastix_int_t M1, pastix_int_t N1, const pastix_lrblock_t *A, pastix_int_t M2, pastix_int_t N2, pastix_lrblock_t *B, pastix_int_t offx, pastix_int_t offy)
Copy a small low-rank structure into a large one.
Definition: core_cgelrops.c:418
solver.h
core_get_rklimit
pastix_int_t(* core_get_rklimit)(pastix_int_t, pastix_int_t)
Compute the maximal rank accepted for a given matrix size. The pointer is set according to the low-ra...
Definition: kernels_trace.c:30
pastix_lrblock_s::v
void * v
Definition: pastix_lowrank.h:116
pastix_lrblock_s::u
void * u
Definition: pastix_lowrank.h:115
PASTE_CORE_CLRMM_VOID
#define PASTE_CORE_CLRMM_VOID
Void all the parameters of the core_clrmm family functions to silent warnings.
Definition: pastix_clrcores.h:128
pastix_trans_t
enum pastix_trans_e pastix_trans_t
Transpostion.
core_clrmm_getws
static pastix_complex32_t * core_clrmm_getws(core_clrmm_t *params, ssize_t newsize)
Function to get a workspace pointer if space is available in the one provided.
Definition: pastix_clrcores.h:155
pastix_lrblock_s
The block low-rank structure to hold a matrix in low-rank form.
Definition: pastix_lowrank.h:112
core_clr2fr
static pastix_fixdbl_t core_clr2fr(core_clrmm_t *params, const pastix_lrblock_t *AB, pastix_trans_t transV)
Perform the addition of the low-rank matrix AB and the full-rank matrix C.
Definition: core_clr2xx.c:60
PastixNoTrans
@ PastixNoTrans
Definition: api.h:424
core_clrmm_s::lock
pastix_atomic_lock_t * lock
Definition: pastix_clrcores.h:99
core_clrmm_s
Structure to store all the parameters of the core_clrmm family functions.
Definition: pastix_clrcores.h:80
core_clradd
pastix_fixdbl_t core_clradd(core_clrmm_t *params, const pastix_lrblock_t *A, pastix_trans_t transV, int infomask)
Perform the addition of two low-rank matrices.
Definition: core_clr2xx.c:383
core_clr2lr
static pastix_fixdbl_t core_clr2lr(core_clrmm_t *params, const pastix_lrblock_t *AB, pastix_trans_t transV)
Perform the addition of the low-rank matrix AB and the low-rank matrix C.
Definition: core_clr2xx.c:127
pastix_ccores.h
PASTIX_LRM3_ORTHOU
#define PASTIX_LRM3_ORTHOU
Macro to specify if the U part of a low-rank matrix is orthogonal or not (Used in LRMM functions).
Definition: pastix_lowrank.h:41
core_clr2null
static pastix_fixdbl_t core_clr2null(core_clrmm_t *params, const pastix_lrblock_t *AB, pastix_trans_t transV, int infomask)
Perform the addition of the low-rank matrix AB into the null matrix C.
Definition: core_clr2xx.c:240
pastix_lrblock_s::rk
int rk
Definition: pastix_lowrank.h:113
core_clrmm_s::C
pastix_lrblock_t * C
Definition: pastix_clrcores.h:95
PASTE_CORE_CLRMM_PARAMS
#define PASTE_CORE_CLRMM_PARAMS(_a_)
Initialize all the parameters of the core_clrmm family functions to ease the access.
Definition: pastix_clrcores.h:105
core_clrfree
void core_clrfree(pastix_lrblock_t *A)
Free a low-rank matrix.
Definition: core_cgelrops.c:116
pastix_lrblock_s::rkmax
int rkmax
Definition: pastix_lowrank.h:114
pastix_clrcores.h
core_cgeadd
int core_cgeadd(pastix_trans_t trans, pastix_int_t M, pastix_int_t N, pastix_complex32_t alpha, const pastix_complex32_t *A, pastix_int_t LDA, pastix_complex32_t beta, pastix_complex32_t *B, pastix_int_t LDB)
Add two matrices together.
Definition: core_cgeadd.c:78