Date: 2023-01-26T12:17:49+00:00

Actual source code: pool.c
slepc-3.18.2 2023-01-26
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain

      6:    This file is part of SLEPc.
      7:    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
      8:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      9: */
     10: /*
     11:    Implementation of a pool of Vec using VecDuplicateVecs
     12: */

     14: #include <slepc/private/vecimplslepc.h>

     16: /*@C
     17:    SlepcVecPoolCreate - Create a pool of Vec.

     19:    Collective on v

     21:    Input Parameters:
     22: +  v - template vector.
     23: -  init_size - first guess of maximum vectors.

     25:    Output Parameter:
     26: .  p - the pool context.

     28:    Level: developer

     30: .seealso: SlepcVecPoolGetVecs(), SlepcVecPoolDestroy()
     31: @*/
     32: PetscErrorCode SlepcVecPoolCreate(Vec v,PetscInt init_size,VecPool *p)
     33: {
     34:   VecPool_       *pool;

     40:   PetscCalloc1(1,&pool);
     41:   PetscObjectReference((PetscObject)v);
     42:   pool->v     = v;
     43:   pool->guess = init_size;
     44:   *p = pool;
     45:   return 0;
     46: }

     48: /*@C
     49:    SlepcVecPoolDestroy - Destroy the pool of Vec.

     51:    Collective on p

     53:    Input Parameters:
     54: .  p - pool of Vec.

     56:    Level: developer

     58: .seealso: SlepcVecPoolGetVecs(), SlepcVecPoolCreate()
     59: @*/
     60: PetscErrorCode SlepcVecPoolDestroy(VecPool *p)
     61: {
     62:   VecPool_       *pool = (VecPool_*)*p;

     64:   if (!*p) return 0;
     65:   VecDestroy(&pool->v);
     66:   VecDestroyVecs(pool->n,&pool->vecs);
     67:   pool->n     = 0;
     68:   pool->used  = 0;
     69:   pool->guess = 0;
     70:   SlepcVecPoolDestroy((VecPool*)&pool->next);
     71:   PetscFree(pool);
     72:   *p = NULL;
     73:   return 0;
     74: }

     76: /*@C
     77:    SlepcVecPoolGetVecs - Get an array of Vec from the pool.

     79:    Collective on p

     81:    Input Parameters:
     82: +  p - pool of Vec.
     83: -  n - number of vectors.

     85:    Output Parameter:
     86: .  vecs - vectors

     88:    Level: developer

     90: .seealso: SlepcVecPoolRestoreVecs()
     91: @*/
     92: PetscErrorCode SlepcVecPoolGetVecs(VecPool p,PetscInt n,Vec **vecs)
     93: {
     94:   VecPool_       *pool = (VecPool_*)p;

     99:   while (pool->next) pool = pool->next;
    100:   if (pool->n-pool->used < n) {
    101:     pool->guess = PetscMax(p->guess,pool->used+n);
    102:     if (pool->vecs && pool->used == 0) VecDestroyVecs(pool->n,&pool->vecs);
    103:     if (pool->vecs) {
    104:       SlepcVecPoolCreate(p->v,pool->guess-pool->used,&pool->next);
    105:       pool = pool->next;
    106:     }
    107:     pool->n = pool->guess;
    108:     VecDuplicateVecs(p->v,pool->n,&pool->vecs);
    109:   }
    110:   *vecs = pool->vecs + pool->used;
    111:   pool->used += n;
    112:   return 0;
    113: }

    115: /*@C
    116:    SlepcVecPoolRestoreVecs - Get back an array of Vec previously returned by
    117:    SlepcVecPoolGetVecs().

    119:    Collective on p

    121:    Input Parameters:
    122: +  p    - pool of Vec.
    123: .  n    - number of vectors.
    124: -  vecs - vectors

    126:    Level: developer

    128: .seealso: SlepcVecPoolGetVecs()
    129: @*/
    130: PetscErrorCode SlepcVecPoolRestoreVecs(VecPool p,PetscInt n,Vec **vecs)
    131: {
    132:   VecPool_       *pool = (VecPool_*)p, *pool0 = pool;

    134:   while (pool->next) pool = (pool0 = pool)->next;
    135:   if (pool->used == 0 && pool0 != pool) {
    136:     pool0->guess = pool0->used + pool->guess;
    137:     SlepcVecPoolDestroy((VecPool*)&pool);
    138:     pool = pool0;
    139:     pool->next = NULL;
    140:   }
    141:   pool->used -= n;
    143:   return 0;
    144: }