LCOV - code coverage report
Current view: top level - sys/vec - pool.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 56 56 100.0 %
Date: 2024-11-21 00:40:22 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       3             :    SLEPc - Scalable Library for Eigenvalue Problem Computations
       4             :    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
       5             : 
       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             : */
      13             : 
      14             : #include <slepc/private/vecimplslepc.h>       /*I "slepcvec.h" I*/
      15             : 
      16             : /*
      17             :    SlepcVecPoolCreate - Create a pool of Vec.
      18             : 
      19             :    Collective
      20             : 
      21             :    Input Parameters:
      22             : +  v - template vector.
      23             : -  init_size - first guess of maximum vectors.
      24             : 
      25             :    Output Parameter:
      26             : .  p - the pool context.
      27             : 
      28             :    Level: developer
      29             : 
      30             : .seealso: SlepcVecPoolGetVecs(), SlepcVecPoolDestroy()
      31             : */
      32       28797 : PetscErrorCode SlepcVecPoolCreate(Vec v,PetscInt init_size,VecPool *p)
      33             : {
      34       28797 :   VecPool_       *pool;
      35             : 
      36       28797 :   PetscFunctionBegin;
      37       28797 :   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
      38       86391 :   PetscValidLogicalCollectiveInt(v,init_size,2);
      39       28797 :   PetscAssertPointer(p,3);
      40       28797 :   PetscCheck(init_size>=0,PetscObjectComm((PetscObject)v),PETSC_ERR_ARG_WRONG,"init_size should be positive");
      41       28797 :   PetscCall(PetscCalloc1(1,&pool));
      42       28797 :   PetscCall(PetscObjectReference((PetscObject)v));
      43       28797 :   pool->v     = v;
      44       28797 :   pool->guess = init_size;
      45       28797 :   *p = pool;
      46       28797 :   PetscFunctionReturn(PETSC_SUCCESS);
      47             : }
      48             : 
      49             : /*
      50             :    SlepcVecPoolDestroy - Destroy the pool of Vec.
      51             : 
      52             :    Collective
      53             : 
      54             :    Input Parameters:
      55             : .  p - pool of Vec.
      56             : 
      57             :    Level: developer
      58             : 
      59             : .seealso: SlepcVecPoolGetVecs(), SlepcVecPoolCreate()
      60             : */
      61       57370 : PetscErrorCode SlepcVecPoolDestroy(VecPool *p)
      62             : {
      63       57370 :   VecPool_       *pool = (VecPool_*)*p;
      64             : 
      65       57370 :   PetscFunctionBegin;
      66       57370 :   if (!*p) PetscFunctionReturn(PETSC_SUCCESS);
      67       28797 :   PetscCall(VecDestroy(&pool->v));
      68       28797 :   PetscCall(VecDestroyVecs(pool->n,&pool->vecs));
      69       28797 :   pool->n     = 0;
      70       28797 :   pool->used  = 0;
      71       28797 :   pool->guess = 0;
      72       28797 :   PetscCall(SlepcVecPoolDestroy((VecPool*)&pool->next));
      73       28797 :   PetscCall(PetscFree(pool));
      74       28797 :   *p = NULL;
      75       28797 :   PetscFunctionReturn(PETSC_SUCCESS);
      76             : }
      77             : 
      78             : /*
      79             :    SlepcVecPoolGetVecs - Get an array of Vec from the pool.
      80             : 
      81             :    Collective
      82             : 
      83             :    Input Parameters:
      84             : +  p - pool of Vec.
      85             : -  n - number of vectors.
      86             : 
      87             :    Output Parameter:
      88             : .  vecs - vectors
      89             : 
      90             :    Level: developer
      91             : 
      92             : .seealso: SlepcVecPoolRestoreVecs()
      93             : */
      94      144678 : PetscErrorCode SlepcVecPoolGetVecs(VecPool p,PetscInt n,Vec **vecs)
      95             : {
      96      144678 :   VecPool_       *pool = (VecPool_*)p;
      97             : 
      98      144678 :   PetscFunctionBegin;
      99      144678 :   PetscAssertPointer(p,1);
     100      144678 :   PetscAssertPointer(vecs,3);
     101      144678 :   PetscCheck(n>=0,PetscObjectComm((PetscObject)pool->v),PETSC_ERR_ARG_OUTOFRANGE,"n should be positive");
     102      331239 :   while (pool->next) pool = pool->next;
     103      144678 :   if (pool->n-pool->used < n) {
     104       28927 :     pool->guess = PetscMax(p->guess,pool->used+n);
     105       28927 :     if (pool->vecs && pool->used == 0) PetscCall(VecDestroyVecs(pool->n,&pool->vecs));
     106       28927 :     if (pool->vecs) {
     107       28700 :       PetscCall(SlepcVecPoolCreate(p->v,pool->guess-pool->used,&pool->next));
     108       28700 :       pool = pool->next;
     109             :     }
     110       28927 :     pool->n = pool->guess;
     111       28927 :     PetscCall(VecDuplicateVecs(p->v,pool->n,&pool->vecs));
     112             :   }
     113      144678 :   *vecs = pool->vecs + pool->used;
     114      144678 :   pool->used += n;
     115      144678 :   PetscFunctionReturn(PETSC_SUCCESS);
     116             : }
     117             : 
     118             : /*
     119             :    SlepcVecPoolRestoreVecs - Get back an array of Vec previously returned by
     120             :    SlepcVecPoolGetVecs().
     121             : 
     122             :    Collective
     123             : 
     124             :    Input Parameters:
     125             : +  p    - pool of Vec.
     126             : .  n    - number of vectors.
     127             : -  vecs - vectors
     128             : 
     129             :    Level: developer
     130             : 
     131             : .seealso: SlepcVecPoolGetVecs()
     132             : */
     133      144415 : PetscErrorCode SlepcVecPoolRestoreVecs(VecPool p,PetscInt n,Vec **vecs)
     134             : {
     135      144415 :   VecPool_       *pool = (VecPool_*)p, *pool0 = pool;
     136             : 
     137      144415 :   PetscFunctionBegin;
     138      387795 :   while (pool->next) pool = (pool0 = pool)->next;
     139      144415 :   if (pool->used == 0 && pool0 != pool) {
     140       28476 :     pool0->guess = pool0->used + pool->guess;
     141       28476 :     PetscCall(SlepcVecPoolDestroy((VecPool*)&pool));
     142       28476 :     pool = pool0;
     143       28476 :     pool->next = NULL;
     144             :   }
     145      144415 :   pool->used -= n;
     146      144415 :   PetscCheck(pool->used>=0,PetscObjectComm((PetscObject)pool->v),PETSC_ERR_ARG_OUTOFRANGE,"Unmatched SlepcVecPoolRestoreVecs");
     147      144415 :   PetscFunctionReturn(PETSC_SUCCESS);
     148             : }

Generated by: LCOV version 1.14