xref: /petsc/src/dm/impls/da/dadist.c (revision 3ca8f39cfabd2c8520a25f135cf7f3fdd2b2d0b5)
147c6ae99SBarry Smith 
247c6ae99SBarry Smith /*
347c6ae99SBarry Smith   Code for manipulating distributed regular arrays in parallel.
447c6ae99SBarry Smith */
547c6ae99SBarry Smith 
6af0996ceSBarry Smith #include <petsc/private/dmdaimpl.h>    /*I   "petscdmda.h"   I*/
747c6ae99SBarry Smith 
87087cfbeSBarry Smith PetscErrorCode  VecDuplicate_MPI_DA(Vec g,Vec *gg)
92dcb2ebcSBarry Smith {
102dcb2ebcSBarry Smith   PetscErrorCode ierr;
112dcb2ebcSBarry Smith   DM             da;
12077aedafSJed Brown   PetscLayout    map;
132dcb2ebcSBarry Smith 
142dcb2ebcSBarry Smith   PetscFunctionBegin;
15c688c046SMatthew G Knepley   ierr = VecGetDM(g, &da);CHKERRQ(ierr);
162dcb2ebcSBarry Smith   ierr = DMCreateGlobalVector(da,gg);CHKERRQ(ierr);
17077aedafSJed Brown   ierr = VecGetLayout(g,&map);CHKERRQ(ierr);
18077aedafSJed Brown   ierr = VecSetLayout(*gg,map);CHKERRQ(ierr);
192dcb2ebcSBarry Smith   PetscFunctionReturn(0);
202dcb2ebcSBarry Smith }
212dcb2ebcSBarry Smith 
222dcb2ebcSBarry Smith 
237087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector_DA(DM da,Vec *g)
2447c6ae99SBarry Smith {
2547c6ae99SBarry Smith   PetscErrorCode ierr;
2647c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
2747c6ae99SBarry Smith 
2847c6ae99SBarry Smith   PetscFunctionBegin;
2947c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
3047c6ae99SBarry Smith   PetscValidPointer(g,2);
3111689aebSJed Brown   if (da->defaultSection) {
3211689aebSJed Brown     ierr = DMCreateGlobalVector_Section_Private(da,g);CHKERRQ(ierr);
3320001361SMatthew G. Knepley     /* The view and load functions break for general layouts */
3420001361SMatthew G. Knepley     PetscFunctionReturn(0);
3511689aebSJed Brown   } else {
36ce94432eSBarry Smith     ierr = VecCreate(PetscObjectComm((PetscObject)da),g);CHKERRQ(ierr);
3747c6ae99SBarry Smith     ierr = VecSetSizes(*g,dd->Nlocal,PETSC_DETERMINE);CHKERRQ(ierr);
38401ddaa8SBarry Smith     ierr = VecSetBlockSize(*g,dd->w);CHKERRQ(ierr);
3947c6ae99SBarry Smith     ierr = VecSetType(*g,da->vectype);CHKERRQ(ierr);
40c688c046SMatthew G Knepley     ierr = VecSetDM(*g, da);CHKERRQ(ierr);
411411c6eeSJed Brown     ierr = VecSetLocalToGlobalMapping(*g,da->ltogmap);CHKERRQ(ierr);
4211689aebSJed Brown   }
4347c6ae99SBarry Smith   ierr = VecSetOperation(*g,VECOP_VIEW,(void (*)(void))VecView_MPI_DA);CHKERRQ(ierr);
4447c6ae99SBarry Smith   ierr = VecSetOperation(*g,VECOP_LOAD,(void (*)(void))VecLoad_Default_DA);CHKERRQ(ierr);
452dcb2ebcSBarry Smith   ierr = VecSetOperation(*g,VECOP_DUPLICATE,(void (*)(void))VecDuplicate_MPI_DA);CHKERRQ(ierr);
4647c6ae99SBarry Smith   PetscFunctionReturn(0);
4747c6ae99SBarry Smith }
4847c6ae99SBarry Smith 
4947c6ae99SBarry Smith /*@
50aa219208SBarry Smith    DMDACreateNaturalVector - Creates a parallel PETSc vector that
5147c6ae99SBarry Smith    will hold vector values in the natural numbering, rather than in
52aa219208SBarry Smith    the PETSc parallel numbering associated with the DMDA.
5347c6ae99SBarry Smith 
54*3ca8f39cSDave May    Collective
5547c6ae99SBarry Smith 
5647c6ae99SBarry Smith    Input Parameter:
5747c6ae99SBarry Smith .  da - the distributed array
5847c6ae99SBarry Smith 
5947c6ae99SBarry Smith    Output Parameter:
6047c6ae99SBarry Smith .  g - the distributed global vector
6147c6ae99SBarry Smith 
6247c6ae99SBarry Smith    Level: developer
6347c6ae99SBarry Smith 
6447c6ae99SBarry Smith    Note:
6547c6ae99SBarry Smith    The output parameter, g, is a regular PETSc vector that should be destroyed
6647c6ae99SBarry Smith    with a call to VecDestroy() when usage is finished.
6747c6ae99SBarry Smith 
6847c6ae99SBarry Smith    The number of local entries in the vector on each process is the same
69564755cdSBarry Smith    as in a vector created with DMCreateGlobalVector().
7047c6ae99SBarry Smith 
7147c6ae99SBarry Smith .keywords: distributed array, create, global, distributed, vector
7247c6ae99SBarry Smith 
73564755cdSBarry Smith .seealso: DMCreateLocalVector(), VecDuplicate(), VecDuplicateVecs(),
74aa219208SBarry Smith           DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMGlobalToLocalBegin(),
75aa219208SBarry Smith           DMGlobalToLocalEnd(), DMDALocalToGlobalBegin()
7647c6ae99SBarry Smith @*/
777087cfbeSBarry Smith PetscErrorCode  DMDACreateNaturalVector(DM da,Vec *g)
7847c6ae99SBarry Smith {
7947c6ae99SBarry Smith   PetscErrorCode ierr;
8047c6ae99SBarry Smith   PetscInt       cnt;
8147c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
8247c6ae99SBarry Smith 
8347c6ae99SBarry Smith   PetscFunctionBegin;
84a9a02de4SBarry Smith   PetscValidHeaderSpecificType(da,DM_CLASSID,1,DMDA);
8547c6ae99SBarry Smith   PetscValidPointer(g,2);
8647c6ae99SBarry Smith   if (dd->natural) {
8747c6ae99SBarry Smith     ierr = PetscObjectGetReference((PetscObject)dd->natural,&cnt);CHKERRQ(ierr);
8847c6ae99SBarry Smith     if (cnt == 1) { /* object is not currently used by anyone */
8947c6ae99SBarry Smith       ierr = PetscObjectReference((PetscObject)dd->natural);CHKERRQ(ierr);
9047c6ae99SBarry Smith       *g   = dd->natural;
9147c6ae99SBarry Smith     } else {
9247c6ae99SBarry Smith       ierr = VecDuplicate(dd->natural,g);CHKERRQ(ierr);
9347c6ae99SBarry Smith     }
9447c6ae99SBarry Smith   } else { /* create the first version of this guy */
95ce94432eSBarry Smith     ierr = VecCreate(PetscObjectComm((PetscObject)da),g);CHKERRQ(ierr);
96a34bdc0bSBarry Smith     ierr = VecSetSizes(*g,dd->Nlocal,PETSC_DETERMINE);CHKERRQ(ierr);
9747c6ae99SBarry Smith     ierr = VecSetBlockSize(*g, dd->w);CHKERRQ(ierr);
98265bdeeeSBarry Smith     ierr = VecSetType(*g,da->vectype);CHKERRQ(ierr);
9947c6ae99SBarry Smith     ierr = PetscObjectReference((PetscObject)*g);CHKERRQ(ierr);
1008865f1eaSKarl Rupp 
10147c6ae99SBarry Smith     dd->natural = *g;
10247c6ae99SBarry Smith   }
10347c6ae99SBarry Smith   PetscFunctionReturn(0);
10447c6ae99SBarry Smith }
10547c6ae99SBarry Smith 
10647c6ae99SBarry Smith 
10747c6ae99SBarry Smith 
108