1: /*
2: Implementation of a pool of Vec using VecDuplicateVecs.
4: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
5: SLEPc - Scalable Library for Eigenvalue Problem Computations
6: Copyright (c) 2002-2015, 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: PetscMalloc(sizeof(VecPool_),&pool);
55: PetscObjectReference((PetscObject)v);
56: pool->v = v;
57: pool->vecs = NULL;
58: pool->n = 0;
59: pool->used = 0;
60: pool->guess = init_size;
61: pool->next = NULL;
62: *p = pool;
63: return(0);
64: }
68: /*@C
69: SlepcVecPoolDestroy - Destroy the pool of Vec.
71: Collective on VecPool
73: Input Parameters:
74: . pool - pool of Vec.
76: Level: developer
78: .seealso: SlepcVecPoolGetVecs(), SlepcVecPoolCreate()
79: @*/
80: PetscErrorCode SlepcVecPoolDestroy(VecPool *p) 81: {
83: VecPool_ *pool = (VecPool_*)*p;
87: VecDestroy(&pool->v);
88: if (pool->vecs) { VecDestroyVecs(pool->n,&pool->vecs); }
89: pool->n = 0;
90: pool->used = 0;
91: pool->guess = 0;
92: if (pool->next) { SlepcVecPoolDestroy((VecPool*)&pool->next); }
93: PetscFree(pool);
94: *p = NULL;
95: return(0);
96: }
100: /*@C
101: SlepcVecPoolGetVecs - Get an array of Vec from the pool.
103: Collective on VecPool
105: Input Parameters:
106: + pool - pool of Vec.
107: - n - number of vectors.
109: Output Paramter:
110: . vecs - vectors
112: Level: developer
114: .seealso: SlepcVecPoolRestoreVecs()
115: @*/
116: PetscErrorCode SlepcVecPoolGetVecs(VecPool p,PetscInt n,Vec **vecs)117: {
119: VecPool_ *pool = (VecPool_*)p;
124: if (n<0) SETERRQ(PetscObjectComm((PetscObject)pool->v),PETSC_ERR_ARG_OUTOFRANGE,"n should be positive");
125: while (pool->next) pool = pool->next;
126: if (pool->n-pool->used < n) {
127: pool->guess = PetscMax(p->guess,pool->used+n);
128: if (pool->vecs && pool->used == 0) {
129: VecDestroyVecs(pool->n,&pool->vecs);
130: }
131: if (pool->vecs) {
132: SlepcVecPoolCreate(p->v,pool->guess-pool->used,&pool->next);
133: pool = pool->next;
134: }
135: pool->n = pool->guess;
136: VecDuplicateVecs(p->v,pool->n,&pool->vecs);
137: }
138: *vecs = pool->vecs + pool->used;
139: pool->used += n;
140: return(0);
141: }
145: /*@C
146: SlepcVecPoolRestoreVecs - Get back an array of Vec previously returned by
147: SlepcVecPoolGetVecs().
149: Collective on VecPool
151: Input Parameters:
152: + pool - pool of Vec.
153: . n - number of vectors.
154: - vecs - vectors
156: Level: developer
158: .seealso: SlepcVecPoolGetVecs()
159: @*/
160: PetscErrorCode SlepcVecPoolRestoreVecs(VecPool p,PetscInt n,Vec **vecs)161: {
163: VecPool_ *pool = (VecPool_*)p, *pool0 = pool;
166: while (pool->next) pool = (pool0 = pool)->next;
167: if (pool->used == 0 && pool0 != pool) {
168: pool0->guess = pool0->used + pool->guess;
169: SlepcVecPoolDestroy((VecPool*)&pool);
170: pool = pool0;
171: pool->next = NULL;
172: }
173: pool->used -= n;
174: if (pool->used < 0) SETERRQ(PetscObjectComm((PetscObject)pool->v),PETSC_ERR_ARG_OUTOFRANGE,"Unmatched SlepcVecPoolRestoreVecs");
175: return(0);
176: }