Actual source code: blzpack.c

slepc-3.6.3 2016-03-29
Report Typos and Errors
  1: /*
  2:    This file implements a wrapper to the BLZPACK package

  4:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  5:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  6:    Copyright (c) 2002-2015, Universitat Politecnica de Valencia, Spain

  8:    This file is part of SLEPc.

 10:    SLEPc is free software: you can redistribute it and/or modify it under  the
 11:    terms of version 3 of the GNU Lesser General Public License as published by
 12:    the Free Software Foundation.

 14:    SLEPc  is  distributed in the hope that it will be useful, but WITHOUT  ANY
 15:    WARRANTY;  without even the implied warranty of MERCHANTABILITY or  FITNESS
 16:    FOR  A  PARTICULAR PURPOSE. See the GNU Lesser General Public  License  for
 17:    more details.

 19:    You  should have received a copy of the GNU Lesser General  Public  License
 20:    along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
 21:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 22: */

 24: #include <slepc/private/epsimpl.h>    /*I "slepceps.h" I*/
 25: #include <../src/eps/impls/external/blzpack/blzpackp.h>

 27: PetscErrorCode EPSSolve_BLZPACK(EPS);

 29: const char* blzpack_error[33] = {
 30:   "",
 31:   "illegal data, LFLAG ",
 32:   "illegal data, dimension of (U), (V), (X) ",
 33:   "illegal data, leading dimension of (U), (V), (X) ",
 34:   "illegal data, leading dimension of (EIG) ",
 35:   "illegal data, number of required eigenpairs ",
 36:   "illegal data, Lanczos algorithm block size ",
 37:   "illegal data, maximum number of steps ",
 38:   "illegal data, number of starting vectors ",
 39:   "illegal data, number of eigenpairs provided ",
 40:   "illegal data, problem type flag ",
 41:   "illegal data, spectrum slicing flag ",
 42:   "illegal data, eigenvectors purification flag ",
 43:   "illegal data, level of output ",
 44:   "illegal data, output file unit ",
 45:   "illegal data, LCOMM (MPI or PVM) ",
 46:   "illegal data, dimension of ISTOR ",
 47:   "illegal data, convergence threshold ",
 48:   "illegal data, dimension of RSTOR ",
 49:   "illegal data on at least one PE ",
 50:   "ISTOR(3:14) must be equal on all PEs ",
 51:   "RSTOR(1:3) must be equal on all PEs ",
 52:   "not enough space in ISTOR to start eigensolution ",
 53:   "not enough space in RSTOR to start eigensolution ",
 54:   "illegal data, number of negative eigenvalues ",
 55:   "illegal data, entries of V ",
 56:   "illegal data, entries of X ",
 57:   "failure in computational subinterval ",
 58:   "file I/O error, blzpack.__.BQ ",
 59:   "file I/O error, blzpack.__.BX ",
 60:   "file I/O error, blzpack.__.Q ",
 61:   "file I/O error, blzpack.__.X ",
 62:   "parallel interface error "
 63: };

 67: PetscErrorCode EPSSetUp_BLZPACK(EPS eps)
 68: {
 70:   PetscInt       listor,lrstor,ncuv,k1,k2,k3,k4;
 71:   EPS_BLZPACK    *blz = (EPS_BLZPACK*)eps->data;
 72:   PetscBool      issinv,istrivial,flg;

 75:   if (eps->ncv) {
 76:     if (eps->ncv < PetscMin(eps->nev+10,eps->nev*2)) SETERRQ(PetscObjectComm((PetscObject)eps),0,"Warning: BLZpack recommends that ncv be larger than min(nev+10,nev*2)");
 77:   } else eps->ncv = PetscMin(eps->nev+10,eps->nev*2);
 78:   if (eps->mpd) { PetscInfo(eps,"Warning: parameter mpd ignored\n"); }
 79:   if (!eps->max_it) eps->max_it = PetscMax(1000,eps->n);

 81:   if (!blz->block_size) blz->block_size = 3;
 82:   if (!eps->ishermitian) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Requested method is only available for Hermitian problems");
 83:   if (eps->which==EPS_ALL) {
 84:     if (eps->inta==0.0 && eps->intb==0.0) SETERRQ(PetscObjectComm((PetscObject)eps),1,"Must define a computational interval when using EPS_ALL");
 85:     blz->slice = 1;
 86:   }
 87:   PetscObjectTypeCompare((PetscObject)eps->st,STSINVERT,&issinv);
 88:   if (blz->slice || eps->isgeneralized) {
 89:     if (!issinv) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Shift-and-invert ST is needed for generalized problems or spectrum slicing");
 90:   }
 91:   if (blz->slice) {
 92:     if (eps->intb >= PETSC_MAX_REAL) { /* right-open interval */
 93:       if (eps->inta <= PETSC_MIN_REAL) SETERRQ(PetscObjectComm((PetscObject)eps),1,"The defined computational interval should have at least one of their sides bounded");
 94:       STSetDefaultShift(eps->st,eps->inta);
 95:     } else {
 96:       STSetDefaultShift(eps->st,eps->intb);
 97:     }
 98:   }
 99:   if (!eps->which) {
100:     if (issinv) eps->which = EPS_TARGET_REAL;
101:     else eps->which = EPS_SMALLEST_REAL;
102:   }
103:   if ((issinv && eps->which!=EPS_TARGET_REAL && eps->which!=EPS_TARGET_MAGNITUDE && eps->which!=EPS_ALL) || (!issinv && eps->which!=EPS_SMALLEST_REAL)) SETERRQ(PetscObjectComm((PetscObject)eps),1,"Wrong value of eps->which");
104:   if (eps->arbitrary) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Arbitrary selection of eigenpairs not supported in this solver");

106:   k1 = PetscMin(eps->n,180);
107:   k2 = blz->block_size;
108:   k4 = PetscMin(eps->ncv,eps->n);
109:   k3 = 484+k1*(13+k1*2+k2+PetscMax(18,k2+2))+k2*k2*3+k4*2;

111:   listor = 123+k1*12;
112:   PetscFree(blz->istor);
113:   PetscMalloc1((17+listor),&blz->istor);
114:   PetscLogObjectMemory((PetscObject)eps,(17+listor)*sizeof(PetscBLASInt));
115:   PetscBLASIntCast(listor,&blz->istor[14]);

117:   if (blz->slice) lrstor = eps->nloc*(k2*4+k1*2+k4)+k3;
118:   else lrstor = eps->nloc*(k2*4+k1)+k3;
119: lrstor*=10;
120:   PetscFree(blz->rstor);
121:   PetscMalloc1((4+lrstor),&blz->rstor);
122:   PetscLogObjectMemory((PetscObject)eps,(4+lrstor)*sizeof(PetscReal));
123:   blz->rstor[3] = lrstor;

125:   ncuv = PetscMax(3,blz->block_size);
126:   PetscFree(blz->u);
127:   PetscMalloc1(ncuv*eps->nloc,&blz->u);
128:   PetscLogObjectMemory((PetscObject)eps,ncuv*eps->nloc*sizeof(PetscScalar));
129:   PetscFree(blz->v);
130:   PetscMalloc1(ncuv*eps->nloc,&blz->v);
131:   PetscLogObjectMemory((PetscObject)eps,ncuv*eps->nloc*sizeof(PetscScalar));

133:   PetscFree(blz->eig);
134:   PetscMalloc1(2*eps->ncv,&blz->eig);
135:   PetscLogObjectMemory((PetscObject)eps,2*eps->ncv*sizeof(PetscReal));

137:   if (eps->extraction) { PetscInfo(eps,"Warning: extraction type ignored\n"); }

139:   EPSAllocateSolution(eps,0);
140:   EPS_SetInnerProduct(eps);
141:   PetscObjectTypeCompare((PetscObject)eps->V,BVVECS,&flg);
142:   if (flg) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"This solver requires a BV with contiguous storage");
143:   RGIsTrivial(eps->rg,&istrivial);
144:   if (!istrivial) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"This solver does not support region filtering");

146:   /* dispatch solve method */
147:   eps->ops->solve = EPSSolve_BLZPACK;
148:   return(0);
149: }

153: PetscErrorCode EPSSolve_BLZPACK(EPS eps)
154: {
156:   EPS_BLZPACK    *blz = (EPS_BLZPACK*)eps->data;
157:   PetscInt       nn;
158:   PetscBLASInt   i,nneig,lflag,nvopu;
159:   Vec            x,y,v0;
160:   PetscScalar    sigma,*pV;
161:   Mat            A;
162:   KSP            ksp;
163:   PC             pc;

166:   VecCreateMPIWithArray(PetscObjectComm((PetscObject)eps),1,eps->nloc,PETSC_DECIDE,NULL,&x);
167:   VecCreateMPIWithArray(PetscObjectComm((PetscObject)eps),1,eps->nloc,PETSC_DECIDE,NULL,&y);
168:   EPSGetStartVector(eps,0,NULL);
169:   BVSetActiveColumns(eps->V,0,0);  /* just for deflation space */
170:   BVGetColumn(eps->V,0,&v0);
171:   VecGetArray(v0,&pV);

173:   if (eps->isgeneralized && !blz->slice) {
174:     STGetShift(eps->st,&sigma); /* shift of origin */
175:     blz->rstor[0]  = sigma;        /* lower limit of eigenvalue interval */
176:     blz->rstor[1]  = sigma;        /* upper limit of eigenvalue interval */
177:   } else {
178:     sigma = 0.0;
179:     blz->rstor[0]  = eps->inta;    /* lower limit of eigenvalue interval */
180:     blz->rstor[1]  = eps->intb;    /* upper limit of eigenvalue interval */
181:   }
182:   nneig = 0;                       /* no. of eigs less than sigma */

184:   PetscBLASIntCast(eps->nloc,&blz->istor[0]); /* no. of rows of U, V, X */
185:   PetscBLASIntCast(eps->nloc,&blz->istor[1]); /* leading dim of U, V, X */
186:   PetscBLASIntCast(eps->nev,&blz->istor[2]);  /* required eigenpairs */
187:   PetscBLASIntCast(eps->ncv,&blz->istor[3]);  /* working eigenpairs */
188:   blz->istor[4]  = blz->block_size;    /* number of vectors in a block */
189:   blz->istor[5]  = blz->nsteps;        /* maximun number of steps per run */
190:   blz->istor[6]  = 1;                  /* number of starting vectors as input */
191:   blz->istor[7]  = 0;                  /* number of eigenpairs given as input */
192:   blz->istor[8]  = (blz->slice || eps->isgeneralized) ? 1 : 0;   /* problem type */
193:   blz->istor[9]  = blz->slice;         /* spectrum slicing */
194:   blz->istor[10] = eps->isgeneralized ? 1 : 0;   /* solutions refinement (purify) */
195:   blz->istor[11] = 0;                  /* level of printing */
196:   blz->istor[12] = 6;                  /* file unit for output */
197:   PetscBLASIntCast(MPI_Comm_c2f(PetscObjectComm((PetscObject)eps)),&blz->istor[13]);

199:   blz->rstor[2]  = eps->tol;           /* threshold for convergence */

201:   lflag = 0;           /* reverse communication interface flag */

203:   do {
204:     BLZpack_(blz->istor,blz->rstor,&sigma,&nneig,blz->u,blz->v,&lflag,&nvopu,blz->eig,pV);

206:     switch (lflag) {
207:     case 1:
208:       /* compute v = OP u */
209:       for (i=0;i<nvopu;i++) {
210:         VecPlaceArray(x,blz->u+i*eps->nloc);
211:         VecPlaceArray(y,blz->v+i*eps->nloc);
212:         if (blz->slice || eps->isgeneralized) {
213:           STMatSolve(eps->st,x,y);
214:         } else {
215:           STApply(eps->st,x,y);
216:         }
217:         BVOrthogonalizeVec(eps->V,y,NULL,NULL,NULL);
218:         VecResetArray(x);
219:         VecResetArray(y);
220:       }
221:       /* monitor */
222:       eps->nconv  = BLZistorr_(blz->istor,"NTEIG",5);
223:       EPSMonitor(eps,eps->its,eps->nconv,
224:         blz->rstor+BLZistorr_(blz->istor,"IRITZ",5),
225:         eps->eigi,
226:         blz->rstor+BLZistorr_(blz->istor,"IRITZ",5)+BLZistorr_(blz->istor,"JT",2),
227:         BLZistorr_(blz->istor,"NRITZ",5));
228:       eps->its = eps->its + 1;
229:       if (eps->its >= eps->max_it || eps->nconv >= eps->nev) lflag = 5;
230:       break;
231:     case 2:
232:       /* compute v = B u */
233:       for (i=0;i<nvopu;i++) {
234:         VecPlaceArray(x,blz->u+i*eps->nloc);
235:         VecPlaceArray(y,blz->v+i*eps->nloc);
236:         BVApplyMatrix(eps->V,x,y);
237:         VecResetArray(x);
238:         VecResetArray(y);
239:       }
240:       break;
241:     case 3:
242:       /* update shift */
243:       PetscInfo1(eps,"Factorization update (sigma=%g)\n",(double)sigma);
244:       STSetShift(eps->st,sigma);
245:       STGetKSP(eps->st,&ksp);
246:       KSPGetPC(ksp,&pc);
247:       PCFactorGetMatrix(pc,&A);
248:       MatGetInertia(A,&nn,NULL,NULL);
249:       PetscBLASIntCast(nn,&nneig);
250:       break;
251:     case 4:
252:       /* copy the initial vector */
253:       VecPlaceArray(x,blz->v);
254:       VecCopy(v0,x);
255:       VecResetArray(x);
256:       break;
257:     }

259:   } while (lflag > 0);

261:   VecRestoreArray(v0,&pV);
262:   BVRestoreColumn(eps->V,0,&v0);

264:   eps->nconv  = BLZistorr_(blz->istor,"NTEIG",5);
265:   eps->reason = EPS_CONVERGED_TOL;

267:   for (i=0;i<eps->nconv;i++) {
268:     eps->eigr[i]=blz->eig[i];
269:   }

271:   if (lflag!=0) {
272:     char msg[2048] = "";
273:     for (i = 0; i < 33; i++) {
274:       if (blz->istor[15] & (1 << i)) PetscStrcat(msg,blzpack_error[i]);
275:     }
276:     SETERRQ2(PetscObjectComm((PetscObject)eps),PETSC_ERR_LIB,"Error in BLZPACK (code=%d): '%s'",blz->istor[15],msg);
277:   }
278:   VecDestroy(&x);
279:   VecDestroy(&y);
280:   return(0);
281: }

285: PetscErrorCode EPSBackTransform_BLZPACK(EPS eps)
286: {
288:   EPS_BLZPACK    *blz = (EPS_BLZPACK*)eps->data;

291:   if (!blz->slice && !eps->isgeneralized) {
292:     EPSBackTransform_Default(eps);
293:   }
294:   return(0);
295: }

299: PetscErrorCode EPSReset_BLZPACK(EPS eps)
300: {
302:   EPS_BLZPACK    *blz = (EPS_BLZPACK*)eps->data;

305:   PetscFree(blz->istor);
306:   PetscFree(blz->rstor);
307:   PetscFree(blz->u);
308:   PetscFree(blz->v);
309:   PetscFree(blz->eig);
310:   return(0);
311: }

315: PetscErrorCode EPSDestroy_BLZPACK(EPS eps)
316: {

320:   PetscFree(eps->data);
321:   PetscObjectComposeFunction((PetscObject)eps,"EPSBlzpackSetBlockSize_C",NULL);
322:   PetscObjectComposeFunction((PetscObject)eps,"EPSBlzpackSetNSteps_C",NULL);
323:   return(0);
324: }

328: PetscErrorCode EPSView_BLZPACK(EPS eps,PetscViewer viewer)
329: {
331:   EPS_BLZPACK    *blz = (EPS_BLZPACK*)eps->data;
332:   PetscBool      isascii;

335:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
336:   if (isascii) {
337:     PetscViewerASCIIPrintf(viewer,"  BLZPACK: block size=%d\n",blz->block_size);
338:     PetscViewerASCIIPrintf(viewer,"  BLZPACK: maximum number of steps per run=%d\n",blz->nsteps);
339:     if (blz->slice) {
340:       PetscViewerASCIIPrintf(viewer,"  BLZPACK: computational interval [%f,%f]\n",eps->inta,eps->intb);
341:     }
342:   }
343:   return(0);
344: }

348: PetscErrorCode EPSSetFromOptions_BLZPACK(PetscOptions *PetscOptionsObject,EPS eps)
349: {
351:   EPS_BLZPACK    *blz = (EPS_BLZPACK*)eps->data;
352:   PetscInt       bs,n;
353:   PetscBool      flg;

356:   PetscOptionsHead(PetscOptionsObject,"EPS BLZPACK Options");

358:   bs = blz->block_size;
359:   PetscOptionsInt("-eps_blzpack_block_size","Block size","EPSBlzpackSetBlockSize",bs,&bs,&flg);
360:   if (flg) {
361:     EPSBlzpackSetBlockSize(eps,bs);
362:   }

364:   n = blz->nsteps;
365:   PetscOptionsInt("-eps_blzpack_nsteps","Number of steps","EPSBlzpackSetNSteps",n,&n,&flg);
366:   if (flg) {
367:     EPSBlzpackSetNSteps(eps,n);
368:   }

370:   PetscOptionsTail();
371:   return(0);
372: }

376: static PetscErrorCode EPSBlzpackSetBlockSize_BLZPACK(EPS eps,PetscInt bs)
377: {
379:   EPS_BLZPACK    *blz = (EPS_BLZPACK*)eps->data;

382:   if (bs == PETSC_DEFAULT) blz->block_size = 3;
383:   else if (bs <= 0) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"Block size must be positive");
384:   else {
385:     PetscBLASIntCast(bs,&blz->block_size);
386:   }
387:   return(0);
388: }

392: /*@
393:    EPSBlzpackSetBlockSize - Sets the block size for the BLZPACK package.

395:    Collective on EPS

397:    Input Parameters:
398: +  eps - the eigenproblem solver context
399: -  bs - block size

401:    Options Database Key:
402: .  -eps_blzpack_block_size - Sets the value of the block size

404:    Level: advanced
405: @*/
406: PetscErrorCode EPSBlzpackSetBlockSize(EPS eps,PetscInt bs)
407: {

413:   PetscTryMethod(eps,"EPSBlzpackSetBlockSize_C",(EPS,PetscInt),(eps,bs));
414:   return(0);
415: }

419: static PetscErrorCode EPSBlzpackSetNSteps_BLZPACK(EPS eps,PetscInt nsteps)
420: {
422:   EPS_BLZPACK    *blz = (EPS_BLZPACK*)eps->data;

425:   if (nsteps == PETSC_DEFAULT) blz->nsteps = 0;
426:   else {
427:     PetscBLASIntCast(nsteps,&blz->nsteps);
428:   }
429:   return(0);
430: }

434: /*@
435:    EPSBlzpackSetNSteps - Sets the maximum number of steps per run for the BLZPACK
436:    package.

438:    Collective on EPS

440:    Input Parameters:
441: +  eps     - the eigenproblem solver context
442: -  nsteps  - maximum number of steps

444:    Options Database Key:
445: .  -eps_blzpack_nsteps - Sets the maximum number of steps per run

447:    Level: advanced

449: @*/
450: PetscErrorCode EPSBlzpackSetNSteps(EPS eps,PetscInt nsteps)
451: {

457:   PetscTryMethod(eps,"EPSBlzpackSetNSteps_C",(EPS,PetscInt),(eps,nsteps));
458:   return(0);
459: }

463: PETSC_EXTERN PetscErrorCode EPSCreate_BLZPACK(EPS eps)
464: {
466:   EPS_BLZPACK    *blzpack;

469:   PetscNewLog(eps,&blzpack);
470:   eps->data = (void*)blzpack;

472:   eps->ops->setup                = EPSSetUp_BLZPACK;
473:   eps->ops->setfromoptions       = EPSSetFromOptions_BLZPACK;
474:   eps->ops->destroy              = EPSDestroy_BLZPACK;
475:   eps->ops->reset                = EPSReset_BLZPACK;
476:   eps->ops->view                 = EPSView_BLZPACK;
477:   eps->ops->backtransform        = EPSBackTransform_BLZPACK;
478:   PetscObjectComposeFunction((PetscObject)eps,"EPSBlzpackSetBlockSize_C",EPSBlzpackSetBlockSize_BLZPACK);
479:   PetscObjectComposeFunction((PetscObject)eps,"EPSBlzpackSetNSteps_C",EPSBlzpackSetNSteps_BLZPACK);
480:   return(0);
481: }