LCOV - code coverage report
Current view: top level - eps/impls/external/arpack - arpack.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 143 156 91.7 %
Date: 2019-10-20 05:41:02 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       3             :    SLEPc - Scalable Library for Eigenvalue Problem Computations
       4             :    Copyright (c) 2002-2019, Universitat Politecnica de Valencia, Spain
       5             : 
       6             :    This file is part of SLEPc.
       7             :    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
       8             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       9             : */
      10             : /*
      11             :    This file implements a wrapper to the ARPACK package
      12             : */
      13             : 
      14             : #include <slepc/private/epsimpl.h>
      15             : #include "arpack.h"
      16             : 
      17           8 : PetscErrorCode EPSSetUp_ARPACK(EPS eps)
      18             : {
      19             :   PetscErrorCode ierr;
      20             :   PetscInt       ncv;
      21             :   PetscBool      istrivial;
      22           8 :   EPS_ARPACK     *ar = (EPS_ARPACK*)eps->data;
      23             : 
      24           8 :   PetscFunctionBegin;
      25           8 :   if (eps->ncv) {
      26           3 :     if (eps->ncv<eps->nev+2) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The value of ncv must be at least nev+2");
      27           5 :   } else eps->ncv = PetscMin(PetscMax(20,2*eps->nev+1),eps->n); /* set default value of ncv */
      28           8 :   if (eps->mpd) { ierr = PetscInfo(eps,"Warning: parameter mpd ignored\n");CHKERRQ(ierr); }
      29           8 :   if (!eps->max_it) eps->max_it = PetscMax(300,(PetscInt)(2*eps->n/eps->ncv));
      30           8 :   if (!eps->which) eps->which = EPS_LARGEST_MAGNITUDE;
      31             : 
      32           8 :   ncv = eps->ncv;
      33             : #if defined(PETSC_USE_COMPLEX)
      34             :   ierr = PetscFree(ar->rwork);CHKERRQ(ierr);
      35             :   ierr = PetscMalloc1(ncv,&ar->rwork);CHKERRQ(ierr);
      36             :   ierr = PetscLogObjectMemory((PetscObject)eps,ncv*sizeof(PetscReal));CHKERRQ(ierr);
      37             :   ierr = PetscBLASIntCast(3*ncv*ncv+5*ncv,&ar->lworkl);CHKERRQ(ierr);
      38             :   ierr = PetscFree(ar->workev);CHKERRQ(ierr);
      39             :   ierr = PetscMalloc1(3*ncv,&ar->workev);CHKERRQ(ierr);
      40             :   ierr = PetscLogObjectMemory((PetscObject)eps,3*ncv*sizeof(PetscScalar));CHKERRQ(ierr);
      41             : #else
      42           8 :   if (eps->ishermitian) {
      43           6 :     ierr = PetscBLASIntCast(ncv*(ncv+8),&ar->lworkl);CHKERRQ(ierr);
      44             :   } else {
      45           2 :     ierr = PetscBLASIntCast(3*ncv*ncv+6*ncv,&ar->lworkl);CHKERRQ(ierr);
      46           2 :     ierr = PetscFree(ar->workev);CHKERRQ(ierr);
      47           2 :     ierr = PetscMalloc1(3*ncv,&ar->workev);CHKERRQ(ierr);
      48           2 :     ierr = PetscLogObjectMemory((PetscObject)eps,3*ncv*sizeof(PetscScalar));CHKERRQ(ierr);
      49             :   }
      50             : #endif
      51           8 :   ierr = PetscFree(ar->workl);CHKERRQ(ierr);
      52           8 :   ierr = PetscMalloc1(ar->lworkl,&ar->workl);CHKERRQ(ierr);
      53           8 :   ierr = PetscLogObjectMemory((PetscObject)eps,ar->lworkl*sizeof(PetscScalar));CHKERRQ(ierr);
      54           8 :   ierr = PetscFree(ar->select);CHKERRQ(ierr);
      55           8 :   ierr = PetscMalloc1(ncv,&ar->select);CHKERRQ(ierr);
      56           8 :   ierr = PetscLogObjectMemory((PetscObject)eps,ncv*sizeof(PetscBool));CHKERRQ(ierr);
      57           8 :   ierr = PetscFree(ar->workd);CHKERRQ(ierr);
      58           8 :   ierr = PetscMalloc1(3*eps->nloc,&ar->workd);CHKERRQ(ierr);
      59           8 :   ierr = PetscLogObjectMemory((PetscObject)eps,3*eps->nloc*sizeof(PetscScalar));CHKERRQ(ierr);
      60             : 
      61           8 :   if (eps->extraction) { ierr = PetscInfo(eps,"Warning: extraction type ignored\n");CHKERRQ(ierr); }
      62             : 
      63           8 :   if (eps->balance!=EPS_BALANCE_NONE) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Balancing not supported in the Arpack interface");
      64           8 :   if (eps->arbitrary) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Arbitrary selection of eigenpairs not supported in this solver");
      65           8 :   if (eps->stopping!=EPSStoppingBasic) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"External packages do not support user-defined stopping test");
      66             : 
      67           8 :   ierr = EPSAllocateSolution(eps,0);CHKERRQ(ierr);
      68           8 :   ierr = EPS_SetInnerProduct(eps);CHKERRQ(ierr);
      69           8 :   ierr = EPSSetWorkVecs(eps,2);CHKERRQ(ierr);
      70             : 
      71           8 :   ierr = RGIsTrivial(eps->rg,&istrivial);CHKERRQ(ierr);
      72           8 :   if (!istrivial) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"This solver does not support region filtering");
      73           8 :   PetscFunctionReturn(0);
      74             : }
      75             : 
      76           8 : PetscErrorCode EPSSolve_ARPACK(EPS eps)
      77             : {
      78             :   PetscErrorCode ierr;
      79           8 :   EPS_ARPACK     *ar = (EPS_ARPACK*)eps->data;
      80           8 :   char           bmat[1],howmny[] = "A";
      81             :   const char     *which;
      82             :   PetscBLASInt   n,iparam[11],ipntr[14],ido,info,nev,ncv;
      83             : #if !defined(PETSC_HAVE_MPIUNI)
      84             :   PetscBLASInt   fcomm;
      85             : #endif
      86             :   PetscScalar    sigmar,*pV,*resid;
      87           8 :   Vec            x,y,w = eps->work[0];
      88             :   Mat            A;
      89             :   PetscBool      isSinv,isShift,rvec;
      90             : #if !defined(PETSC_USE_COMPLEX)
      91           8 :   PetscScalar    sigmai = 0.0;
      92             : #endif
      93             : 
      94           8 :   PetscFunctionBegin;
      95           8 :   ierr = PetscBLASIntCast(eps->nev,&nev);CHKERRQ(ierr);
      96           8 :   ierr = PetscBLASIntCast(eps->ncv,&ncv);CHKERRQ(ierr);
      97             : #if !defined(PETSC_HAVE_MPIUNI)
      98           8 :   ierr = PetscBLASIntCast(MPI_Comm_c2f(PetscObjectComm((PetscObject)eps)),&fcomm);CHKERRQ(ierr);
      99             : #endif
     100           8 :   ierr = PetscBLASIntCast(eps->nloc,&n);CHKERRQ(ierr);
     101           8 :   ierr = EPSGetStartVector(eps,0,NULL);CHKERRQ(ierr);
     102           8 :   ierr = BVSetActiveColumns(eps->V,0,0);CHKERRQ(ierr);  /* just for deflation space */
     103           8 :   ierr = BVCopyVec(eps->V,0,eps->work[1]);CHKERRQ(ierr);
     104           8 :   ierr = BVGetArray(eps->V,&pV);CHKERRQ(ierr);
     105           8 :   ierr = VecGetArray(eps->work[1],&resid);CHKERRQ(ierr);
     106             : 
     107           8 :   ido  = 0;            /* first call to reverse communication interface */
     108           8 :   info = 1;            /* indicates an initial vector is provided */
     109           8 :   iparam[0] = 1;       /* use exact shifts */
     110           8 :   ierr = PetscBLASIntCast(eps->max_it,&iparam[2]);CHKERRQ(ierr);  /* max Arnoldi iterations */
     111           8 :   iparam[3] = 1;       /* blocksize */
     112           8 :   iparam[4] = 0;       /* number of converged Ritz values */
     113             : 
     114             :   /*
     115             :      Computational modes ([]=not supported):
     116             :             symmetric    non-symmetric    complex
     117             :         1     1  'I'        1  'I'         1  'I'
     118             :         2     3  'I'        3  'I'         3  'I'
     119             :         3     2  'G'        2  'G'         2  'G'
     120             :         4     3  'G'        3  'G'         3  'G'
     121             :         5   [ 4  'G' ]    [ 3  'G' ]
     122             :         6   [ 5  'G' ]    [ 4  'G' ]
     123             :    */
     124           8 :   ierr = PetscObjectTypeCompare((PetscObject)eps->st,STSINVERT,&isSinv);CHKERRQ(ierr);
     125           8 :   ierr = PetscObjectTypeCompare((PetscObject)eps->st,STSHIFT,&isShift);CHKERRQ(ierr);
     126           8 :   ierr = STGetShift(eps->st,&sigmar);CHKERRQ(ierr);
     127           8 :   ierr = STGetMatrix(eps->st,0,&A);CHKERRQ(ierr);
     128           8 :   ierr = MatCreateVecsEmpty(A,&x,&y);CHKERRQ(ierr);
     129             : 
     130           8 :   if (isSinv) {
     131             :     /* shift-and-invert mode */
     132           1 :     iparam[6] = 3;
     133           1 :     if (eps->ispositive) bmat[0] = 'G';
     134           1 :     else bmat[0] = 'I';
     135           7 :   } else if (isShift && eps->ispositive) {
     136             :     /* generalized shift mode with B positive definite */
     137           1 :     iparam[6] = 2;
     138           1 :     bmat[0] = 'G';
     139             :   } else {
     140             :     /* regular mode */
     141           6 :     if (eps->ishermitian && eps->isgeneralized) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Spectral transformation not supported by ARPACK hermitian solver");
     142           6 :     iparam[6] = 1;
     143           6 :     bmat[0] = 'I';
     144             :   }
     145             : 
     146             : #if !defined(PETSC_USE_COMPLEX)
     147           8 :     if (eps->ishermitian) {
     148           6 :       switch (eps->which) {
     149             :         case EPS_TARGET_MAGNITUDE:
     150             :         case EPS_LARGEST_MAGNITUDE:  which = "LM"; break;
     151           0 :         case EPS_SMALLEST_MAGNITUDE: which = "SM"; break;
     152             :         case EPS_TARGET_REAL:
     153           0 :         case EPS_LARGEST_REAL:       which = "LA"; break;
     154           2 :         case EPS_SMALLEST_REAL:      which = "SA"; break;
     155           0 :         default: SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_WRONG,"Wrong value of eps->which");
     156             :       }
     157             :     } else {
     158             : #endif
     159           2 :       switch (eps->which) {
     160             :         case EPS_TARGET_MAGNITUDE:
     161             :         case EPS_LARGEST_MAGNITUDE:  which = "LM"; break;
     162           0 :         case EPS_SMALLEST_MAGNITUDE: which = "SM"; break;
     163             :         case EPS_TARGET_REAL:
     164           1 :         case EPS_LARGEST_REAL:       which = "LR"; break;
     165           0 :         case EPS_SMALLEST_REAL:      which = "SR"; break;
     166             :         case EPS_TARGET_IMAGINARY:
     167           0 :         case EPS_LARGEST_IMAGINARY:  which = "LI"; break;
     168           0 :         case EPS_SMALLEST_IMAGINARY: which = "SI"; break;
     169           0 :         default: SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_WRONG,"Wrong value of eps->which");
     170             :       }
     171             : #if !defined(PETSC_USE_COMPLEX)
     172             :     }
     173             : #endif
     174             : 
     175             :   do {
     176             : 
     177             : #if !defined(PETSC_USE_COMPLEX)
     178        1493 :     if (eps->ishermitian) {
     179         695 :       PetscStackCall("ARPACKsaupd",ARPACKsaupd_(&fcomm,&ido,bmat,&n,which,&nev,&eps->tol,resid,&ncv,pV,&n,iparam,ipntr,ar->workd,ar->workl,&ar->lworkl,&info));
     180             :     } else {
     181         798 :       PetscStackCall("ARPACKnaupd",ARPACKnaupd_(&fcomm,&ido,bmat,&n,which,&nev,&eps->tol,resid,&ncv,pV,&n,iparam,ipntr,ar->workd,ar->workl,&ar->lworkl,&info));
     182             :     }
     183             : #else
     184             :     PetscStackCall("ARPACKnaupd",ARPACKnaupd_(&fcomm,&ido,bmat,&n,which,&nev,&eps->tol,resid,&ncv,pV,&n,iparam,ipntr,ar->workd,ar->workl,&ar->lworkl,ar->rwork,&info));
     185             : #endif
     186             : 
     187        1493 :     if (ido == -1 || ido == 1 || ido == 2) {
     188        1485 :       if (ido == 1 && iparam[6] == 3 && bmat[0] == 'G') {
     189             :         /* special case for shift-and-invert with B semi-positive definite*/
     190           0 :         ierr = VecPlaceArray(x,&ar->workd[ipntr[2]-1]);CHKERRQ(ierr);
     191             :       } else {
     192        1485 :         ierr = VecPlaceArray(x,&ar->workd[ipntr[0]-1]);CHKERRQ(ierr);
     193             :       }
     194        1485 :       ierr = VecPlaceArray(y,&ar->workd[ipntr[1]-1]);CHKERRQ(ierr);
     195             : 
     196        1485 :       if (ido == -1) {
     197             :         /* Y = OP * X for for the initialization phase to
     198             :            force the starting vector into the range of OP */
     199           1 :         ierr = STApply(eps->st,x,y);CHKERRQ(ierr);
     200        1484 :       } else if (ido == 2) {
     201             :         /* Y = B * X */
     202         579 :         ierr = BVApplyMatrix(eps->V,x,y);CHKERRQ(ierr);
     203             :       } else { /* ido == 1 */
     204         905 :         if (iparam[6] == 3 && bmat[0] == 'G') {
     205             :           /* Y = OP * X for shift-and-invert with B semi-positive definite */
     206           0 :           ierr = STMatSolve(eps->st,x,y);CHKERRQ(ierr);
     207         905 :         } else if (iparam[6] == 2) {
     208             :           /* X=A*X Y=B^-1*X for shift with B positive definite */
     209         189 :           ierr = MatMult(A,x,y);CHKERRQ(ierr);
     210         189 :           if (sigmar != 0.0) {
     211           0 :             ierr = BVApplyMatrix(eps->V,x,w);CHKERRQ(ierr);
     212           0 :             ierr = VecAXPY(y,sigmar,w);CHKERRQ(ierr);
     213             :           }
     214         189 :           ierr = VecCopy(y,x);CHKERRQ(ierr);
     215         189 :           ierr = STMatSolve(eps->st,x,y);CHKERRQ(ierr);
     216             :         } else {
     217             :           /* Y = OP * X */
     218         716 :           ierr = STApply(eps->st,x,y);CHKERRQ(ierr);
     219             :         }
     220         905 :         ierr = BVOrthogonalizeVec(eps->V,y,NULL,NULL,NULL);CHKERRQ(ierr);
     221             :       }
     222             : 
     223        1485 :       ierr = VecResetArray(x);CHKERRQ(ierr);
     224        1485 :       ierr = VecResetArray(y);CHKERRQ(ierr);
     225           8 :     } else if (ido != 99) SETERRQ1(PetscObjectComm((PetscObject)eps),PETSC_ERR_LIB,"Internal error in ARPACK reverse comunication interface (ido=%d)",ido);
     226             : 
     227        1493 :   } while (ido != 99);
     228             : 
     229           8 :   eps->nconv = iparam[4];
     230           8 :   eps->its = iparam[2];
     231             : 
     232           8 :   if (info==3) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_LIB,"No shift could be applied in xxAUPD.\nTry increasing the size of NCV relative to NEV");
     233           8 :   else if (info!=0 && info!=1) SETERRQ1(PetscObjectComm((PetscObject)eps),PETSC_ERR_LIB,"Error reported by ARPACK subroutine xxAUPD (%d)",(int)info);
     234             : 
     235           8 :   rvec = PETSC_TRUE;
     236             : 
     237           8 :   if (eps->nconv > 0) {
     238             : #if !defined(PETSC_USE_COMPLEX)
     239           8 :     if (eps->ishermitian) {
     240           6 :       PetscStackCall("ARPACKseupd",ARPACKseupd_(&fcomm,&rvec,howmny,ar->select,eps->eigr,pV,&n,&sigmar,bmat,&n,which,&nev,&eps->tol,resid,&ncv,pV,&n,iparam,ipntr,ar->workd,ar->workl,&ar->lworkl,&info));
     241             :     } else {
     242           2 :       PetscStackCall("ARPACKneupd",ARPACKneupd_(&fcomm,&rvec,howmny,ar->select,eps->eigr,eps->eigi,pV,&n,&sigmar,&sigmai,ar->workev,bmat,&n,which,&nev,&eps->tol,resid,&ncv,pV,&n,iparam,ipntr,ar->workd,ar->workl,&ar->lworkl,&info));
     243             :     }
     244             : #else
     245             :     PetscStackCall("ARPACKneupd",ARPACKneupd_(&fcomm,&rvec,howmny,ar->select,eps->eigr,pV,&n,&sigmar,ar->workev,bmat,&n,which,&nev,&eps->tol,resid,&ncv,pV,&n,iparam,ipntr,ar->workd,ar->workl,&ar->lworkl,ar->rwork,&info));
     246             : #endif
     247           8 :     if (info!=0) SETERRQ1(PetscObjectComm((PetscObject)eps),PETSC_ERR_LIB,"Error reported by ARPACK subroutine xxEUPD (%d)",(int)info);
     248             :   }
     249             : 
     250           8 :   ierr = BVRestoreArray(eps->V,&pV);CHKERRQ(ierr);
     251           8 :   ierr = VecRestoreArray(eps->work[1],&resid);CHKERRQ(ierr);
     252           8 :   if (eps->nconv >= eps->nev) eps->reason = EPS_CONVERGED_TOL;
     253           0 :   else eps->reason = EPS_DIVERGED_ITS;
     254             : 
     255           8 :   ierr = VecDestroy(&x);CHKERRQ(ierr);
     256           8 :   ierr = VecDestroy(&y);CHKERRQ(ierr);
     257           8 :   PetscFunctionReturn(0);
     258             : }
     259             : 
     260           8 : PetscErrorCode EPSBackTransform_ARPACK(EPS eps)
     261             : {
     262             :   PetscErrorCode ierr;
     263             :   PetscBool      isSinv;
     264             : 
     265           8 :   PetscFunctionBegin;
     266           8 :   ierr = PetscObjectTypeCompare((PetscObject)eps->st,STSINVERT,&isSinv);CHKERRQ(ierr);
     267           8 :   if (!isSinv) {
     268           7 :     ierr = EPSBackTransform_Default(eps);CHKERRQ(ierr);
     269             :   }
     270           8 :   PetscFunctionReturn(0);
     271             : }
     272             : 
     273           7 : PetscErrorCode EPSReset_ARPACK(EPS eps)
     274             : {
     275             :   PetscErrorCode ierr;
     276           7 :   EPS_ARPACK     *ar = (EPS_ARPACK*)eps->data;
     277             : 
     278           7 :   PetscFunctionBegin;
     279           7 :   ierr = PetscFree(ar->workev);CHKERRQ(ierr);
     280           7 :   ierr = PetscFree(ar->workl);CHKERRQ(ierr);
     281           7 :   ierr = PetscFree(ar->select);CHKERRQ(ierr);
     282           7 :   ierr = PetscFree(ar->workd);CHKERRQ(ierr);
     283             : #if defined(PETSC_USE_COMPLEX)
     284             :   ierr = PetscFree(ar->rwork);CHKERRQ(ierr);
     285             : #endif
     286           7 :   PetscFunctionReturn(0);
     287             : }
     288             : 
     289           7 : PetscErrorCode EPSDestroy_ARPACK(EPS eps)
     290             : {
     291             :   PetscErrorCode ierr;
     292             : 
     293           7 :   PetscFunctionBegin;
     294           7 :   ierr = PetscFree(eps->data);CHKERRQ(ierr);
     295           7 :   PetscFunctionReturn(0);
     296             : }
     297             : 
     298           7 : SLEPC_EXTERN PetscErrorCode EPSCreate_ARPACK(EPS eps)
     299             : {
     300             :   EPS_ARPACK     *ctx;
     301             :   PetscErrorCode ierr;
     302             : 
     303           7 :   PetscFunctionBegin;
     304           7 :   ierr = PetscNewLog(eps,&ctx);CHKERRQ(ierr);
     305           7 :   eps->data = (void*)ctx;
     306             : 
     307           7 :   eps->ops->solve          = EPSSolve_ARPACK;
     308           7 :   eps->ops->setup          = EPSSetUp_ARPACK;
     309           7 :   eps->ops->destroy        = EPSDestroy_ARPACK;
     310           7 :   eps->ops->reset          = EPSReset_ARPACK;
     311           7 :   eps->ops->backtransform  = EPSBackTransform_ARPACK;
     312           7 :   PetscFunctionReturn(0);
     313             : }
     314             : 

Generated by: LCOV version 1.13