Date: 2026-03-31T13:23:23+00:00

Actual source code: pool.c

      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: /*
     17:    SlepcVecPoolCreate - Create a pool of Vec.

     19:    Collective

     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;

     36:   PetscFunctionBegin;
     39:   PetscAssertPointer(p,3);
     40:   PetscCheck(init_size>=0,PetscObjectComm((PetscObject)v),PETSC_ERR_ARG_WRONG,"init_size should be positive");
     41:   PetscCall(PetscCalloc1(1,&pool));
     42:   PetscCall(PetscObjectReference((PetscObject)v));
     43:   pool->v     = v;
     44:   pool->guess = init_size;
     45:   *p = pool;
     46:   PetscFunctionReturn(PETSC_SUCCESS);
     47: }

     49: /*
     50:    SlepcVecPoolDestroy - Destroy the pool of Vec.

     52:    Collective

     54:    Input Parameter:
     55: .  p - pool of Vec.

     57:    Level: developer

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

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

     78: /*
     79:    SlepcVecPoolGetVecs - Get an array of Vec from the pool.

     81:    Collective

     83:    Input Parameters:
     84: +  p - pool of Vec.
     85: -  n - number of vectors.

     87:    Output Parameter:
     88: .  vecs - vectors

     90:    Level: developer

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

     98:   PetscFunctionBegin;
     99:   PetscAssertPointer(p,1);
    100:   PetscAssertPointer(vecs,3);
    101:   PetscCheck(n>=0,PetscObjectComm((PetscObject)pool->v),PETSC_ERR_ARG_OUTOFRANGE,"n should be positive");
    102:   while (pool->next) pool = pool->next;
    103:   if (pool->n-pool->used < n) {
    104:     pool->guess = PetscMax(p->guess,pool->used+n);
    105:     if (pool->vecs && pool->used == 0) PetscCall(VecDestroyVecs(pool->n,&pool->vecs));
    106:     if (pool->vecs) {
    107:       PetscCall(SlepcVecPoolCreate(p->v,pool->guess-pool->used,&pool->next));
    108:       pool = pool->next;
    109:     }
    110:     pool->n = pool->guess;
    111:     PetscCall(VecDuplicateVecs(p->v,pool->n,&pool->vecs));
    112:   }
    113:   *vecs = pool->vecs + pool->used;
    114:   pool->used += n;
    115:   PetscFunctionReturn(PETSC_SUCCESS);
    116: }

    118: /*
    119:    SlepcVecPoolRestoreVecs - Get back an array of Vec previously returned by
    120:    SlepcVecPoolGetVecs().

    122:    Collective

    124:    Input Parameters:
    125: +  p    - pool of Vec.
    126: .  n    - number of vectors.
    127: -  vecs - vectors

    129:    Level: developer

    131: .seealso: `SlepcVecPoolGetVecs()`
    132: */
    133: PetscErrorCode SlepcVecPoolRestoreVecs(VecPool p,PetscInt n,Vec **vecs)
    134: {
    135:   VecPool_       *pool = (VecPool_*)p, *pool0 = pool;

    137:   PetscFunctionBegin;
    138:   while (pool->next) pool = (pool0 = pool)->next;
    139:   if (pool->used == 0 && pool0 != pool) {
    140:     pool0->guess = pool0->used + pool->guess;
    141:     PetscCall(SlepcVecPoolDestroy((VecPool*)&pool));
    142:     pool = pool0;
    143:     pool->next = NULL;
    144:   }
    145:   pool->used -= n;
    146:   PetscCheck(pool->used>=0,PetscObjectComm((PetscObject)pool->v),PETSC_ERR_ARG_OUTOFRANGE,"Unmatched SlepcVecPoolRestoreVecs");
    147:   PetscFunctionReturn(PETSC_SUCCESS);
    148: }