xref: /petsc/src/dm/interface/dmget.c (revision 85ea3df1142f4a00a5df7fe265a0d81e3c93e61f)
1af0996ceSBarry Smith #include <petsc/private/dmimpl.h> /*I "petscdm.h" I*/
268260fa0SJed Brown 
368260fa0SJed Brown /*@
468260fa0SJed Brown    DMGetLocalVector - Gets a Seq PETSc vector that
568260fa0SJed Brown    may be used with the DMXXX routines. This vector has spaces for the ghost values.
668260fa0SJed Brown 
768260fa0SJed Brown    Not Collective
868260fa0SJed Brown 
968260fa0SJed Brown    Input Parameter:
1068260fa0SJed Brown .  dm - the distributed array
1168260fa0SJed Brown 
1268260fa0SJed Brown    Output Parameter:
1368260fa0SJed Brown .  g - the local vector
1468260fa0SJed Brown 
1568260fa0SJed Brown    Level: beginner
1668260fa0SJed Brown 
1768260fa0SJed Brown    Note:
1868260fa0SJed Brown    The vector values are NOT initialized and may have garbage in them, so you may need
1968260fa0SJed Brown    to zero them.
2068260fa0SJed Brown 
2168260fa0SJed Brown    The output parameter, g, is a regular PETSc vector that should be returned with
2268260fa0SJed Brown    DMRestoreLocalVector() DO NOT call VecDestroy() on it.
2368260fa0SJed Brown 
24f1978aafSBarry Smith    This is intended to be used for vectors you need for a short time, like within a single function call.
25f1978aafSBarry Smith    For vectors that you intend to keep around (for example in a C struct) or pass around large parts of your
26f1978aafSBarry Smith    code you should use DMCreateLocalVector().
27f1978aafSBarry Smith 
2868260fa0SJed Brown    VecStride*() operations can be useful when using DM with dof > 1
2968260fa0SJed Brown 
3068260fa0SJed Brown .keywords: distributed array, create, local, vector
3168260fa0SJed Brown 
3268260fa0SJed Brown .seealso: DMCreateGlobalVector(), VecDuplicate(), VecDuplicateVecs(),
3368260fa0SJed Brown           DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMGlobalToLocalBegin(),
3468260fa0SJed Brown           DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMCreateLocalVector(), DMRestoreLocalVector(),
3568260fa0SJed Brown           VecStrideMax(), VecStrideMin(), VecStrideNorm()
3668260fa0SJed Brown @*/
3768260fa0SJed Brown PetscErrorCode  DMGetLocalVector(DM dm,Vec *g)
3868260fa0SJed Brown {
3968260fa0SJed Brown   PetscErrorCode ierr,i;
4068260fa0SJed Brown 
4168260fa0SJed Brown   PetscFunctionBegin;
4268260fa0SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4368260fa0SJed Brown   PetscValidPointer(g,2);
4468260fa0SJed Brown   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4568260fa0SJed Brown     if (dm->localin[i]) {
4668260fa0SJed Brown       *g             = dm->localin[i];
470298fd71SBarry Smith       dm->localin[i] = NULL;
4868260fa0SJed Brown       goto alldone;
4968260fa0SJed Brown     }
5068260fa0SJed Brown   }
5168260fa0SJed Brown   ierr = DMCreateLocalVector(dm,g);CHKERRQ(ierr);
5268260fa0SJed Brown 
5368260fa0SJed Brown alldone:
5468260fa0SJed Brown   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
5568260fa0SJed Brown     if (!dm->localout[i]) {
5668260fa0SJed Brown       dm->localout[i] = *g;
5768260fa0SJed Brown       break;
5868260fa0SJed Brown     }
5968260fa0SJed Brown   }
6068260fa0SJed Brown   PetscFunctionReturn(0);
6168260fa0SJed Brown }
6268260fa0SJed Brown 
6368260fa0SJed Brown /*@
6468260fa0SJed Brown    DMRestoreLocalVector - Returns a Seq PETSc vector that
6568260fa0SJed Brown      obtained from DMGetLocalVector(). Do not use with vector obtained via
6668260fa0SJed Brown      DMCreateLocalVector().
6768260fa0SJed Brown 
6868260fa0SJed Brown    Not Collective
6968260fa0SJed Brown 
7068260fa0SJed Brown    Input Parameter:
7168260fa0SJed Brown +  dm - the distributed array
7268260fa0SJed Brown -  g - the local vector
7368260fa0SJed Brown 
7468260fa0SJed Brown    Level: beginner
7568260fa0SJed Brown 
7668260fa0SJed Brown .keywords: distributed array, create, local, vector
7768260fa0SJed Brown 
7868260fa0SJed Brown .seealso: DMCreateGlobalVector(), VecDuplicate(), VecDuplicateVecs(),
7968260fa0SJed Brown           DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMGlobalToLocalBegin(),
8068260fa0SJed Brown           DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMCreateLocalVector(), DMGetLocalVector()
8168260fa0SJed Brown @*/
8268260fa0SJed Brown PetscErrorCode  DMRestoreLocalVector(DM dm,Vec *g)
8368260fa0SJed Brown {
8468260fa0SJed Brown   PetscErrorCode ierr;
8568260fa0SJed Brown   PetscInt       i,j;
8668260fa0SJed Brown 
8768260fa0SJed Brown   PetscFunctionBegin;
8868260fa0SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8968260fa0SJed Brown   PetscValidPointer(g,2);
9068260fa0SJed Brown   for (j=0; j<DM_MAX_WORK_VECTORS; j++) {
9168260fa0SJed Brown     if (*g == dm->localout[j]) {
920298fd71SBarry Smith       dm->localout[j] = NULL;
9368260fa0SJed Brown       for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
9468260fa0SJed Brown         if (!dm->localin[i]) {
9568260fa0SJed Brown           dm->localin[i] = *g;
9668260fa0SJed Brown           goto alldone;
9768260fa0SJed Brown         }
9868260fa0SJed Brown       }
9968260fa0SJed Brown     }
10068260fa0SJed Brown   }
10168260fa0SJed Brown   ierr = VecDestroy(g);CHKERRQ(ierr);
10268260fa0SJed Brown alldone:
103dd6887adSBarry Smith   *g = NULL;
10468260fa0SJed Brown   PetscFunctionReturn(0);
10568260fa0SJed Brown }
10668260fa0SJed Brown 
10768260fa0SJed Brown /*@
10868260fa0SJed Brown    DMGetGlobalVector - Gets a MPI PETSc vector that
10968260fa0SJed Brown    may be used with the DMXXX routines.
11068260fa0SJed Brown 
11168260fa0SJed Brown    Collective on DM
11268260fa0SJed Brown 
11368260fa0SJed Brown    Input Parameter:
11468260fa0SJed Brown .  dm - the distributed array
11568260fa0SJed Brown 
11668260fa0SJed Brown    Output Parameter:
11768260fa0SJed Brown .  g - the global vector
11868260fa0SJed Brown 
11968260fa0SJed Brown    Level: beginner
12068260fa0SJed Brown 
12168260fa0SJed Brown    Note:
12268260fa0SJed Brown    The vector values are NOT initialized and may have garbage in them, so you may need
12368260fa0SJed Brown    to zero them.
12468260fa0SJed Brown 
12568260fa0SJed Brown    The output parameter, g, is a regular PETSc vector that should be returned with
12668260fa0SJed Brown    DMRestoreGlobalVector() DO NOT call VecDestroy() on it.
12768260fa0SJed Brown 
128f1978aafSBarry Smith    This is intended to be used for vectors you need for a short time, like within a single function call.
129f1978aafSBarry Smith    For vectors that you intend to keep around (for example in a C struct) or pass around large parts of your
130f1978aafSBarry Smith    code you should use DMCreateGlobalVector().
131f1978aafSBarry Smith 
13268260fa0SJed Brown    VecStride*() operations can be useful when using DM with dof > 1
13368260fa0SJed Brown 
13468260fa0SJed Brown .keywords: distributed array, create, Global, vector
13568260fa0SJed Brown 
13668260fa0SJed Brown .seealso: DMCreateGlobalVector(), VecDuplicate(), VecDuplicateVecs(),
13768260fa0SJed Brown           DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMGlobalToLocalBegin(),
13868260fa0SJed Brown           DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMCreateLocalVector(), DMRestoreLocalVector()
13968260fa0SJed Brown           VecStrideMax(), VecStrideMin(), VecStrideNorm()
14068260fa0SJed Brown 
14168260fa0SJed Brown @*/
14268260fa0SJed Brown PetscErrorCode  DMGetGlobalVector(DM dm,Vec *g)
14368260fa0SJed Brown {
14468260fa0SJed Brown   PetscErrorCode ierr;
14568260fa0SJed Brown   PetscInt       i;
14668260fa0SJed Brown 
14768260fa0SJed Brown   PetscFunctionBegin;
14868260fa0SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14968260fa0SJed Brown   PetscValidPointer(g,2);
15068260fa0SJed Brown   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
15168260fa0SJed Brown     if (dm->globalin[i]) {
15268260fa0SJed Brown       *g              = dm->globalin[i];
1530298fd71SBarry Smith       dm->globalin[i] = NULL;
15468260fa0SJed Brown       goto alldone;
15568260fa0SJed Brown     }
15668260fa0SJed Brown   }
15768260fa0SJed Brown   ierr = DMCreateGlobalVector(dm,g);CHKERRQ(ierr);
15868260fa0SJed Brown 
15968260fa0SJed Brown alldone:
16068260fa0SJed Brown   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
16168260fa0SJed Brown     if (!dm->globalout[i]) {
16268260fa0SJed Brown       dm->globalout[i] = *g;
16368260fa0SJed Brown       break;
16468260fa0SJed Brown     }
16568260fa0SJed Brown   }
16668260fa0SJed Brown   PetscFunctionReturn(0);
16768260fa0SJed Brown }
16868260fa0SJed Brown 
16968260fa0SJed Brown /*@
17068260fa0SJed Brown    DMClearGlobalVectors - Destroys all the global vectors that have been stashed in this DM
17168260fa0SJed Brown 
17268260fa0SJed Brown    Collective on DM
17368260fa0SJed Brown 
17468260fa0SJed Brown    Input Parameter:
17568260fa0SJed Brown .  dm - the distributed array
17668260fa0SJed Brown 
17768260fa0SJed Brown    Level: developer
17868260fa0SJed Brown 
17968260fa0SJed Brown .keywords: distributed array, create, Global, vector
18068260fa0SJed Brown 
18168260fa0SJed Brown .seealso: DMCreateGlobalVector(), VecDuplicate(), VecDuplicateVecs(),
18268260fa0SJed Brown           DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMGlobalToLocalBegin(),
18368260fa0SJed Brown           DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMCreateLocalVector(), DMRestoreLocalVector()
18468260fa0SJed Brown           VecStrideMax(), VecStrideMin(), VecStrideNorm()
18568260fa0SJed Brown 
18668260fa0SJed Brown @*/
18768260fa0SJed Brown PetscErrorCode  DMClearGlobalVectors(DM dm)
18868260fa0SJed Brown {
18968260fa0SJed Brown   PetscErrorCode ierr;
19068260fa0SJed Brown   PetscInt       i;
19168260fa0SJed Brown 
19268260fa0SJed Brown   PetscFunctionBegin;
19368260fa0SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19468260fa0SJed Brown   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
19562d839e9SJed Brown     Vec g;
19668260fa0SJed Brown     if (dm->globalout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Clearing DM of global vectors that has a global vector obtained with DMGetGlobalVector()");
19762d839e9SJed Brown     g = dm->globalin[i];
19862d839e9SJed Brown     dm->globalin[i] = NULL;
19962d839e9SJed Brown     ierr = VecDestroy(&g);CHKERRQ(ierr);
20068260fa0SJed Brown   }
20168260fa0SJed Brown   PetscFunctionReturn(0);
20268260fa0SJed Brown }
20368260fa0SJed Brown 
20450eeb1caSToby Isaac /*@
20550eeb1caSToby Isaac    DMClearLocalVectors - Destroys all the local vectors that have been stashed in this DM
20650eeb1caSToby Isaac 
20750eeb1caSToby Isaac    Collective on DM
20850eeb1caSToby Isaac 
20950eeb1caSToby Isaac    Input Parameter:
21050eeb1caSToby Isaac .  dm - the distributed array
21150eeb1caSToby Isaac 
21250eeb1caSToby Isaac    Level: developer
21350eeb1caSToby Isaac 
21450eeb1caSToby Isaac .keywords: distributed array, create, Local, vector
21550eeb1caSToby Isaac 
21650eeb1caSToby Isaac .seealso: DMCreateLocalVector(), VecDuplicate(), VecDuplicateVecs(),
21750eeb1caSToby Isaac           DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMLocalToLocalBegin(),
21850eeb1caSToby Isaac           DMLocalToLocalEnd(), DMLocalToLocalBegin(), DMCreateLocalVector(), DMRestoreLocalVector()
21950eeb1caSToby Isaac           VecStrideMax(), VecStrideMin(), VecStrideNorm()
22050eeb1caSToby Isaac 
22150eeb1caSToby Isaac @*/
22250eeb1caSToby Isaac PetscErrorCode  DMClearLocalVectors(DM dm)
22350eeb1caSToby Isaac {
22450eeb1caSToby Isaac   PetscErrorCode ierr;
22550eeb1caSToby Isaac   PetscInt       i;
22650eeb1caSToby Isaac 
22750eeb1caSToby Isaac   PetscFunctionBegin;
22850eeb1caSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22950eeb1caSToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
23050eeb1caSToby Isaac     Vec g;
23150eeb1caSToby Isaac     if (dm->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Clearing DM of local vectors that has a local vector obtained with DMGetLocalVector()");
23250eeb1caSToby Isaac     g = dm->localin[i];
23350eeb1caSToby Isaac     dm->localin[i] = NULL;
23450eeb1caSToby Isaac     ierr = VecDestroy(&g);CHKERRQ(ierr);
23550eeb1caSToby Isaac   }
23650eeb1caSToby Isaac   PetscFunctionReturn(0);
23750eeb1caSToby Isaac }
23850eeb1caSToby Isaac 
23968260fa0SJed Brown /*@
24068260fa0SJed Brown    DMRestoreGlobalVector - Returns a Seq PETSc vector that
24168260fa0SJed Brown      obtained from DMGetGlobalVector(). Do not use with vector obtained via
24268260fa0SJed Brown      DMCreateGlobalVector().
24368260fa0SJed Brown 
24468260fa0SJed Brown    Not Collective
24568260fa0SJed Brown 
24668260fa0SJed Brown    Input Parameter:
24768260fa0SJed Brown +  dm - the distributed array
24868260fa0SJed Brown -  g - the global vector
24968260fa0SJed Brown 
25068260fa0SJed Brown    Level: beginner
25168260fa0SJed Brown 
25268260fa0SJed Brown .keywords: distributed array, create, global, vector
25368260fa0SJed Brown 
25468260fa0SJed Brown .seealso: DMCreateGlobalVector(), VecDuplicate(), VecDuplicateVecs(),
25568260fa0SJed Brown           DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMGlobalToGlobalBegin(),
25668260fa0SJed Brown           DMGlobalToGlobalEnd(), DMGlobalToGlobal(), DMCreateLocalVector(), DMGetGlobalVector()
25768260fa0SJed Brown @*/
25868260fa0SJed Brown PetscErrorCode  DMRestoreGlobalVector(DM dm,Vec *g)
25968260fa0SJed Brown {
26068260fa0SJed Brown   PetscErrorCode ierr;
26168260fa0SJed Brown   PetscInt       i,j;
26268260fa0SJed Brown 
26368260fa0SJed Brown   PetscFunctionBegin;
26468260fa0SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26568260fa0SJed Brown   PetscValidPointer(g,2);
266*85ea3df1SSatish Balay   ierr = VecSetErrorIfLocked(*g, 2);CHKERRQ(ierr);
26768260fa0SJed Brown   for (j=0; j<DM_MAX_WORK_VECTORS; j++) {
26868260fa0SJed Brown     if (*g == dm->globalout[j]) {
2690298fd71SBarry Smith       dm->globalout[j] = NULL;
27068260fa0SJed Brown       for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
27168260fa0SJed Brown         if (!dm->globalin[i]) {
27268260fa0SJed Brown           dm->globalin[i] = *g;
27368260fa0SJed Brown           goto alldone;
27468260fa0SJed Brown         }
27568260fa0SJed Brown       }
27668260fa0SJed Brown     }
27768260fa0SJed Brown   }
27868260fa0SJed Brown   ierr = VecDestroy(g);CHKERRQ(ierr);
27968260fa0SJed Brown alldone:
280dd6887adSBarry Smith   *g = NULL;
28168260fa0SJed Brown   PetscFunctionReturn(0);
28268260fa0SJed Brown }
28368260fa0SJed Brown 
284e77ac854SMatthew G. Knepley /*@C
285e77ac854SMatthew G. Knepley    DMHasNamedGlobalVector - check for a named, persistent global vector
286e77ac854SMatthew G. Knepley 
287e77ac854SMatthew G. Knepley    Not Collective
288e77ac854SMatthew G. Knepley 
289e77ac854SMatthew G. Knepley    Input Arguments:
290e77ac854SMatthew G. Knepley +  dm - DM to hold named vectors
291e77ac854SMatthew G. Knepley -  name - unique name for Vec
292e77ac854SMatthew G. Knepley 
293e77ac854SMatthew G. Knepley    Output Arguments:
294e77ac854SMatthew G. Knepley .  exists - true if the vector was previously created
295e77ac854SMatthew G. Knepley 
296e77ac854SMatthew G. Knepley    Level: developer
297e77ac854SMatthew G. Knepley 
298e77ac854SMatthew G. Knepley    Note: If a Vec with the given name does not exist, it is created.
299e77ac854SMatthew G. Knepley 
300e77ac854SMatthew G. Knepley .seealso: DMGetNamedGlobalVector(),DMRestoreNamedLocalVector()
301e77ac854SMatthew G. Knepley @*/
302e77ac854SMatthew G. Knepley PetscErrorCode DMHasNamedGlobalVector(DM dm,const char *name,PetscBool *exists)
303e77ac854SMatthew G. Knepley {
304e77ac854SMatthew G. Knepley   PetscErrorCode ierr;
305e77ac854SMatthew G. Knepley   DMNamedVecLink link;
306e77ac854SMatthew G. Knepley 
307e77ac854SMatthew G. Knepley   PetscFunctionBegin;
308e77ac854SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
309e77ac854SMatthew G. Knepley   PetscValidCharPointer(name,2);
310e77ac854SMatthew G. Knepley   PetscValidPointer(exists,3);
311e77ac854SMatthew G. Knepley   *exists = PETSC_FALSE;
312e77ac854SMatthew G. Knepley   for (link=dm->namedglobal; link; link=link->next) {
313e77ac854SMatthew G. Knepley     PetscBool match;
314e77ac854SMatthew G. Knepley     ierr = PetscStrcmp(name,link->name,&match);CHKERRQ(ierr);
315e77ac854SMatthew G. Knepley     if (match) {
316e77ac854SMatthew G. Knepley       *exists = PETSC_TRUE;
317e77ac854SMatthew G. Knepley       break;
318e77ac854SMatthew G. Knepley     }
319e77ac854SMatthew G. Knepley   }
320e77ac854SMatthew G. Knepley   PetscFunctionReturn(0);
321e77ac854SMatthew G. Knepley }
322e77ac854SMatthew G. Knepley 
32368260fa0SJed Brown /*@C
32468260fa0SJed Brown    DMGetNamedGlobalVector - get access to a named, persistent global vector
32568260fa0SJed Brown 
32668260fa0SJed Brown    Collective on DM
32768260fa0SJed Brown 
32868260fa0SJed Brown    Input Arguments:
32968260fa0SJed Brown +  dm - DM to hold named vectors
33068260fa0SJed Brown -  name - unique name for Vec
33168260fa0SJed Brown 
33268260fa0SJed Brown    Output Arguments:
33368260fa0SJed Brown .  X - named Vec
33468260fa0SJed Brown 
33568260fa0SJed Brown    Level: developer
33668260fa0SJed Brown 
33768260fa0SJed Brown    Note: If a Vec with the given name does not exist, it is created.
33868260fa0SJed Brown 
33968260fa0SJed Brown .seealso: DMRestoreNamedGlobalVector()
34068260fa0SJed Brown @*/
34168260fa0SJed Brown PetscErrorCode DMGetNamedGlobalVector(DM dm,const char *name,Vec *X)
34268260fa0SJed Brown {
34368260fa0SJed Brown   PetscErrorCode ierr;
34468260fa0SJed Brown   DMNamedVecLink link;
34568260fa0SJed Brown 
34668260fa0SJed Brown   PetscFunctionBegin;
34768260fa0SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34868260fa0SJed Brown   PetscValidCharPointer(name,2);
34968260fa0SJed Brown   PetscValidPointer(X,3);
35068260fa0SJed Brown   for (link=dm->namedglobal; link; link=link->next) {
35168260fa0SJed Brown     PetscBool match;
35268260fa0SJed Brown     ierr = PetscStrcmp(name,link->name,&match);CHKERRQ(ierr);
35368260fa0SJed Brown     if (match) {
354ce94432eSBarry Smith       if (link->status != DMVEC_STATUS_IN) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Vec name '%s' already checked out",name);
35568260fa0SJed Brown       goto found;
35668260fa0SJed Brown     }
35768260fa0SJed Brown   }
35868260fa0SJed Brown 
35968260fa0SJed Brown   /* Create the Vec */
360854ce69bSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
36168260fa0SJed Brown   ierr            = PetscStrallocpy(name,&link->name);CHKERRQ(ierr);
36268260fa0SJed Brown   ierr            = DMCreateGlobalVector(dm,&link->X);CHKERRQ(ierr);
36368260fa0SJed Brown   link->next      = dm->namedglobal;
36468260fa0SJed Brown   dm->namedglobal = link;
36568260fa0SJed Brown 
36668260fa0SJed Brown found:
36768260fa0SJed Brown   *X           = link->X;
36868260fa0SJed Brown   link->status = DMVEC_STATUS_OUT;
36968260fa0SJed Brown   PetscFunctionReturn(0);
37068260fa0SJed Brown }
37168260fa0SJed Brown 
37268260fa0SJed Brown /*@C
37368260fa0SJed Brown    DMRestoreNamedGlobalVector - restore access to a named, persistent global vector
37468260fa0SJed Brown 
37568260fa0SJed Brown    Collective on DM
37668260fa0SJed Brown 
37768260fa0SJed Brown    Input Arguments:
37868260fa0SJed Brown +  dm - DM on which the vector was gotten
37968260fa0SJed Brown .  name - name under which the vector was gotten
38068260fa0SJed Brown -  X - Vec to restore
38168260fa0SJed Brown 
38268260fa0SJed Brown    Output Arguments:
38368260fa0SJed Brown 
38468260fa0SJed Brown    Level: developer
38568260fa0SJed Brown 
38668260fa0SJed Brown .seealso: DMGetNamedGlobalVector()
38768260fa0SJed Brown @*/
38868260fa0SJed Brown PetscErrorCode DMRestoreNamedGlobalVector(DM dm,const char *name,Vec *X)
38968260fa0SJed Brown {
39068260fa0SJed Brown   PetscErrorCode ierr;
39168260fa0SJed Brown   DMNamedVecLink link;
39268260fa0SJed Brown 
39368260fa0SJed Brown   PetscFunctionBegin;
39468260fa0SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39568260fa0SJed Brown   PetscValidCharPointer(name,2);
39668260fa0SJed Brown   PetscValidPointer(X,3);
39768260fa0SJed Brown   PetscValidHeaderSpecific(*X,VEC_CLASSID,3);
39868260fa0SJed Brown   for (link=dm->namedglobal; link; link=link->next) {
39968260fa0SJed Brown     PetscBool match;
40068260fa0SJed Brown     ierr = PetscStrcmp(name,link->name,&match);CHKERRQ(ierr);
40168260fa0SJed Brown     if (match) {
402ce94432eSBarry Smith       if (link->status != DMVEC_STATUS_OUT) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Vec name '%s' was not checked out",name);
403ce94432eSBarry Smith       if (link->X != *X) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_INCOMP,"Attempt to restore Vec name '%s', but Vec does not match the cache",name);
40468260fa0SJed Brown       link->status = DMVEC_STATUS_IN;
4050298fd71SBarry Smith       *X           = NULL;
40668260fa0SJed Brown       PetscFunctionReturn(0);
40768260fa0SJed Brown     }
40868260fa0SJed Brown   }
409ce94432eSBarry Smith   SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_INCOMP,"Could not find Vec name '%s' to restore",name);
41068260fa0SJed Brown   PetscFunctionReturn(0);
41168260fa0SJed Brown }
4122348bcf4SPeter Brune 
413e77ac854SMatthew G. Knepley /*@C
414e77ac854SMatthew G. Knepley    DMHasNamedLocalVector - check for a named, persistent local vector
415e77ac854SMatthew G. Knepley 
416e77ac854SMatthew G. Knepley    Not Collective
417e77ac854SMatthew G. Knepley 
418e77ac854SMatthew G. Knepley    Input Arguments:
419e77ac854SMatthew G. Knepley +  dm - DM to hold named vectors
420e77ac854SMatthew G. Knepley -  name - unique name for Vec
421e77ac854SMatthew G. Knepley 
422e77ac854SMatthew G. Knepley    Output Arguments:
423e77ac854SMatthew G. Knepley .  exists - true if the vector was previously created
424e77ac854SMatthew G. Knepley 
425e77ac854SMatthew G. Knepley    Level: developer
426e77ac854SMatthew G. Knepley 
427e77ac854SMatthew G. Knepley    Note: If a Vec with the given name does not exist, it is created.
428e77ac854SMatthew G. Knepley 
429e77ac854SMatthew G. Knepley .seealso: DMGetNamedGlobalVector(),DMRestoreNamedLocalVector()
430e77ac854SMatthew G. Knepley @*/
431e77ac854SMatthew G. Knepley PetscErrorCode DMHasNamedLocalVector(DM dm,const char *name,PetscBool *exists)
432e77ac854SMatthew G. Knepley {
433e77ac854SMatthew G. Knepley   PetscErrorCode ierr;
434e77ac854SMatthew G. Knepley   DMNamedVecLink link;
435e77ac854SMatthew G. Knepley 
436e77ac854SMatthew G. Knepley   PetscFunctionBegin;
437e77ac854SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
438e77ac854SMatthew G. Knepley   PetscValidCharPointer(name,2);
439e77ac854SMatthew G. Knepley   PetscValidPointer(exists,3);
440e77ac854SMatthew G. Knepley   *exists = PETSC_FALSE;
441e77ac854SMatthew G. Knepley   for (link=dm->namedlocal; link; link=link->next) {
442e77ac854SMatthew G. Knepley     PetscBool match;
443e77ac854SMatthew G. Knepley     ierr = PetscStrcmp(name,link->name,&match);CHKERRQ(ierr);
444e77ac854SMatthew G. Knepley     if (match) {
445e77ac854SMatthew G. Knepley       *exists = PETSC_TRUE;
446e77ac854SMatthew G. Knepley       break;
447e77ac854SMatthew G. Knepley     }
448e77ac854SMatthew G. Knepley   }
449e77ac854SMatthew G. Knepley   PetscFunctionReturn(0);
450e77ac854SMatthew G. Knepley }
451e77ac854SMatthew G. Knepley 
4522348bcf4SPeter Brune /*@C
4532348bcf4SPeter Brune    DMGetNamedLocalVector - get access to a named, persistent local vector
4542348bcf4SPeter Brune 
4552348bcf4SPeter Brune    Not Collective
4562348bcf4SPeter Brune 
4572348bcf4SPeter Brune    Input Arguments:
4582348bcf4SPeter Brune +  dm - DM to hold named vectors
4592348bcf4SPeter Brune -  name - unique name for Vec
4602348bcf4SPeter Brune 
4612348bcf4SPeter Brune    Output Arguments:
4622348bcf4SPeter Brune .  X - named Vec
4632348bcf4SPeter Brune 
4642348bcf4SPeter Brune    Level: developer
4652348bcf4SPeter Brune 
4662348bcf4SPeter Brune    Note: If a Vec with the given name does not exist, it is created.
4672348bcf4SPeter Brune 
4682348bcf4SPeter Brune .seealso: DMGetNamedGlobalVector(),DMRestoreNamedLocalVector()
4692348bcf4SPeter Brune @*/
4702348bcf4SPeter Brune PetscErrorCode DMGetNamedLocalVector(DM dm,const char *name,Vec *X)
4712348bcf4SPeter Brune {
4722348bcf4SPeter Brune   PetscErrorCode ierr;
4732348bcf4SPeter Brune   DMNamedVecLink link;
4742348bcf4SPeter Brune 
4752348bcf4SPeter Brune   PetscFunctionBegin;
4762348bcf4SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4772348bcf4SPeter Brune   PetscValidCharPointer(name,2);
4782348bcf4SPeter Brune   PetscValidPointer(X,3);
4792348bcf4SPeter Brune   for (link=dm->namedlocal; link; link=link->next) {
4802348bcf4SPeter Brune     PetscBool match;
4812348bcf4SPeter Brune     ierr = PetscStrcmp(name,link->name,&match);CHKERRQ(ierr);
4822348bcf4SPeter Brune     if (match) {
483ce94432eSBarry Smith       if (link->status != DMVEC_STATUS_IN) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Vec name '%s' already checked out",name);
4842348bcf4SPeter Brune       goto found;
4852348bcf4SPeter Brune     }
4862348bcf4SPeter Brune   }
4872348bcf4SPeter Brune 
4882348bcf4SPeter Brune   /* Create the Vec */
489854ce69bSBarry Smith   ierr           = PetscNew(&link);CHKERRQ(ierr);
4902348bcf4SPeter Brune   ierr           = PetscStrallocpy(name,&link->name);CHKERRQ(ierr);
4912348bcf4SPeter Brune   ierr           = DMCreateLocalVector(dm,&link->X);CHKERRQ(ierr);
4922348bcf4SPeter Brune   link->next     = dm->namedlocal;
4932348bcf4SPeter Brune   dm->namedlocal = link;
4942348bcf4SPeter Brune 
4952348bcf4SPeter Brune found:
4962348bcf4SPeter Brune   *X           = link->X;
4972348bcf4SPeter Brune   link->status = DMVEC_STATUS_OUT;
4982348bcf4SPeter Brune   PetscFunctionReturn(0);
4992348bcf4SPeter Brune }
5002348bcf4SPeter Brune 
5012348bcf4SPeter Brune /*@C
5022348bcf4SPeter Brune    DMRestoreNamedLocalVector - restore access to a named, persistent local vector
5032348bcf4SPeter Brune 
5042348bcf4SPeter Brune    Not Collective
5052348bcf4SPeter Brune 
5062348bcf4SPeter Brune    Input Arguments:
5072348bcf4SPeter Brune +  dm - DM on which the vector was gotten
5082348bcf4SPeter Brune .  name - name under which the vector was gotten
5092348bcf4SPeter Brune -  X - Vec to restore
5102348bcf4SPeter Brune 
5112348bcf4SPeter Brune    Output Arguments:
5122348bcf4SPeter Brune 
5132348bcf4SPeter Brune    Level: developer
5142348bcf4SPeter Brune 
5152348bcf4SPeter Brune .seealso: DMRestoreNamedGlobalVector(),DMGetNamedLocalVector()
5162348bcf4SPeter Brune @*/
5172348bcf4SPeter Brune PetscErrorCode DMRestoreNamedLocalVector(DM dm,const char *name,Vec *X)
5182348bcf4SPeter Brune {
5192348bcf4SPeter Brune   PetscErrorCode ierr;
5202348bcf4SPeter Brune   DMNamedVecLink link;
5212348bcf4SPeter Brune 
5222348bcf4SPeter Brune   PetscFunctionBegin;
5232348bcf4SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5242348bcf4SPeter Brune   PetscValidCharPointer(name,2);
5252348bcf4SPeter Brune   PetscValidPointer(X,3);
5262348bcf4SPeter Brune   PetscValidHeaderSpecific(*X,VEC_CLASSID,3);
5272348bcf4SPeter Brune   for (link=dm->namedlocal; link; link=link->next) {
5282348bcf4SPeter Brune     PetscBool match;
5292348bcf4SPeter Brune     ierr = PetscStrcmp(name,link->name,&match);CHKERRQ(ierr);
5302348bcf4SPeter Brune     if (match) {
531ce94432eSBarry Smith       if (link->status != DMVEC_STATUS_OUT) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Vec name '%s' was not checked out",name);
532ce94432eSBarry Smith       if (link->X != *X) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_INCOMP,"Attempt to restore Vec name '%s', but Vec does not match the cache",name);
5332348bcf4SPeter Brune       link->status = DMVEC_STATUS_IN;
5340298fd71SBarry Smith       *X           = NULL;
5352348bcf4SPeter Brune       PetscFunctionReturn(0);
5362348bcf4SPeter Brune     }
5372348bcf4SPeter Brune   }
538ce94432eSBarry Smith   SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_INCOMP,"Could not find Vec name '%s' to restore",name);
5392348bcf4SPeter Brune   PetscFunctionReturn(0);
5402348bcf4SPeter Brune }
541