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

Generated by: LCOV version 1.14