-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathcdrive.c
575 lines (504 loc) · 17.9 KB
/
cdrive.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*
Copyright (c) 2003, The Regents of the University of California, through
Lawrence Berkeley National Laboratory (subject to receipt of any required
approvals from U.S. Dept. of Energy)
All rights reserved.
The source code is distributed under BSD license, see the file License.txt
at the top-level directory.
*/
/*
* -- SuperLU routine (version 7.0.0) --
* Univ. of California Berkeley, Xerox Palo Alto Research Center,
* and Lawrence Berkeley National Lab.
* October 15, 2003
* August 2024
*
*/
/*! \file
* CDRIVE is the main test program for the SINGLE COMPLEX linear
* equation driver routines CGSSV and CGSSVX.
*
* The program is invoked by a shell script file -- ctest.csh.
* The output from the tests are written into a file -- ctest.out.
*
* \ingroup TestingC
*/
#include <getopt.h>
#include <string.h>
#include "slu_cdefs.h"
#include "MATGEN/matgen.h"
#define NTESTS 5 /* Number of test types */
#define NTYPES 11 /* Number of matrix types */
#define NTRAN 2
#define THRESH 20.0
#define FMT1 "%10s:n=%d, test(%d)=%12.5g\n"
#define FMT2 "%10s:fact=%4d, trans=%4d, equed=%c, n=%d, imat=%d, test(%d)=%12.5g\n"
#define FMT3 "%10s:info=%d, izero=%d, n=%d, nrhs=%d, imat=%d, nfail=%d\n"
static void
parse_command_line(int argc, char *argv[], char *matrix_type,
int *n, int *w, int *relax, int *nrhs, int *maxsuper,
int *rowblk, int *colblk, int_t *lwork, double *u, FILE **fp);
/*!
* Entry point of test program.
*/
int main(int argc, char *argv[])
{
singlecomplex *a, *a_save;
int_t *asub, *asub_save;
int_t *xa, *xa_save;
SuperMatrix A, B, X, L, U;
SuperMatrix ASAV, AC;
GlobalLU_t Glu; /* Not needed on return. */
mem_usage_t mem_usage;
int *perm_r; /* row permutation from partial pivoting */
int *perm_c, *pc_save; /* column permutation */
int *etree;
singlecomplex zero = {0.0, 0.0};
float *R, *C;
float *ferr, *berr;
float *rwork;
singlecomplex *wwork;
void *work = NULL;
int nrhs, panel_size, relax;
int m, n, info1;
int_t nnz, lwork, info;
singlecomplex *xact;
singlecomplex *rhsb, *solx, *bsav;
int ldb, ldx;
float rpg, rcond;
int i, j, k1;
float rowcnd, colcnd, amax;
int maxsuper, rowblk, colblk;
int prefact, equil, iequed;
int nt, nrun, nfail, nerrs, imat, fimat, nimat;
int nfact, ifact, itran;
int kl, ku, mode, lda, ioff;
int zerot; /* indicate whether the matrix is singular */
int izero; /* indicate the first column that is entirely zero */
double u;
float anorm, cndnum;
singlecomplex *Afull;
float result[NTESTS];
superlu_options_t options;
fact_t fact;
trans_t trans;
SuperLUStat_t stat;
static char matrix_type[8];
static char equed[1], path[4], sym[1], dist[1];
FILE *fp;
/* Fixed set of parameters */
int iseed[] = {1988, 1989, 1990, 1991};
static char equeds[] = {'N', 'R', 'C', 'B'};
static fact_t facts[] = {FACTORED, DOFACT, SamePattern,
SamePattern_SameRowPerm};
static trans_t transs[] = {NOTRANS, TRANS, CONJ};
/* Some function prototypes */
extern int cgst01(int, int, SuperMatrix *, SuperMatrix *,
SuperMatrix *, int *, int *, float *);
extern int cgst02(trans_t, int, int, int, SuperMatrix *, singlecomplex *,
int, singlecomplex *, int, float *resid);
extern int cgst04(int, int, singlecomplex *, int,
singlecomplex *, int, float rcond, float *resid);
extern int cgst07(trans_t, int, int, SuperMatrix *, singlecomplex *, int,
singlecomplex *, int, singlecomplex *, int,
float *, float *, float *);
extern int clatb4_slu(char *, int *, int *, int *, char *, int *, int *,
float *, int *, float *, char *);
extern int clatms_slu(int *, int *, char *, int *, char *, float *d,
int *, float *, float *, int *, int *,
char *, singlecomplex *, int *, singlecomplex *, int *);
extern int sp_cconvert(int, int, singlecomplex *, int, int, int,
singlecomplex *a, int_t *, int_t *, int_t *);
/* Executable statements */
strcpy(path, "CGE");
nrun = 0;
nfail = 0;
nerrs = 0;
/* Defaults */
lwork = 0;
n = 1;
nrhs = 1;
panel_size = sp_ienv(1);
relax = sp_ienv(2);
u = 1.0;
strcpy(matrix_type, "LA");
parse_command_line(argc, argv, matrix_type, &n,
&panel_size, &relax, &nrhs, &maxsuper,
&rowblk, &colblk, &lwork, &u, &fp);
if ( lwork > 0 ) {
work = SUPERLU_MALLOC(lwork);
if ( !work ) {
fprintf(stderr, "expert: cannot allocate %lld bytes\n", (long long) lwork);
exit (-1);
}
}
/* Set the default input options. */
set_default_options(&options);
options.DiagPivotThresh = u;
options.PrintStat = NO;
options.PivotGrowth = YES;
options.ConditionNumber = YES;
options.IterRefine = SLU_SINGLE;
if ( strcmp(matrix_type, "LA") == 0 ) {
/* Test LAPACK matrix suite. */
m = n;
lda = SUPERLU_MAX(n, 1);
nnz = n * n; /* upper bound */
fimat = 1;
nimat = NTYPES;
Afull = singlecomplexCalloc(lda * n);
callocateA(n, nnz, &a, &asub, &xa);
} else {
/* Read a sparse matrix */
fimat = nimat = 0;
creadhb(fp, &m, &n, &nnz, &a, &asub, &xa);
}
callocateA(n, nnz, &a_save, &asub_save, &xa_save);
rhsb = singlecomplexMalloc(m * nrhs);
bsav = singlecomplexMalloc(m * nrhs);
solx = singlecomplexMalloc(n * nrhs);
xact = singlecomplexMalloc(n * nrhs);
wwork = singlecomplexCalloc( SUPERLU_MAX(m,n) * SUPERLU_MAX(4,nrhs) );
ldb = m;
ldx = n;
cCreate_Dense_Matrix(&B, m, nrhs, rhsb, ldb, SLU_DN, SLU_C, SLU_GE);
cCreate_Dense_Matrix(&X, n, nrhs, solx, ldx, SLU_DN, SLU_C, SLU_GE);
etree = int32Malloc(n);
perm_r = int32Malloc(n);
perm_c = int32Malloc(n);
pc_save = int32Malloc(n);
R = (float *) SUPERLU_MALLOC(m*sizeof(float));
C = (float *) SUPERLU_MALLOC(n*sizeof(float));
ferr = (float *) SUPERLU_MALLOC(nrhs*sizeof(float));
berr = (float *) SUPERLU_MALLOC(nrhs*sizeof(float));
j = SUPERLU_MAX(m,n) * SUPERLU_MAX(4,nrhs);
rwork = (float *) SUPERLU_MALLOC(j*sizeof(float));
for (i = 0; i < j; ++i) rwork[i] = 0.;
if ( !R ) ABORT("SUPERLU_MALLOC fails for R");
if ( !C ) ABORT("SUPERLU_MALLOC fails for C");
if ( !ferr ) ABORT("SUPERLU_MALLOC fails for ferr");
if ( !berr ) ABORT("SUPERLU_MALLOC fails for berr");
if ( !rwork ) ABORT("SUPERLU_MALLOC fails for rwork");
for (i = 0; i < n; ++i) perm_c[i] = pc_save[i] = i;
options.ColPerm = MY_PERMC;
for (imat = fimat; imat <= nimat; ++imat) { /* All matrix types */
if ( imat ) {
/* Skip types 5, 6, or 7 if the matrix size is too small. */
zerot = (imat >= 5 && imat <= 7);
if ( zerot && n < imat-4 )
continue;
/* Set up parameters with CLATB4 and generate a test matrix
with CLATMS. */
clatb4_slu(path, &imat, &n, &n, sym, &kl, &ku, &anorm, &mode,
&cndnum, dist);
clatms_slu(&n, &n, dist, iseed, sym, &rwork[0], &mode, &cndnum,
&anorm, &kl, &ku, "No packing", Afull, &lda,
&wwork[0], &info1);
if ( info1 ) {
printf(FMT3, "CLATMS", info1, izero, n, nrhs, imat, nfail);
continue;
}
/* For types 5-7, zero one or more columns of the matrix
to test that INFO is returned correctly. */
if ( zerot ) {
if ( imat == 5 ) izero = 1;
else if ( imat == 6 ) izero = n;
else izero = n / 2 + 1;
ioff = (izero - 1) * lda;
if ( imat < 7 ) {
for (i = 0; i < n; ++i) Afull[ioff + i] = zero;
} else {
for (j = 0; j < n - izero + 1; ++j)
for (i = 0; i < n; ++i)
Afull[ioff + i + j*lda] = zero;
}
} else {
izero = n+1; /* none of the column is zero */
}
/* Convert to sparse representation. */
sp_cconvert(n, n, Afull, lda, kl, ku, a, asub, xa, &nnz);
} else {
izero = n+1; /* none of the column is zero */
zerot = 0;
}
cCreate_CompCol_Matrix(&A, m, n, nnz, a, asub, xa, SLU_NC, SLU_C, SLU_GE);
/* Save a copy of matrix A in ASAV */
cCreate_CompCol_Matrix(&ASAV, m, n, nnz, a_save, asub_save, xa_save,
SLU_NC, SLU_C, SLU_GE);
cCopy_CompCol_Matrix(&A, &ASAV);
/* Form exact solution. */
cGenXtrue(n, nrhs, xact, ldx);
StatInit(&stat);
for (iequed = 0; iequed < 4; ++iequed) {
*equed = equeds[iequed];
if (iequed == 0) nfact = 4;
else nfact = 1; /* Only test factored, pre-equilibrated matrix */
for (ifact = 0; ifact < nfact; ++ifact) {
fact = facts[ifact];
options.Fact = fact;
for (equil = 0; equil < 2; ++equil) {
options.Equil = equil;
prefact = ( options.Fact == FACTORED ||
options.Fact == SamePattern_SameRowPerm );
/* Need a first factor */
/* Restore the matrix A. */
cCopy_CompCol_Matrix(&ASAV, &A);
if ( zerot ) {
if ( prefact ) continue;
} else if ( options.Fact == FACTORED ) {
if ( equil || iequed ) {
/* Compute row and column scale factors to
equilibrate matrix A. */
cgsequ(&A, R, C, &rowcnd, &colcnd, &amax, &info1);
/* Force equilibration. */
if ( !info && n > 0 ) {
if ( strncmp(equed, "R", 1)==0 ) {
rowcnd = 0.;
colcnd = 1.;
} else if ( strncmp(equed, "C", 1)==0 ) {
rowcnd = 1.;
colcnd = 0.;
} else if ( strncmp(equed, "B", 1)==0 ) {
rowcnd = 0.;
colcnd = 0.;
}
}
/* Equilibrate the matrix. */
claqgs(&A, R, C, rowcnd, colcnd, amax, equed);
}
}
if ( prefact ) { /* Need a factor for the first time */
/* Save Fact option. */
fact = options.Fact;
options.Fact = DOFACT;
/* Preorder the matrix, obtain the column etree. */
sp_preorder(&options, &A, perm_c, etree, &AC);
/* Factor the matrix AC. */
cgstrf(&options, &AC, relax, panel_size,
etree, work, lwork, perm_c, perm_r, &L, &U,
&Glu, &stat, &info);
if ( info ) {
printf("** First factor: info %lld, equed %c\n",
(long long) info, *equed);
if ( lwork == -1 ) {
printf("** Estimated memory: %lld bytes\n",
(long long) info - n);
exit(0);
}
}
Destroy_CompCol_Permuted(&AC);
/* Restore Fact option. */
options.Fact = fact;
} /* if .. first time factor */
for (itran = 0; itran < NTRAN; ++itran) {
trans = transs[itran];
options.Trans = trans;
/* Restore the matrix A. */
cCopy_CompCol_Matrix(&ASAV, &A);
/* Set the right hand side. */
cFillRHS(trans, nrhs, xact, ldx, &A, &B);
cCopy_Dense_Matrix(m, nrhs, rhsb, ldb, bsav, ldb);
/*----------------
* Test cgssv
*----------------*/
if ( options.Fact == DOFACT && itran == 0) {
/* Not yet factored, and untransposed */
cCopy_Dense_Matrix(m, nrhs, rhsb, ldb, solx, ldx);
cgssv(&options, &A, perm_c, perm_r, &L, &U, &X,
&stat, &info);
if ( info && info != izero ) {
printf(FMT3, "cgssv",
(int) info, izero, n, nrhs, imat, nfail);
} else {
/* Reconstruct matrix from factors and compute residual.
* Only compute the leading 'izero' nonzero columns.
*/
cgst01(m, izero-1, &A, &L, &U, perm_c, perm_r,
&result[0]);
nt = 1;
if ( izero == (n+1) ) {
/* Compute residual of the computed
solution. */
cCopy_Dense_Matrix(m, nrhs, rhsb, ldb,
wwork, ldb);
cgst02(trans, m, n, nrhs, &A, solx,
ldx, wwork,ldb, &result[1]);
nt = 2;
}
/* Print information about the tests that
did not pass the threshold. */
for (i = 0; i < nt; ++i) {
if ( result[i] >= THRESH ) {
printf(FMT1, "cgssv", n, i,
result[i]);
++nfail;
}
}
nrun += nt;
} /* else .. info == 0 */
/* Restore perm_c. */
for (i = 0; i < n; ++i) perm_c[i] = pc_save[i];
if (lwork == 0) {
Destroy_SuperNode_Matrix(&L);
Destroy_CompCol_Matrix(&U);
}
} /* if .. end of testing cgssv */
/*----------------
* Test cgssvx
*----------------*/
/* Equilibrate the matrix if fact = FACTORED and
equed = 'R', 'C', or 'B'. */
if ( options.Fact == FACTORED &&
(equil || iequed) && n > 0 ) {
claqgs(&A, R, C, rowcnd, colcnd, amax, equed);
}
/* Solve the system and compute the condition number
and error bounds using cgssvx. */
cgssvx(&options, &A, perm_c, perm_r, etree,
equed, R, C, &L, &U, work, lwork, &B, &X, &rpg,
&rcond, ferr, berr, &Glu,
&mem_usage, &stat, &info);
if ( info && info != izero ) {
printf(FMT3, "cgssvx",
(int) info, izero, n, nrhs, imat, nfail);
if ( lwork == -1 ) {
printf("** Estimated memory: %.0f bytes\n",
mem_usage.total_needed);
exit(0);
}
} else {
if ( !prefact ) {
/* Reconstruct matrix from factors and compute residual.
* Only compute the leading 'izero' nonzero columns.
*/
cgst01(m, izero-1, &A, &L, &U, perm_c, perm_r,
&result[0]);
k1 = 0;
} else {
k1 = 1;
}
if ( !info ) {
/* Compute residual of the computed solution.*/
cCopy_Dense_Matrix(m, nrhs, bsav, ldb,
wwork, ldb);
cgst02(trans, m, n, nrhs, &ASAV, solx, ldx,
wwork, ldb, &result[1]);
/* Check solution from generated exact
solution. */
cgst04(n, nrhs, solx, ldx, xact, ldx, rcond,
&result[2]);
/* Check the error bounds from iterative
refinement. */
cgst07(trans, n, nrhs, &ASAV, bsav, ldb,
solx, ldx, xact, ldx, ferr, berr,
&result[3]);
/* Print information about the tests that did
not pass the threshold. */
for (i = k1; i < NTESTS; ++i) {
if ( result[i] >= THRESH ) {
printf(FMT2, "cgssvx",
options.Fact, trans, *equed,
n, imat, i, result[i]);
++nfail;
}
}
nrun += NTESTS;
} /* if .. info == 0 */
} /* else .. end of testing cgssvx */
} /* for itran ... */
if ( lwork == 0 ) {
Destroy_SuperNode_Matrix(&L);
Destroy_CompCol_Matrix(&U);
}
} /* for equil ... */
} /* for ifact ... */
} /* for iequed ... */
#if 0
if ( !info ) {
PrintPerf(&L, &U, &mem_usage, rpg, rcond, ferr, berr, equed);
}
#endif
Destroy_SuperMatrix_Store(&A);
Destroy_SuperMatrix_Store(&ASAV);
StatFree(&stat);
} /* for imat ... */
/* Print a summary of the results. */
PrintSumm("CGE", nfail, nrun, nerrs);
if ( strcmp(matrix_type, "LA") == 0 ) SUPERLU_FREE (Afull);
SUPERLU_FREE (rhsb);
SUPERLU_FREE (bsav);
SUPERLU_FREE (solx);
SUPERLU_FREE (xact);
SUPERLU_FREE (etree);
SUPERLU_FREE (perm_r);
SUPERLU_FREE (perm_c);
SUPERLU_FREE (pc_save);
SUPERLU_FREE (R);
SUPERLU_FREE (C);
SUPERLU_FREE (ferr);
SUPERLU_FREE (berr);
SUPERLU_FREE (rwork);
SUPERLU_FREE (wwork);
Destroy_SuperMatrix_Store(&B);
Destroy_SuperMatrix_Store(&X);
#if 0
Destroy_CompCol_Matrix(&A);
Destroy_CompCol_Matrix(&ASAV);
#else
SUPERLU_FREE(a); SUPERLU_FREE(asub); SUPERLU_FREE(xa);
SUPERLU_FREE(a_save); SUPERLU_FREE(asub_save); SUPERLU_FREE(xa_save);
#endif
if ( lwork > 0 ) {
SUPERLU_FREE (work);
Destroy_SuperMatrix_Store(&L);
Destroy_SuperMatrix_Store(&U);
}
return (nfail == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
} /* end main */
/*!
* Parse command line options to get relaxed snode size, panel size, etc.
*/
static void
parse_command_line(int argc, char *argv[], char *matrix_type,
int *n, int *w, int *relax, int *nrhs, int *maxsuper,
int *rowblk, int *colblk, int_t *lwork, double *u, FILE **fp)
{
int c;
extern char *optarg;
while ( (c = getopt(argc, argv, "ht:n:w:r:s:m:b:c:l:u:f:")) != EOF ) {
switch (c) {
case 'h':
printf("Options:\n");
printf("\t-w <int> - panel size\n");
printf("\t-r <int> - granularity of relaxed supernodes\n");
exit(1);
break;
case 't': strcpy(matrix_type, optarg);
break;
case 'n': *n = atoi(optarg);
break;
case 'w': *w = atoi(optarg);
break;
case 'r': *relax = atoi(optarg);
break;
case 's': *nrhs = atoi(optarg);
break;
case 'm': *maxsuper = atoi(optarg);
break;
case 'b': *rowblk = atoi(optarg);
break;
case 'c': *colblk = atoi(optarg);
break;
case 'l': *lwork = atoi(optarg);
break;
case 'u': *u = atof(optarg);
break;
case 'f':
if ( !(*fp = fopen(optarg, "r")) ) {
ABORT("File does not exist");
}
printf(".. test sparse matrix in file: %s\n", optarg);
break;
}
}
}