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 <private/stimpl.h>     /*I "slepcst.h" I*/
 26: #include <../src/eps/impls/external/blzpack/blzpackp.h>

 28: PetscErrorCode EPSSolve_BLZPACK(EPS);

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

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

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

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

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

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

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

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

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

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

136:   EPSAllocateSolution(eps);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

305: PetscErrorCode EPSDestroy_BLZPACK(EPS eps)
306: {

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

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

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

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

347:   PetscOptionsHead("EPS BLZPACK Options");

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

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

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

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

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

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

382:    Collective on EPS

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

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

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

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

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

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

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

424:    Collective on EPS

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

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

433:    Level: advanced

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

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

447: EXTERN_C_BEGIN
450: PetscErrorCode EPSCreate_BLZPACK(EPS eps)
451: {
453:   EPS_BLZPACK    *blzpack;

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

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

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