xref: /petsc/src/dm/impls/da/dadist.c (revision 9a42bb27a39f0cdf3306a1e22d33cd9809484eaa)
147c6ae99SBarry Smith #define PETSCDM_DLL
247c6ae99SBarry Smith 
347c6ae99SBarry Smith /*
447c6ae99SBarry Smith   Code for manipulating distributed regular arrays in parallel.
547c6ae99SBarry Smith */
647c6ae99SBarry Smith 
747c6ae99SBarry Smith #include "private/daimpl.h"    /*I   "petscda.h"   I*/
847c6ae99SBarry Smith 
947c6ae99SBarry Smith #undef __FUNCT__
1047c6ae99SBarry Smith #define __FUNCT__ "DACreateGlobalVector"
1147c6ae99SBarry Smith /*@
1247c6ae99SBarry Smith    DACreateGlobalVector - Creates a parallel PETSc vector that
1347c6ae99SBarry Smith    may be used with the DAXXX routines.
1447c6ae99SBarry Smith 
1547c6ae99SBarry Smith    Collective on DA
1647c6ae99SBarry Smith 
1747c6ae99SBarry Smith    Input Parameter:
1847c6ae99SBarry Smith .  da - the distributed array
1947c6ae99SBarry Smith 
2047c6ae99SBarry Smith    Output Parameter:
2147c6ae99SBarry Smith .  g - the distributed global vector
2247c6ae99SBarry Smith 
2347c6ae99SBarry Smith    Level: beginner
2447c6ae99SBarry Smith 
2547c6ae99SBarry Smith    Note:
2647c6ae99SBarry Smith    The output parameter, g, is a regular PETSc vector that should be destroyed
2747c6ae99SBarry Smith    with a call to VecDestroy() when usage is finished.
2847c6ae99SBarry Smith 
2947c6ae99SBarry Smith    When you view this vector (or one obtained via VecDuplicate()) it is printed in the global natural ordering NOT
3047c6ae99SBarry Smith    in the PETSc parallel global ordering that is used internally. Similarly VecLoad() into this vector loads from a global natural ordering.
3147c6ae99SBarry Smith    This means that vectors saved to disk from one DA parallel distribution can be reloaded into a different DA parallel distribution correctly.
3247c6ae99SBarry Smith 
3347c6ae99SBarry Smith .keywords: distributed array, create, global, distributed, vector
3447c6ae99SBarry Smith 
3547c6ae99SBarry Smith .seealso: DACreateLocalVector(), VecDuplicate(), VecDuplicateVecs(),
36*9a42bb27SBarry Smith           DACreate1d(), DACreate2d(), DACreate3d(), DMGlobalToLocalBegin(),
37*9a42bb27SBarry Smith           DMGlobalToLocalEnd(), DALocalToGlobalBegin(), DACreateNaturalVector()
3847c6ae99SBarry Smith @*/
39*9a42bb27SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DACreateGlobalVector(DM da,Vec* g)
4047c6ae99SBarry Smith {
4147c6ae99SBarry Smith   PetscErrorCode ierr;
4247c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
4347c6ae99SBarry Smith 
4447c6ae99SBarry Smith   PetscFunctionBegin;
4547c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
4647c6ae99SBarry Smith   PetscValidPointer(g,2);
4747c6ae99SBarry Smith   ierr = VecCreate(((PetscObject)da)->comm,g);CHKERRQ(ierr);
4847c6ae99SBarry Smith   ierr = VecSetSizes(*g,dd->Nlocal,PETSC_DETERMINE);CHKERRQ(ierr);
4947c6ae99SBarry Smith   ierr = VecSetType(*g,da->vectype);CHKERRQ(ierr);
5047c6ae99SBarry Smith   ierr = PetscObjectCompose((PetscObject)*g,"DA",(PetscObject)da);CHKERRQ(ierr);
5147c6ae99SBarry Smith   ierr = VecSetLocalToGlobalMapping(*g,dd->ltogmap);CHKERRQ(ierr);
5247c6ae99SBarry Smith   ierr = VecSetLocalToGlobalMappingBlock(*g,dd->ltogmapb);CHKERRQ(ierr);
5347c6ae99SBarry Smith   ierr = VecSetBlockSize(*g,dd->w);CHKERRQ(ierr);
5447c6ae99SBarry Smith   ierr = VecSetOperation(*g,VECOP_VIEW,(void(*)(void))VecView_MPI_DA);CHKERRQ(ierr);
5547c6ae99SBarry Smith   ierr = VecSetOperation(*g,VECOP_LOAD,(void(*)(void))VecLoad_Default_DA);CHKERRQ(ierr);
5647c6ae99SBarry Smith   PetscFunctionReturn(0);
5747c6ae99SBarry Smith }
5847c6ae99SBarry Smith 
5947c6ae99SBarry Smith #undef __FUNCT__
6047c6ae99SBarry Smith #define __FUNCT__ "DACreateNaturalVector"
6147c6ae99SBarry Smith /*@
6247c6ae99SBarry Smith    DACreateNaturalVector - Creates a parallel PETSc vector that
6347c6ae99SBarry Smith    will hold vector values in the natural numbering, rather than in
6447c6ae99SBarry Smith    the PETSc parallel numbering associated with the DA.
6547c6ae99SBarry Smith 
6647c6ae99SBarry Smith    Collective on DA
6747c6ae99SBarry Smith 
6847c6ae99SBarry Smith    Input Parameter:
6947c6ae99SBarry Smith .  da - the distributed array
7047c6ae99SBarry Smith 
7147c6ae99SBarry Smith    Output Parameter:
7247c6ae99SBarry Smith .  g - the distributed global vector
7347c6ae99SBarry Smith 
7447c6ae99SBarry Smith    Level: developer
7547c6ae99SBarry Smith 
7647c6ae99SBarry Smith    Note:
7747c6ae99SBarry Smith    The output parameter, g, is a regular PETSc vector that should be destroyed
7847c6ae99SBarry Smith    with a call to VecDestroy() when usage is finished.
7947c6ae99SBarry Smith 
8047c6ae99SBarry Smith    The number of local entries in the vector on each process is the same
8147c6ae99SBarry Smith    as in a vector created with DACreateGlobalVector().
8247c6ae99SBarry Smith 
8347c6ae99SBarry Smith .keywords: distributed array, create, global, distributed, vector
8447c6ae99SBarry Smith 
8547c6ae99SBarry Smith .seealso: DACreateLocalVector(), VecDuplicate(), VecDuplicateVecs(),
86*9a42bb27SBarry Smith           DACreate1d(), DACreate2d(), DACreate3d(), DMGlobalToLocalBegin(),
87*9a42bb27SBarry Smith           DMGlobalToLocalEnd(), DALocalToGlobalBegin()
8847c6ae99SBarry Smith @*/
89*9a42bb27SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DACreateNaturalVector(DM da,Vec* g)
9047c6ae99SBarry Smith {
9147c6ae99SBarry Smith   PetscErrorCode ierr;
9247c6ae99SBarry Smith   PetscInt       cnt;
9347c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
9447c6ae99SBarry Smith 
9547c6ae99SBarry Smith   PetscFunctionBegin;
9647c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
9747c6ae99SBarry Smith   PetscValidPointer(g,2);
9847c6ae99SBarry Smith   if (dd->natural) {
9947c6ae99SBarry Smith     ierr = PetscObjectGetReference((PetscObject)dd->natural,&cnt);CHKERRQ(ierr);
10047c6ae99SBarry Smith     if (cnt == 1) { /* object is not currently used by anyone */
10147c6ae99SBarry Smith       ierr = PetscObjectReference((PetscObject)dd->natural);CHKERRQ(ierr);
10247c6ae99SBarry Smith       *g   = dd->natural;
10347c6ae99SBarry Smith     } else {
10447c6ae99SBarry Smith       ierr = VecDuplicate(dd->natural,g);CHKERRQ(ierr);
10547c6ae99SBarry Smith     }
10647c6ae99SBarry Smith   } else { /* create the first version of this guy */
10747c6ae99SBarry Smith     ierr = VecCreateMPI(((PetscObject)da)->comm,dd->Nlocal,PETSC_DETERMINE,g);CHKERRQ(ierr);
10847c6ae99SBarry Smith     ierr = VecSetBlockSize(*g, dd->w);CHKERRQ(ierr);
10947c6ae99SBarry Smith     ierr = PetscObjectReference((PetscObject)*g);CHKERRQ(ierr);
11047c6ae99SBarry Smith     dd->natural = *g;
11147c6ae99SBarry Smith   }
11247c6ae99SBarry Smith   PetscFunctionReturn(0);
11347c6ae99SBarry Smith }
11447c6ae99SBarry Smith 
11547c6ae99SBarry Smith 
11647c6ae99SBarry Smith 
117