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"
#include "allocate.h"
Kurt A. O'Hearn
committed
#include "tool_box.h"
Kurt A. O'Hearn
committed
typedef enum
{
LOWER = 0,
UPPER = 1,
} TRIANGULARITY;
/* global to make OpenMP shared (Sparse_MatVec) */
#ifdef _OPENMP
real *b_local = NULL;
#endif
/* global to make OpenMP shared (apply_preconditioner) */
real *Dinv_L = NULL, *Dinv_U = NULL;
/* global to make OpenMP shared (tri_solve_level_sched) */
int levels = 1;
int levels_L = 1, levels_U = 1;
unsigned int *row_levels_L = NULL, *level_rows_L = NULL, *level_rows_cnt_L = NULL;
unsigned int *row_levels_U = NULL, *level_rows_U = NULL, *level_rows_cnt_U = NULL;
unsigned int *row_levels, *level_rows, *level_rows_cnt;
unsigned int *top = NULL;
Kurt A. O'Hearn
committed
/* global to make OpenMP shared (graph_coloring) */
unsigned int *color = NULL;
unsigned int *to_color = NULL;
Kurt A. O'Hearn
committed
unsigned int *conflict = NULL;
unsigned int *temp_ptr;
unsigned int *recolor = NULL;
unsigned int recolor_cnt;
unsigned int *color_top = NULL;
Kurt A. O'Hearn
committed
/* global to make OpenMP shared (sort_colors) */
unsigned int *permuted_row_col = NULL;
Kurt A. O'Hearn
committed
unsigned int *permuted_row_col_inv = NULL;
real *y_p = NULL;
/* global to make OpenMP shared (permute_vector) */
real *x_p = NULL;
unsigned int *mapping = NULL;
/* global to make OpenMP shared (jacobi_iter) */
real *Dinv_b = NULL, *rp = NULL, *rp2 = NULL, *rp3 = NULL;
/* sparse matrix-vector product Ax=b
* where:
Kurt A. O'Hearn
committed
* A: lower triangular matrix, stored in CSR format
* x: vector
* b: vector (result) */
static void Sparse_MatVec( 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
#pragma omp master
{
/* keep b_local for program duration to avoid allocate/free
* overhead per Sparse_MatVec call*/
if ( b_local == NULL )
{
if ( (b_local = (real*) malloc( omp_get_num_threads() * n * sizeof(real))) == NULL )
exit( INSUFFICIENT_MEMORY );
}
}
#pragma omp barrier
Vector_MakeZero( (real * const)b_local, omp_get_num_threads() * n );
#endif
#pragma omp for schedule(static)
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
b_local[tid * n + j] += H * x[i];
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
b_local[tid * n + i] += A->val[k] * x[i];
#else
b[i] += A->val[k] * x[i];
#endif
#ifdef _OPENMP
#pragma omp for schedule(static)
for ( i = 0; i < n; ++i )
{
for ( j = 0; j < omp_get_num_threads(); ++j )
b[i] += b_local[j * n + i];
Kurt A. O'Hearn
committed
}
Kurt A. O'Hearn
committed
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
/* Transpose A and copy into A^T
*
* A: symmetric, lower triangular (half-format), stored in CSR
* A_t: symmetric, upper triangular (half-format), stored in CSR
*
* Assumptions:
* A has non-zero diagonals
* Each row of A has at least one non-zero (i.e., no rows with all zeros) */
void Transpose( const sparse_matrix const *A, sparse_matrix const *A_t )
{
unsigned int i, j, pj, *A_t_top;
if ( (A_t_top = (unsigned int*) calloc( A->n + 1, sizeof(unsigned int))) == NULL )
{
fprintf( stderr, "Not enough space for matrix tranpose. Terminating...\n" );
exit( INSUFFICIENT_MEMORY );
}
memset( A_t->start, 0, (A->n + 1) * sizeof(unsigned int) );
/* count nonzeros in each column of A^T */
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];
}
}
free( A_t_top );
}
/* Transpose A in-place
*
* A: symmetric, lower triangular (half-format), stored in CSR
*
* Assumptions:
* A has non-zero diagonals
* Each row of A has at least one non-zero (i.e., no rows with all zeros) */
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
* N: length 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
unsigned int i;
#pragma omp for schedule(static)
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
* 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) */
static void tri_solve( const sparse_matrix * const LU, const real * const y,
real * const x, const TRIANGULARITY tri )
#pragma omp master
if ( tri == LOWER )
for ( i = 0; i < LU->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
{
for ( i = LU->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
*
* LU: lower/upper triangular, stored in CSR
* y: constants in linear system (RHS)
* x: solution
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) */
Kurt A. O'Hearn
committed
static void tri_solve_level_sched( const sparse_matrix * const LU, const real * const y,
real * const x, const TRIANGULARITY tri, int find_levels )
int i, j, pj, local_row, local_level;
Kurt A. O'Hearn
committed
#pragma omp master
Kurt A. O'Hearn
committed
{
if ( tri == LOWER )
Kurt A. O'Hearn
committed
{
row_levels = row_levels_L;
level_rows = level_rows_L;
level_rows_cnt = level_rows_cnt_L;
levels = levels_L;
}
else
{
row_levels = row_levels_U;
level_rows = level_rows_U;
level_rows_cnt = level_rows_cnt_U;
levels = levels_U;
Kurt A. O'Hearn
committed
}
if ( row_levels == NULL || level_rows == NULL || level_rows_cnt == NULL )
Kurt A. O'Hearn
committed
{
if ( (row_levels = (unsigned int*) malloc((size_t)LU->n * sizeof(unsigned int))) == NULL
|| (level_rows = (unsigned int*) malloc((size_t)LU->n * sizeof(unsigned int))) == NULL
|| (level_rows_cnt = (unsigned int*) malloc((size_t)(LU->n + 1) * sizeof(unsigned int))) == NULL )
{
fprintf( stderr, "Not enough space for triangular solve via level scheduling. Terminating...\n" );
exit( INSUFFICIENT_MEMORY );
}
Kurt A. O'Hearn
committed
}
if ( top == NULL )
{
if ( (top = (unsigned int*) malloc((size_t)(LU->n + 1) * sizeof(unsigned int))) == NULL )
{
fprintf( stderr, "Not enough space for triangular solve via level scheduling. Terminating...\n" );
exit( INSUFFICIENT_MEMORY );
}
}
Kurt A. O'Hearn
committed
/* find levels (row dependencies in substitutions) */
Kurt A. O'Hearn
committed
if ( find_levels == TRUE )
memset( row_levels, 0, LU->n * sizeof(unsigned int) );
memset( level_rows_cnt, 0, LU->n * sizeof(unsigned int) );
memset( top, 0, LU->n * sizeof(unsigned int) );
levels = 1;
if ( tri == LOWER )
for ( i = 0; i < LU->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
}
Kurt A. O'Hearn
committed
//#if defined(DEBUG)
fprintf(stderr, "levels(L): %d\n", levels);
fprintf(stderr, "NNZ(L): %d\n", LU->start[LU->n]);
Kurt A. O'Hearn
committed
//#endif
for ( i = LU->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
}
Kurt A. O'Hearn
committed
//#if defined(DEBUG)
fprintf(stderr, "levels(U): %d\n", levels);
fprintf(stderr, "NNZ(U): %d\n", LU->start[LU->n]);
Kurt A. O'Hearn
committed
//#endif
Kurt A. O'Hearn
committed
for ( i = 1; i < levels + 1; ++i )
{
level_rows_cnt[i] += level_rows_cnt[i - 1];
top[i] = level_rows_cnt[i];
}
Kurt A. O'Hearn
committed
for ( i = 0; i < LU->n; ++i )
{
level_rows[top[row_levels[i] - 1]] = i;
++top[row_levels[i] - 1];
}
#pragma omp barrier
/* perform substitutions by level */
if ( tri == LOWER )
for ( i = 0; i < levels; ++i )
#pragma omp for schedule(static)
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 )
#pragma omp for schedule(static)
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]];
}
}
}
#pragma omp master
Kurt A. O'Hearn
committed
{
/* save level info for re-use if performing repeated triangular solves via preconditioning */
if ( tri == LOWER )
{
row_levels_L = row_levels;
level_rows_L = level_rows;
level_rows_cnt_L = level_rows_cnt;
levels_L = levels;
}
else
{
row_levels_U = row_levels;
level_rows_U = level_rows;
level_rows_cnt_U = level_rows_cnt;
levels_U = levels;
}
Kurt A. O'Hearn
committed
}
Kurt A. O'Hearn
committed
#pragma omp barrier
static void compute_H_full( const sparse_matrix * const H )
{
Kurt A. O'Hearn
committed
if ( Allocate_Matrix( &H_t, H->n, H->m ) == FAILURE )
Kurt A. O'Hearn
committed
fprintf( stderr, "not enough memory for full H. terminating.\n" );
exit( INSUFFICIENT_MEMORY );
}
Kurt A. O'Hearn
committed
/* Set up the sparse matrix data structure for A. */
Transpose( H, H_t );
Kurt A. O'Hearn
committed
count = 0;
for ( i = 0; i < H->n; ++i )
{
Kurt A. O'Hearn
committed
/* H: symmetric, lower triangular portion only stored */
for ( pj = H->start[i]; pj < H->start[i + 1]; ++pj )
{
H_full->val[count] = H->val[pj];
H_full->j[count] = H->j[pj];
++count;
}
/* H^T: symmetric, upper triangular portion only stored;
* skip diagonal from H^T, as included from H above */
for ( pj = H_t->start[i] + 1; pj < H_t->start[i + 1]; ++pj )
{
H_full->val[count] = H_t->val[pj];
H_full->j[count] = H_t->j[pj];
++count;
}
Kurt A. O'Hearn
committed
H_full->start[i] = count;
Kurt A. O'Hearn
committed
Deallocate_Matrix( H_t );
Kurt A. O'Hearn
committed
/* Iterative greedy shared-memory parallel graph coloring
*
* 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
* and Massively Threaded Architectures
* Parallel Computing, 2012
*/
void graph_coloring( const sparse_matrix * const A, const TRIANGULARITY tri )
Kurt A. O'Hearn
committed
#pragma omp parallel
{
Kurt A. O'Hearn
committed
int i, pj, v;
unsigned int temp;
int *fb_color;
Kurt A. O'Hearn
committed
#pragma omp master
Kurt A. O'Hearn
committed
memset( color, 0, sizeof(unsigned int) * A->n );
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 )
Kurt A. O'Hearn
committed
#pragma omp for schedule(static)
for ( i = 0; i < A->n; ++i )
Kurt A. O'Hearn
committed
to_color[i] = i;
Kurt A. O'Hearn
committed
}
else
{
#pragma omp for schedule(static)
for ( i = 0; i < A->n; ++i )
{
to_color[i] = A->n - 1 - i;
}
}
Kurt A. O'Hearn
committed
if ( (fb_color = (int*) malloc(sizeof(int) * MAX_COLOR)) == NULL )
{
fprintf( stderr, "not enough memory for graph coloring. terminating.\n" );
exit( INSUFFICIENT_MEMORY );
Kurt A. O'Hearn
committed
#pragma omp barrier
while ( recolor_cnt > 0 )
Kurt A. O'Hearn
committed
memset( fb_color, -1, sizeof(int) * MAX_COLOR );
Kurt A. O'Hearn
committed
/* color vertices */
#pragma omp for schedule(static)
for ( i = 0; i < recolor_cnt; ++i )
Kurt A. O'Hearn
committed
v = to_color[i];
/* 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] )
{
fb_color[color[A->j[pj]]] = v;
}
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 */
for ( pj = 1; fb_color[pj] == v; ++pj );
Kurt A. O'Hearn
committed
/* assign discovered color (no conflict in neighborhood of adjacent vertices) */
color[v] = pj;
}
/* determine if recoloring required */
//TODO: switch to reduction on recolor_cnt (+) via parallel scan through recolor
#pragma omp master
Kurt A. O'Hearn
committed
temp = recolor_cnt;
recolor_cnt = 0;
for ( i = 0; i < temp; ++i )
Kurt A. O'Hearn
committed
v = to_color[i];
/* search for color conflicts with adjacent vertices */
for ( pj = A->start[v]; pj < A->start[v + 1]; ++pj )
{
if ( color[v] == color[A->j[pj]] && v > A->j[pj] )
{
conflict[recolor_cnt] = v;
color[v] = 0;
++recolor_cnt;
break;
}
}
Kurt A. O'Hearn
committed
temp_ptr = to_color;
to_color = conflict;
conflict = temp_ptr;
Kurt A. O'Hearn
committed
#pragma omp barrier
Kurt A. O'Hearn
committed
free( fb_color );
//#if defined(DEBUG)
// #pragma omp master
// {
// for ( i = 0; i < A->n; ++i )
// printf("Vertex: %5d, Color: %5d\n", i, color[i] );
// }
//#endif
#pragma omp barrier
}
}
Kurt A. O'Hearn
committed
/* Sort coloring
*
* n: number of entries in coloring
* tri: coloring to triangular factor to use (lower/upper)
*/
void sort_colors( const unsigned int n, const TRIANGULARITY tri )
Kurt A. O'Hearn
committed
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
unsigned int i;
memset( color_top, 0, sizeof(unsigned int) * (n + 1) );
/* sort vertices by color (ascending within a color)
* 1) count colors
* 2) determine offsets of color ranges
* 3) sort by color
*
* note: color is 1-based */
for ( i = 0; i < n; ++i )
{
++color_top[color[i]];
}
for ( i = 1; i < n + 1; ++i )
{
color_top[i] += color_top[i - 1];
}
for ( i = 0; i < n; ++i )
{
permuted_row_col[color_top[color[i] - 1]] = i;
++color_top[color[i] - 1];
}
/* invert mapping to get map from current row/column to permuted (new) row/column */
for ( i = 0; i < n; ++i )
{
permuted_row_col_inv[permuted_row_col[i]] = i;
}
}
/* Apply permutation Q^T*x or Q*x based on graph coloring
*
* 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( real * const x, const unsigned int n, const int invert_map,
const TRIANGULARITY tri )
{
unsigned int i;
Kurt A. O'Hearn
committed
if ( x_p == NULL )
Kurt A. O'Hearn
committed
if ( (x_p = (real*) malloc(sizeof(real) * n)) == NULL )
{
fprintf( stderr, "not enough memory for permuting vector. terminating.\n" );
exit( INSUFFICIENT_MEMORY );
}
Kurt A. O'Hearn
committed
if ( invert_map == TRUE )
Kurt A. O'Hearn
committed
mapping = permuted_row_col_inv;
}
else
{
mapping = permuted_row_col;
}
}
Kurt A. O'Hearn
committed
#pragma omp barrier
Kurt A. O'Hearn
committed
#pragma omp for schedule(static)
for ( i = 0; i < n; ++i )
{
x_p[i] = x[mapping[i]];
}
Kurt A. O'Hearn
committed
#pragma omp master
{
memcpy( x, x_p, sizeof(real) * n );
}
Kurt A. O'Hearn
committed
#pragma omp barrier
}
Kurt A. O'Hearn
committed
/* Apply permutation Q^T*(LU)*Q based on graph coloring
*
* 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( sparse_matrix * const LU, const TRIANGULARITY tri )
{
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) */
memset( color_top, 0, sizeof(unsigned int) * (LU->n + 1) );
if ( tri == LOWER )
{
for ( i = 0; i < LU->n; ++i )
Kurt A. O'Hearn
committed
nr = permuted_row_col_inv[i];
for ( pj = LU->start[i]; pj < LU->start[i + 1]; ++pj )
Kurt A. O'Hearn
committed
nc = permuted_row_col_inv[LU->j[pj]];
Kurt A. O'Hearn
committed
if ( nc <= nr )
{
++color_top[nr + 1];
}
/* correct entries to maintain triangularity (lower) */
else
{
++color_top[nc + 1];
Kurt A. O'Hearn
committed
}
else
{
for ( i = LU->n - 1; i >= 0; --i )
Kurt A. O'Hearn
committed
nr = permuted_row_col_inv[i];
for ( pj = LU->start[i]; pj < LU->start[i + 1]; ++pj )
Kurt A. O'Hearn
committed
nc = permuted_row_col_inv[LU->j[pj]];
Kurt A. O'Hearn
committed
if ( nc >= nr )
{
++color_top[nr + 1];
}
/* correct entries to maintain triangularity (upper) */
else
{
++color_top[nc + 1];
Kurt A. O'Hearn
committed
}
Kurt A. O'Hearn
committed
for ( i = 1; i < LU->n + 1; ++i )
{
color_top[i] += color_top[i - 1];
}
Kurt A. O'Hearn
committed
memcpy( LUtemp->start, 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 = permuted_row_col_inv[i];
for ( pj = LU->start[i]; pj < LU->start[i + 1]; ++pj )
Kurt A. O'Hearn
committed
nc = permuted_row_col_inv[LU->j[pj]];
Kurt A. O'Hearn
committed
if ( nc <= nr )
{
LUtemp->j[color_top[nr]] = nc;
LUtemp->val[color_top[nr]] = LU->val[pj];
++color_top[nr];
}
/* correct entries to maintain triangularity (lower) */
else
{
LUtemp->j[color_top[nc]] = nr;
LUtemp->val[color_top[nc]] = LU->val[pj];
++color_top[nc];
Kurt A. O'Hearn
committed
}
else
{
for ( i = LU->n - 1; i >= 0; --i )
Kurt A. O'Hearn
committed
nr = permuted_row_col_inv[i];
for ( pj = LU->start[i]; pj < LU->start[i + 1]; ++pj )
Kurt A. O'Hearn
committed
nc = permuted_row_col_inv[LU->j[pj]];
Kurt A. O'Hearn
committed
if ( nc >= nr )
{
LUtemp->j[color_top[nr]] = nc;
LUtemp->val[color_top[nr]] = LU->val[pj];
++color_top[nr];
}
/* correct entries to maintain triangularity (upper) */
else
{
LUtemp->j[color_top[nc]] = nr;
LUtemp->val[color_top[nc]] = LU->val[pj];
++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)
*
* H: symmetric, lower triangular portion only, stored in CSR format;
* H is permuted in-place
*/
void setup_graph_coloring( sparse_matrix * const H )
{
if ( color == NULL )
{
/* internal storage for graph coloring (global to facilitate simultaneous access to OpenMP threads) */
if ( (color = (unsigned int*) malloc(sizeof(unsigned int) * H->n)) == NULL ||
(to_color =(unsigned int*) malloc(sizeof(unsigned int) * H->n)) == NULL ||
(conflict = (unsigned int*) malloc(sizeof(unsigned int) * H->n)) == NULL ||
(recolor = (unsigned int*) malloc(sizeof(unsigned int) * H->n)) == NULL ||
(color_top = (unsigned int*) malloc(sizeof(unsigned int) * (H->n + 1))) == NULL ||
(permuted_row_col = (unsigned int*) malloc(sizeof(unsigned int) * H->n)) == NULL ||
(permuted_row_col_inv = (unsigned int*) malloc(sizeof(unsigned int) * H->n)) == NULL ||
(y_p = (real*) malloc(sizeof(real) * H->n)) == NULL ||
(Allocate_Matrix( &H_full, H->n, 2 * H->m - H->n ) == FAILURE ) )
Kurt A. O'Hearn
committed
fprintf( stderr, "not enough memory for graph coloring. terminating.\n" );
exit( INSUFFICIENT_MEMORY );
Kurt A. O'Hearn
committed
compute_H_full( H );
Kurt A. O'Hearn
committed
graph_coloring( H_full, LOWER );
sort_colors( H_full->n, LOWER );
Kurt A. O'Hearn
committed
permute_matrix( H, 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 */
Kurt A. O'Hearn
committed
static void jacobi_iter( 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 = 0, ei = 0, iter;
iter = 0;
#pragma omp master
{
if ( Dinv_b == NULL )
{
if ( (Dinv_b = (real*) malloc(sizeof(real) * R->n)) == NULL )
Kurt A. O'Hearn
committed
{
fprintf( stderr, "not enough memory for Jacobi iteration matrices. terminating.\n" );
exit( INSUFFICIENT_MEMORY );
Kurt A. O'Hearn
committed
}
}
if ( rp == NULL )
{
if ( (rp = (real*) malloc(sizeof(real) * R->n)) == NULL )
Kurt A. O'Hearn
committed
{
fprintf( stderr, "not enough memory for Jacobi iteration matrices. terminating.\n" );
exit( INSUFFICIENT_MEMORY );
Kurt A. O'Hearn
committed
}
}
if ( rp2 == NULL )
{
if ( (rp2 = (real*) malloc(sizeof(real) * R->n)) == NULL )
Kurt A. O'Hearn
committed
{
fprintf( stderr, "not enough memory for Jacobi iteration matrices. terminating.\n" );
exit( INSUFFICIENT_MEMORY );
Kurt A. O'Hearn
committed
}
#pragma omp barrier
Vector_MakeZero( rp, R->n );
Kurt A. O'Hearn
committed
/* precompute and cache, as invariant in loop below */
#pragma omp for schedule(static)
for ( i = 0; i < R->n; ++i )
{
Dinv_b[i] = Dinv[i] * b[i];
}
do
{
// x_{k+1} = G*x_{k} + Dinv*b;
#pragma omp for schedule(guided)
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
}
rp2[i] = 0.;
for ( k = si; k < ei; ++k )
Kurt A. O'Hearn
committed
{
rp2[i] += R->val[k] * rp[R->j[k]];
Kurt A. O'Hearn
committed
}
rp2[i] *= -Dinv[i];
rp2[i] += Dinv_b[i];
}
#pragma omp master
{