예제 #1
0
파일: zpcgr.c 프로젝트: fransklaver/SPOOLES
int
zpcgr (
   int             n_matrixSize,
   int             type,
   int             symmetryflag,
   InpMtx          *mtxA,
   FrontMtx        *Precond,
   DenseMtx        *mtxX,
   DenseMtx        *mtxB,
   int             itermax,
   double          convergetol,
   int             msglvl,
   FILE            *msgFile 
 )
{
Chv             *chv, *rootchv ;
ChvManager      *chvmanager ;
DenseMtx        *mtxZ ;
DenseMtx        *vecP, *vecR, *vecQ ;
DenseMtx        *vecX,  *vecZ  ;
double          Alpha[2], Beta[2], Rho[2], Rho0[2], Rtmp[2];
double          Init_norm, ratio, Res_norm ;
double          t1, t2,  cpus[9] ;
double          one[2] = {1.0, 0.0}, zero[2] = {0.0, 0.0} ;
double          Tiny[2] = {0.1e-28, 0.0};
int             Iter, neqns;
int             stats[6] ;

if (symmetryflag != SPOOLES_HERMITIAN){
      fprintf(msgFile, "\n\n Fatal Error, \n"
                    " Matrix is not Hermitian in ZPCGR !!") ;
       spoolesFatal();
    };

neqns = n_matrixSize;

/*
   --------------------
   init the vectors in ZPCGR
   --------------------
*/
vecP = DenseMtx_new() ;
DenseMtx_init(vecP, type, 0, 0, neqns, 1, 1, neqns) ;

vecR = DenseMtx_new() ;
DenseMtx_init(vecR, type, 0, 0, neqns, 1, 1, neqns) ;

vecX = DenseMtx_new() ;
DenseMtx_init(vecX, type, 0, 0, neqns, 1, 1, neqns) ;

vecQ = DenseMtx_new() ;
DenseMtx_init(vecQ, type, 0, 0, neqns, 1, 1, neqns) ;

vecZ = DenseMtx_new() ;
DenseMtx_init(vecZ, type, 0, 0, neqns, 1, 1, neqns) ;


/*
   --------------------------
   Initialize the iterations
   --------------------------
*/
Init_norm = DenseMtx_twoNormOfColumn (mtxB, 0);
if ( Init_norm == 0.0 ){
  Init_norm = 1.0; };
ratio = 1.0;
DenseMtx_zero(vecX) ;
DenseMtx_colCopy (vecR, 0, mtxB, 0);

MARKTIME(t1) ;
Iter = 0;

/*
   ------------------------------
    Main Loop of the iterations
   ------------------------------
*/

while ( ratio > convergetol && Iter <= itermax )
  {
    Iter++;
/*                                                         */
    FrontMtx_solve(Precond, vecZ, vecR, Precond->manager,
               cpus, msglvl, msgFile) ;

    DenseMtx_colDotProduct(vecR, 0, vecZ, 0, Rho);
    if ( Rho[0] == 0.0 & Rho[1] == 0.0){
      fprintf(stderr, "\n   breakdown in ZPCGR !! "
	      "\n Fatal error   \n");
      spoolesFatal(); 
    }
    
/*                                                         */
    if ( Iter == 1 ) {
      DenseMtx_colCopy (vecP, 0, vecZ, 0);
    } else {
      zdiv(Rho, Rho0, Beta);
      DenseMtx_colGenAxpy (Beta, vecP, 0, one, vecZ, 0);
    };

    InpMtx_herm_gmmm(mtxA, zero, vecQ, one, vecP) ;
    DenseMtx_colDotProduct (vecP, 0, vecQ,0, Rtmp);
    zdiv(Rho, Rtmp, Alpha);

    DenseMtx_colGenAxpy (one, vecX, 0, Alpha, vecP, 0);
    Rtmp[0] = -Alpha[0];
    Rtmp[1] = -Alpha[1];
    DenseMtx_colGenAxpy (one, vecR, 0, Rtmp, vecQ, 0);
    Rho0[0]  = Rho[0];
    Rho0[1]  = Rho[1];
    /*                                                */
    Res_norm = DenseMtx_twoNormOfColumn (vecR, 0);
    ratio = Res_norm/Init_norm;
    fprintf(msgFile, "\n\n At iteration %d"
	    "  the convergence ratio is  %12.4e", 
	    Iter, ratio) ;
  }
/*            End of while loop              */
MARKTIME(t2) ;
fprintf(msgFile, "\n CPU  : Converges in time: %8.3f ", t2 - t1) ;
fprintf(msgFile, "\n # iterations = %d", Iter) ;

fprintf(msgFile, "\n\n after ZPCGR") ;
DenseMtx_colCopy (mtxX, 0, vecX, 0);

/*
 
   ------------------------
   free the working storage
   ------------------------
*/

DenseMtx_free(vecP) ;
DenseMtx_free(vecR) ;
DenseMtx_free(vecX) ;
DenseMtx_free(vecQ) ;
DenseMtx_free(vecZ) ;


fprintf(msgFile, "\n") ;

return(1) ; }
예제 #2
0
파일: tfqmrl.c 프로젝트: damiannz/spooles
int
tfqmrl (
   int             n_matrixSize,
   int             type,
   int             symmetryflag,
   InpMtx          *mtxA,
   FrontMtx        *Precond,
   DenseMtx        *mtxX,
   DenseMtx        *mtxB,
   int             itermax,
   double          convergetol,
   int             msglvl,
   FILE            *msgFile
 )
{
Chv             *chv, *rootchv ;
ChvManager      *chvmanager ;
DenseMtx        *vecD, *vecR, *vecT, *vecU1, *vecU2,  *vecV, *vecW;
DenseMtx        *vecX, *vecY1, *vecY2 ;
double          Alpha, Beta, Cee, Eta, Rho, Rho_new ;
double          Sigma, Tau, Theta;
double          Init_norm,  ratio,  Res_norm;
double          error_trol, m, Rtmp;
double          t1, t2,  cpus[9] ;
double          one[2] = {1.0, 0.0}, zero[2] ={0.0, 0.0} ;
double          Tiny = 0.1e-28;
int             Iter, Imv, neqns;
int             stats[6] ;

neqns = n_matrixSize;


/*
   --------------------
   init the vectors in TFQMRL
   --------------------
*/
vecD = DenseMtx_new() ;
DenseMtx_init(vecD, type, 0, 0, neqns, 1, 1, neqns) ;

vecR = DenseMtx_new() ;
DenseMtx_init(vecR, type, 0, 0, neqns, 1, 1, neqns) ;


vecT = DenseMtx_new() ;
DenseMtx_init(vecT, type, 0, 0, neqns, 1, 1, neqns) ;

vecU1 = DenseMtx_new() ;
DenseMtx_init(vecU1, type, 0, 0, neqns, 1, 1, neqns) ;

vecU2 = DenseMtx_new() ;
DenseMtx_init(vecU2, type, 0, 0, neqns, 1, 1, neqns) ;

vecV = DenseMtx_new() ;
DenseMtx_init(vecV, type, 0, 0, neqns, 1, 1, neqns) ;

vecW = DenseMtx_new() ;
DenseMtx_init(vecW, type, 0, 0, neqns, 1, 1, neqns) ;

vecX = DenseMtx_new() ;
DenseMtx_init(vecX, type, 0, 0, neqns, 1, 1, neqns) ;

vecY1 = DenseMtx_new() ;
DenseMtx_init(vecY1, type, 0, 0, neqns, 1, 1, neqns) ;

vecY2 = DenseMtx_new() ;
DenseMtx_init(vecY2, type, 0, 0, neqns, 1, 1, neqns) ;


/*
   --------------------------
   Initialize the iterations
   --------------------------
*/
/*          ----     Set initial guess as zero  ----               */
DenseMtx_zero(vecX) ;

DenseMtx_colCopy(vecT, 0, mtxB, 0);
/*                                                         */
    FrontMtx_solve(Precond, vecR, vecT, Precond->manager,
               cpus, msglvl, msgFile) ;
/*                                                      */

  
Init_norm = DenseMtx_twoNormOfColumn(vecR,0);
if ( Init_norm == 0.0 ){
  Init_norm = 1.0; 
};
error_trol = Init_norm * convergetol ;

  fprintf(msgFile, "\n TFQMRL Initial norml: %6.2e ", Init_norm ) ;
  fprintf(msgFile, "\n TFQMRL Conveg. Control: %7.3e ", convergetol ) ;
  fprintf(msgFile, "\n TFQMRL Convergen Control: %7.3e ",error_trol ) ;

DenseMtx_zero(vecD) ;
DenseMtx_zero(vecU1) ;
DenseMtx_zero(vecU2) ;
DenseMtx_zero(vecY2) ;

/*       DenseMtx_copy(vecR, mtxB);              */
DenseMtx_colCopy(vecW, 0, vecR, 0);
DenseMtx_colCopy(vecY1, 0, vecR, 0);

Iter = 0;
Imv  = 0;


  switch ( symmetryflag ) {
  case SPOOLES_SYMMETRIC : 
    InpMtx_sym_gmmm(mtxA, zero, vecT, one, vecY1) ;
    break ;
  case SPOOLES_HERMITIAN :
    fprintf(msgFile, "\n TFQMRL Matrix type wrong");
    fprintf(msgFile, "\n Fatal error");
    goto end;
  case SPOOLES_NONSYMMETRIC :
      InpMtx_nonsym_gmmm(mtxA, zero, vecT, one, vecY1) ;
    break ;
  default :
    fprintf(msgFile, "\n TFQMRL Matrix type wrong");
    fprintf(msgFile, "\n Fatal error");
    goto end;
  }
/*                                                         */
    FrontMtx_solve(Precond, vecV, vecT, Precond->manager,
               cpus, msglvl, msgFile) ;
/*                                                      */
    Imv++;
    DenseMtx_colCopy(vecU1, 0, vecV, 0);
/*

*/
Theta   = 0.0;
Eta     = 0.0;
Tau     = Init_norm ;
Rho     = Tau * Tau ;

/*
   ------------------------------
   TFQMRL   Iteration start
   ------------------------------
*/

MARKTIME(t1) ;


while (  Iter <= itermax )
  {
    Iter++;
    DenseMtx_colDotProduct(vecV, 0, vecR, 0, &Sigma);

    if (Sigma == 0){
      fprintf(msgFile, "\n\n Fatal Error, \n"
	      "  TFQMRL Breakdown, Sigma = 0 !!") ;
      Imv = -1;
      goto end;
    };

    Alpha   = Rho/Sigma;
/*
    ----------------
    Odd step
    ---------------
*/
	
    m      = 2 * Iter - 1;
    Rtmp=-Alpha;
    DenseMtx_colGenAxpy(one, vecW, 0, &Rtmp, vecU1, 0);
    Rtmp   = Theta * Theta * Eta / Alpha ;
    DenseMtx_colGenAxpy(&Rtmp, vecD, 0, one, vecY1, 0);
    Theta  = DenseMtx_twoNormOfColumn(vecW,0)/Tau;
    Cee    = 1.0/sqrt(1.0 + Theta*Theta);
    Tau    = Tau * Theta * Cee ;
    Eta    = Cee * Cee * Alpha ;
    DenseMtx_colGenAxpy(one, vecX, 0, &Eta, vecD, 0);
      fprintf(msgFile, "\n\n Odd step at %d", Imv);
      fprintf(msgFile, " \n Tau is   : %7.3e", Tau) ; 
/*                   
        Debug purpose:  Check the convergence history
	for the true residual norm
*/
/*
      DenseMtx_zero(vecT) ;
      InpMtx_nonsym_mmm(mtxA, vecT, one, vecX) ;
      DenseMtx_sub(vecT, mtxB) ;
      Rtmp = DenseMtx_twoNormOfColumn(vecT,0);
      fprintf(msgFile, "\n TFQMRL Residual norm: %6.2e ", Rtmp) ;
*/
 
/*
    ----------------
    Convergence Test
    ---------------
*/
    if (Tau * sqrt(m + 1)  <= error_trol ) {
/*                                                             */
      DenseMtx_colCopy(mtxX, 0, vecX, 0);
/*
      DenseMtx_zero(vecT) ;
      InpMtx_nonsym_mmm(mtxA, vecT, one, mtxX) ;
*/
      switch ( symmetryflag ) {
      case SPOOLES_SYMMETRIC : 
	InpMtx_sym_gmmm(mtxA, zero, vecT, one, mtxX) ;
	break ;
      case SPOOLES_HERMITIAN :
	fprintf(msgFile, "\n TFQMRL Matrix type wrong");
	fprintf(msgFile, "\n Fatal error");
	goto end;
      case SPOOLES_NONSYMMETRIC :
	InpMtx_nonsym_gmmm(mtxA, zero, vecT, one, mtxX) ;
	break ;
      default :
	fprintf(msgFile, "\n TFQMRL Matrix type wrong");
	fprintf(msgFile, "\n Fatal error");
	goto end;
      }
      DenseMtx_sub(vecT, mtxB) ;
      Rtmp = DenseMtx_twoNormOfColumn(vecT,0);
      fprintf(msgFile, "\n TFQMRL Residual norm: %6.2e ", Rtmp) ;
      MARKTIME(t2) ;
      fprintf(msgFile, "\n CPU  : Converges in time: %8.3f ", t2 - t1) ;
      fprintf(msgFile, "\n # iterations = %d", Imv) ;
      fprintf(msgFile, "\n\n after TFQMRL") ;  
      goto end;
    };

/*
    ----------------
    Even step
    ---------------
*/
    DenseMtx_colCopy(vecY2, 0, vecY1, 0);
    Rtmp=-Alpha;
    DenseMtx_colGenAxpy(one, vecY2, 0, &Rtmp, vecV, 0);
/*
    DenseMtx_zero(vecT) ;
    InpMtx_nonsym_mmm(mtxA, vecT, one, vecY2) ;
*/
      switch ( symmetryflag ) {
      case SPOOLES_SYMMETRIC : 
	InpMtx_sym_gmmm(mtxA, zero, vecT, one, vecY2) ;
	break ;
      case SPOOLES_HERMITIAN :
	fprintf(msgFile, "\n TFQMRL Matrix type wrong");
	fprintf(msgFile, "\n Fatal error");
	goto end;
      case SPOOLES_NONSYMMETRIC :
	InpMtx_nonsym_gmmm(mtxA, zero, vecT, one, vecY2) ;
	break ;
      default :
	fprintf(msgFile, "\n TFQMRL Matrix type wrong");
	fprintf(msgFile, "\n Fatal error");
	goto end;
      }
    
    FrontMtx_solve(Precond, vecU2, vecT, Precond->manager,
		   cpus, msglvl, msgFile) ;
    Imv++;
  
    m      = 2 * Iter ;
    Rtmp = -Alpha;
    DenseMtx_colGenAxpy(one, vecW, 0, &Rtmp, vecU2, 0);
    Rtmp   = Theta * Theta * Eta / Alpha ;
    DenseMtx_colGenAxpy(&Rtmp, vecD, 0, one, vecY2, 0);
    Theta  = DenseMtx_twoNormOfColumn(vecW,0)/Tau;
    Cee    = 1.0/sqrt(1.0 + Theta*Theta);
    Tau    = Tau * Theta * Cee ;
    Eta    = Cee * Cee * Alpha ;
    DenseMtx_colGenAxpy(one, vecX, 0, &Eta, vecD, 0);
      fprintf(msgFile, "\n\n Even step at %d", Imv) ;  
    
/*
    ----------------
    Convergence Test for even step
    ---------------
*/
    if (Tau * sqrt(m + 1)  <= error_trol ) {
      DenseMtx_colCopy(mtxX, 0, vecX, 0);
/*
      DenseMtx_zero(vecT) ;
      InpMtx_nonsym_mmm(mtxA, vecT, one, mtxX) ;
*/
      switch ( symmetryflag ) {
      case SPOOLES_SYMMETRIC : 
	InpMtx_sym_gmmm(mtxA, zero, vecT, one, mtxX) ;
	break ;
      case SPOOLES_HERMITIAN :
	fprintf(msgFile, "\n TFQMRL Matrix type wrong");
	fprintf(msgFile, "\n Fatal error");
	goto end;
      case SPOOLES_NONSYMMETRIC :
	InpMtx_nonsym_gmmm(mtxA, zero, vecT, one, mtxX) ;
	break ;
      default :
	fprintf(msgFile, "\n TFQMRL Matrix type wrong");
	fprintf(msgFile, "\n Fatal error");
	goto end;
      }

      DenseMtx_sub(vecT, mtxB) ;
      Rtmp = DenseMtx_twoNormOfColumn(vecT,0);
      fprintf(msgFile, "\n TFQMRL Residual norm: %6.2e ", Rtmp) ;
      MARKTIME(t2) ;
      fprintf(msgFile, "\n CPU  : Converges in time: %8.3f ", t2 - t1) ;
      fprintf(msgFile, "\n # iterations = %d", Imv) ;

      fprintf(msgFile, "\n\n after TFQMRL") ;  
      goto end;
    };



    if (Rho == 0){
      fprintf(msgFile, "\n\n Fatal Error, \n"
	      "  TFQMRL Breakdown, Rho = 0 !!") ;
      Imv = -1;
      goto end;
    };

    DenseMtx_colDotProduct(vecW, 0, vecR, 0, &Rho_new);
    Beta    = Rho_new / Rho;
    Rho     = Rho_new ;

    DenseMtx_colCopy(vecY1, 0, vecY2, 0);
    DenseMtx_colGenAxpy(&Beta, vecY1, 0, one, vecW, 0);
/*
    DenseMtx_zero(vecT) ;
    InpMtx_nonsym_mmm(mtxA, vecT, one, vecY1) ;
*/
      switch ( symmetryflag ) {
      case SPOOLES_SYMMETRIC : 
	InpMtx_sym_gmmm(mtxA, zero, vecT, one, vecY1) ;
	break ;
      case SPOOLES_HERMITIAN :
	fprintf(msgFile, "\n TFQMRL Matrix type wrong");
	fprintf(msgFile, "\n Fatal error");
	goto end;
      case SPOOLES_NONSYMMETRIC :
	InpMtx_nonsym_gmmm(mtxA, zero, vecT, one, vecY1) ;
	break ;
      default :
	fprintf(msgFile, "\n TFQMRL Matrix type wrong");
	fprintf(msgFile, "\n Fatal error");
	goto end;
      }

    FrontMtx_solve(Precond, vecU1, vecT, Precond->manager,
		   cpus, msglvl, msgFile) ;
    Imv++;

/*                                                         */
    DenseMtx_colCopy(vecT, 0, vecU2, 0);
    DenseMtx_colGenAxpy(one, vecT, 0, &Beta, vecV, 0);
    DenseMtx_colCopy(vecV, 0, vecT, 0);
    DenseMtx_colGenAxpy(&Beta, vecV, 0, one, vecU1, 0);

    Rtmp = Tau*sqrt(m + 1)/Init_norm ;
    fprintf(msgFile, "\n\n At iteration %d"
	    "  the convergence ratio is  %12.4e", 
	    Imv, Rtmp) ;

  }
/*            End of while loop              */
MARKTIME(t2) ;
fprintf(msgFile, "\n CPU  : Total iteration time is : %8.3f ", t2 - t1) ;
fprintf(msgFile, "\n # iterations = %d", Imv) ;
fprintf(msgFile, "\n\n  TFQMRL did not Converge !") ;

fprintf(msgFile, "\n\n after TFQMRL") ;
DenseMtx_colCopy(mtxX, 0, vecX, 0);

/*
 
   ------------------------
   free the working storage
   ------------------------
*/
 end:
DenseMtx_free(vecD) ;
DenseMtx_free(vecR) ;
DenseMtx_free(vecT) ;
DenseMtx_free(vecU1) ;
DenseMtx_free(vecU2) ;
DenseMtx_free(vecV) ;
DenseMtx_free(vecW) ;
DenseMtx_free(vecX) ;
DenseMtx_free(vecY1) ;
DenseMtx_free(vecY2) ;

fprintf(msgFile, "\n") ;

return(Imv) ; }
예제 #3
0
/*--------------------------------------------------------------------*/
int
main ( int argc, char *argv[] )
/*
   -----------------------------------------------
   test the DenseMtx_twoNormOfColumn routine.

   when msglvl > 1, the output of this program
   can be fed into Matlab to check for errors

   created -- 98dec03, ycp
   -----------------------------------------------
*/
{
DenseMtx   *A ;
double     t1, t2, value ;
Drand      *drand ;
FILE       *msgFile ;
int        inc1, inc2, jcol, msglvl, nrow, ncol, seed, type ;

if ( argc != 10 ) {
   fprintf(stdout, 
"\n\n usage : %s msglvl msgFile type nrow ncol inc1 inc2 "
"\n         , jcol, seed "
"\n    msglvl  -- message level"
"\n    msgFile -- message file"
"\n    type    -- entries type"
"\n      1 -- real"
"\n      2 -- complex"
"\n    nrow    -- # of rows "
"\n    ncol    -- # of columns "
"\n    inc1    -- row increment "
"\n    inc2    -- column increment "
"\n    jcol    -- vector x: j-th column of A "
"\n    seed    -- random number seed"
"\n", argv[0]) ;
   return(0) ;
}
if ( (msglvl = atoi(argv[1])) < 0 ) {
   fprintf(stderr, "\n message level must be positive\n") ;
   exit(-1) ;
}
if ( strcmp(argv[2], "stdout") == 0 ) {
   msgFile = stdout ;
} else if ( (msgFile = fopen(argv[2], "a")) == NULL ) {
   fprintf(stderr, "\n unable to open file %s\n", argv[2]) ;
   return(-1) ;
}
type = atoi(argv[3]) ;
nrow = atoi(argv[4]) ;
ncol = atoi(argv[5]) ;
inc1 = atoi(argv[6]) ;
inc2 = atoi(argv[7]) ;
if (   type < 1 || type > 2 || nrow < 0 || ncol < 0 
    || inc1 < 1 || inc2 < 1 ) {
   fprintf(stderr, 
       "\n fatal error, type %d, nrow %d, ncol %d, inc1 %d, inc2 %d",
       type, nrow, ncol, inc1, inc2) ;
   exit(-1) ;
}
jcol = atoi(argv[8]) ;
seed = atoi(argv[9]) ;
fprintf(msgFile, "\n\n %% %s :"
        "\n %% msglvl  = %d"
        "\n %% msgFile = %s"
        "\n %% type    = %d"
        "\n %% nrow    = %d"
        "\n %% ncol    = %d"
        "\n %% inc1    = %d"
        "\n %% inc2    = %d"
        "\n %% jcol    = %d"
        "\n %% seed    = %d"
        "\n",
        argv[0], msglvl, argv[2], type, nrow, ncol, inc1, inc2, jcol, seed) ;
/*
   ----------------------------
   initialize the matrix object
   ----------------------------
*/
MARKTIME(t1) ;
A = DenseMtx_new() ;
DenseMtx_init(A, type, 0, 0, nrow, ncol, inc1, inc2) ;
MARKTIME(t2) ;
fprintf(msgFile, "\n %% CPU : %.3f to initialize matrix object",
        t2 - t1) ;
MARKTIME(t1) ;
drand = Drand_new() ;
Drand_setSeed(drand, seed) ;
seed++ ;
Drand_setUniform(drand, -1.0, 1.0) ;
DenseMtx_fillRandomEntries(A, drand) ;
MARKTIME(t2) ;
fprintf(msgFile, 
      "\n %% CPU : %.3f to fill matrix with random numbers", t2 - t1) ;
if ( msglvl > 3 ) {
   fprintf(msgFile, "\n matrix A") ;
   DenseMtx_writeForHumanEye(A, msgFile) ;
}
if ( msglvl > 1 ) {
   fprintf(msgFile, "\n %% matrix A") ;
   fprintf(msgFile, "\n nrow = %d ;", nrow) ;
   fprintf(msgFile, "\n ncol = %d ;", ncol) ;
   fprintf(msgFile, "\n");
   DenseMtx_writeForMatlab(A, "A", msgFile) ;
}
/*
   --------------------------
   compute the frobenius norm 
   --------------------------
*/
  value = DenseMtx_twoNormOfColumn(A,jcol);

if ( msglvl > 1 ) {
   fprintf(msgFile, "\n %% Two Norm = %e", value) ;
   fprintf(msgFile, "\n");
   fflush(msgFile) ;
}
/*
   ------------------------
   free the working storage
   ------------------------
*/
DenseMtx_free(A) ;
Drand_free(drand) ;

return(1) ; }