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 "GMRES.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;
/* sparse matrix-vector product Ax=b
* where:
* A: lower triangular matrix
* x: vector
* b: vector (result) */
static void Sparse_MatVec( const sparse_matrix * const A,
Kurt A. O'Hearn
committed
const real * const x, real * const b )
#ifdef _OPENMP
static real *b_local;
unsigned int tid;
#endif
Kurt A. O'Hearn
committed
#pragma omp parallel \
Kurt A. O'Hearn
committed
default(none) shared(n, b_local) private(si, ei, H, i, j, k, tid)
Kurt A. O'Hearn
committed
#ifdef _OPENMP
tid = omp_get_thread_num();
#pragma omp master
{
Kurt A. O'Hearn
committed
/* 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 )
{
Kurt A. O'Hearn
committed
exit( INSUFFICIENT_MEMORY );
}
Kurt A. O'Hearn
committed
Vector_MakeZero( (real * const)b_local, omp_get_num_threads() * n );
}
Kurt A. O'Hearn
committed
#pragma omp barrier
#endif
#pragma omp for schedule(guided)
for ( i = 0; i < n; ++i )
{
si = A->start[i];
ei = A->start[i + 1] - 1;
for ( k = si; k < ei; ++k )
{
Kurt A. O'Hearn
committed
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
Kurt A. O'Hearn
committed
b_local[tid * n + i] += A->val[k] * x[i];
#else
Kurt A. O'Hearn
committed
b[i] += A->val[k] * x[i];
#endif
}
#ifdef _OPENMP
#pragma omp for schedule(guided)
for ( i = 0; i < n; ++i )
for ( j = 0; j < omp_get_num_threads(); ++j )
Kurt A. O'Hearn
committed
{
b[i] += b_local[j * n + i];
Kurt A. O'Hearn
committed
}
Kurt A. O'Hearn
committed
}
/* sparse matrix-vector product Gx=b (for Jacobi iteration),
* followed by vector addition of D^{-1}b,
* where G = (I - D^{-1}R) (G not explicitly computed and stored)
* R: strictly triangular matrix (diagonals not used)
* tri: triangularity of A (lower/upper)
* D^{-1} (D_inv): inverse of the diagonal of R
* b: vector (result)
* D^{-1}b (Dinv_b): precomputed vector-vector product */
static void Sparse_MatVec_Vector_Add( const sparse_matrix * const R,
Kurt A. O'Hearn
committed
const TRIANGULARITY tri, const real * const Dinv,
const real * const x, real * const b, const real * const Dinv_b)
int i, k, si = 0, ei = 0;
#ifdef _OPENMP
static real *b_local;
unsigned int tid;
#endif
Vector_MakeZero( b, R->n );
#pragma omp parallel \
Kurt A. O'Hearn
committed
default(none) shared(b_local) private(si, ei, i, k, tid)
#ifdef _OPENMP
tid = omp_get_thread_num();
#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() * R->n * sizeof(real))) == NULL )
{
Kurt A. O'Hearn
committed
exit( INSUFFICIENT_MEMORY );
}
}
Vector_MakeZero( b_local, omp_get_num_threads() * R->n );
}
#pragma omp barrier
#endif
#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 if (tri == UPPER)
{
si = R->start[i] + 1;
ei = R->start[i + 1];
}
for ( k = si; k < ei; ++k )
{
#ifdef _OPENMP
Kurt A. O'Hearn
committed
b_local[tid * R->n + i] += R->val[k] * x[R->j[k]];
#else
Kurt A. O'Hearn
committed
b[i] += R->val[k] * x[R->j[k]];
#endif
}
#ifdef _OPENMP
b_local[tid * R->n + i] *= -Dinv[i];
#else
b[i] *= -Dinv[i];
#endif
#ifdef _OPENMP
#pragma omp for schedule(guided)
for ( i = 0; i < R->n; ++i )
for ( k = 0; k < omp_get_num_threads(); ++k )
{
b[i] += b_local[k * R->n + i];
}
b[i] += Dinv_b[i];
}
#endif
Kurt A. O'Hearn
committed
static void diag_pre_app( const real * const Hdia_inv, const real * const y,
real * const x, const int N )
Kurt A. O'Hearn
committed
unsigned int i;
Kurt A. O'Hearn
committed
#pragma omp parallel for schedule(guided) \
default(none) private(i)
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)
*
* 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 )
Kurt A. O'Hearn
committed
if ( tri == LOWER )
Kurt A. O'Hearn
committed
for ( i = 0; i < LU->n; ++i )
Kurt A. O'Hearn
committed
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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];
}
}
else
{
for ( i = LU->n - 1; i >= 0; --i )
{
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
/* 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 )
Kurt A. O'Hearn
committed
int i, j, pj, local_row, local_level, levels;
static int levels_L = 1, levels_U = 1;
static unsigned int *row_levels_L = NULL, *level_rows_L = NULL, *level_rows_cnt_L = NULL;
static unsigned int *row_levels_U = NULL, *level_rows_U = NULL, *level_rows_cnt_U = NULL;
unsigned int *row_levels, *level_rows, *level_rows_cnt;
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;
}
if ( row_levels == NULL || level_rows == NULL || level_rows_cnt == NULL )
{
if ( (row_levels = (unsigned int*) malloc(LU->n * sizeof(unsigned int))) == NULL
|| (level_rows = (unsigned int*) malloc(LU->n * LU->n * sizeof(unsigned int))) == NULL
|| (level_rows_cnt = (unsigned int*) malloc(LU->n * sizeof(unsigned int))) == NULL )
{
fprintf( stderr, "Not enough space for triangular solve via level scheduling. Terminating...\n" );
exit( INSUFFICIENT_MEMORY );
}
}
/* find levels (row dependencies in substitutions) */
Kurt A. O'Hearn
committed
if ( find_levels )
Kurt A. O'Hearn
committed
memset( row_levels, 0, LU->n * sizeof( unsigned int) );
memset( level_rows_cnt, 0, LU->n * sizeof( unsigned int) );
if ( tri == LOWER )
Kurt A. O'Hearn
committed
for ( i = 0; i < LU->n; ++i )
Kurt A. O'Hearn
committed
local_level = 0;
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 + 1 );
row_levels[i] = local_level;
level_rows[local_level * LU->n + level_rows_cnt[local_level]] = i;
++level_rows_cnt[local_level];
Kurt A. O'Hearn
committed
else
Kurt A. O'Hearn
committed
for ( i = LU->n - 1; i >= 0; --i )
Kurt A. O'Hearn
committed
local_level = 0;
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 + 1 );
row_levels[i] = local_level;
level_rows[local_level * LU->n + level_rows_cnt[local_level]] = i;
++level_rows_cnt[local_level];
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
}
}
}
/* perform substitutions by level */
if ( tri == LOWER )
{
for ( i = 0; i < levels; ++i )
{
#pragma omp parallel for schedule(guided) \
default(none) private(j, pj, local_row) shared(stderr, i, level_rows_cnt, level_rows)
for ( j = 0; j < level_rows_cnt[i]; ++j )
{
local_row = level_rows[i * LU->n + 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 parallel for schedule(guided) \
default(none) private(j, pj, local_row) shared(i, level_rows_cnt, level_rows)
for ( j = 0; j < level_rows_cnt[i]; ++j )
{
local_row = level_rows[i * LU->n + 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
/* 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;
}
/* 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 = 0;
#ifdef _OPENMP
static real *b_local;
unsigned int tid;
#endif
static real *Dinv_b, *rp, *rp2, *rp3;
Kurt A. O'Hearn
committed
#pragma omp parallel \
Kurt A. O'Hearn
committed
default(none) shared(b_local, Dinv_b, rp, rp2, rp3, iter, stderr) private(si, ei, i, k, tid)
Kurt A. O'Hearn
committed
#ifdef _OPENMP
tid = omp_get_thread_num();
#endif
#pragma omp master
{
Kurt A. O'Hearn
committed
/* keep b_local for program duration to avoid allocate/free
* overhead per Sparse_MatVec call*/
if ( Dinv_b == NULL )
{
Kurt A. O'Hearn
committed
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" );
Kurt A. O'Hearn
committed
exit( INSUFFICIENT_MEMORY );
Kurt A. O'Hearn
committed
}
}
if ( rp == NULL )
{
Kurt A. O'Hearn
committed
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" );
Kurt A. O'Hearn
committed
exit( INSUFFICIENT_MEMORY );
Kurt A. O'Hearn
committed
}
}
if ( rp2 == NULL )
{
Kurt A. O'Hearn
committed
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" );
Kurt A. O'Hearn
committed
exit( INSUFFICIENT_MEMORY );
Kurt A. O'Hearn
committed
}
}
#ifdef _OPENMP
if ( b_local == NULL )
{
if ( (b_local = (real*) malloc( omp_get_num_threads() * R->n * sizeof(real))) == NULL )
{
fprintf( stderr, "not enough memory for Jacobi iteration matrices. terminating.\n" );
Kurt A. O'Hearn
committed
exit( INSUFFICIENT_MEMORY );
Kurt A. O'Hearn
committed
}
}
#endif
Kurt A. O'Hearn
committed
Vector_MakeZero( rp, R->n );
Kurt A. O'Hearn
committed
}
#pragma omp barrier
/* precompute and cache, as invariant in loop below */
#pragma omp for schedule(guided)
Kurt A. O'Hearn
committed
for ( i = 0; i < R->n; ++i )
{
Kurt A. O'Hearn
committed
Dinv_b[i] = Dinv[i] * b[i];
}
Kurt A. O'Hearn
committed
#pragma omp barrier
do
{
Kurt A. O'Hearn
committed
// x_{k+1} = G*x_{k} + Dinv*b;
//Sparse_MatVec_Vector_Add( (sparse_matrix*)R, tri, Dinv, rp, rp2, Dinv_b );
#pragma omp master
{
Vector_MakeZero( rp2, R->n );
#ifdef _OPENMP
Vector_MakeZero( b_local, omp_get_num_threads() * R->n );
#endif
}
#pragma omp barrier
Kurt A. O'Hearn
committed
#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;
}
Kurt A. O'Hearn
committed
else
Kurt A. O'Hearn
committed
{
si = R->start[i] + 1;
ei = R->start[i + 1];
}
for ( k = si; k < ei; ++k )
{
#ifdef _OPENMP
Kurt A. O'Hearn
committed
b_local[tid * R->n + i] += R->val[k] * rp[R->j[k]];
Kurt A. O'Hearn
committed
#else
Kurt A. O'Hearn
committed
rp2[i] += R->val[k] * rp[R->j[k]];
Kurt A. O'Hearn
committed
#endif
}
#ifdef _OPENMP
b_local[tid * R->n + i] *= -Dinv[i];
#else
rp2[i] *= -Dinv[i];
#endif
}
#pragma omp barrier
Kurt A. O'Hearn
committed
#pragma omp for schedule(guided)
for ( i = 0; i < R->n; ++i )
{
#ifdef _OPENMP
for ( k = 0; k < omp_get_num_threads(); ++k )
{
rp2[i] += b_local[k * R->n + i];
}
#endif
Kurt A. O'Hearn
committed
rp2[i] += Dinv_b[i];
}
#pragma omp master
{
rp3 = rp;
rp = rp2;
rp2 = rp3;
++iter;
}
#pragma omp barrier
}
while ( iter < maxiter );
Kurt A. O'Hearn
committed
Vector_Copy( x, rp, R->n );
Kurt A. O'Hearn
committed
/* Solve triangular system LU*x = y using level scheduling
*
* workspace: data struct containing matrices, lower/upper triangular, stored in CSR
* control: data struct containing parameters
Kurt A. O'Hearn
committed
* y: constants in linear system (RHS)
* x: solution
* fresh_pre: parameter indicating if this is a newly computed (fresh) preconditioner
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) */
void apply_preconditioner( const static_storage * const workspace, const control_params * const control,
const real * const y, real * const x, const int fresh_pre )
Kurt A. O'Hearn
committed
int i, si;
static real *Dinv_L = NULL, *Dinv_U = NULL;
switch ( control->pre_app_type )
Kurt A. O'Hearn
committed
{
case NONE_PA:
break;
case TRI_SOLVE_PA:
switch ( control->pre_comp_type )
Kurt A. O'Hearn
committed
{
case DIAG_PC:
diag_pre_app( workspace->Hdia_inv, y, x, workspace->H->n );
break;
case ICHOLT_PC:
case ILU_PAR_PC:
Kurt A. O'Hearn
committed
case ILUT_PAR_PC:
tri_solve( workspace->L, y, x, LOWER );
tri_solve( workspace->U, y, x, UPPER );
Kurt A. O'Hearn
committed
break;
default:
fprintf( stderr, "Unrecognized preconditioner application method. Terminating...\n" );
exit( INVALID_INPUT );
Kurt A. O'Hearn
committed
break;
}
break;
case TRI_SOLVE_LEVEL_SCHED_PA:
switch ( control->pre_comp_type )
Kurt A. O'Hearn
committed
{
case DIAG_PC:
diag_pre_app( workspace->Hdia_inv, y, x, workspace->H->n );
break;
case ICHOLT_PC:
case ILU_PAR_PC:
Kurt A. O'Hearn
committed
case ILUT_PAR_PC:
tri_solve_level_sched( workspace->L, y, x, LOWER, fresh_pre );
tri_solve_level_sched( workspace->U, y, x, UPPER, fresh_pre );
Kurt A. O'Hearn
committed
break;
default:
fprintf( stderr, "Unrecognized preconditioner application method. Terminating...\n" );
exit( INVALID_INPUT );
Kurt A. O'Hearn
committed
break;
}
break;
case JACOBI_ITER_PA:
switch ( control->pre_comp_type )
Kurt A. O'Hearn
committed
{
case DIAG_PC:
fprintf( stderr, "Unsupported preconditioner computation/application method combination. Terminating...\n" );
exit( INVALID_INPUT );
break;
case ICHOLT_PC:
case ILU_PAR_PC:
Kurt A. O'Hearn
committed
case ILUT_PAR_PC:
if ( Dinv_L == NULL )
Kurt A. O'Hearn
committed
{
if ( (Dinv_L = (real*) malloc(sizeof(real) * workspace->L->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
}
}
/* construct D^{-1}_L */
if ( fresh_pre )
Kurt A. O'Hearn
committed
{
for ( i = 0; i < workspace->L->n; ++i )
{
si = workspace->L->start[i + 1] - 1;
Dinv_L[i] = 1. / workspace->L->val[si];
}
Kurt A. O'Hearn
committed
jacobi_iter( workspace->L, Dinv_L, y, x, LOWER, control->pre_app_jacobi_iters );
if ( Dinv_U == NULL )
{
if ( (Dinv_U = (real*) malloc(sizeof(real) * workspace->U->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
}
Kurt A. O'Hearn
committed
/* construct D^{-1}_U */
if ( fresh_pre )
for ( i = 0; i < workspace->U->n; ++i )
{
si = workspace->U->start[i];
Dinv_U[i] = 1. / workspace->U->val[si];
}
Kurt A. O'Hearn
committed
}
jacobi_iter( workspace->U, Dinv_U, y, x, UPPER, control->pre_app_jacobi_iters );
Kurt A. O'Hearn
committed
break;
default:
fprintf( stderr, "Unrecognized preconditioner application method. Terminating...\n" );
exit( INVALID_INPUT );
Kurt A. O'Hearn
committed
break;
}
break;
default:
fprintf( stderr, "Unrecognized preconditioner application method. Terminating...\n" );
exit( INVALID_INPUT );
break;
}
return;
}
/* generalized minimual residual iterative solver for sparse linear systems */
int GMRES( const static_storage * const workspace, const control_params * const control,
const sparse_matrix * const H, const real * const b, real tol, real * const x,
const FILE * const fout, real * const time, real * const spmv_time, const int fresh_pre )
Kurt A. O'Hearn
committed
{
int i, j, k, itr, N, si;
real cc, tmp1, tmp2, temp, bnorm, time_start;
Kurt A. O'Hearn
committed
if ( control->pre_comp_type == DIAG_PC )
{
/* apply preconditioner to RHS */
apply_preconditioner( workspace, control, b, workspace->b_prc, fresh_pre );
Kurt A. O'Hearn
committed
}
/* GMRES outer-loop */
for ( itr = 0; itr < MAX_ITR; ++itr )
{
/* calculate r0 */
Kurt A. O'Hearn
committed
time_start = Get_Time( );
Kurt A. O'Hearn
committed
*spmv_time += Get_Timing_Info( time_start );
if ( control->pre_comp_type == DIAG_PC )
{
time_start = Get_Time( );
apply_preconditioner( workspace, control, workspace->b_prm, workspace->b_prm, fresh_pre );
Kurt A. O'Hearn
committed
*time += Get_Timing_Info( time_start );
}
if ( control->pre_comp_type == DIAG_PC )
{
Vector_Sum( workspace->v[0], 1., workspace->b_prc, -1., workspace->b_prm, N );
}
else
{
Vector_Sum( workspace->v[0], 1., b, -1., workspace->b_prm, N );
}
if ( control->pre_comp_type != DIAG_PC )
{
time_start = Get_Time( );
apply_preconditioner( workspace, control, workspace->v[0], workspace->v[0],
itr == 0 ? fresh_pre : 0 );
Kurt A. O'Hearn
committed
*time += Get_Timing_Info( time_start );
}
workspace->g[0] = Norm( workspace->v[0], N );
Vector_Scale( workspace->v[0], 1. / workspace->g[0], workspace->v[0], N );
//fprintf( stderr, "res: %.15e\n", workspace->g[0] );
/* GMRES inner-loop */
for ( j = 0; j < RESTART && fabs(workspace->g[j]) / bnorm > tol; j++ )
{
/* matvec */
Kurt A. O'Hearn
committed
time_start = Get_Time( );
Kurt A. O'Hearn
committed
*spmv_time += Get_Timing_Info( time_start );
time_start = Get_Time( );
apply_preconditioner( workspace, control, workspace->v[j + 1], workspace->v[j + 1], 0 );
Kurt A. O'Hearn
committed
*time += Get_Timing_Info( time_start );
if ( control->pre_comp_type == DIAG_PC )
Kurt A. O'Hearn
committed
{
768
769
770
771
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
/* apply modified Gram-Schmidt to orthogonalize the new residual */
for( i = 0; i <= j; i++ )
{
workspace->h[i][j] = Dot( workspace->v[i], workspace->v[j+1], N );
Vector_Add( workspace->v[j+1], -workspace->h[i][j], workspace->v[i], N );
}
workspace->h[j+1][j] = Norm( workspace->v[j+1], N );
Vector_Scale( workspace->v[j+1], 1. / workspace->h[j+1][j], workspace->v[j+1], N );
// fprintf( stderr, "%d-%d: orthogonalization completed.\n", itr, j );
/* Givens rotations on the upper-Hessenberg matrix to make it U */
for( i = 0; i <= j; i++ )
{
if( i == j )
{
cc = SQRT( SQR(workspace->h[j][j])+SQR(workspace->h[j+1][j]) );
workspace->hc[j] = workspace->h[j][j] / cc;
workspace->hs[j] = workspace->h[j+1][j] / cc;
}
tmp1 = workspace->hc[i] * workspace->h[i][j] +
workspace->hs[i] * workspace->h[i+1][j];
tmp2 = -workspace->hs[i] * workspace->h[i][j] +
workspace->hc[i] * workspace->h[i+1][j];
workspace->h[i][j] = tmp1;
workspace->h[i+1][j] = tmp2;
}
/* apply Givens rotations to the rhs as well */
tmp1 = workspace->hc[j] * workspace->g[j];
tmp2 = -workspace->hs[j] * workspace->g[j];
workspace->g[j] = tmp1;
workspace->g[j+1] = tmp2;
Kurt A. O'Hearn
committed
}
else
/* apply modified Gram-Schmidt to orthogonalize the new residual */
for ( i = 0; i < j - 1; i++ )
workspace->h[i][j] = 0;
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
//for( i = 0; i <= j; i++ ) {
for ( i = MAX(j - 1, 0); i <= j; i++ )
{
workspace->h[i][j] = Dot( workspace->v[i], workspace->v[j + 1], N );
Vector_Add( workspace->v[j + 1], -workspace->h[i][j], workspace->v[i], N );
}
workspace->h[j + 1][j] = Norm( workspace->v[j + 1], N );
Vector_Scale( workspace->v[j + 1],
1. / workspace->h[j + 1][j], workspace->v[j + 1], N );
// fprintf( stderr, "%d-%d: orthogonalization completed.\n", itr, j );
/* Givens rotations on the upper-Hessenberg matrix to make it U */
for ( i = MAX(j - 1, 0); i <= j; i++ )
{
if ( i == j )
{
cc = SQRT( SQR(workspace->h[j][j]) + SQR(workspace->h[j + 1][j]) );
workspace->hc[j] = workspace->h[j][j] / cc;
workspace->hs[j] = workspace->h[j + 1][j] / cc;
}
tmp1 = workspace->hc[i] * workspace->h[i][j] +
workspace->hs[i] * workspace->h[i + 1][j];
tmp2 = -workspace->hs[i] * workspace->h[i][j] +
workspace->hc[i] * workspace->h[i + 1][j];
workspace->h[i][j] = tmp1;
workspace->h[i + 1][j] = tmp2;
}
/* apply Givens rotations to the rhs as well */
tmp1 = workspace->hc[j] * workspace->g[j];
tmp2 = -workspace->hs[j] * workspace->g[j];
workspace->g[j] = tmp1;
workspace->g[j + 1] = tmp2;
Kurt A. O'Hearn
committed
//fprintf( stderr, "h: " );
//for( i = 0; i <= j+1; ++i )
//fprintf( stderr, "%.6f ", workspace->h[i][j] );
//fprintf( stderr, "\n" );
//fprintf( stderr, "res: %.15e\n", workspace->g[j+1] );
}
Kurt A. O'Hearn
committed
/* TODO: solve using Jacobi iteration? */
/* solve Hy = g: H is now upper-triangular, do back-substitution */
for ( i = j - 1; i >= 0; i-- )
{
temp = workspace->g[i];
for ( k = j - 1; k > i; k-- )
Kurt A. O'Hearn
committed
{
Kurt A. O'Hearn
committed
}
workspace->y[i] = temp / workspace->h[i][i];
}
/* update x = x_0 + Vy */
Kurt A. O'Hearn
committed
Vector_MakeZero( workspace->p, N );
Kurt A. O'Hearn
committed
{
Vector_Add( workspace->p, workspace->y[i], workspace->v[i], N );
}
Vector_Add( x, 1., workspace->p, N );
/* stopping condition */
if ( fabs(workspace->g[j]) / bnorm <= tol )
Kurt A. O'Hearn
committed
{
Kurt A. O'Hearn
committed
}
// Sparse_MatVec( H, x, workspace->b_prm );
// for( i = 0; i < N; ++i )
// workspace->b_prm[i] *= workspace->Hdia_inv[i];
// fprintf( fout, "\n%10s%15s%15s\n", "b_prc", "b_prm", "x" );
// for( i = 0; i < N; ++i )
// fprintf( fout, "%10.5f%15.12f%15.12f\n",
// workspace->b_prc[i], workspace->b_prm[i], x[i] );*/
// fprintf(fout,"GMRES outer:%d, inner:%d iters - residual norm: %25.20f\n",
// itr, j, fabs( workspace->g[j] ) / bnorm );
// data->timing.matvec += itr * RESTART + j;
if ( itr >= MAX_ITR )
{
fprintf( stderr, "GMRES convergence failed\n" );
// return -1;
return itr * (RESTART + 1) + j + 1;
Kurt A. O'Hearn
committed
int GMRES_HouseHolder( const static_storage * const workspace, const control_params * const control,
const sparse_matrix * const H, const real * const b, real tol, real * const x,
const FILE * const fout, real * const time, real * const spmv_time, const int fresh_pre )
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
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
int i, j, k, itr, N;
real cc, tmp1, tmp2, temp, bnorm;
real v[10000], z[RESTART + 2][10000], w[RESTART + 2];
real u[RESTART + 2][10000];
N = H->n;
bnorm = Norm( b, N );
/* apply the diagonal pre-conditioner to rhs */
for ( i = 0; i < N; ++i )
workspace->b_prc[i] = b[i] * workspace->Hdia_inv[i];
// memset( x, 0, sizeof(real) * N );
/* GMRES outer-loop */
for ( itr = 0; itr < MAX_ITR; ++itr )
{
/* compute z = r0 */
Sparse_MatVec( H, x, workspace->b_prm );
for ( i = 0; i < N; ++i )
workspace->b_prm[i] *= workspace->Hdia_inv[i]; /* pre-conditioner */
Vector_Sum( z[0], 1., workspace->b_prc, -1., workspace->b_prm, N );
Vector_MakeZero( w, RESTART + 1 );
w[0] = Norm( z[0], N );
Vector_Copy( u[0], z[0], N );
u[0][0] += ( u[0][0] < 0.0 ? -1 : 1 ) * w[0];
Vector_Scale( u[0], 1 / Norm( u[0], N ), u[0], N );
w[0] *= ( u[0][0] < 0.0 ? 1 : -1 );
// fprintf( stderr, "\n\n%12.6f\n", w[0] );
/* GMRES inner-loop */
for ( j = 0; j < RESTART && fabs( w[j] ) / bnorm > tol; j++ )
{
/* compute v_j */
Vector_Scale( z[j], -2 * u[j][j], u[j], N );
z[j][j] += 1.; /* due to e_j */
for ( i = j - 1; i >= 0; --i )
Vector_Add( z[j] + i, -2 * Dot( u[i] + i, z[j] + i, N - i ), u[i] + i, N - i );
/* matvec */
Sparse_MatVec( H, z[j], v );
for ( k = 0; k < N; ++k )
v[k] *= workspace->Hdia_inv[k]; /* pre-conditioner */
for ( i = 0; i <= j; ++i )
Vector_Add( v + i, -2 * Dot( u[i] + i, v + i, N - i ), u[i] + i, N - i );
if ( !Vector_isZero( v + (j + 1), N - (j + 1) ) )
{
/* compute the HouseHolder unit vector u_j+1 */
for ( i = 0; i <= j; ++i )
u[j + 1][i] = 0;
Vector_Copy( u[j + 1] + (j + 1), v + (j + 1), N - (j + 1) );
u[j + 1][j + 1] += ( v[j + 1] < 0.0 ? -1 : 1 ) * Norm( v + (j + 1), N - (j + 1) );
Vector_Scale( u[j + 1], 1 / Norm( u[j + 1], N ), u[j + 1], N );
/* overwrite v with P_m+1 * v */
v[j + 1] -= 2 * Dot( u[j + 1] + (j + 1), v + (j + 1), N - (j + 1) ) * u[j + 1][j + 1];
Vector_MakeZero( v + (j + 2), N - (j + 2) );
// Vector_Add( v, -2 * Dot( u[j+1], v, N ), u[j+1], N );
}
/* prev Givens rots on the upper-Hessenberg matrix to make it U */
for ( i = 0; i < j; i++ )
{
tmp1 = workspace->hc[i] * v[i] + workspace->hs[i] * v[i + 1];
tmp2 = -workspace->hs[i] * v[i] + workspace->hc[i] * v[i + 1];
v[i] = tmp1;
v[i + 1] = tmp2;
}
/* apply the new Givens rotation to H and right-hand side */
if ( fabs(v[j + 1]) >= ALMOST_ZERO )
{
cc = SQRT( SQR( v[j] ) + SQR( v[j + 1] ) );