Newer
Older
/*----------------------------------------------------------------------
SerialReax - Reax Force Field Simulator
Copyright (2010) Purdue University
Hasan Metin Aktulga, haktulga@cs.purdue.edu
Joseph Fogarty, jcfogart@mail.usf.edu
Sagar Pandit, pandit@usf.edu
Ananth Y Grama, ayg@cs.purdue.edu
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
See the GNU General Public License for more details:
<http://www.gnu.org/licenses/>.
----------------------------------------------------------------------*/
#include "lin_alg.h"
Kurt A. O'Hearn
committed
#include "allocate.h"
Kurt A. O'Hearn
committed
#include "tool_box.h"
Kurt A. O'Hearn
committed
#if defined(HAVE_LAPACKE_MKL)
Kurt A. O'Hearn
committed
Kurt A. O'Hearn
committed
typedef struct
{
unsigned int j;
real val;
} sparse_matrix_entry;
Kurt A. O'Hearn
committed
enum preconditioner_type
{
LEFT = 0,
RIGHT = 1,
};
Kurt A. O'Hearn
committed
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
#if defined(TEST_MAT)
static sparse_matrix * create_test_mat( void )
{
unsigned int i, n;
sparse_matrix *H_test;
if ( Allocate_Matrix( &H_test, 3, 6 ) == FAILURE )
{
fprintf( stderr, "not enough memory for test matrices. terminating.\n" );
exit( INSUFFICIENT_MEMORY );
}
//3x3, SPD, store lower half
i = 0;
n = 0;
H_test->start[n] = i;
H_test->j[i] = 0;
H_test->val[i] = 4.;
++i;
++n;
H_test->start[n] = i;
H_test->j[i] = 0;
H_test->val[i] = 12.;
++i;
H_test->j[i] = 1;
H_test->val[i] = 37.;
++i;
++n;
H_test->start[n] = i;
H_test->j[i] = 0;
H_test->val[i] = -16.;
++i;
H_test->j[i] = 1;
H_test->val[i] = -43.;
++i;
H_test->j[i] = 2;
H_test->val[i] = 98.;
++i;
++n;
H_test->start[n] = i;
return H_test;
}
#endif
/* Routine used with qsort for sorting nonzeros within a sparse matrix row
*
* v1/v2: pointers to column indices of nonzeros within a row (unsigned int)
*/
static int compare_matrix_entry(const void *v1, const void *v2)
{
/* larger element has larger column index */
return ((sparse_matrix_entry *)v1)->j - ((sparse_matrix_entry *)v2)->j;
}
/* Routine used for sorting nonzeros within a sparse matrix row;
* internally, a combination of qsort and manual sorting is utilized
* (parallel calls to qsort when multithreading, rows mapped to threads)
*
* A: sparse matrix for which to sort nonzeros within a row, stored in CSR format
*/
void Sort_Matrix_Rows( sparse_matrix * const A )
{
unsigned int i, j, si, ei;
sparse_matrix_entry *temp;
#ifdef _OPENMP
// #pragma omp parallel default(none) private(i, j, si, ei, temp) shared(stderr)
Kurt A. O'Hearn
committed
#endif
{
temp = (sparse_matrix_entry *) smalloc( A->n * sizeof(sparse_matrix_entry),
Kurt A. O'Hearn
committed
"Sort_Matrix_Rows::temp" );
Kurt A. O'Hearn
committed
/* sort each row of A using column indices */
#ifdef _OPENMP
// #pragma omp for schedule(guided)
Kurt A. O'Hearn
committed
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
#endif
for ( i = 0; i < A->n; ++i )
{
si = A->start[i];
ei = A->start[i + 1];
for ( j = 0; j < (ei - si); ++j )
{
(temp + j)->j = A->j[si + j];
(temp + j)->val = A->val[si + j];
}
/* polymorphic sort in standard C library using column indices */
qsort( temp, ei - si, sizeof(sparse_matrix_entry), compare_matrix_entry );
for ( j = 0; j < (ei - si); ++j )
{
A->j[si + j] = (temp + j)->j;
A->val[si + j] = (temp + j)->val;
}
}
sfree( temp, "Sort_Matrix_Rows::temp" );
}
}
/* Convert a symmetric, half-sored sparse matrix into
* a full-stored sparse matrix
*
* A: symmetric sparse matrix, lower half stored in CSR
* A_full: resultant full sparse matrix in CSR
* If A_full is NULL, allocate space, otherwise do not
*
* Assumptions:
* A has non-zero diagonals
* Each row of A has at least one non-zero (i.e., no rows with all zeros) */
static void compute_full_sparse_matrix( const sparse_matrix * const A,
Kurt A. O'Hearn
committed
sparse_matrix ** A_full )
{
int count, i, pj;
sparse_matrix *A_t;
if ( *A_full == NULL )
{
if ( Allocate_Matrix( A_full, A->n, 2 * A->m - A->n ) == FAILURE )
{
fprintf( stderr, "not enough memory for full A. terminating.\n" );
exit( INSUFFICIENT_MEMORY );
}
}
Kurt A. O'Hearn
committed
else if ( (*A_full)->m < 2 * A->m - A->n )
{
Deallocate_Matrix( *A_full );
if ( Allocate_Matrix( A_full, A->n, 2 * A->m - A->n ) == FAILURE )
{
fprintf( stderr, "not enough memory for full A. terminating.\n" );
exit( INSUFFICIENT_MEMORY );
}
}
if ( Allocate_Matrix( &A_t, A->n, A->m ) == FAILURE )
{
fprintf( stderr, "not enough memory for full A. terminating.\n" );
exit( INSUFFICIENT_MEMORY );
}
/* Set up the sparse matrix data structure for A. */
Transpose( A, A_t );
count = 0;
for ( i = 0; i < A->n; ++i )
{
Kurt A. O'Hearn
committed
if ((*A_full)->start == NULL)
{
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
(*A_full)->start[i] = count;
/* A: symmetric, lower triangular portion only stored */
for ( pj = A->start[i]; pj < A->start[i + 1]; ++pj )
{
(*A_full)->val[count] = A->val[pj];
(*A_full)->j[count] = A->j[pj];
++count;
}
/* A^T: symmetric, upper triangular portion only stored;
* skip diagonal from A^T, as included from A above */
for ( pj = A_t->start[i] + 1; pj < A_t->start[i + 1]; ++pj )
{
(*A_full)->val[count] = A_t->val[pj];
(*A_full)->j[count] = A_t->j[pj];
++count;
}
}
(*A_full)->start[i] = count;
Deallocate_Matrix( A_t );
}
/* Setup routines for sparse approximate inverse preconditioner
*
* A: symmetric sparse matrix, lower half stored in CSR
Kurt A. O'Hearn
committed
* filter:
* A_spar_patt:
*
* Assumptions:
* A has non-zero diagonals
* Each row of A has at least one non-zero (i.e., no rows with all zeros) */
Kurt A. O'Hearn
committed
void setup_sparse_approx_inverse( const sparse_matrix * const A, sparse_matrix ** A_full,
sparse_matrix ** A_spar_patt, sparse_matrix **A_spar_patt_full,
sparse_matrix ** A_app_inv, const real filter )
int left, right, k, p, turn;
real pivot, tmp;
real threshold;
real *list;
if ( Allocate_Matrix( A_spar_patt, A->n, A->m ) == FAILURE )
{
fprintf( stderr, "[SAI] Not enough memory for preconditioning matrices. terminating.\n" );
exit( INSUFFICIENT_MEMORY );
}
}
else if ( ((*A_spar_patt)->m) < (A->m) )
Deallocate_Matrix( *A_spar_patt );
if ( Allocate_Matrix( A_spar_patt, A->n, A->m ) == FAILURE )
{
fprintf( stderr, "[SAI] Not enough memory for preconditioning matrices. terminating.\n" );
exit( INSUFFICIENT_MEMORY );
}
}
/* quick-select algorithm for finding the kth greatest element in the matrix*/
/* list: values from the matrix*/
/* left-right: search space of the quick-select */
list = (real *) smalloc( sizeof(real) * (A->start[A->n]),"Sparse_Approx_Inverse::list" );
left = 0;
right = A->start[A->n] - 1;
k = (int)( (A->start[A->n])*filter );
threshold = 0.0;
for( i = left; i <= right ; ++i )
list[i] = abs( A->val[i] );
}
turn = 0;
while( k ) {
p = left;
turn = 1 - turn;
if( turn == 1)
pivot = list[right];
}
else
{
pivot = list[left];
}
for( i = left + 1 - turn; i <= right-turn; ++i )
{
if( list[i] > pivot )
tmp = list[i];
list[i] = list[p];
list[p] = tmp;
p++;
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
if(turn == 1)
{
tmp = list[p];
list[p] = list[right];
list[right] = tmp;
}
else
{
tmp = list[p];
list[p] = list[left];
list[left] = tmp;
}
if( p == k - 1)
{
threshold = list[p];
break;
}
else if( p > k - 1 )
{
right = p - 1;
}
else
{
left = p + 1;
}
sfree( list, "setup_sparse_approx_inverse::list" );
Kurt A. O'Hearn
committed
/* fill sparsity pattern */
/* diagonal entries are always included */
for ( size = 0, i = 0; i < A->n; ++i )
{
(*A_spar_patt)->start[i] = size;
for ( pj = A->start[i]; pj < A->start[i + 1]; ++pj )
{
Kurt A. O'Hearn
committed
if ( ( A->val[pj] >= threshold ) || ( A->j[pj] == i ) )
(*A_spar_patt)->val[size] = A->val[pj];
(*A_spar_patt)->j[size] = A->j[pj];
(*A_spar_patt)->start[A->n] = size;
Kurt A. O'Hearn
committed
Kurt A. O'Hearn
committed
compute_full_sparse_matrix( A, A_full );
compute_full_sparse_matrix( *A_spar_patt, A_spar_patt_full );
/* A_app_inv has the same sparsity pattern
* * as A_spar_patt_full (omit non-zero values) */
Kurt A. O'Hearn
committed
if ( Allocate_Matrix( A_app_inv, (*A_spar_patt_full)->n, (*A_spar_patt_full)->m ) == FAILURE )
{
fprintf( stderr, "not enough memory for approximate inverse matrix. terminating.\n" );
exit( INSUFFICIENT_MEMORY );
}
Kurt A. O'Hearn
committed
void Calculate_Droptol( const sparse_matrix * const A,
Kurt A. O'Hearn
committed
real * const droptol, const real dtol )
Kurt A. O'Hearn
committed
{
int i, j, k;
real val;
#ifdef _OPENMP
static real *droptol_local;
unsigned int tid;
#endif
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel default(none) private(i, j, k, val, tid), shared(droptol_local, stderr)
Kurt A. O'Hearn
committed
#endif
{
#ifdef _OPENMP
tid = omp_get_thread_num();
Kurt A. O'Hearn
committed
#pragma omp master
Kurt A. O'Hearn
committed
{
if ( droptol_local == NULL )
{
droptol_local = (real*) smalloc( omp_get_num_threads() * A->n * sizeof(real),
Kurt A. O'Hearn
committed
"Calculate_Droptol::droptol_local" );
Kurt A. O'Hearn
committed
}
}
Kurt A. O'Hearn
committed
#pragma omp barrier
Kurt A. O'Hearn
committed
#endif
/* init droptol to 0 */
for ( i = 0; i < A->n; ++i )
{
#ifdef _OPENMP
droptol_local[tid * A->n + i] = 0.0;
#else
droptol[i] = 0.0;
#endif
}
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp barrier
Kurt A. O'Hearn
committed
#endif
/* calculate sqaure of the norm of each row */
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp for schedule(static)
Kurt A. O'Hearn
committed
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
#endif
for ( i = 0; i < A->n; ++i )
{
for ( k = A->start[i]; k < A->start[i + 1] - 1; ++k )
{
j = A->j[k];
val = A->val[k];
#ifdef _OPENMP
droptol_local[tid * A->n + i] += val * val;
droptol_local[tid * A->n + j] += val * val;
#else
droptol[i] += val * val;
droptol[j] += val * val;
#endif
}
// diagonal entry
val = A->val[k];
#ifdef _OPENMP
droptol_local[tid * A->n + i] += val * val;
#else
droptol[i] += val * val;
#endif
}
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp barrier
Kurt A. O'Hearn
committed
Kurt A. O'Hearn
committed
#pragma omp for schedule(static)
Kurt A. O'Hearn
committed
for ( i = 0; i < A->n; ++i )
{
droptol[i] = 0.0;
for ( k = 0; k < omp_get_num_threads(); ++k )
{
droptol[i] += droptol_local[k * A->n + i];
}
}
Kurt A. O'Hearn
committed
#pragma omp barrier
Kurt A. O'Hearn
committed
#endif
/* calculate local droptol for each row */
//fprintf( stderr, "droptol: " );
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp for schedule(static)
Kurt A. O'Hearn
committed
#endif
for ( i = 0; i < A->n; ++i )
{
//fprintf( stderr, "%f-->", droptol[i] );
droptol[i] = SQRT( droptol[i] ) * dtol;
//fprintf( stderr, "%f ", droptol[i] );
}
//fprintf( stderr, "\n" );
}
}
int Estimate_LU_Fill( const sparse_matrix * const A, const real * const droptol )
{
int i, pj;
int fillin;
real val;
fillin = 0;
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel for schedule(static) \
default(none) private(i, pj, val) reduction(+: fillin)
Kurt A. O'Hearn
committed
#endif
for ( i = 0; i < A->n; ++i )
{
for ( pj = A->start[i]; pj < A->start[i + 1] - 1; ++pj )
{
val = A->val[pj];
if ( FABS(val) > droptol[i] )
{
++fillin;
}
}
}
return fillin + A->n;
}
#if defined(HAVE_SUPERLU_MT)
real SuperLU_Factorize( const sparse_matrix * const A,
Kurt A. O'Hearn
committed
sparse_matrix * const L, sparse_matrix * const U )
Kurt A. O'Hearn
committed
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
{
unsigned int i, pj, count, *Ltop, *Utop, r;
sparse_matrix *A_t;
SuperMatrix A_S, AC_S, L_S, U_S;
NCformat *A_S_store;
SCPformat *L_S_store;
NCPformat *U_S_store;
superlumt_options_t superlumt_options;
pxgstrf_shared_t pxgstrf_shared;
pdgstrf_threadarg_t *pdgstrf_threadarg;
int_t nprocs;
fact_t fact;
trans_t trans;
yes_no_t refact, usepr;
real u, drop_tol;
real *a, *at;
int_t *asub, *atsub, *xa, *xat;
int_t *perm_c; /* column permutation vector */
int_t *perm_r; /* row permutations from partial pivoting */
void *work;
int_t info, lwork;
int_t permc_spec, panel_size, relax;
Gstat_t Gstat;
flops_t flopcnt;
/* Default parameters to control factorization. */
#ifdef _OPENMP
//TODO: set as global parameter and use
Kurt A. O'Hearn
committed
#pragma omp parallel \
Kurt A. O'Hearn
committed
{
Kurt A. O'Hearn
committed
#pragma omp master
Kurt A. O'Hearn
committed
{
/* SuperLU_MT spawns threads internally, so set and pass parameter */
nprocs = omp_get_num_threads();
}
}
#else
nprocs = 1;
#endif
// fact = EQUILIBRATE; /* equilibrate A (i.e., scale rows & cols to have unit norm), then factorize */
Kurt A. O'Hearn
committed
fact = DOFACT; /* factor from scratch */
trans = NOTRANS;
refact = NO; /* first time factorization */
//TODO: add to control file and use the value there to set these
panel_size = sp_ienv(1); /* # consec. cols treated as unit task */
relax = sp_ienv(2); /* # cols grouped as relaxed supernode */
u = 1.0; /* diagonal pivoting threshold */
usepr = NO;
drop_tol = 0.0;
work = NULL;
lwork = 0;
Kurt A. O'Hearn
committed
fprintf( stderr, "nprocs = %d\n", nprocs );
fprintf( stderr, "Panel size = %d\n", panel_size );
fprintf( stderr, "Relax = %d\n", relax );
Kurt A. O'Hearn
committed
if ( !(perm_r = intMalloc(A->n)) )
{
SUPERLU_ABORT("Malloc fails for perm_r[].");
}
if ( !(perm_c = intMalloc(A->n)) )
{
SUPERLU_ABORT("Malloc fails for perm_c[].");
}
if ( !(superlumt_options.etree = intMalloc(A->n)) )
{
SUPERLU_ABORT("Malloc fails for etree[].");
}
if ( !(superlumt_options.colcnt_h = intMalloc(A->n)) )
{
SUPERLU_ABORT("Malloc fails for colcnt_h[].");
}
if ( !(superlumt_options.part_super_h = intMalloc(A->n)) )
{
SUPERLU_ABORT("Malloc fails for part_super__h[].");
}
a = (real*) smalloc( (2 * A->start[A->n] - A->n) * sizeof(real),
Kurt A. O'Hearn
committed
"SuperLU_Factorize::a" );
asub = (int_t*) smalloc( (2 * A->start[A->n] - A->n) * sizeof(int_t),
Kurt A. O'Hearn
committed
"SuperLU_Factorize::asub" );
xa = (int_t*) smalloc( (A->n + 1) * sizeof(int_t),
Kurt A. O'Hearn
committed
"SuperLU_Factorize::xa" );
Ltop = (unsigned int*) smalloc( (A->n + 1) * sizeof(unsigned int),
Kurt A. O'Hearn
committed
"SuperLU_Factorize::Ltop" );
Utop = (unsigned int*) smalloc( (A->n + 1) * sizeof(unsigned int),
Kurt A. O'Hearn
committed
"SuperLU_Factorize::Utop" );
Kurt A. O'Hearn
committed
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
if ( Allocate_Matrix( &A_t, A->n, A->m ) == FAILURE )
{
fprintf( stderr, "not enough memory for preconditioning matrices. terminating.\n" );
exit( INSUFFICIENT_MEMORY );
}
/* Set up the sparse matrix data structure for A. */
Transpose( A, A_t );
count = 0;
for ( i = 0; i < A->n; ++i )
{
xa[i] = count;
for ( pj = A->start[i]; pj < A->start[i + 1]; ++pj )
{
a[count] = A->entries[pj].val;
asub[count] = A->entries[pj].j;
++count;
}
for ( pj = A_t->start[i] + 1; pj < A_t->start[i + 1]; ++pj )
{
a[count] = A_t->entries[pj].val;
asub[count] = A_t->entries[pj].j;
++count;
}
}
xa[i] = count;
dCompRow_to_CompCol( A->n, A->n, 2 * A->start[A->n] - A->n, a, asub, xa,
Kurt A. O'Hearn
committed
&at, &atsub, &xat );
Kurt A. O'Hearn
committed
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
for ( i = 0; i < (2 * A->start[A->n] - A->n); ++i )
fprintf( stderr, "%6d", asub[i] );
fprintf( stderr, "\n" );
for ( i = 0; i < (2 * A->start[A->n] - A->n); ++i )
fprintf( stderr, "%6.1f", a[i] );
fprintf( stderr, "\n" );
for ( i = 0; i <= A->n; ++i )
fprintf( stderr, "%6d", xa[i] );
fprintf( stderr, "\n" );
for ( i = 0; i < (2 * A->start[A->n] - A->n); ++i )
fprintf( stderr, "%6d", atsub[i] );
fprintf( stderr, "\n" );
for ( i = 0; i < (2 * A->start[A->n] - A->n); ++i )
fprintf( stderr, "%6.1f", at[i] );
fprintf( stderr, "\n" );
for ( i = 0; i <= A->n; ++i )
fprintf( stderr, "%6d", xat[i] );
fprintf( stderr, "\n" );
A_S.Stype = SLU_NC; /* column-wise, no supernode */
A_S.Dtype = SLU_D; /* double-precision */
A_S.Mtype = SLU_GE; /* full (general) matrix -- required for parallel factorization */
A_S.nrow = A->n;
A_S.ncol = A->n;
A_S.Store = (void *) SUPERLU_MALLOC( sizeof(NCformat) );
A_S_store = (NCformat *) A_S.Store;
A_S_store->nnz = 2 * A->start[A->n] - A->n;
A_S_store->nzval = at;
A_S_store->rowind = atsub;
A_S_store->colptr = xat;
/* ------------------------------------------------------------
Allocate storage and initialize statistics variables.
------------------------------------------------------------*/
StatAlloc( A->n, nprocs, panel_size, relax, &Gstat );
StatInit( A->n, nprocs, &Gstat );
/* ------------------------------------------------------------
Get column permutation vector perm_c[], according to permc_spec:
permc_spec = 0: natural ordering
permc_spec = 1: minimum degree ordering on structure of A'*A
permc_spec = 2: minimum degree ordering on structure of A'+A
permc_spec = 3: approximate minimum degree for unsymmetric matrices
------------------------------------------------------------*/
permc_spec = 0;
get_perm_c( permc_spec, &A_S, perm_c );
/* ------------------------------------------------------------
Initialize the option structure superlumt_options using the
user-input parameters;
Apply perm_c to the columns of original A to form AC.
------------------------------------------------------------*/
pdgstrf_init( nprocs, fact, trans, refact, panel_size, relax,
Kurt A. O'Hearn
committed
u, usepr, drop_tol, perm_c, perm_r,
work, lwork, &A_S, &AC_S, &superlumt_options, &Gstat );
Kurt A. O'Hearn
committed
for ( i = 0; i < ((NCPformat*)AC_S.Store)->nnz; ++i )
fprintf( stderr, "%6.1f", ((real*)(((NCPformat*)AC_S.Store)->nzval))[i] );
fprintf( stderr, "\n" );
/* ------------------------------------------------------------
Compute the LU factorization of A.
The following routine will create nprocs threads.
------------------------------------------------------------*/
pdgstrf( &superlumt_options, &AC_S, perm_r, &L_S, &U_S, &Gstat, &info );
fprintf( stderr, "INFO: %d\n", info );
flopcnt = 0;
for (i = 0; i < nprocs; ++i)
{
flopcnt += Gstat.procstat[i].fcops;
}
Gstat.ops[FACT] = flopcnt;
Kurt A. O'Hearn
committed
printf("\n** Result of sparse LU **\n");
L_S_store = (SCPformat *) L_S.Store;
U_S_store = (NCPformat *) U_S.Store;
printf( "No of nonzeros in factor L = " IFMT "\n", L_S_store->nnz );
printf( "No of nonzeros in factor U = " IFMT "\n", U_S_store->nnz );
fflush( stdout );
Kurt A. O'Hearn
committed
/* convert L and R from SuperLU formats to CSR */
memset( Ltop, 0, (A->n + 1) * sizeof(int) );
memset( Utop, 0, (A->n + 1) * sizeof(int) );
memset( L->start, 0, (A->n + 1) * sizeof(int) );
memset( U->start, 0, (A->n + 1) * sizeof(int) );
for ( i = 0; i < 2 * L_S_store->nnz; ++i )
fprintf( stderr, "%6.1f", ((real*)(L_S_store->nzval))[i] );
fprintf( stderr, "\n" );
for ( i = 0; i < 2 * U_S_store->nnz; ++i )
fprintf( stderr, "%6.1f", ((real*)(U_S_store->nzval))[i] );
fprintf( stderr, "\n" );
printf( "No of supernodes in factor L = " IFMT "\n", L_S_store->nsuper );
for ( i = 0; i < A->n; ++i )
{
fprintf( stderr, "nzval_col_beg[%5d] = %d\n", i, L_S_store->nzval_colbeg[i] );
fprintf( stderr, "nzval_col_end[%5d] = %d\n", i, L_S_store->nzval_colend[i] );
//TODO: correct for SCPformat for L?
//for( pj = L_S_store->rowind_colbeg[i]; pj < L_S_store->rowind_colend[i]; ++pj )
// for( pj = 0; pj < L_S_store->rowind_colend[i] - L_S_store->rowind_colbeg[i]; ++pj )
// {
// ++Ltop[L_S_store->rowind[L_S_store->rowind_colbeg[i] + pj] + 1];
// }
Kurt A. O'Hearn
committed
fprintf( stderr, "col_beg[%5d] = %d\n", i, U_S_store->colbeg[i] );
fprintf( stderr, "col_end[%5d] = %d\n", i, U_S_store->colend[i] );
for ( pj = U_S_store->colbeg[i]; pj < U_S_store->colend[i]; ++pj )
{
++Utop[U_S_store->rowind[pj] + 1];
fprintf( stderr, "Utop[%5d] = %d\n", U_S_store->rowind[pj] + 1, Utop[U_S_store->rowind[pj] + 1] );
}
}
for ( i = 1; i <= A->n; ++i )
{
// Ltop[i] = L->start[i] = Ltop[i] + Ltop[i - 1];
Kurt A. O'Hearn
committed
Utop[i] = U->start[i] = Utop[i] + Utop[i - 1];
// fprintf( stderr, "Utop[%5d] = %d\n", i, Utop[i] );
// fprintf( stderr, "U->start[%5d] = %d\n", i, U->start[i] );
Kurt A. O'Hearn
committed
}
for ( i = 0; i < A->n; ++i )
{
// for( pj = 0; pj < L_S_store->nzval_colend[i] - L_S_store->nzval_colbeg[i]; ++pj )
// {
// r = L_S_store->rowind[L_S_store->rowind_colbeg[i] + pj];
// L->entries[Ltop[r]].j = r;
// L->entries[Ltop[r]].val = ((real*)L_S_store->nzval)[L_S_store->nzval_colbeg[i] + pj];
// ++Ltop[r];
// }
Kurt A. O'Hearn
committed
for ( pj = U_S_store->colbeg[i]; pj < U_S_store->colend[i]; ++pj )
{
r = U_S_store->rowind[pj];
U->entries[Utop[r]].j = i;
U->entries[Utop[r]].val = ((real*)U_S_store->nzval)[pj];
++Utop[r];
}
}
/* ------------------------------------------------------------
Deallocate storage after factorization.
------------------------------------------------------------*/
Kurt A. O'Hearn
committed
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
pxgstrf_finalize( &superlumt_options, &AC_S );
Deallocate_Matrix( A_t );
sfree( xa, "SuperLU_Factorize::xa" );
sfree( asub, "SuperLU_Factorize::asub" );
sfree( a, "SuperLU_Factorize::a" );
SUPERLU_FREE( perm_r );
SUPERLU_FREE( perm_c );
SUPERLU_FREE( ((NCformat *)A_S.Store)->rowind );
SUPERLU_FREE( ((NCformat *)A_S.Store)->colptr );
SUPERLU_FREE( ((NCformat *)A_S.Store)->nzval );
SUPERLU_FREE( A_S.Store );
if ( lwork == 0 )
{
Destroy_SuperNode_SCP(&L_S);
Destroy_CompCol_NCP(&U_S);
}
else if ( lwork > 0 )
{
SUPERLU_FREE(work);
}
StatFree(&Gstat);
sfree( Utop, "SuperLU_Factorize::Utop" );
sfree( Ltop, "SuperLU_Factorize::Ltop" );
//TODO: return iters
return 0.;
}
#endif
/* Diagonal (Jacobi) preconditioner computation */
real diag_pre_comp( const sparse_matrix * const H, real * const Hdia_inv )
{
unsigned int i;
real start;
start = Get_Time( );
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel for schedule(static) \
Kurt A. O'Hearn
committed
#endif
for ( i = 0; i < H->n; ++i )
{
if ( H->val[H->start[i + 1] - 1] != 0.0 )
{
Hdia_inv[i] = 1.0 / H->val[H->start[i + 1] - 1];
}
else
{
Hdia_inv[i] = 1.0;
}
}
return Get_Timing_Info( start );
}
/* Incomplete Cholesky factorization with dual thresholding */
real ICHOLT( const sparse_matrix * const A, const real * const droptol,
Kurt A. O'Hearn
committed
sparse_matrix * const L, sparse_matrix * const U )
Kurt A. O'Hearn
committed
{
int *tmp_j;
real *tmp_val;
int i, j, pj, k1, k2, tmptop, Ltop;
real val, start;
unsigned int *Utop;
start = Get_Time( );
Utop = (unsigned int*) smalloc( (A->n + 1) * sizeof(unsigned int),
Kurt A. O'Hearn
committed
"ICHOLT::Utop" );
tmp_j = (int*) smalloc( A->n * sizeof(int),
Kurt A. O'Hearn
committed
"ICHOLT::Utop" );
tmp_val = (real*) smalloc( A->n * sizeof(real),
Kurt A. O'Hearn
committed
"ICHOLT::Utop" );
Kurt A. O'Hearn
committed
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
// clear variables
Ltop = 0;
tmptop = 0;
memset( L->start, 0, (A->n + 1) * sizeof(unsigned int) );
memset( U->start, 0, (A->n + 1) * sizeof(unsigned int) );
memset( Utop, 0, A->n * sizeof(unsigned int) );
for ( i = 0; i < A->n; ++i )
{
L->start[i] = Ltop;
tmptop = 0;
for ( pj = A->start[i]; pj < A->start[i + 1] - 1; ++pj )
{
j = A->j[pj];
val = A->val[pj];
if ( FABS(val) > droptol[i] )
{
k1 = 0;
k2 = L->start[j];
while ( k1 < tmptop && k2 < L->start[j + 1] )
{
if ( tmp_j[k1] < L->j[k2] )
{
++k1;
}
else if ( tmp_j[k1] > L->j[k2] )
{
++k2;
}
else
{
val -= (tmp_val[k1++] * L->val[k2++]);
}
}
// L matrix is lower triangular,
// so right before the start of next row comes jth diagonal
val /= L->val[L->start[j + 1] - 1];
tmp_j[tmptop] = j;
tmp_val[tmptop] = val;
++tmptop;
}
}
// sanity check
if ( A->j[pj] != i )
{
fprintf( stderr, "[ICHOLT] badly built A matrix!\n (i = %d) ", i );
exit( NUMERIC_BREAKDOWN );
}
// compute the ith diagonal in L
val = A->val[pj];
for ( k1 = 0; k1 < tmptop; ++k1 )
{
val -= (tmp_val[k1] * tmp_val[k1]);
}
#if defined(DEBUG)
if ( val < 0.0 )
{
fprintf( stderr, "[ICHOLT] Numeric breakdown (SQRT of negative on diagonal i = %d). Terminating.\n", i );
exit( NUMERIC_BREAKDOWN );
}
#endif
tmp_j[tmptop] = i;
tmp_val[tmptop] = SQRT( val );
// apply the dropping rule once again
//fprintf( stderr, "row%d: tmptop: %d\n", i, tmptop );
//for( k1 = 0; k1<= tmptop; ++k1 )
// fprintf( stderr, "%d(%f) ", tmp[k1].j, tmp[k1].val );
//fprintf( stderr, "\n" );
//fprintf( stderr, "row(%d): droptol=%.4f\n", i+1, droptol[i] );
for ( k1 = 0; k1 < tmptop; ++k1 )
{
if ( FABS(tmp_val[k1]) > droptol[i] / tmp_val[tmptop] )
{
L->j[Ltop] = tmp_j[k1];
L->val[Ltop] = tmp_val[k1];
U->start[tmp_j[k1] + 1]++;
++Ltop;
//fprintf( stderr, "%d(%.4f) ", tmp[k1].j+1, tmp[k1].val );
}
}
// keep the diagonal in any case
L->j[Ltop] = tmp_j[k1];
L->val[Ltop] = tmp_val[k1];
++Ltop;
//fprintf( stderr, "%d(%.4f)\n", tmp[k1].j+1, tmp[k1].val );
}
L->start[i] = Ltop;
// fprintf( stderr, "nnz(L): %d, max: %d\n", Ltop, L->n * 50 );
Kurt A. O'Hearn
committed
/* U = L^T (Cholesky factorization) */
Transpose( L, U );
// for ( i = 1; i <= U->n; ++i )
// {
// Utop[i] = U->start[i] = U->start[i] + U->start[i - 1] + 1;
// }
// for ( i = 0; i < L->n; ++i )
// {
// for ( pj = L->start[i]; pj < L->start[i + 1]; ++pj )
// {
// j = L->j[pj];
// U->j[Utop[j]] = i;
// U->val[Utop[j]] = L->val[pj];
// Utop[j]++;
// }
// }
// fprintf( stderr, "nnz(U): %d, max: %d\n", Utop[U->n], U->n * 50 );
Kurt A. O'Hearn
committed
sfree( tmp_val, "ICHOLT::tmp_val" );
sfree( tmp_j, "ICHOLT::tmp_j" );
sfree( Utop, "ICHOLT::Utop" );
return Get_Timing_Info( start );
}
/* Fine-grained (parallel) incomplete Cholesky factorization
*
* Reference:
* Edmond Chow and Aftab Patel
* Fine-Grained Parallel Incomplete LU Factorization
* SIAM J. Sci. Comp. */
#if defined(TESTING)
real ICHOL_PAR( const sparse_matrix * const A, const unsigned int sweeps,
Kurt A. O'Hearn
committed
sparse_matrix * const U_t, sparse_matrix * const U )
Kurt A. O'Hearn
committed
{
unsigned int i, j, k, pj, x = 0, y = 0, ei_x, ei_y;
real *D, *D_inv, sum, start;
sparse_matrix *DAD;
int *Utop;
start = Get_Time( );
D = (real*) smalloc( A->n * sizeof(real), "ICHOL_PAR::D" );
D_inv = (real*) smalloc( A->n * sizeof(real), "ICHOL_PAR::D_inv" );
Utop = (int*) smalloc( (A->n + 1) * sizeof(int), "ICHOL_PAR::Utop" );
if ( Allocate_Matrix( &DAD, A->n, A->m ) == FAILURE )
Kurt A. O'Hearn
committed
{
fprintf( stderr, "not enough memory for ICHOL_PAR preconditioning matrices. terminating.\n" );
exit( INSUFFICIENT_MEMORY );
}
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel for schedule(static) \
default(none) shared(D_inv, D) private(i)
Kurt A. O'Hearn
committed
#endif
for ( i = 0; i < A->n; ++i )
{
D_inv[i] = SQRT( A->val[A->start[i + 1] - 1] );
D[i] = 1. / D_inv[i];
}
memset( U->start, 0, sizeof(unsigned int) * (A->n + 1) );
memset( Utop, 0, sizeof(unsigned int) * (A->n + 1) );
/* to get convergence, A must have unit diagonal, so apply
* transformation DAD, where D = D(1./SQRT(D(A))) */
memcpy( DAD->start, A->start, sizeof(int) * (A->n + 1) );
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel for schedule(guided) \
default(none) shared(DAD, D_inv, D) private(i, pj)
Kurt A. O'Hearn
committed
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
#endif
for ( i = 0; i < A->n; ++i )
{
/* non-diagonals */
for ( pj = A->start[i]; pj < A->start[i + 1] - 1; ++pj )
{
DAD->j[pj] = A->j[pj];
DAD->val[pj] = A->val[pj] * D[i] * D[A->j[pj]];
}
/* diagonal */
DAD->j[pj] = A->j[pj];
DAD->val[pj] = 1.;
}
/* initial guesses for U^T,
* assume: A and DAD symmetric and stored lower triangular */
memcpy( U_t->start, DAD->start, sizeof(int) * (DAD->n + 1) );
memcpy( U_t->j, DAD->j, sizeof(int) * (DAD->m) );
memcpy( U_t->val, DAD->val, sizeof(real) * (DAD->m) );
for ( i = 0; i < sweeps; ++i )
{
/* for each nonzero */
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel for schedule(static) \
default(none) shared(DAD, stderr) private(sum, ei_x, ei_y, k) firstprivate(x, y)
Kurt A. O'Hearn
committed
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
#endif
for ( j = 0; j < A->start[A->n]; ++j )
{
sum = ZERO;
/* determine row bounds of current nonzero */
x = 0;
ei_x = 0;
for ( k = 0; k <= A->n; ++k )
{
if ( U_t->start[k] > j )
{
x = U_t->start[k - 1];
ei_x = U_t->start[k];
break;
}
}
/* column bounds of current nonzero */
y = U_t->start[U_t->j[j]];
ei_y = U_t->start[U_t->j[j] + 1];
/* sparse dot product: dot( U^T(i,1:j-1), U^T(j,1:j-1) ) */
while ( U_t->j[x] < U_t->j[j] &&
U_t->j[y] < U_t->j[j] &&
x < ei_x && y < ei_y )
{
if ( U_t->j[x] == U_t->j[y] )
{
sum += (U_t->val[x] * U_t->val[y]);
++x;
++y;
}
else if ( U_t->j[x] < U_t->j[y] )
{
++x;
}
else
{
++y;
}
}
sum = DAD->val[j] - sum;
/* diagonal entries */
if ( (k - 1) == U_t->j[j] )
{
/* sanity check */
if ( sum < ZERO )
{
fprintf( stderr, "Numeric breakdown in ICHOL_PAR. Terminating.\n");
#if defined(DEBUG_FOCUS)
fprintf( stderr, "A(%5d,%5d) = %10.3f\n",
Kurt A. O'Hearn
committed
k - 1, A->entries[j].j, A->entries[j].val );
Kurt A. O'Hearn
committed
fprintf( stderr, "sum = %10.3f\n", sum);
#endif
exit(NUMERIC_BREAKDOWN);
}
U_t->val[j] = SQRT( sum );
}
/* non-diagonal entries */
else
{
U_t->val[j] = sum / U_t->val[ei_y - 1];
}
}
}
/* apply inverse transformation D^{-1}U^{T},
* since DAD \approx U^{T}U, so
* D^{-1}DADD^{-1} = A \approx D^{-1}U^{T}UD^{-1} */
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel for schedule(guided) \
default(none) shared(D_inv) private(i, pj)
#endif
Kurt A. O'Hearn
committed
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
for ( i = 0; i < A->n; ++i )
{
for ( pj = A->start[i]; pj < A->start[i + 1]; ++pj )
{
U_t->val[pj] *= D_inv[i];
}
}
#if defined(DEBUG_FOCUS)
fprintf( stderr, "nnz(L): %d, max: %d\n", U_t->start[U_t->n], U_t->n * 50 );
#endif
/* transpose U^{T} and copy into U */
Transpose( U_t, U );
#if defined(DEBUG_FOCUS)
fprintf( stderr, "nnz(U): %d, max: %d\n", Utop[U->n], U->n * 50 );
#endif
Deallocate_Matrix( DAD );
sfree( D_inv, "ICHOL_PAR::D_inv" );
sfree( D, "ICHOL_PAR::D" );
sfree( Utop, "ICHOL_PAR::Utop" );
return Get_Timing_Info( start );
}
#endif
/* Fine-grained (parallel) incomplete LU factorization
*
* Reference:
* Edmond Chow and Aftab Patel
* Fine-Grained Parallel Incomplete LU Factorization
* SIAM J. Sci. Comp.
*
* A: symmetric, half-stored (lower triangular), CSR format
* sweeps: number of loops over non-zeros for computation
* L / U: factorized triangular matrices (A \approx LU), CSR format */
real ILU_PAR( const sparse_matrix * const A, const unsigned int sweeps,
Kurt A. O'Hearn
committed
sparse_matrix * const L, sparse_matrix * const U )
Kurt A. O'Hearn
committed
{
unsigned int i, j, k, pj, x, y, ei_x, ei_y;
real *D, *D_inv, sum, start;
sparse_matrix *DAD;
start = Get_Time( );
D = (real*) smalloc( A->n * sizeof(real), "ILU_PAR::D" );
D_inv = (real*) smalloc( A->n * sizeof(real), "ILU_PAR::D_inv" );
if ( Allocate_Matrix( &DAD, A->n, A->m ) == FAILURE )
Kurt A. O'Hearn
committed
{
fprintf( stderr, "[ILU_PAR] Not enough memory for preconditioning matrices. Terminating.\n" );
exit( INSUFFICIENT_MEMORY );
}
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel for schedule(static) \
default(none) shared(D, D_inv) private(i)
Kurt A. O'Hearn
committed
#endif
for ( i = 0; i < A->n; ++i )
{
D_inv[i] = SQRT( FABS( A->val[A->start[i + 1] - 1] ) );
D[i] = 1.0 / D_inv[i];
// printf( "A->val[%8d] = %f, D[%4d] = %f, D_inv[%4d] = %f\n", A->start[i + 1] - 1, A->val[A->start[i + 1] - 1], i, D[i], i, D_inv[i] );
Kurt A. O'Hearn
committed
}
/* to get convergence, A must have unit diagonal, so apply
* transformation DAD, where D = D(1./SQRT(abs(D(A)))) */
memcpy( DAD->start, A->start, sizeof(int) * (A->n + 1) );
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel for schedule(static) \
default(none) shared(DAD, D) private(i, pj)
Kurt A. O'Hearn
committed
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
#endif
for ( i = 0; i < A->n; ++i )
{
/* non-diagonals */
for ( pj = A->start[i]; pj < A->start[i + 1] - 1; ++pj )
{
DAD->j[pj] = A->j[pj];
DAD->val[pj] = D[i] * A->val[pj] * D[A->j[pj]];
}
/* diagonal */
DAD->j[pj] = A->j[pj];
DAD->val[pj] = 1.0;
}
/* initial guesses for L and U,
* assume: A and DAD symmetric and stored lower triangular */
memcpy( L->start, DAD->start, sizeof(int) * (DAD->n + 1) );
memcpy( L->j, DAD->j, sizeof(int) * (DAD->start[DAD->n]) );
memcpy( L->val, DAD->val, sizeof(real) * (DAD->start[DAD->n]) );
/* store U^T in CSR for row-wise access and tranpose later */
memcpy( U->start, DAD->start, sizeof(int) * (DAD->n + 1) );
memcpy( U->j, DAD->j, sizeof(int) * (DAD->start[DAD->n]) );
memcpy( U->val, DAD->val, sizeof(real) * (DAD->start[DAD->n]) );
/* L has unit diagonal, by convention */
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel for schedule(static) default(none) private(i)
Kurt A. O'Hearn
committed
#endif
for ( i = 0; i < A->n; ++i )
{
L->val[L->start[i + 1] - 1] = 1.0;
}
for ( i = 0; i < sweeps; ++i )
{
/* for each nonzero in L */
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel for schedule(static) \
default(none) shared(DAD) private(j, k, x, y, ei_x, ei_y, sum)
Kurt A. O'Hearn
committed
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
#endif
for ( j = 0; j < DAD->start[DAD->n]; ++j )
{
sum = ZERO;
/* determine row bounds of current nonzero */
x = 0;
ei_x = 0;
for ( k = 1; k <= DAD->n; ++k )
{
if ( DAD->start[k] > j )
{
x = DAD->start[k - 1];
ei_x = DAD->start[k];
break;
}
}
/* determine column bounds of current nonzero */
y = DAD->start[DAD->j[j]];
ei_y = DAD->start[DAD->j[j] + 1];
/* sparse dot product:
* dot( L(i,1:j-1), U(1:j-1,j) ) */
while ( L->j[x] < L->j[j] &&
L->j[y] < L->j[j] &&
x < ei_x && y < ei_y )
{
if ( L->j[x] == L->j[y] )
{
sum += (L->val[x] * U->val[y]);
++x;
++y;
}
else if ( L->j[x] < L->j[y] )
{
++x;
}
else
{
++y;
}
}
if ( j != ei_x - 1 )
{
L->val[j] = ( DAD->val[j] - sum ) / U->val[ei_y - 1];
}
}
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel for schedule(static) \
default(none) shared(DAD) private(j, k, x, y, ei_x, ei_y, sum)
Kurt A. O'Hearn
committed
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
#endif
for ( j = 0; j < DAD->start[DAD->n]; ++j )
{
sum = ZERO;
/* determine row bounds of current nonzero */
x = 0;
ei_x = 0;
for ( k = 1; k <= DAD->n; ++k )
{
if ( DAD->start[k] > j )
{
x = DAD->start[k - 1];
ei_x = DAD->start[k];
break;
}
}
/* determine column bounds of current nonzero */
y = DAD->start[DAD->j[j]];
ei_y = DAD->start[DAD->j[j] + 1];
/* sparse dot product:
* dot( L(i,1:i-1), U(1:i-1,j) ) */
while ( U->j[x] < U->j[j] &&
U->j[y] < U->j[j] &&
x < ei_x && y < ei_y )
{
if ( U->j[x] == U->j[y] )
{
sum += (L->val[y] * U->val[x]);
++x;
++y;
}
else if ( U->j[x] < U->j[y] )
{
++x;
}
else
{
++y;
}
}
U->val[j] = DAD->val[j] - sum;
}
}
/* apply inverse transformation:
* since DAD \approx LU, then
* D^{-1}DADD^{-1} = A \approx D^{-1}LUD^{-1} */
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel for schedule(static) \
default(none) shared(DAD, D_inv) private(i, pj)
Kurt A. O'Hearn
committed
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
#endif
for ( i = 0; i < DAD->n; ++i )
{
for ( pj = DAD->start[i]; pj < DAD->start[i + 1]; ++pj )
{
L->val[pj] = D_inv[i] * L->val[pj];
/* currently storing U^T, so use row index instead of column index */
U->val[pj] = U->val[pj] * D_inv[i];
}
}
Transpose_I( U );
#if defined(DEBUG_FOCUS)
fprintf( stderr, "nnz(L): %d, max: %d\n", L->start[L->n], L->n * 50 );
fprintf( stderr, "nnz(U): %d, max: %d\n", Utop[U->n], U->n * 50 );
#endif
Deallocate_Matrix( DAD );
sfree( D_inv, "ILU_PAR::D_inv" );
sfree( D, "ILU_PAR::D_inv" );
return Get_Timing_Info( start );
}
/* Fine-grained (parallel) incomplete LU factorization with thresholding
*
* Reference:
* Edmond Chow and Aftab Patel
* Fine-Grained Parallel Incomplete LU Factorization
* SIAM J. Sci. Comp.
*
* A: symmetric, half-stored (lower triangular), CSR format
* droptol: row-wise tolerances used for dropping
* sweeps: number of loops over non-zeros for computation
* L / U: factorized triangular matrices (A \approx LU), CSR format */
real ILUT_PAR( const sparse_matrix * const A, const real * droptol,
Kurt A. O'Hearn
committed
const unsigned int sweeps, sparse_matrix * const L, sparse_matrix * const U )
Kurt A. O'Hearn
committed
{
unsigned int i, j, k, pj, x, y, ei_x, ei_y, Ltop, Utop;
real *D, *D_inv, sum, start;
sparse_matrix *DAD, *L_temp, *U_temp;
start = Get_Time( );
if ( Allocate_Matrix( &DAD, A->n, A->m ) == FAILURE ||
Allocate_Matrix( &L_temp, A->n, A->m ) == FAILURE ||
Allocate_Matrix( &U_temp, A->n, A->m ) == FAILURE )
{
fprintf( stderr, "not enough memory for ILUT_PAR preconditioning matrices. terminating.\n" );
exit( INSUFFICIENT_MEMORY );
}
D = (real*) smalloc( A->n * sizeof(real), "ILUT_PAR::D" );
D_inv = (real*) smalloc( A->n * sizeof(real), "ILUT_PAR::D_inv" );
Kurt A. O'Hearn
committed
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel for schedule(static) \
default(none) shared(D, D_inv) private(i)
Kurt A. O'Hearn
committed
#endif
for ( i = 0; i < A->n; ++i )
{
D_inv[i] = SQRT( FABS( A->val[A->start[i + 1] - 1] ) );
D[i] = 1.0 / D_inv[i];
}
/* to get convergence, A must have unit diagonal, so apply
* transformation DAD, where D = D(1./SQRT(D(A))) */
memcpy( DAD->start, A->start, sizeof(int) * (A->n + 1) );
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel for schedule(static) \
default(none) shared(DAD, D) private(i, pj)
Kurt A. O'Hearn
committed
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
#endif
for ( i = 0; i < A->n; ++i )
{
/* non-diagonals */
for ( pj = A->start[i]; pj < A->start[i + 1] - 1; ++pj )
{
DAD->j[pj] = A->j[pj];
DAD->val[pj] = D[i] * A->val[pj] * D[A->j[pj]];
}
/* diagonal */
DAD->j[pj] = A->j[pj];
DAD->val[pj] = 1.0;
}
/* initial guesses for L and U,
* assume: A and DAD symmetric and stored lower triangular */
memcpy( L_temp->start, DAD->start, sizeof(int) * (DAD->n + 1) );
memcpy( L_temp->j, DAD->j, sizeof(int) * (DAD->start[DAD->n]) );
memcpy( L_temp->val, DAD->val, sizeof(real) * (DAD->start[DAD->n]) );
/* store U^T in CSR for row-wise access and tranpose later */
memcpy( U_temp->start, DAD->start, sizeof(int) * (DAD->n + 1) );
memcpy( U_temp->j, DAD->j, sizeof(int) * (DAD->start[DAD->n]) );
memcpy( U_temp->val, DAD->val, sizeof(real) * (DAD->start[DAD->n]) );
/* L has unit diagonal, by convention */
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel for schedule(static) \
default(none) private(i) shared(L_temp)
Kurt A. O'Hearn
committed
#endif
for ( i = 0; i < A->n; ++i )
{
L_temp->val[L_temp->start[i + 1] - 1] = 1.0;
}
for ( i = 0; i < sweeps; ++i )
{
/* for each nonzero in L */
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel for schedule(static) \
default(none) shared(DAD, L_temp, U_temp) private(j, k, x, y, ei_x, ei_y, sum)
Kurt A. O'Hearn
committed
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
#endif
for ( j = 0; j < DAD->start[DAD->n]; ++j )
{
sum = ZERO;
/* determine row bounds of current nonzero */
x = 0;
ei_x = 0;
for ( k = 1; k <= DAD->n; ++k )
{
if ( DAD->start[k] > j )
{
x = DAD->start[k - 1];
ei_x = DAD->start[k];
break;
}
}
/* determine column bounds of current nonzero */
y = DAD->start[DAD->j[j]];
ei_y = DAD->start[DAD->j[j] + 1];
/* sparse dot product:
* dot( L(i,1:j-1), U(1:j-1,j) ) */
while ( L_temp->j[x] < L_temp->j[j] &&
L_temp->j[y] < L_temp->j[j] &&
x < ei_x && y < ei_y )
{
if ( L_temp->j[x] == L_temp->j[y] )
{
sum += (L_temp->val[x] * U_temp->val[y]);
++x;
++y;
}
else if ( L_temp->j[x] < L_temp->j[y] )
{
++x;
}
else
{
++y;
}
}
if ( j != ei_x - 1 )
{
L_temp->val[j] = ( DAD->val[j] - sum ) / U_temp->val[ei_y - 1];
}
}
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel for schedule(static) \
default(none) shared(DAD, L_temp, U_temp) private(j, k, x, y, ei_x, ei_y, sum)
Kurt A. O'Hearn
committed
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
#endif
for ( j = 0; j < DAD->start[DAD->n]; ++j )
{
sum = ZERO;
/* determine row bounds of current nonzero */
x = 0;
ei_x = 0;
for ( k = 1; k <= DAD->n; ++k )
{
if ( DAD->start[k] > j )
{
x = DAD->start[k - 1];
ei_x = DAD->start[k];
break;
}
}
/* determine column bounds of current nonzero */
y = DAD->start[DAD->j[j]];
ei_y = DAD->start[DAD->j[j] + 1];
/* sparse dot product:
* dot( L(i,1:i-1), U(1:i-1,j) ) */
while ( U_temp->j[x] < U_temp->j[j] &&
U_temp->j[y] < U_temp->j[j] &&
x < ei_x && y < ei_y )
{
if ( U_temp->j[x] == U_temp->j[y] )
{
sum += (L_temp->val[y] * U_temp->val[x]);
++x;
++y;
}
else if ( U_temp->j[x] < U_temp->j[y] )
{
++x;
}
else
{
++y;
}
}
U_temp->val[j] = DAD->val[j] - sum;
}
}
/* apply inverse transformation:
* since DAD \approx LU, then
* D^{-1}DADD^{-1} = A \approx D^{-1}LUD^{-1} */
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel for schedule(static) \
default(none) shared(DAD, L_temp, U_temp, D_inv) private(i, pj)
Kurt A. O'Hearn
committed
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
#endif
for ( i = 0; i < DAD->n; ++i )
{
for ( pj = DAD->start[i]; pj < DAD->start[i + 1]; ++pj )
{
L_temp->val[pj] = D_inv[i] * L_temp->val[pj];
/* currently storing U^T, so use row index instead of column index */
U_temp->val[pj] = U_temp->val[pj] * D_inv[i];
}
}
/* apply the dropping rule */
Ltop = 0;
Utop = 0;
for ( i = 0; i < DAD->n; ++i )
{
L->start[i] = Ltop;
U->start[i] = Utop;
for ( pj = L_temp->start[i]; pj < L_temp->start[i + 1] - 1; ++pj )
{
if ( FABS( L_temp->val[pj] ) > FABS( droptol[i] / L_temp->val[L_temp->start[i + 1] - 1] ) )
{
L->j[Ltop] = L_temp->j[pj];
L->val[Ltop] = L_temp->val[pj];
++Ltop;
}
}
/* diagonal */
L->j[Ltop] = L_temp->j[pj];
L->val[Ltop] = L_temp->val[pj];
++Ltop;
for ( pj = U_temp->start[i]; pj < U_temp->start[i + 1] - 1; ++pj )
{
if ( FABS( U_temp->val[pj] ) > FABS( droptol[i] / U_temp->val[U_temp->start[i + 1] - 1] ) )
{
U->j[Utop] = U_temp->j[pj];
U->val[Utop] = U_temp->val[pj];
++Utop;
}
}
/* diagonal */
U->j[Utop] = U_temp->j[pj];
U->val[Utop] = U_temp->val[pj];
++Utop;
}
L->start[i] = Ltop;
U->start[i] = Utop;
Transpose_I( U );
#if defined(DEBUG_FOCUS)
fprintf( stderr, "nnz(L): %d\n", L->start[L->n] );
fprintf( stderr, "nnz(U): %d\n", U->start[U->n] );
#endif
Deallocate_Matrix( U_temp );
Deallocate_Matrix( L_temp );
Deallocate_Matrix( DAD );
sfree( D_inv, "ILUT_PAR::D_inv" );
sfree( D, "ILUT_PAR::D_inv" );
return Get_Timing_Info( start );
}
#if defined(HAVE_LAPACKE) || defined(HAVE_LAPACKE_MKL)
Kurt A. O'Hearn
committed
/* Compute M^{1} \approx A which minimizes
Kurt A. O'Hearn
committed
* \sum_{j=1}^{N} ||e_j - Am_j||_2^2
* where: e_j is the j-th column of the NxN identify matrix,
* m_j is the j-th column of the NxN approximate sparse matrix M
*
* Internally, use LAPACKE to solve the least-squares problems
Kurt A. O'Hearn
committed
*
* A: symmetric, sparse matrix, stored in full CSR format
* A_spar_patt: sparse matrix used as template sparsity pattern
* for approximating the inverse, stored in full CSR format
Kurt A. O'Hearn
committed
* A_app_inv: approximate inverse to A, stored in full CSR format (result)
*
* Reference:
* Michele Benzi et al.
* A Comparative Study of Sparse Approximate Inverse
* Preconditioners
* Applied Numerical Mathematics 30, 1999
* */
Kurt A. O'Hearn
committed
real sparse_approx_inverse( const sparse_matrix * const A,
Kurt A. O'Hearn
committed
const sparse_matrix * const A_spar_patt,
sparse_matrix ** A_app_inv )
Kurt A. O'Hearn
committed
lapack_int m, n, nrhs, lda, ldb, info;
int *pos_x, *pos_y;
Kurt A. O'Hearn
committed
char *X, *Y;
Kurt A. O'Hearn
committed
(*A_app_inv)->start[(*A_app_inv)->n] = A_spar_patt->start[A_spar_patt->n];
#ifdef _OPENMP
#pragma omp parallel default(none) \
Kurt A. O'Hearn
committed
private(i, k, pj, j_temp, identity_pos, N, M, d_i, d_j, m, n, \
nrhs, lda, ldb, info, X, Y, pos_x, pos_y, e_j, dense_matrix) \
shared(A_app_inv, stderr)
#endif
X = (char *) smalloc( sizeof(char) * A->n, "Sparse_Approx_Inverse::X" );
Y = (char *) smalloc( sizeof(char) * A->n, "Sparse_Approx_Inverse::Y" );
pos_x = (int *) smalloc( sizeof(int) * A->n, "Sparse_Approx_Inverse::pos_x" );
pos_y = (int *) smalloc( sizeof(int) * A->n, "Sparse_Approx_Inverse::pos_y" );
for ( i = 0; i < A->n; ++i )
X[i] = 0;
Y[i] = 0;
pos_x[i] = 0;
pos_y[i] = 0;
}
#ifdef _OPENMP
#pragma omp for schedule(static)
#endif
Kurt A. O'Hearn
committed
for ( i = 0; i < A_spar_patt->n; ++i )
{
N = 0;
M = 0;
// find column indices of nonzeros (which will be the columns indices of the dense matrix)
Kurt A. O'Hearn
committed
for ( pj = A_spar_patt->start[i]; pj < A_spar_patt->start[i + 1]; ++pj )
Kurt A. O'Hearn
committed
j_temp = A_spar_patt->j[pj];
Y[j_temp] = 1;
pos_y[j_temp] = N;
++N;
// for each of those indices
// search through the row of full A of that index
Kurt A. O'Hearn
committed
for ( k = A->start[j_temp]; k < A->start[j_temp + 1]; ++k )
{
// and accumulate the nonzero column indices to serve as the row indices of the dense matrix
Kurt A. O'Hearn
committed
X[A->j[k]] = 1;
}
// enumerate the row indices from 0 to (# of nonzero rows - 1) for the dense matrix
identity_pos = M;
Kurt A. O'Hearn
committed
for ( k = 0; k < A->n; k++)
if ( X[k] != 0 )
pos_x[M] = k;
if ( k == i )
{
identity_pos = M;
}
++M;
// allocate memory for NxM dense matrix
dense_matrix = (real *) smalloc( sizeof(real) * N * M,
"Sparse_Approx_Inverse::dense_matrix" );
// fill in the entries of dense matrix
for ( d_i = 0; d_i < M; ++d_i)
// all rows are initialized to zero
for ( d_j = 0; d_j < N; ++d_j )
dense_matrix[d_i * N + d_j] = 0.0;
}
// change the value if any of the column indices is seen
Kurt A. O'Hearn
committed
for ( d_j = A->start[pos_x[d_i]];
d_j < A->start[pos_x[d_i] + 1]; ++d_j )
{
Kurt A. O'Hearn
committed
if ( Y[A->j[d_j]] == 1 )
{
Kurt A. O'Hearn
committed
dense_matrix[d_i * N + pos_y[A->j[d_j]]] = A->val[d_j];
}
/* create the right hand side of the linear equation
that is the full column of the identity matrix*/
e_j = (real *) smalloc( sizeof(real) * M,
"Sparse_Approx_Inverse::e_j" );
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
for ( k = 0; k < M; ++k )
{
e_j[k] = 0.0;
}
e_j[identity_pos] = 1.0;
/* Solve the overdetermined system AX = B through the least-squares problem:
* min ||B - AX||_2 */
m = M;
n = N;
nrhs = 1;
lda = N;
ldb = nrhs;
info = LAPACKE_dgels( LAPACK_ROW_MAJOR, 'N', m, n, nrhs, dense_matrix, lda,
e_j, ldb );
/* Check for the full rank */
if ( info > 0 )
{
fprintf( stderr, "The diagonal element %i of the triangular factor ", info );
fprintf( stderr, "of A is zero, so that A does not have full rank;\n" );
fprintf( stderr, "the least squares solution could not be computed.\n" );
exit( INVALID_INPUT );
}
Kurt A. O'Hearn
committed
/* Print least squares solution */
// print_matrix( "Least squares solution", n, nrhs, b, ldb );
// accumulate the resulting vector to build A_app_inv
Kurt A. O'Hearn
committed
(*A_app_inv)->start[i] = A_spar_patt->start[i];
for ( k = A_spar_patt->start[i]; k < A_spar_patt->start[i + 1]; ++k)
{
Kurt A. O'Hearn
committed
(*A_app_inv)->j[k] = A_spar_patt->j[k];
(*A_app_inv)->val[k] = e_j[k - A_spar_patt->start[i]];
}
//empty variables that will be used next iteration
sfree( dense_matrix, "Sparse_Approx_Inverse::dense_matrix" );
sfree( e_j, "Sparse_Approx_Inverse::e_j" );
for ( k = 0; k < A->n; ++k )
{
X[k] = 0;
Y[k] = 0;
pos_x[k] = 0;
pos_y[k] = 0;
}
sfree( pos_y, "Sparse_Approx_Inverse::pos_y" );
sfree( pos_x, "Sparse_Approx_Inverse::pos_x" );
sfree( Y, "Sparse_Approx_Inverse::Y" );
sfree( X, "Sparse_Approx_Inverse::X" );
}
return Get_Timing_Info( start );
}
Kurt A. O'Hearn
committed
/* sparse matrix-vector product Ax = b
*
* workspace: storage container for workspace structures
* A: lower triangular matrix, stored in CSR format
* x: vector
* b (output): vector */
static void Sparse_MatVec( const static_storage * const workspace,
const sparse_matrix * const A, const real * const x, real * const b )
#ifdef _OPENMP
unsigned int tid;
#endif
Kurt A. O'Hearn
committed
#ifdef _OPENMP
tid = omp_get_thread_num( );
Kurt A. O'Hearn
committed
Vector_MakeZero( workspace->b_local, omp_get_num_threads() * n );
Kurt A. O'Hearn
committed
#pragma omp for schedule(static)
#endif
for ( i = 0; i < n; ++i )
{
si = A->start[i];
ei = A->start[i + 1] - 1;
for ( k = si; k < ei; ++k )
{
j = A->j[k];
H = A->val[k];
#ifdef _OPENMP
workspace->b_local[tid * n + j] += H * x[i];
workspace->b_local[tid * n + i] += H * x[j];
#else
b[j] += H * x[i];
b[i] += H * x[j];
#endif
// the diagonal entry is the last one in
#ifdef _OPENMP
workspace->b_local[tid * n + i] += A->val[k] * x[i];
#else
b[i] += A->val[k] * x[i];
#endif
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp for schedule(static)
for ( i = 0; i < n; ++i )
{
for ( j = 0; j < omp_get_num_threads(); ++j )
b[i] += workspace->b_local[j * n + i];
Kurt A. O'Hearn
committed
}
#endif
Kurt A. O'Hearn
committed
/* sparse matrix-vector product Ax = b
* where:
* A: matrix, stored in CSR format
* x: vector
* b: vector (result) */
static void Sparse_MatVec_full( const sparse_matrix * const A,
const real * const x, real * const b )
{
int i, pj;
Kurt A. O'Hearn
committed
Vector_MakeZero( b, A->n );
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp for schedule(static)
Kurt A. O'Hearn
committed
#endif
for ( i = 0; i < A->n; ++i )
{
for ( pj = A->start[i]; pj < A->start[i + 1]; ++pj )
Kurt A. O'Hearn
committed
{
b[i] += A->val[pj] * x[A->j[pj]];
Kurt A. O'Hearn
committed
}
}
}
/* Transpose A and copy into A^T
*
Kurt A. O'Hearn
committed
* A: stored in CSR
* A_t: stored in CSR
*/
Kurt A. O'Hearn
committed
void Transpose( const sparse_matrix * const A, sparse_matrix * const A_t )
{
unsigned int i, j, pj, *A_t_top;
A_t_top = (unsigned int*) scalloc( A->n + 1, sizeof(unsigned int),
Kurt A. O'Hearn
committed
"Transpose::A_t_top" );
memset( A_t->start, 0, (A->n + 1) * sizeof(unsigned int) );
Kurt A. O'Hearn
committed
/* count nonzeros in each column of A^T, store one row greater (see next loop) */
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
for ( i = 0; i < A->n; ++i )
{
for ( pj = A->start[i]; pj < A->start[i + 1]; ++pj )
{
++A_t->start[A->j[pj] + 1];
}
}
/* setup the row pointers for A^T */
for ( i = 1; i <= A->n; ++i )
{
A_t_top[i] = A_t->start[i] = A_t->start[i] + A_t->start[i - 1];
}
/* fill in A^T */
for ( i = 0; i < A->n; ++i )
{
for ( pj = A->start[i]; pj < A->start[i + 1]; ++pj )
{
j = A->j[pj];
A_t->j[A_t_top[j]] = i;
A_t->val[A_t_top[j]] = A->val[pj];
++A_t_top[j];
}
}
sfree( A_t_top, "Transpose::A_t_top" );
}
/* Transpose A in-place
*
Kurt A. O'Hearn
committed
* A: stored in CSR
*/
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
void Transpose_I( sparse_matrix * const A )
{
sparse_matrix * A_t;
if ( Allocate_Matrix( &A_t, A->n, A->m ) == FAILURE )
{
fprintf( stderr, "not enough memory for transposing matrices. terminating.\n" );
exit( INSUFFICIENT_MEMORY );
}
Transpose( A, A_t );
memcpy( A->start, A_t->start, sizeof(int) * (A_t->n + 1) );
memcpy( A->j, A_t->j, sizeof(int) * (A_t->start[A_t->n]) );
memcpy( A->val, A_t->val, sizeof(real) * (A_t->start[A_t->n]) );
Deallocate_Matrix( A_t );
}
/* Apply diagonal inverse (Jacobi) preconditioner to system residual
*
* Hdia_inv: diagonal inverse preconditioner (constructed using H)
* y: current residual
* x: preconditioned residual
Kurt A. O'Hearn
committed
* N: dimensions of preconditioner and vectors (# rows in H)
Kurt A. O'Hearn
committed
static void diag_pre_app( const real * const Hdia_inv, const real * const y,
Kurt A. O'Hearn
committed
real * const x, const int N )
Kurt A. O'Hearn
committed
unsigned int i;
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp for schedule(static)
#endif
Kurt A. O'Hearn
committed
for ( i = 0; i < N; ++i )
Kurt A. O'Hearn
committed
x[i] = y[i] * Hdia_inv[i];
Kurt A. O'Hearn
committed
/* Solve triangular system LU*x = y using level scheduling
*
* LU: lower/upper triangular, stored in CSR
* y: constants in linear system (RHS)
* x: solution
Kurt A. O'Hearn
committed
* N: dimensions of matrix and vectors
Kurt A. O'Hearn
committed
* tri: triangularity of LU (lower/upper)
Kurt A. O'Hearn
committed
* Assumptions:
* LU has non-zero diagonals
* Each row of LU has at least one non-zero (i.e., no rows with all zeros) */
void tri_solve( const sparse_matrix * const LU, const real * const y,
Kurt A. O'Hearn
committed
real * const x, const int N, const TRIANGULARITY tri )
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp single
#endif
if ( tri == LOWER )
Kurt A. O'Hearn
committed
for ( i = 0; i < N; ++i )
Kurt A. O'Hearn
committed
{
x[i] = y[i];
si = LU->start[i];
ei = LU->start[i + 1];
for ( pj = si; pj < ei - 1; ++pj )
{
j = LU->j[pj];
val = LU->val[pj];
x[i] -= val * x[j];
}
x[i] /= LU->val[pj];
Kurt A. O'Hearn
committed
}
}
Kurt A. O'Hearn
committed
{
Kurt A. O'Hearn
committed
for ( i = N - 1; i >= 0; --i )
Kurt A. O'Hearn
committed
{
x[i] = y[i];
si = LU->start[i];
ei = LU->start[i + 1];
for ( pj = si + 1; pj < ei; ++pj )
{
j = LU->j[pj];
val = LU->val[pj];
x[i] -= val * x[j];
}
x[i] /= LU->val[si];
Kurt A. O'Hearn
committed
}
Kurt A. O'Hearn
committed
/* Solve triangular system LU*x = y using level scheduling
* workspace: storage container for workspace structures
* LU: lower/upper triangular, stored in CSR
* y: constants in linear system (RHS)
* x: solution
Kurt A. O'Hearn
committed
* N: dimensions of matrix and vectors
Kurt A. O'Hearn
committed
* tri: triangularity of LU (lower/upper)
* find_levels: perform level search if positive, otherwise reuse existing levels
* Assumptions:
* LU has non-zero diagonals
* Each row of LU has at least one non-zero (i.e., no rows with all zeros) */
void tri_solve_level_sched( static_storage * workspace,
const sparse_matrix * const LU,
const real * const y, real * const x, const int N,
const TRIANGULARITY tri, int find_levels )
int i, j, pj, local_row, local_level;
unsigned int *row_levels, *level_rows, *level_rows_cnt;
int levels;
if ( tri == LOWER )
{
row_levels = workspace->row_levels_L;
level_rows = workspace->level_rows_L;
level_rows_cnt = workspace->level_rows_cnt_L;
}
else
{
row_levels = workspace->row_levels_U;
level_rows = workspace->level_rows_U;
level_rows_cnt = workspace->level_rows_cnt_U;
}
Kurt A. O'Hearn
committed
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp single
#endif
Kurt A. O'Hearn
committed
{
/* find levels (row dependencies in substitutions) */
Kurt A. O'Hearn
committed
if ( find_levels == TRUE )
Kurt A. O'Hearn
committed
memset( row_levels, 0, N * sizeof(unsigned int) );
memset( level_rows_cnt, 0, N * sizeof(unsigned int) );
memset( workspace->top, 0, N * sizeof(unsigned int) );
levels = 1;
if ( tri == LOWER )
Kurt A. O'Hearn
committed
for ( i = 0; i < N; ++i )
Kurt A. O'Hearn
committed
{
local_level = 1;
for ( pj = LU->start[i]; pj < LU->start[i + 1] - 1; ++pj )
{
local_level = MAX( local_level, row_levels[LU->j[pj]] + 1 );
}
levels = MAX( levels, local_level );
row_levels[i] = local_level;
++level_rows_cnt[local_level];
Kurt A. O'Hearn
committed
}
workspace->levels_L = levels;
fprintf(stderr, "levels(L): %d\n", levels);
Kurt A. O'Hearn
committed
fprintf(stderr, "NNZ(L): %d\n", LU->start[N]);
Kurt A. O'Hearn
committed
for ( i = N - 1; i >= 0; --i )
Kurt A. O'Hearn
committed
{
local_level = 1;
for ( pj = LU->start[i] + 1; pj < LU->start[i + 1]; ++pj )
{
local_level = MAX( local_level, row_levels[LU->j[pj]] + 1 );
}
levels = MAX( levels, local_level );
row_levels[i] = local_level;
++level_rows_cnt[local_level];
Kurt A. O'Hearn
committed
}
workspace->levels_U = levels;
fprintf(stderr, "levels(U): %d\n", levels);
Kurt A. O'Hearn
committed
fprintf(stderr, "NNZ(U): %d\n", LU->start[N]);
Kurt A. O'Hearn
committed
for ( i = 1; i < levels + 1; ++i )
{
level_rows_cnt[i] += level_rows_cnt[i - 1];
workspace->top[i] = level_rows_cnt[i];
Kurt A. O'Hearn
committed
Kurt A. O'Hearn
committed
for ( i = 0; i < N; ++i )
level_rows[workspace->top[row_levels[i] - 1]] = i;
++workspace->top[row_levels[i] - 1];
if ( tri == LOWER )
{
levels = workspace->levels_L;
}
else
{
levels = workspace->levels_U;
}
/* perform substitutions by level */
if ( tri == LOWER )
for ( i = 0; i < levels; ++i )
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp for schedule(static)
#endif
for ( j = level_rows_cnt[i]; j < level_rows_cnt[i + 1]; ++j )
local_row = level_rows[j];
x[local_row] = y[local_row];
for ( pj = LU->start[local_row]; pj < LU->start[local_row + 1] - 1; ++pj )
x[local_row] -= LU->val[pj] * x[LU->j[pj]];
x[local_row] /= LU->val[pj];
}
else
{
for ( i = 0; i < levels; ++i )
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp for schedule(static)
#endif
for ( j = level_rows_cnt[i]; j < level_rows_cnt[i + 1]; ++j )
local_row = level_rows[j];
x[local_row] = y[local_row];
for ( pj = LU->start[local_row] + 1; pj < LU->start[local_row + 1]; ++pj )
x[local_row] -= LU->val[pj] * x[LU->j[pj]];
x[local_row] /= LU->val[LU->start[local_row]];
}
}
}
}
Kurt A. O'Hearn
committed
/* Iterative greedy shared-memory parallel graph coloring
*
Kurt A. O'Hearn
committed
* control: container for control info
* workspace: storage container for workspace structures
Kurt A. O'Hearn
committed
* A: matrix to use for coloring, stored in CSR format;
* rows represent vertices, columns of entries within a row represent adjacent vertices
* (i.e., dependent rows for elimination during LU factorization)
* tri: triangularity of LU (lower/upper)
* color: vertex color (1-based)
*
* Reference:
* Umit V. Catalyurek et al.
* Graph Coloring Algorithms for Multi-core
Kurt A. O'Hearn
committed
* and Massively Threaded Architectures
* Parallel Computing, 2012
*/
void graph_coloring( const control_params * const control,
static_storage * workspace,
Kurt A. O'Hearn
committed
const sparse_matrix * const A, const TRIANGULARITY tri )
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp parallel
#endif
Kurt A. O'Hearn
committed
{
int i, pj, v;
unsigned int temp, recolor_cnt_local, *conflict_local;
Kurt A. O'Hearn
committed
int tid, *fb_color;
unsigned int *p_to_color, *p_conflict, *p_temp;
#ifdef _OPENMP
tid = omp_get_thread_num( );
#else
tid = 0;
#endif
p_to_color = workspace->to_color;
p_conflict = workspace->conflict;
#ifdef _OPENMP
#pragma omp for schedule(static)
#endif
for ( i = 0; i < A->n; ++i )
{
workspace->color[i] = 0;
}
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp single
#endif
Kurt A. O'Hearn
committed
workspace->recolor_cnt = A->n;
Kurt A. O'Hearn
committed
/* ordering of vertices to color depends on triangularity of factor
* for which coloring is to be used for */
if ( tri == LOWER )
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp for schedule(static)
#endif
Kurt A. O'Hearn
committed
for ( i = 0; i < A->n; ++i )
p_to_color[i] = i;
Kurt A. O'Hearn
committed
}
else
{
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp for schedule(static)
#endif
Kurt A. O'Hearn
committed
for ( i = 0; i < A->n; ++i )
{
p_to_color[i] = A->n - 1 - i;
Kurt A. O'Hearn
committed
}
}
Kurt A. O'Hearn
committed
fb_color = (int*) smalloc( sizeof(int) * A->n,
"graph_coloring::fb_color" );
conflict_local = (unsigned int*) smalloc( sizeof(unsigned int) * A->n,
"graph_coloring::fb_color" );
Kurt A. O'Hearn
committed
while ( workspace->recolor_cnt > 0 )
Kurt A. O'Hearn
committed
memset( fb_color, -1, sizeof(int) * A->n );
Kurt A. O'Hearn
committed
/* color vertices */
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp for schedule(static)
#endif
Kurt A. O'Hearn
committed
for ( i = 0; i < workspace->recolor_cnt; ++i )
v = p_to_color[i];
Kurt A. O'Hearn
committed
/* colors of adjacent vertices are forbidden */
for ( pj = A->start[v]; pj < A->start[v + 1]; ++pj )
Kurt A. O'Hearn
committed
if ( v != A->j[pj] )
{
Kurt A. O'Hearn
committed
fb_color[workspace->color[A->j[pj]]] = v;
Kurt A. O'Hearn
committed
}
Kurt A. O'Hearn
committed
/* search for min. color which is not in conflict with adjacent vertices;
* start at 1 since 0 is default (invalid) color for all vertices */
Kurt A. O'Hearn
committed
for ( pj = 1; fb_color[pj] == v; ++pj )
;
Kurt A. O'Hearn
committed
/* assign discovered color (no conflict in neighborhood of adjacent vertices) */
Kurt A. O'Hearn
committed
workspace->color[v] = pj;
Kurt A. O'Hearn
committed
}
/* determine if recoloring required */
Kurt A. O'Hearn
committed
temp = workspace->recolor_cnt;
recolor_cnt_local = 0;
#ifdef _OPENMP
#pragma omp barrier
Kurt A. O'Hearn
committed
#pragma omp single
#endif
Kurt A. O'Hearn
committed
workspace->recolor_cnt = 0;
}
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp for schedule(static)
#endif
for ( i = 0; i < temp; ++i )
{
v = p_to_color[i];
Kurt A. O'Hearn
committed
/* search for color conflicts with adjacent vertices */
for ( pj = A->start[v]; pj < A->start[v + 1]; ++pj )
{
Kurt A. O'Hearn
committed
if ( workspace->color[v] == workspace->color[A->j[pj]] && v > A->j[pj] )
Kurt A. O'Hearn
committed
{
conflict_local[recolor_cnt_local] = v;
++recolor_cnt_local;
break;
Kurt A. O'Hearn
committed
}
}
/* count thread-local conflicts and compute offsets for copying into shared buffer */
Kurt A. O'Hearn
committed
workspace->conflict_cnt[tid + 1] = recolor_cnt_local;
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp barrier
Kurt A. O'Hearn
committed
#pragma omp single
#endif
{
Kurt A. O'Hearn
committed
workspace->conflict_cnt[0] = 0;
for ( i = 1; i < control->num_threads + 1; ++i )
{
Kurt A. O'Hearn
committed
workspace->conflict_cnt[i] += workspace->conflict_cnt[i - 1];
}
Kurt A. O'Hearn
committed
workspace->recolor_cnt = workspace->conflict_cnt[control->num_threads];
}
/* copy thread-local conflicts into shared buffer */
for ( i = 0; i < recolor_cnt_local; ++i )
{
p_conflict[workspace->conflict_cnt[tid] + i] = conflict_local[i];
Kurt A. O'Hearn
committed
workspace->color[conflict_local[i]] = 0;
}
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp barrier
#endif
p_temp = p_to_color;
p_to_color = p_conflict;
p_conflict = p_temp;
sfree( conflict_local, "graph_coloring::conflict_local" );
sfree( fb_color, "graph_coloring::fb_color" );
Kurt A. O'Hearn
committed
/* Sort rows by coloring
Kurt A. O'Hearn
committed
*
Kurt A. O'Hearn
committed
* workspace: storage container for workspace structures
Kurt A. O'Hearn
committed
* n: number of entries in coloring
* tri: coloring to triangular factor to use (lower/upper)
*/
void sort_rows_by_colors( const static_storage * const workspace,
Kurt A. O'Hearn
committed
const unsigned int n, const TRIANGULARITY tri )
Kurt A. O'Hearn
committed
unsigned int i;
Kurt A. O'Hearn
committed
memset( workspace->color_top, 0, sizeof(unsigned int) * (n + 1) );
Kurt A. O'Hearn
committed
/* sort vertices by color (ascending within a color)
* 1) count colors
Kurt A. O'Hearn
committed
* 3) sort rows by color
Kurt A. O'Hearn
committed
*
* note: color is 1-based */
for ( i = 0; i < n; ++i )
{
Kurt A. O'Hearn
committed
++workspace->color_top[workspace->color[i]];
Kurt A. O'Hearn
committed
}
for ( i = 1; i < n + 1; ++i )
{
Kurt A. O'Hearn
committed
workspace->color_top[i] += workspace->color_top[i - 1];
Kurt A. O'Hearn
committed
}
for ( i = 0; i < n; ++i )
{
Kurt A. O'Hearn
committed
workspace->permuted_row_col[workspace->color_top[workspace->color[i] - 1]] = i;
++workspace->color_top[workspace->color[i] - 1];
Kurt A. O'Hearn
committed
}
/* invert mapping to get map from current row/column to permuted (new) row/column */
for ( i = 0; i < n; ++i )
{
Kurt A. O'Hearn
committed
workspace->permuted_row_col_inv[workspace->permuted_row_col[i]] = i;
Kurt A. O'Hearn
committed
}
}
/* Apply permutation Q^T*x or Q*x based on graph coloring
*
Kurt A. O'Hearn
committed
* workspace: storage container for workspace structures
Kurt A. O'Hearn
committed
* color: vertex color (1-based); vertices represent matrix rows/columns
* x: vector to permute (in-place)
* n: number of entries in x
* invert_map: if TRUE, use Q^T, otherwise use Q
* tri: coloring to triangular factor to use (lower/upper)
*/
static void permute_vector( const static_storage * const workspace,
Kurt A. O'Hearn
committed
real * const x, const unsigned int n, const int invert_map,
const TRIANGULARITY tri )
Kurt A. O'Hearn
committed
{
unsigned int i;
Kurt A. O'Hearn
committed
unsigned int *mapping;
Kurt A. O'Hearn
committed
if ( invert_map == TRUE )
Kurt A. O'Hearn
committed
mapping = workspace->permuted_row_col_inv;
}
else
{
mapping = workspace->permuted_row_col;
Kurt A. O'Hearn
committed
}
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp for schedule(static)
#endif
Kurt A. O'Hearn
committed
for ( i = 0; i < n; ++i )
{
Kurt A. O'Hearn
committed
workspace->x_p[i] = x[mapping[i]];
Kurt A. O'Hearn
committed
}
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp for schedule(static)
#endif
Kurt A. O'Hearn
committed
for ( i = 0; i < n; ++i )
Kurt A. O'Hearn
committed
{
Kurt A. O'Hearn
committed
x[i] = workspace->x_p[i];
Kurt A. O'Hearn
committed
}
}
Kurt A. O'Hearn
committed
/* Apply permutation Q^T*(LU)*Q based on graph coloring
*
Kurt A. O'Hearn
committed
* workspace: storage container for workspace structures
Kurt A. O'Hearn
committed
* color: vertex color (1-based); vertices represent matrix rows/columns
* LU: matrix to permute, stored in CSR format
* tri: triangularity of LU (lower/upper)
*/
void permute_matrix( const static_storage * const workspace,
Kurt A. O'Hearn
committed
sparse_matrix * const LU, const TRIANGULARITY tri )
Kurt A. O'Hearn
committed
{
int i, pj, nr, nc;
sparse_matrix *LUtemp;
if ( Allocate_Matrix( &LUtemp, LU->n, LU->m ) == FAILURE )
{
fprintf( stderr, "Not enough space for graph coloring (factor permutation). Terminating...\n" );
exit( INSUFFICIENT_MEMORY );
}
/* count nonzeros in each row of permuted factor (re-use color_top for counting) */
Kurt A. O'Hearn
committed
memset( workspace->color_top, 0, sizeof(unsigned int) * (LU->n + 1) );
Kurt A. O'Hearn
committed
if ( tri == LOWER )
{
for ( i = 0; i < LU->n; ++i )
Kurt A. O'Hearn
committed
nr = workspace->permuted_row_col_inv[i];
Kurt A. O'Hearn
committed
for ( pj = LU->start[i]; pj < LU->start[i + 1]; ++pj )
Kurt A. O'Hearn
committed
nc = workspace->permuted_row_col_inv[LU->j[pj]];
Kurt A. O'Hearn
committed
if ( nc <= nr )
{
Kurt A. O'Hearn
committed
++workspace->color_top[nr + 1];
Kurt A. O'Hearn
committed
}
/* correct entries to maintain triangularity (lower) */
else
{
Kurt A. O'Hearn
committed
++workspace->color_top[nc + 1];
Kurt A. O'Hearn
committed
}
else
{
for ( i = LU->n - 1; i >= 0; --i )
Kurt A. O'Hearn
committed
nr = workspace->permuted_row_col_inv[i];
Kurt A. O'Hearn
committed
for ( pj = LU->start[i]; pj < LU->start[i + 1]; ++pj )
Kurt A. O'Hearn
committed
nc = workspace->permuted_row_col_inv[LU->j[pj]];
Kurt A. O'Hearn
committed
if ( nc >= nr )
{
Kurt A. O'Hearn
committed
++workspace->color_top[nr + 1];
Kurt A. O'Hearn
committed
}
/* correct entries to maintain triangularity (upper) */
else
{
Kurt A. O'Hearn
committed
++workspace->color_top[nc + 1];
Kurt A. O'Hearn
committed
}
Kurt A. O'Hearn
committed
for ( i = 1; i < LU->n + 1; ++i )
{
Kurt A. O'Hearn
committed
workspace->color_top[i] += workspace->color_top[i - 1];
Kurt A. O'Hearn
committed
}
Kurt A. O'Hearn
committed
memcpy( LUtemp->start, workspace->color_top, sizeof(unsigned int) * (LU->n + 1) );
Kurt A. O'Hearn
committed
/* permute factor */
if ( tri == LOWER )
{
for ( i = 0; i < LU->n; ++i )
Kurt A. O'Hearn
committed
nr = workspace->permuted_row_col_inv[i];
Kurt A. O'Hearn
committed
for ( pj = LU->start[i]; pj < LU->start[i + 1]; ++pj )
Kurt A. O'Hearn
committed
nc = workspace->permuted_row_col_inv[LU->j[pj]];
Kurt A. O'Hearn
committed
if ( nc <= nr )
{
Kurt A. O'Hearn
committed
LUtemp->j[workspace->color_top[nr]] = nc;
LUtemp->val[workspace->color_top[nr]] = LU->val[pj];
++workspace->color_top[nr];
Kurt A. O'Hearn
committed
}
/* correct entries to maintain triangularity (lower) */
else
{
Kurt A. O'Hearn
committed
LUtemp->j[workspace->color_top[nc]] = nr;
LUtemp->val[workspace->color_top[nc]] = LU->val[pj];
++workspace->color_top[nc];
Kurt A. O'Hearn
committed
}
else
{
for ( i = LU->n - 1; i >= 0; --i )
Kurt A. O'Hearn
committed
nr = workspace->permuted_row_col_inv[i];
Kurt A. O'Hearn
committed
for ( pj = LU->start[i]; pj < LU->start[i + 1]; ++pj )
Kurt A. O'Hearn
committed
nc = workspace->permuted_row_col_inv[LU->j[pj]];
Kurt A. O'Hearn
committed
if ( nc >= nr )
{
Kurt A. O'Hearn
committed
LUtemp->j[workspace->color_top[nr]] = nc;
LUtemp->val[workspace->color_top[nr]] = LU->val[pj];
++workspace->color_top[nr];
Kurt A. O'Hearn
committed
}
/* correct entries to maintain triangularity (upper) */
else
{
Kurt A. O'Hearn
committed
LUtemp->j[workspace->color_top[nc]] = nr;
LUtemp->val[workspace->color_top[nc]] = LU->val[pj];
++workspace->color_top[nc];
Kurt A. O'Hearn
committed
}
Kurt A. O'Hearn
committed
memcpy( LU->start, LUtemp->start, sizeof(unsigned int) * (LU->n + 1) );
memcpy( LU->j, LUtemp->j, sizeof(unsigned int) * LU->start[LU->n] );
memcpy( LU->val, LUtemp->val, sizeof(real) * LU->start[LU->n] );
Kurt A. O'Hearn
committed
Deallocate_Matrix( LUtemp );
}
Kurt A. O'Hearn
committed
/* Setup routines to build permuted QEq matrix H (via graph coloring),
* used for preconditioning (incomplete factorizations computed based on
* permuted H)
*
Kurt A. O'Hearn
committed
* control: container for control info
* workspace: storage container for workspace structures
Kurt A. O'Hearn
committed
* H: symmetric, lower triangular portion only, stored in CSR format;
Kurt A. O'Hearn
committed
* H_full: symmetric, stored in CSR format;
* H_p (output): permuted copy of H based on coloring, lower half stored, CSR format
Kurt A. O'Hearn
committed
*/
Kurt A. O'Hearn
committed
void setup_graph_coloring( const control_params * const control,
const static_storage * const workspace, const sparse_matrix * const H,
Kurt A. O'Hearn
committed
sparse_matrix ** H_full, sparse_matrix ** H_p )
Kurt A. O'Hearn
committed
{
Kurt A. O'Hearn
committed
if ( *H_p == NULL )
Kurt A. O'Hearn
committed
{
Kurt A. O'Hearn
committed
if ( Allocate_Matrix( H_p, H->n, H->m ) == FAILURE )
{
Kurt A. O'Hearn
committed
fprintf( stderr, "[ERROR] not enough memory for graph coloring. terminating.\n" );
exit( INSUFFICIENT_MEMORY );
}
Kurt A. O'Hearn
committed
}
else if ( (*H_p)->m < H->m )
{
Deallocate_Matrix( *H_p );
if ( Allocate_Matrix( H_p, H->n, H->m ) == FAILURE )
{
fprintf( stderr, "[ERROR] not enough memory for graph coloring. terminating.\n" );
Kurt A. O'Hearn
committed
exit( INSUFFICIENT_MEMORY );
Kurt A. O'Hearn
committed
compute_full_sparse_matrix( H, H_full );
graph_coloring( control, (static_storage *) workspace, *H_full, LOWER );
Kurt A. O'Hearn
committed
sort_rows_by_colors( workspace, (*H_full)->n, LOWER );
Kurt A. O'Hearn
committed
memcpy( (*H_p)->start, H->start, sizeof(int) * (H->n + 1) );
memcpy( (*H_p)->j, H->j, sizeof(int) * (H->start[H->n]) );
memcpy( (*H_p)->val, H->val, sizeof(real) * (H->start[H->n]) );
permute_matrix( workspace, (*H_p), LOWER );
/* Jacobi iteration using truncated Neumann series: x_{k+1} = Gx_k + D^{-1}b
* where:
* G = I - D^{-1}R
* R = triangular matrix
* D = diagonal matrix, diagonals from R
*
* Note: used during the backsolves when applying preconditioners with
* triangular factors in iterative linear solvers
*
* Note: Newmann series arises from series expansion of the inverse of
* the coefficient matrix in the triangular system
*
* workspace: storage container for workspace structures
* R:
* Dinv:
* b:
* x (output):
* tri:
* maxiter:
* */
void jacobi_iter( const static_storage * const workspace,
const sparse_matrix * const R, const real * const Dinv,
const real * const b, real * const x, const TRIANGULARITY tri,
const unsigned int maxiter )
Kurt A. O'Hearn
committed
unsigned int i, k, si, ei, iter;
real *p1, *p2, *p3;
Kurt A. O'Hearn
committed
si = 0;
ei = 0;
iter = 0;
p1 = workspace->rp;
p2 = workspace->rp2;
Vector_MakeZero( p1, R->n );
Kurt A. O'Hearn
committed
/* precompute and cache, as invariant in loop below */
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp for schedule(static)
#endif
for ( i = 0; i < R->n; ++i )
{
workspace->Dinv_b[i] = Dinv[i] * b[i];
do
{
/* x_{k+1} = G*x_{k} + Dinv*b */
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp for schedule(guided)
#endif
for ( i = 0; i < R->n; ++i )
{
if (tri == LOWER)
{
si = R->start[i];
ei = R->start[i + 1] - 1;
}
else
Kurt A. O'Hearn
committed
{
si = R->start[i] + 1;
ei = R->start[i + 1];
Kurt A. O'Hearn
committed
}
p2[i] = 0.;
for ( k = si; k < ei; ++k )
Kurt A. O'Hearn
committed
{
p2[i] += R->val[k] * p1[R->j[k]];
Kurt A. O'Hearn
committed
}
p2[i] *= -Dinv[i];
p2[i] += workspace->Dinv_b[i];
}
p3 = p1;
p1 = p2;
p2 = p3;
++iter;
while ( iter < maxiter );
Vector_Copy( x, p1, R->n );
Kurt A. O'Hearn
committed
/* Apply left-sided preconditioning while solver M^{-1}Ax = M^{-1}b
Kurt A. O'Hearn
committed
*
Kurt A. O'Hearn
committed
* workspace: data struct containing matrices, stored in CSR
* control: data struct containing parameters
Kurt A. O'Hearn
committed
* y: vector to which to apply preconditioning,
* specific to internals of iterative solver being used
* x: preconditioned vector (output)
* fresh_pre: parameter indicating if this is a newly computed (fresh) preconditioner
Kurt A. O'Hearn
committed
* side: used in determining how to apply preconditioner if the preconditioner is
* factorized as M = M_{1}M_{2} (e.g., incomplete LU, A \approx LU)
Kurt A. O'Hearn
committed
* Assumptions:
* Matrices have non-zero diagonals
* Each row of a matrix has at least one non-zero (i.e., no rows with all zeros) */
static void apply_preconditioner( const static_storage * const workspace, const control_params * const control,
Kurt A. O'Hearn
committed
const real * const y, real * const x, const int fresh_pre,
const int side )
Kurt A. O'Hearn
committed
int i, si;
Kurt A. O'Hearn
committed
/* no preconditioning */
if ( control->cm_solver_pre_comp_type == NONE_PC )
Kurt A. O'Hearn
committed
{
Kurt A. O'Hearn
committed
if ( x != y )
{
Vector_Copy( x, y, workspace->H->n );
}
Kurt A. O'Hearn
committed
}
else
{
Kurt A. O'Hearn
committed
switch ( side )
Kurt A. O'Hearn
committed
case LEFT:
switch ( control->cm_solver_pre_app_type )
Kurt A. O'Hearn
committed
{
Kurt A. O'Hearn
committed
case TRI_SOLVE_PA:
switch ( control->cm_solver_pre_comp_type )
{
case DIAG_PC:
diag_pre_app( workspace->Hdia_inv, y, x, workspace->H->n );
break;
case ICHOLT_PC:
case ILU_PAR_PC:
case ILUT_PAR_PC:
tri_solve( workspace->L, y, x, workspace->L->n, LOWER );
break;
case SAI_PC:
Sparse_MatVec_full( workspace->H_app_inv, y, x );
break;
default:
fprintf( stderr, "Unrecognized preconditioner application method. Terminating...\n" );
exit( INVALID_INPUT );
break;
}
Kurt A. O'Hearn
committed
break;
Kurt A. O'Hearn
committed
case TRI_SOLVE_LEVEL_SCHED_PA:
switch ( control->cm_solver_pre_comp_type )
{
case DIAG_PC:
diag_pre_app( workspace->Hdia_inv, y, x, workspace->H->n );
break;
case ICHOLT_PC:
case ILU_PAR_PC:
case ILUT_PAR_PC:
tri_solve_level_sched( (static_storage *) workspace,
workspace->L, y, x, workspace->L->n, LOWER, fresh_pre );
Kurt A. O'Hearn
committed
break;
case SAI_PC:
Sparse_MatVec_full( workspace->H_app_inv, y, x );
break;
default:
fprintf( stderr, "Unrecognized preconditioner application method. Terminating...\n" );
exit( INVALID_INPUT );
break;
}
Kurt A. O'Hearn
committed
break;
Kurt A. O'Hearn
committed
case TRI_SOLVE_GC_PA:
switch ( control->cm_solver_pre_comp_type )
{
case DIAG_PC:
case SAI_PC:
fprintf( stderr, "Unsupported preconditioner computation/application method combination. Terminating...\n" );
exit( INVALID_INPUT );
break;
case ICHOLT_PC:
case ILU_PAR_PC:
case ILUT_PAR_PC:
#ifdef _OPENMP
#pragma omp for schedule(static)
Kurt A. O'Hearn
committed
#endif
for ( i = 0; i < workspace->H->n; ++i )
Kurt A. O'Hearn
committed
{
workspace->y_p[i] = y[i];
Kurt A. O'Hearn
committed
}
Kurt A. O'Hearn
committed
permute_vector( workspace, workspace->y_p, workspace->H->n, FALSE, LOWER );
tri_solve_level_sched( (static_storage *) workspace,
workspace->L, workspace->y_p, x, workspace->L->n, LOWER, fresh_pre );
Kurt A. O'Hearn
committed
break;
Kurt A. O'Hearn
committed
default:
fprintf( stderr, "Unrecognized preconditioner application method. Terminating...\n" );
exit( INVALID_INPUT );
break;
}
Kurt A. O'Hearn
committed
break;
Kurt A. O'Hearn
committed
case JACOBI_ITER_PA:
switch ( control->cm_solver_pre_comp_type )
{
case DIAG_PC:
case SAI_PC:
fprintf( stderr, "Unsupported preconditioner computation/application method combination. Terminating...\n" );
exit( INVALID_INPUT );
break;
case ICHOLT_PC:
case ILU_PAR_PC:
case ILUT_PAR_PC:
/* construct D^{-1}_L */
Kurt A. O'Hearn
committed
if ( fresh_pre == TRUE )
{
#ifdef _OPENMP
#pragma omp for schedule(static)
#endif
for ( i = 0; i < workspace->L->n; ++i )
{
si = workspace->L->start[i + 1] - 1;
workspace->Dinv_L[i] = 1.0 / workspace->L->val[si];
Kurt A. O'Hearn
committed
}
}
jacobi_iter( workspace, workspace->L, workspace->Dinv_L,
y, x, LOWER, control->cm_solver_pre_app_jacobi_iters );
Kurt A. O'Hearn
committed
break;
Kurt A. O'Hearn
committed
default:
fprintf( stderr, "Unrecognized preconditioner application method. Terminating...\n" );
exit( INVALID_INPUT );
break;
}
break;
Kurt A. O'Hearn
committed
default:
fprintf( stderr, "Unrecognized preconditioner application method. Terminating...\n" );
exit( INVALID_INPUT );
Kurt A. O'Hearn
committed
Kurt A. O'Hearn
committed
}
break;
Kurt A. O'Hearn
committed
case RIGHT:
switch ( control->cm_solver_pre_app_type )
Kurt A. O'Hearn
committed
{
Kurt A. O'Hearn
committed
case TRI_SOLVE_PA:
switch ( control->cm_solver_pre_comp_type )
{
case DIAG_PC:
case SAI_PC:
if ( x != y )
{
Vector_Copy( x, y, workspace->H->n );
}
break;
case ICHOLT_PC:
case ILU_PAR_PC:
case ILUT_PAR_PC:
tri_solve( workspace->U, y, x, workspace->U->n, UPPER );
break;
default:
fprintf( stderr, "Unrecognized preconditioner application method. Terminating...\n" );
exit( INVALID_INPUT );
break;
}
Kurt A. O'Hearn
committed
break;
Kurt A. O'Hearn
committed
case TRI_SOLVE_LEVEL_SCHED_PA:
switch ( control->cm_solver_pre_comp_type )
Kurt A. O'Hearn
committed
{
Kurt A. O'Hearn
committed
case DIAG_PC:
case SAI_PC:
if ( x != y )
{
Vector_Copy( x, y, workspace->H->n );
}
break;
case ICHOLT_PC:
case ILU_PAR_PC:
case ILUT_PAR_PC:
tri_solve_level_sched( (static_storage *) workspace,
workspace->U, y, x, workspace->U->n, UPPER, fresh_pre );
Kurt A. O'Hearn
committed
break;
default:
fprintf( stderr, "Unrecognized preconditioner application method. Terminating...\n" );
exit( INVALID_INPUT );
break;
Kurt A. O'Hearn
committed
}
Kurt A. O'Hearn
committed
break;
case TRI_SOLVE_GC_PA:
switch ( control->cm_solver_pre_comp_type )
Kurt A. O'Hearn
committed
{
Kurt A. O'Hearn
committed
case DIAG_PC:
case SAI_PC:
fprintf( stderr, "Unsupported preconditioner computation/application method combination. Terminating...\n" );
exit( INVALID_INPUT );
break;
case ICHOLT_PC:
case ILU_PAR_PC:
case ILUT_PAR_PC:
tri_solve_level_sched( (static_storage *) workspace,
workspace->U, y, x, workspace->U->n, UPPER, fresh_pre );
permute_vector( workspace, x, workspace->H->n, TRUE, UPPER );
Kurt A. O'Hearn
committed
break;
Kurt A. O'Hearn
committed
default:
fprintf( stderr, "Unrecognized preconditioner application method. Terminating...\n" );
exit( INVALID_INPUT );
break;
Kurt A. O'Hearn
committed
}
Kurt A. O'Hearn
committed
break;
case JACOBI_ITER_PA:
switch ( control->cm_solver_pre_comp_type )
{
case DIAG_PC:
case SAI_PC:
fprintf( stderr, "Unsupported preconditioner computation/application method combination. Terminating...\n" );
exit( INVALID_INPUT );
break;
case ICHOLT_PC:
case ILU_PAR_PC:
case ILUT_PAR_PC:
/* construct D^{-1}_U */
Kurt A. O'Hearn
committed
if ( fresh_pre == TRUE )
{
#ifdef _OPENMP
Kurt A. O'Hearn
committed
#pragma omp for schedule(static)
#endif
Kurt A. O'Hearn
committed
for ( i = 0; i < workspace->U->n; ++i )
{
si = workspace->U->start[i];
workspace->Dinv_U[i] = 1.0 / workspace->U->val[si];
Kurt A. O'Hearn
committed
}
Kurt A. O'Hearn
committed
}
Kurt A. O'Hearn
committed
jacobi_iter( workspace, workspace->U, workspace->Dinv_U,
y, x, UPPER, control->cm_solver_pre_app_jacobi_iters );
Kurt A. O'Hearn
committed
break;
Kurt A. O'Hearn
committed
default:
fprintf( stderr, "Unrecognized preconditioner application method. Terminating...\n" );
exit( INVALID_INPUT );
break;
}
break;
Kurt A. O'Hearn
committed
default:
fprintf( stderr, "Unrecognized preconditioner application method. Terminating...\n" );
exit( INVALID_INPUT );
break;
Kurt A. O'Hearn
committed
Kurt A. O'Hearn
committed
}
break;
Kurt A. O'Hearn
committed
}
Loading
Loading full blame...