Actual source code: blzpack.c

  1: /*
  2:        This file implements a wrapper to the BLZPACK package

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

  8:    This file is part of SLEPc.
  9:       
 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 <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;

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

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

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

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

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:   PetscMalloc((4+lrstor)*sizeof(PetscReal),&blz->rstor);
122:   blz->rstor[3] = lrstor;

124:   ncuv = PetscMax(3,blz->block_size);
125:   PetscFree(blz->u);
126:   PetscMalloc(ncuv*eps->nloc*sizeof(PetscScalar),&blz->u);
127:   PetscFree(blz->v);
128:   PetscMalloc(ncuv*eps->nloc*sizeof(PetscScalar),&blz->v);

130:   PetscFree(blz->eig);
131:   PetscMalloc(2*eps->ncv*sizeof(PetscReal),&blz->eig);

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

135:   EPSAllocateSolution(eps);

137:   /* dispatch solve method */
138:   if (eps->leftvecs) SETERRQ(((PetscObject)eps)->comm,PETSC_ERR_SUP,"Left vectors not supported in this solver");
139:   eps->ops->solve = EPSSolve_BLZPACK;
140:   return(0);
141: }

145: PetscErrorCode EPSSolve_BLZPACK(EPS eps)
146: {
148:   EPS_BLZPACK    *blz = (EPS_BLZPACK *)eps->data;
149:   PetscInt       nn;
150:   PetscBLASInt   i,nneig,lflag,nvopu;
151:   Vec            x,y;
152:   PetscScalar    sigma,*pV;
153:   Mat            A;
154:   KSP            ksp;
155:   PC             pc;
156: 
158:   VecCreateMPIWithArray(((PetscObject)eps)->comm,eps->nloc,PETSC_DECIDE,PETSC_NULL,&x);
159:   VecCreateMPIWithArray(((PetscObject)eps)->comm,eps->nloc,PETSC_DECIDE,PETSC_NULL,&y);
160:   VecGetArray(eps->V[0],&pV);
161: 
162:   if (eps->isgeneralized && !blz->slice) {
163:     STGetShift(eps->OP,&sigma); /* shift of origin */
164:     blz->rstor[0]  = sigma;        /* lower limit of eigenvalue interval */
165:     blz->rstor[1]  = sigma;        /* upper limit of eigenvalue interval */
166:   } else {
167:     sigma = 0.0;
168:     blz->rstor[0]  = eps->inta;    /* lower limit of eigenvalue interval */
169:     blz->rstor[1]  = eps->intb;    /* upper limit of eigenvalue interval */
170:   }
171:   nneig = 0;                       /* no. of eigs less than sigma */

173:   blz->istor[0]  = PetscBLASIntCast(eps->nloc); /* number of rows of U, V, X*/
174:   blz->istor[1]  = PetscBLASIntCast(eps->nloc); /* leading dimension of U, V, X */
175:   blz->istor[2]  = PetscBLASIntCast(eps->nev); /* number of required eigenpairs */
176:   blz->istor[3]  = PetscBLASIntCast(eps->ncv); /* number of working eigenpairs */
177:   blz->istor[4]  = blz->block_size;    /* number of vectors in a block */
178:   blz->istor[5]  = blz->nsteps;  /* maximun number of steps per run */
179:   blz->istor[6]  = 1;            /* number of starting vectors as input */
180:   blz->istor[7]  = 0;            /* number of eigenpairs given as input */
181:   blz->istor[8]  = (blz->slice || eps->isgeneralized) ? 1 : 0;   /* problem type */
182:   blz->istor[9]  = blz->slice;   /* spectrum slicing */
183:   blz->istor[10] = eps->isgeneralized ? 1 : 0;   /* solutions refinement (purify) */
184:   blz->istor[11] = 0;            /* level of printing */
185:   blz->istor[12] = 6;            /* file unit for output */
186:   blz->istor[13] = PetscBLASIntCast(MPI_Comm_c2f(((PetscObject)eps)->comm)); /* communicator */

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

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

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

195:     switch (lflag) {
196:     case 1:
197:       /* compute v = OP u */
198:       for (i=0;i<nvopu;i++) {
199:         VecPlaceArray(x,blz->u+i*eps->nloc);
200:         VecPlaceArray(y,blz->v+i*eps->nloc);
201:         if (blz->slice || eps->isgeneralized) {
202:           STAssociatedKSPSolve(eps->OP,x,y);
203:         } else {
204:           STApply(eps->OP,x,y);
205:         }
206:         IPOrthogonalize(eps->ip,0,PETSC_NULL,eps->nds,PETSC_NULL,eps->DS,y,PETSC_NULL,PETSC_NULL,PETSC_NULL);
207:         VecResetArray(x);
208:         VecResetArray(y);
209:       }
210:       /* monitor */
211:       eps->nconv  = BLZistorr_(blz->istor,"NTEIG",5);
212:       EPSMonitor(eps,eps->its,eps->nconv,
213:         blz->rstor+BLZistorr_(blz->istor,"IRITZ",5),
214:         eps->eigi,
215:         blz->rstor+BLZistorr_(blz->istor,"IRITZ",5)+BLZistorr_(blz->istor,"JT",2),
216:         BLZistorr_(blz->istor,"NRITZ",5));
217:       eps->its = eps->its + 1;
218:       if (eps->its >= eps->max_it || eps->nconv >= eps->nev) lflag = 5;
219:       break;
220:     case 2:
221:       /* compute v = B u */
222:       for (i=0;i<nvopu;i++) {
223:         VecPlaceArray(x,blz->u+i*eps->nloc);
224:         VecPlaceArray(y,blz->v+i*eps->nloc);
225:         IPApplyMatrix(eps->ip,x,y);
226:         VecResetArray(x);
227:         VecResetArray(y);
228:       }
229:       break;
230:     case 3:
231:       /* update shift */
232:       PetscInfo1(eps,"Factorization update (sigma=%g)\n",sigma);
233:       STSetShift(eps->OP,sigma);
234:       STGetKSP(eps->OP,&ksp);
235:       KSPGetPC(ksp,&pc);
236:       PCFactorGetMatrix(pc,&A);
237:       MatGetInertia(A,&nn,PETSC_NULL,PETSC_NULL);
238:       nneig = PetscBLASIntCast(nn);
239:       break;
240:     case 4:
241:       /* copy the initial vector */
242:       VecPlaceArray(x,blz->v);
243:       EPSGetStartVector(eps,0,x,PETSC_NULL);
244:       VecResetArray(x);
245:       break;
246:     }
247: 
248:   } while (lflag > 0);

250:   VecRestoreArray(eps->V[0],&pV);

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

255:   for (i=0;i<eps->nconv;i++) {
256:     eps->eigr[i]=blz->eig[i];
257:   }

259:   if (lflag!=0) {
260:     char msg[2048] = "";
261:     for (i = 0; i < 33; i++) {
262:       if (blz->istor[15] & (1 << i)) PetscStrcat(msg,blzpack_error[i]);
263:     }
264:     SETERRQ2(((PetscObject)eps)->comm,PETSC_ERR_LIB,"Error in BLZPACK (code=%d): '%s'",blz->istor[15],msg);
265:   }
266:   VecDestroy(&x);
267:   VecDestroy(&y);
268:   return(0);
269: }

273: PetscErrorCode EPSBackTransform_BLZPACK(EPS eps)
274: {
276:   EPS_BLZPACK    *blz = (EPS_BLZPACK *)eps->data;

279:   if (!blz->slice && !eps->isgeneralized) {
280:     EPSBackTransform_Default(eps);
281:   }
282:   return(0);
283: }

287: PetscErrorCode EPSReset_BLZPACK(EPS eps)
288: {
290:   EPS_BLZPACK    *blz = (EPS_BLZPACK *)eps->data;

293:   PetscFree(blz->istor);
294:   PetscFree(blz->rstor);
295:   PetscFree(blz->u);
296:   PetscFree(blz->v);
297:   PetscFree(blz->eig);
298:   EPSFreeSolution(eps);
299:   return(0);
300: }

304: PetscErrorCode EPSDestroy_BLZPACK(EPS eps)
305: {

309:   PetscFree(eps->data);
310:   PetscObjectComposeFunctionDynamic((PetscObject)eps,"EPSBlzpackSetBlockSize_C","",PETSC_NULL);
311:   PetscObjectComposeFunctionDynamic((PetscObject)eps,"EPSBlzpackSetNSteps_C","",PETSC_NULL);
312:   return(0);
313: }

317: PetscErrorCode EPSView_BLZPACK(EPS eps,PetscViewer viewer)
318: {
320:   EPS_BLZPACK    *blz = (EPS_BLZPACK*)eps->data;
321:   PetscBool      isascii;

324:   PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
325:   if (!isascii) {
326:     SETERRQ1(((PetscObject)eps)->comm,1,"Viewer type %s not supported for EPSBLZPACK",((PetscObject)viewer)->type_name);
327:   }
328:   PetscViewerASCIIPrintf(viewer,"  BLZPACK: block size=%d\n",blz->block_size);
329:   PetscViewerASCIIPrintf(viewer,"  BLZPACK: maximum number of steps per run=%d\n",blz->nsteps);
330:   if (blz->slice) {
331:     PetscViewerASCIIPrintf(viewer,"  BLZPACK: computational interval [%f,%f]\n",eps->inta,eps->intb);
332:   }
333:   return(0);
334: }

338: PetscErrorCode EPSSetFromOptions_BLZPACK(EPS eps)
339: {
341:   EPS_BLZPACK    *blz = (EPS_BLZPACK *)eps->data;
342:   PetscInt       bs,n;
343:   PetscBool      flg;

346:   PetscOptionsHead("EPS BLZPACK Options");

348:   bs = blz->block_size;
349:   PetscOptionsInt("-eps_blzpack_block_size","Block size","EPSBlzpackSetBlockSize",bs,&bs,&flg);
350:   if (flg) {EPSBlzpackSetBlockSize(eps,bs);}

352:   n = blz->nsteps;
353:   PetscOptionsInt("-eps_blzpack_nsteps","Number of steps","EPSBlzpackSetNSteps",n,&n,&flg);
354:   if (flg) {EPSBlzpackSetNSteps(eps,n);}

356:   PetscOptionsTail();
357:   return(0);
358: }

360: EXTERN_C_BEGIN
363: PetscErrorCode EPSBlzpackSetBlockSize_BLZPACK(EPS eps,PetscInt bs)
364: {
365:   EPS_BLZPACK *blz = (EPS_BLZPACK*)eps->data;;

368:   if (bs == PETSC_DEFAULT) blz->block_size = 3;
369:   else if (bs <= 0) {
370:     SETERRQ(((PetscObject)eps)->comm,1,"Incorrect block size");
371:   } else blz->block_size = PetscBLASIntCast(bs);
372:   return(0);
373: }
374: EXTERN_C_END

378: /*@
379:    EPSBlzpackSetBlockSize - Sets the block size for the BLZPACK package.

381:    Collective on EPS

383:    Input Parameters:
384: +  eps - the eigenproblem solver context
385: -  bs - block size

387:    Options Database Key:
388: .  -eps_blzpack_block_size - Sets the value of the block size

390:    Level: advanced
391: @*/
392: PetscErrorCode EPSBlzpackSetBlockSize(EPS eps,PetscInt bs)
393: {

399:   PetscTryMethod(eps,"EPSBlzpackSetBlockSize_C",(EPS,PetscInt),(eps,bs));
400:   return(0);
401: }

403: EXTERN_C_BEGIN
406: PetscErrorCode EPSBlzpackSetNSteps_BLZPACK(EPS eps,PetscInt nsteps)
407: {
408:   EPS_BLZPACK *blz = (EPS_BLZPACK*)eps->data;

411:   if (nsteps == PETSC_DEFAULT) blz->nsteps = 0;
412:   else { blz->nsteps = PetscBLASIntCast(nsteps); }
413:   return(0);
414: }
415: EXTERN_C_END

419: /*@
420:    EPSBlzpackSetNSteps - Sets the maximum number of steps per run for the BLZPACK
421:    package.

423:    Collective on EPS

425:    Input Parameters:
426: +  eps     - the eigenproblem solver context
427: -  nsteps  - maximum number of steps

429:    Options Database Key:
430: .  -eps_blzpack_nsteps - Sets the maximum number of steps per run

432:    Level: advanced

434: @*/
435: PetscErrorCode EPSBlzpackSetNSteps(EPS eps,PetscInt nsteps)
436: {

442:   PetscTryMethod(eps,"EPSBlzpackSetNSteps_C",(EPS,PetscInt),(eps,nsteps));
443:   return(0);
444: }

446: EXTERN_C_BEGIN
449: PetscErrorCode EPSCreate_BLZPACK(EPS eps)
450: {
452:   EPS_BLZPACK    *blzpack;

455:   PetscNewLog(eps,EPS_BLZPACK,&blzpack);
456:   eps->data                      = (void*)blzpack;
457:   eps->ops->setup                = EPSSetUp_BLZPACK;
458:   eps->ops->setfromoptions       = EPSSetFromOptions_BLZPACK;
459:   eps->ops->destroy              = EPSDestroy_BLZPACK;
460:   eps->ops->reset                = EPSReset_BLZPACK;
461:   eps->ops->view                 = EPSView_BLZPACK;
462:   eps->ops->backtransform        = EPSBackTransform_BLZPACK;
463:   eps->ops->computevectors       = EPSComputeVectors_Default;

465:   blzpack->block_size = 3;
466:   blzpack->slice = 0;
467:   blzpack->nsteps = 0;

469:   PetscObjectComposeFunctionDynamic((PetscObject)eps,"EPSBlzpackSetBlockSize_C","EPSBlzpackSetBlockSize_BLZPACK",EPSBlzpackSetBlockSize_BLZPACK);
470:   PetscObjectComposeFunctionDynamic((PetscObject)eps,"EPSBlzpackSetNSteps_C","EPSBlzpackSetNSteps_BLZPACK",EPSBlzpackSetNSteps_BLZPACK);
471:   return(0);
472: }
473: EXTERN_C_END