xref: /petsc/src/dm/impls/da/dagtona.c (revision a9a02de4071215c8808ed212ec20dc897ad1c2a4)
147c6ae99SBarry Smith 
247c6ae99SBarry Smith /*
347c6ae99SBarry Smith      Tools to help solve the coarse grid problem redundantly.
447c6ae99SBarry Smith   Provides two scatter contexts that (1) map from the usual global vector
547c6ae99SBarry Smith   to all processors the entire vector in NATURAL numbering and (2)
647c6ae99SBarry Smith   from the entire vector on each processor in natural numbering extracts
747c6ae99SBarry Smith   out this processors piece in GLOBAL numbering
847c6ae99SBarry Smith */
947c6ae99SBarry Smith 
10af0996ceSBarry Smith #include <petsc/private/dmdaimpl.h>    /*I   "petscdmda.h"   I*/
1147c6ae99SBarry Smith 
1247c6ae99SBarry Smith /*@
13aa219208SBarry Smith    DMDAGlobalToNaturalAllCreate - Creates a scatter context that maps from the
1447c6ae99SBarry Smith      global vector the entire vector to each processor in natural numbering
1547c6ae99SBarry Smith 
16aa219208SBarry Smith    Collective on DMDA
1747c6ae99SBarry Smith 
1847c6ae99SBarry Smith    Input Parameter:
1947c6ae99SBarry Smith .  da - the distributed array context
2047c6ae99SBarry Smith 
2147c6ae99SBarry Smith    Output Parameter:
2247c6ae99SBarry Smith .  scatter - the scatter context
2347c6ae99SBarry Smith 
2447c6ae99SBarry Smith    Level: advanced
2547c6ae99SBarry Smith 
2647c6ae99SBarry Smith .keywords: distributed array, global to local, begin, coarse problem
2747c6ae99SBarry Smith 
28aa219208SBarry Smith .seealso: DMDAGlobalToNaturalEnd(), DMLocalToGlobalBegin(), DMDACreate2d(),
29aa219208SBarry Smith           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMDACreateNaturalVector()
3047c6ae99SBarry Smith @*/
317087cfbeSBarry Smith PetscErrorCode  DMDAGlobalToNaturalAllCreate(DM da,VecScatter *scatter)
3247c6ae99SBarry Smith {
3347c6ae99SBarry Smith   PetscErrorCode ierr;
3447c6ae99SBarry Smith   PetscInt       N;
3547c6ae99SBarry Smith   IS             from,to;
3647c6ae99SBarry Smith   Vec            tmplocal,global;
3747c6ae99SBarry Smith   AO             ao;
3847c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
3947c6ae99SBarry Smith 
4047c6ae99SBarry Smith   PetscFunctionBegin;
41*a9a02de4SBarry Smith   PetscValidHeaderSpecificType(da,DM_CLASSID,1,DMDA);
4247c6ae99SBarry Smith   PetscValidPointer(scatter,2);
43aa219208SBarry Smith   ierr = DMDAGetAO(da,&ao);CHKERRQ(ierr);
4447c6ae99SBarry Smith 
4547c6ae99SBarry Smith   /* create the scatter context */
46ce94432eSBarry Smith   ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject)da),dd->w,dd->Nlocal,PETSC_DETERMINE,0,&global);CHKERRQ(ierr);
4747c6ae99SBarry Smith   ierr = VecGetSize(global,&N);CHKERRQ(ierr);
48ce94432eSBarry Smith   ierr = ISCreateStride(PetscObjectComm((PetscObject)da),N,0,1,&to);CHKERRQ(ierr);
4947c6ae99SBarry Smith   ierr = AOPetscToApplicationIS(ao,to);CHKERRQ(ierr);
50ce94432eSBarry Smith   ierr = ISCreateStride(PetscObjectComm((PetscObject)da),N,0,1,&from);CHKERRQ(ierr);
51778a2246SBarry Smith   ierr = VecCreateSeqWithArray(PETSC_COMM_SELF,dd->w,N,0,&tmplocal);CHKERRQ(ierr);
5247c6ae99SBarry Smith   ierr = VecScatterCreate(global,from,tmplocal,to,scatter);CHKERRQ(ierr);
53fcfd50ebSBarry Smith   ierr = VecDestroy(&tmplocal);CHKERRQ(ierr);
54fcfd50ebSBarry Smith   ierr = VecDestroy(&global);CHKERRQ(ierr);
55fcfd50ebSBarry Smith   ierr = ISDestroy(&from);CHKERRQ(ierr);
56fcfd50ebSBarry Smith   ierr = ISDestroy(&to);CHKERRQ(ierr);
5747c6ae99SBarry Smith   PetscFunctionReturn(0);
5847c6ae99SBarry Smith }
5947c6ae99SBarry Smith 
6047c6ae99SBarry Smith /*@
61aa219208SBarry Smith    DMDANaturalAllToGlobalCreate - Creates a scatter context that maps from a copy
6247c6ae99SBarry Smith      of the entire vector on each processor to its local part in the global vector.
6347c6ae99SBarry Smith 
64aa219208SBarry Smith    Collective on DMDA
6547c6ae99SBarry Smith 
6647c6ae99SBarry Smith    Input Parameter:
6747c6ae99SBarry Smith .  da - the distributed array context
6847c6ae99SBarry Smith 
6947c6ae99SBarry Smith    Output Parameter:
7047c6ae99SBarry Smith .  scatter - the scatter context
7147c6ae99SBarry Smith 
7247c6ae99SBarry Smith    Level: advanced
7347c6ae99SBarry Smith 
7447c6ae99SBarry Smith .keywords: distributed array, global to local, begin, coarse problem
7547c6ae99SBarry Smith 
76aa219208SBarry Smith .seealso: DMDAGlobalToNaturalEnd(), DMLocalToGlobalBegin(), DMDACreate2d(),
77aa219208SBarry Smith           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMDACreateNaturalVector()
7847c6ae99SBarry Smith @*/
797087cfbeSBarry Smith PetscErrorCode  DMDANaturalAllToGlobalCreate(DM da,VecScatter *scatter)
8047c6ae99SBarry Smith {
8147c6ae99SBarry Smith   PetscErrorCode ierr;
8247c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
8347c6ae99SBarry Smith   PetscInt       M,m = dd->Nlocal,start;
8447c6ae99SBarry Smith   IS             from,to;
8547c6ae99SBarry Smith   Vec            tmplocal,global;
8647c6ae99SBarry Smith   AO             ao;
8747c6ae99SBarry Smith 
8847c6ae99SBarry Smith   PetscFunctionBegin;
89*a9a02de4SBarry Smith   PetscValidHeaderSpecificType(da,DM_CLASSID,1,DMDA);
9047c6ae99SBarry Smith   PetscValidPointer(scatter,2);
91aa219208SBarry Smith   ierr = DMDAGetAO(da,&ao);CHKERRQ(ierr);
9247c6ae99SBarry Smith 
9347c6ae99SBarry Smith   /* create the scatter context */
94b2566f29SBarry Smith   ierr = MPIU_Allreduce(&m,&M,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)da));CHKERRQ(ierr);
95ce94432eSBarry Smith   ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject)da),dd->w,m,PETSC_DETERMINE,0,&global);CHKERRQ(ierr);
960298fd71SBarry Smith   ierr = VecGetOwnershipRange(global,&start,NULL);CHKERRQ(ierr);
97ce94432eSBarry Smith   ierr = ISCreateStride(PetscObjectComm((PetscObject)da),m,start,1,&from);CHKERRQ(ierr);
9847c6ae99SBarry Smith   ierr = AOPetscToApplicationIS(ao,from);CHKERRQ(ierr);
99ce94432eSBarry Smith   ierr = ISCreateStride(PetscObjectComm((PetscObject)da),m,start,1,&to);CHKERRQ(ierr);
100778a2246SBarry Smith   ierr = VecCreateSeqWithArray(PETSC_COMM_SELF,dd->w,M,0,&tmplocal);CHKERRQ(ierr);
10147c6ae99SBarry Smith   ierr = VecScatterCreate(tmplocal,from,global,to,scatter);CHKERRQ(ierr);
102fcfd50ebSBarry Smith   ierr = VecDestroy(&tmplocal);CHKERRQ(ierr);
103fcfd50ebSBarry Smith   ierr = VecDestroy(&global);CHKERRQ(ierr);
104fcfd50ebSBarry Smith   ierr = ISDestroy(&from);CHKERRQ(ierr);
105fcfd50ebSBarry Smith   ierr = ISDestroy(&to);CHKERRQ(ierr);
10647c6ae99SBarry Smith   PetscFunctionReturn(0);
10747c6ae99SBarry Smith }
10847c6ae99SBarry Smith 
109