Actual source code: mem.c

  1: /*
  2:       EPS routines related to memory management.

  4:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  5:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  6:    Copyright (c) 2002-2013, 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*/

 28: /*
 29:   EPSAllocateSolution - Allocate memory storage for common variables such
 30:   as eigenvalues and eigenvectors.
 31: */
 32: PetscErrorCode EPSAllocateSolution(EPS eps)
 33: {
 35:   PetscInt       newc,cnt;

 38:   if (eps->allocated_ncv != eps->ncv) {
 39:     newc = PetscMax(0,eps->ncv-eps->allocated_ncv);
 40:     EPSFreeSolution(eps);
 41:     cnt = 0;
 42:     PetscMalloc(eps->ncv*sizeof(PetscScalar),&eps->eigr);
 43:     PetscMalloc(eps->ncv*sizeof(PetscScalar),&eps->eigi);
 44:     cnt += 2*newc*sizeof(PetscScalar);
 45:     PetscMalloc(eps->ncv*sizeof(PetscReal),&eps->errest);
 46:     PetscMalloc(eps->ncv*sizeof(PetscReal),&eps->errest_left);
 47:     cnt += 2*newc*sizeof(PetscReal);
 48:     PetscMalloc(eps->ncv*sizeof(PetscInt),&eps->perm);
 49:     cnt += newc*sizeof(PetscInt);
 50:     PetscLogObjectMemory(eps,cnt);
 51:     VecDuplicateVecs(eps->t,eps->ncv,&eps->V);
 52:     PetscLogObjectParents(eps,eps->ncv,eps->V);
 53:     if (eps->leftvecs) {
 54:       VecDuplicateVecs(eps->t,eps->ncv,&eps->W);
 55:       PetscLogObjectParents(eps,eps->ncv,eps->W);
 56:     }
 57:     eps->allocated_ncv = eps->ncv;
 58:   }
 59:   /* The following cannot go in the above if, to avoid crash when ncv did not change */
 60:   if (eps->arbitrary) {
 61:     newc = PetscMax(0,eps->ncv-eps->allocated_ncv);
 62:     PetscFree(eps->rr);
 63:     PetscFree(eps->ri);
 64:     PetscMalloc(eps->ncv*sizeof(PetscScalar),&eps->rr);
 65:     PetscMalloc(eps->ncv*sizeof(PetscScalar),&eps->ri);
 66:     PetscLogObjectMemory(eps,2*newc*sizeof(PetscScalar));
 67:   }
 68:   return(0);
 69: }

 73: /*
 74:   EPSFreeSolution - Free memory storage. This routine is related to
 75:   EPSAllocateSolution().
 76: */
 77: PetscErrorCode EPSFreeSolution(EPS eps)
 78: {

 82:   if (eps->allocated_ncv > 0) {
 83:     PetscFree(eps->eigr);
 84:     PetscFree(eps->eigi);
 85:     PetscFree(eps->errest);
 86:     PetscFree(eps->errest_left);
 87:     PetscFree(eps->perm);
 88:     PetscFree(eps->rr);
 89:     PetscFree(eps->ri);
 90:     VecDestroyVecs(eps->allocated_ncv,&eps->V);
 91:     VecDestroyVecs(eps->allocated_ncv,&eps->W);
 92:     eps->allocated_ncv = 0;
 93:   }
 94:   return(0);
 95: }