1: /*
2: Implementation of a pool of Vec using VecDuplicateVecs.
4: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
5: SLEPc - Scalable Library for Eigenvalue Problem Computations
6: Copyright (c) 2002-2016, Universitat Politecnica de Valencia, Spain
8: This file is part of SLEPc.
10: SLEPc is free software: you can redistribute it and/or modify it under the
11: terms of version 3 of the GNU Lesser General Public License as published by
12: the Free Software Foundation.
14: SLEPc is distributed in the hope that it will be useful, but WITHOUT ANY
15: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16: FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
17: more details.
19: You should have received a copy of the GNU Lesser General Public License
20: along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
21: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
22: */
24: #include <slepc/private/vecimplslepc.h> /*I "slepcvec.h" I*/
28: /*@C
29: SlepcVecPoolCreate - Create a pool of Vec.
31: Collective on VecPool
33: Input Parameters:
34: + v - template vector.
35: - init_size - first guess of maximum vectors.
37: Output Parameter:
38: . pool - the pool context.
40: Level: developer
42: .seealso: SlepcVecPoolGetVecs(), SlepcVecPoolDestroy()
43: @*/
44: PetscErrorCode SlepcVecPoolCreate(Vec v,PetscInt init_size,VecPool *p) 45: {
47: VecPool_ *pool;
53: if (init_size<0) SETERRQ(PetscObjectComm((PetscObject)v),PETSC_ERR_ARG_WRONG,"init_size should be positive");
54: PetscCalloc1(1,&pool);
55: PetscObjectReference((PetscObject)v);
56: pool->v = v;
57: pool->guess = init_size;
58: *p = pool;
59: return(0);
60: }
64: /*@C
65: SlepcVecPoolDestroy - Destroy the pool of Vec.
67: Collective on VecPool
69: Input Parameters:
70: . pool - pool of Vec.
72: Level: developer
74: .seealso: SlepcVecPoolGetVecs(), SlepcVecPoolCreate()
75: @*/
76: PetscErrorCode SlepcVecPoolDestroy(VecPool *p) 77: {
79: VecPool_ *pool = (VecPool_*)*p;
83: VecDestroy(&pool->v);
84: if (pool->vecs) { VecDestroyVecs(pool->n,&pool->vecs); }
85: pool->n = 0;
86: pool->used = 0;
87: pool->guess = 0;
88: if (pool->next) { SlepcVecPoolDestroy((VecPool*)&pool->next); }
89: PetscFree(pool);
90: *p = NULL;
91: return(0);
92: }
96: /*@C
97: SlepcVecPoolGetVecs - Get an array of Vec from the pool.
99: Collective on VecPool
101: Input Parameters:
102: + pool - pool of Vec.
103: - n - number of vectors.
105: Output Parameter:
106: . vecs - vectors
108: Level: developer
110: .seealso: SlepcVecPoolRestoreVecs()
111: @*/
112: PetscErrorCode SlepcVecPoolGetVecs(VecPool p,PetscInt n,Vec **vecs)113: {
115: VecPool_ *pool = (VecPool_*)p;
120: if (n<0) SETERRQ(PetscObjectComm((PetscObject)pool->v),PETSC_ERR_ARG_OUTOFRANGE,"n should be positive");
121: while (pool->next) pool = pool->next;
122: if (pool->n-pool->used < n) {
123: pool->guess = PetscMax(p->guess,pool->used+n);
124: if (pool->vecs && pool->used == 0) {
125: VecDestroyVecs(pool->n,&pool->vecs);
126: }
127: if (pool->vecs) {
128: SlepcVecPoolCreate(p->v,pool->guess-pool->used,&pool->next);
129: pool = pool->next;
130: }
131: pool->n = pool->guess;
132: VecDuplicateVecs(p->v,pool->n,&pool->vecs);
133: }
134: *vecs = pool->vecs + pool->used;
135: pool->used += n;
136: return(0);
137: }
141: /*@C
142: SlepcVecPoolRestoreVecs - Get back an array of Vec previously returned by
143: SlepcVecPoolGetVecs().
145: Collective on VecPool
147: Input Parameters:
148: + pool - pool of Vec.
149: . n - number of vectors.
150: - vecs - vectors
152: Level: developer
154: .seealso: SlepcVecPoolGetVecs()
155: @*/
156: PetscErrorCode SlepcVecPoolRestoreVecs(VecPool p,PetscInt n,Vec **vecs)157: {
159: VecPool_ *pool = (VecPool_*)p, *pool0 = pool;
162: while (pool->next) pool = (pool0 = pool)->next;
163: if (pool->used == 0 && pool0 != pool) {
164: pool0->guess = pool0->used + pool->guess;
165: SlepcVecPoolDestroy((VecPool*)&pool);
166: pool = pool0;
167: pool->next = NULL;
168: }
169: pool->used -= n;
170: if (pool->used < 0) SETERRQ(PetscObjectComm((PetscObject)pool->v),PETSC_ERR_ARG_OUTOFRANGE,"Unmatched SlepcVecPoolRestoreVecs");
171: return(0);
172: }