Actual source code: pool.c
slepc-3.11.2 2019-07-30
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-2019, 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> /*I "slepcvec.h" I*/
16: /*@C
17: SlepcVecPoolCreate - Create a pool of Vec.
19: Collective on VecPool
21: Input Parameters:
22: + v - template vector.
23: - init_size - first guess of maximum vectors.
25: Output Parameter:
26: . pool - the pool context.
28: Level: developer
30: .seealso: SlepcVecPoolGetVecs(), SlepcVecPoolDestroy()
31: @*/
32: PetscErrorCode SlepcVecPoolCreate(Vec v,PetscInt init_size,VecPool *p)
33: {
35: VecPool_ *pool;
41: if (init_size<0) SETERRQ(PetscObjectComm((PetscObject)v),PETSC_ERR_ARG_WRONG,"init_size should be positive");
42: PetscCalloc1(1,&pool);
43: PetscObjectReference((PetscObject)v);
44: pool->v = v;
45: pool->guess = init_size;
46: *p = pool;
47: return(0);
48: }
50: /*@C
51: SlepcVecPoolDestroy - Destroy the pool of Vec.
53: Collective on VecPool
55: Input Parameters:
56: . pool - pool of Vec.
58: Level: developer
60: .seealso: SlepcVecPoolGetVecs(), SlepcVecPoolCreate()
61: @*/
62: PetscErrorCode SlepcVecPoolDestroy(VecPool *p)
63: {
65: VecPool_ *pool = (VecPool_*)*p;
68: if (!*p) return(0);
69: VecDestroy(&pool->v);
70: VecDestroyVecs(pool->n,&pool->vecs);
71: pool->n = 0;
72: pool->used = 0;
73: pool->guess = 0;
74: SlepcVecPoolDestroy((VecPool*)&pool->next);
75: PetscFree(pool);
76: *p = NULL;
77: return(0);
78: }
80: /*@C
81: SlepcVecPoolGetVecs - Get an array of Vec from the pool.
83: Collective on VecPool
85: Input Parameters:
86: + pool - pool of Vec.
87: - n - number of vectors.
89: Output Parameter:
90: . vecs - vectors
92: Level: developer
94: .seealso: SlepcVecPoolRestoreVecs()
95: @*/
96: PetscErrorCode SlepcVecPoolGetVecs(VecPool p,PetscInt n,Vec **vecs)
97: {
99: VecPool_ *pool = (VecPool_*)p;
104: if (n<0) SETERRQ(PetscObjectComm((PetscObject)pool->v),PETSC_ERR_ARG_OUTOFRANGE,"n should be positive");
105: while (pool->next) pool = pool->next;
106: if (pool->n-pool->used < n) {
107: pool->guess = PetscMax(p->guess,pool->used+n);
108: if (pool->vecs && pool->used == 0) {
109: VecDestroyVecs(pool->n,&pool->vecs);
110: }
111: if (pool->vecs) {
112: SlepcVecPoolCreate(p->v,pool->guess-pool->used,&pool->next);
113: pool = pool->next;
114: }
115: pool->n = pool->guess;
116: VecDuplicateVecs(p->v,pool->n,&pool->vecs);
117: }
118: *vecs = pool->vecs + pool->used;
119: pool->used += n;
120: return(0);
121: }
123: /*@C
124: SlepcVecPoolRestoreVecs - Get back an array of Vec previously returned by
125: SlepcVecPoolGetVecs().
127: Collective on VecPool
129: Input Parameters:
130: + pool - pool of Vec.
131: . n - number of vectors.
132: - vecs - vectors
134: Level: developer
136: .seealso: SlepcVecPoolGetVecs()
137: @*/
138: PetscErrorCode SlepcVecPoolRestoreVecs(VecPool p,PetscInt n,Vec **vecs)
139: {
141: VecPool_ *pool = (VecPool_*)p, *pool0 = pool;
144: while (pool->next) pool = (pool0 = pool)->next;
145: if (pool->used == 0 && pool0 != pool) {
146: pool0->guess = pool0->used + pool->guess;
147: SlepcVecPoolDestroy((VecPool*)&pool);
148: pool = pool0;
149: pool->next = NULL;
150: }
151: pool->used -= n;
152: if (pool->used < 0) SETERRQ(PetscObjectComm((PetscObject)pool->v),PETSC_ERR_ARG_OUTOFRANGE,"Unmatched SlepcVecPoolRestoreVecs");
153: return(0);
154: }