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-23 00:34:26 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       10405 : PetscErrorCode SlepcVecPoolCreate(Vec v,PetscInt init_size,VecPool *p)
      33             : {
      34       10405 :   VecPool_       *pool;
      35             : 
      36       10405 :   PetscFunctionBegin;
      37       10405 :   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
      38       31215 :   PetscValidLogicalCollectiveInt(v,init_size,2);
      39       10405 :   PetscAssertPointer(p,3);
      40       10405 :   PetscCheck(init_size>=0,PetscObjectComm((PetscObject)v),PETSC_ERR_ARG_WRONG,"init_size should be positive");
      41       10405 :   PetscCall(PetscCalloc1(1,&pool));
      42       10405 :   PetscCall(PetscObjectReference((PetscObject)v));
      43       10405 :   pool->v     = v;
      44       10405 :   pool->guess = init_size;
      45       10405 :   *p = pool;
      46       10405 :   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       20602 : PetscErrorCode SlepcVecPoolDestroy(VecPool *p)
      62             : {
      63       20602 :   VecPool_       *pool = (VecPool_*)*p;
      64             : 
      65       20602 :   PetscFunctionBegin;
      66       20602 :   if (!*p) PetscFunctionReturn(PETSC_SUCCESS);
      67       10405 :   PetscCall(VecDestroy(&pool->v));
      68       10405 :   PetscCall(VecDestroyVecs(pool->n,&pool->vecs));
      69       10405 :   pool->n     = 0;
      70       10405 :   pool->used  = 0;
      71       10405 :   pool->guess = 0;
      72       10405 :   PetscCall(SlepcVecPoolDestroy((VecPool*)&pool->next));
      73       10405 :   PetscCall(PetscFree(pool));
      74       10405 :   *p = NULL;
      75       10405 :   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       45929 : PetscErrorCode SlepcVecPoolGetVecs(VecPool p,PetscInt n,Vec **vecs)
      95             : {
      96       45929 :   VecPool_       *pool = (VecPool_*)p;
      97             : 
      98       45929 :   PetscFunctionBegin;
      99       45929 :   PetscAssertPointer(p,1);
     100       45929 :   PetscAssertPointer(vecs,3);
     101       45929 :   PetscCheck(n>=0,PetscObjectComm((PetscObject)pool->v),PETSC_ERR_ARG_OUTOFRANGE,"n should be positive");
     102       97260 :   while (pool->next) pool = pool->next;
     103       45929 :   if (pool->n-pool->used < n) {
     104       10465 :     pool->guess = PetscMax(p->guess,pool->used+n);
     105       10465 :     if (pool->vecs && pool->used == 0) PetscCall(VecDestroyVecs(pool->n,&pool->vecs));
     106       10465 :     if (pool->vecs) {
     107       10310 :       PetscCall(SlepcVecPoolCreate(p->v,pool->guess-pool->used,&pool->next));
     108       10310 :       pool = pool->next;
     109             :     }
     110       10465 :     pool->n = pool->guess;
     111       10465 :     PetscCall(VecDuplicateVecs(p->v,pool->n,&pool->vecs));
     112             :   }
     113       45929 :   *vecs = pool->vecs + pool->used;
     114       45929 :   pool->used += n;
     115       45929 :   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       45676 : PetscErrorCode SlepcVecPoolRestoreVecs(VecPool p,PetscInt n,Vec **vecs)
     134             : {
     135       45676 :   VecPool_       *pool = (VecPool_*)p, *pool0 = pool;
     136             : 
     137       45676 :   PetscFunctionBegin;
     138      117082 :   while (pool->next) pool = (pool0 = pool)->next;
     139       45676 :   if (pool->used == 0 && pool0 != pool) {
     140       10102 :     pool0->guess = pool0->used + pool->guess;
     141       10102 :     PetscCall(SlepcVecPoolDestroy((VecPool*)&pool));
     142       10102 :     pool = pool0;
     143       10102 :     pool->next = NULL;
     144             :   }
     145       45676 :   pool->used -= n;
     146       45676 :   PetscCheck(pool->used>=0,PetscObjectComm((PetscObject)pool->v),PETSC_ERR_ARG_OUTOFRANGE,"Unmatched SlepcVecPoolRestoreVecs");
     147       45676 :   PetscFunctionReturn(PETSC_SUCCESS);
     148             : }

Generated by: LCOV version 1.14