xref: /petsc/src/dm/impls/da/dagtona.c (revision b45d2f2cb7e031d9c0de5873eca80614ca7b863b)
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 
10*b45d2f2cSJed Brown #include <petsc-private/daimpl.h>    /*I   "petscdmda.h"   I*/
1147c6ae99SBarry Smith 
1247c6ae99SBarry Smith #undef __FUNCT__
13aa219208SBarry Smith #define __FUNCT__ "DMDAGlobalToNaturalAllCreate"
1447c6ae99SBarry Smith /*@
15aa219208SBarry Smith    DMDAGlobalToNaturalAllCreate - Creates a scatter context that maps from the
1647c6ae99SBarry Smith      global vector the entire vector to each processor in natural numbering
1747c6ae99SBarry Smith 
18aa219208SBarry Smith    Collective on DMDA
1947c6ae99SBarry Smith 
2047c6ae99SBarry Smith    Input Parameter:
2147c6ae99SBarry Smith .  da - the distributed array context
2247c6ae99SBarry Smith 
2347c6ae99SBarry Smith    Output Parameter:
2447c6ae99SBarry Smith .  scatter - the scatter context
2547c6ae99SBarry Smith 
2647c6ae99SBarry Smith    Level: advanced
2747c6ae99SBarry Smith 
2847c6ae99SBarry Smith .keywords: distributed array, global to local, begin, coarse problem
2947c6ae99SBarry Smith 
30aa219208SBarry Smith .seealso: DMDAGlobalToNaturalEnd(), DMLocalToGlobalBegin(), DMDACreate2d(),
31aa219208SBarry Smith           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMDACreateNaturalVector()
3247c6ae99SBarry Smith @*/
337087cfbeSBarry Smith PetscErrorCode  DMDAGlobalToNaturalAllCreate(DM da,VecScatter *scatter)
3447c6ae99SBarry Smith {
3547c6ae99SBarry Smith   PetscErrorCode ierr;
3647c6ae99SBarry Smith   PetscInt       N;
3747c6ae99SBarry Smith   IS             from,to;
3847c6ae99SBarry Smith   Vec            tmplocal,global;
3947c6ae99SBarry Smith   AO             ao;
4047c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
4147c6ae99SBarry Smith 
4247c6ae99SBarry Smith   PetscFunctionBegin;
4347c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
4447c6ae99SBarry Smith   PetscValidPointer(scatter,2);
45aa219208SBarry Smith   ierr = DMDAGetAO(da,&ao);CHKERRQ(ierr);
4647c6ae99SBarry Smith 
4747c6ae99SBarry Smith   /* create the scatter context */
4847c6ae99SBarry Smith   ierr = VecCreateMPIWithArray(((PetscObject)da)->comm,dd->Nlocal,PETSC_DETERMINE,0,&global);CHKERRQ(ierr);
4947c6ae99SBarry Smith   ierr = VecGetSize(global,&N);CHKERRQ(ierr);
5047c6ae99SBarry Smith   ierr = ISCreateStride(((PetscObject)da)->comm,N,0,1,&to);CHKERRQ(ierr);
5147c6ae99SBarry Smith   ierr = AOPetscToApplicationIS(ao,to);CHKERRQ(ierr);
5247c6ae99SBarry Smith   ierr = ISCreateStride(((PetscObject)da)->comm,N,0,1,&from);CHKERRQ(ierr);
5347c6ae99SBarry Smith   ierr = VecCreateSeqWithArray(PETSC_COMM_SELF,N,0,&tmplocal);CHKERRQ(ierr);
5447c6ae99SBarry Smith   ierr = VecSetBlockSize(tmplocal,dd->w);CHKERRQ(ierr);
5547c6ae99SBarry Smith   ierr = VecSetBlockSize(global,dd->w);CHKERRQ(ierr);
5647c6ae99SBarry Smith   ierr = VecScatterCreate(global,from,tmplocal,to,scatter);CHKERRQ(ierr);
57fcfd50ebSBarry Smith   ierr = VecDestroy(&tmplocal);CHKERRQ(ierr);
58fcfd50ebSBarry Smith   ierr = VecDestroy(&global);CHKERRQ(ierr);
59fcfd50ebSBarry Smith   ierr = ISDestroy(&from);CHKERRQ(ierr);
60fcfd50ebSBarry Smith   ierr = ISDestroy(&to);CHKERRQ(ierr);
6147c6ae99SBarry Smith   PetscFunctionReturn(0);
6247c6ae99SBarry Smith }
6347c6ae99SBarry Smith 
6447c6ae99SBarry Smith #undef __FUNCT__
65aa219208SBarry Smith #define __FUNCT__ "DMDANaturalAllToGlobalCreate"
6647c6ae99SBarry Smith /*@
67aa219208SBarry Smith    DMDANaturalAllToGlobalCreate - Creates a scatter context that maps from a copy
6847c6ae99SBarry Smith      of the entire vector on each processor to its local part in the global vector.
6947c6ae99SBarry Smith 
70aa219208SBarry Smith    Collective on DMDA
7147c6ae99SBarry Smith 
7247c6ae99SBarry Smith    Input Parameter:
7347c6ae99SBarry Smith .  da - the distributed array context
7447c6ae99SBarry Smith 
7547c6ae99SBarry Smith    Output Parameter:
7647c6ae99SBarry Smith .  scatter - the scatter context
7747c6ae99SBarry Smith 
7847c6ae99SBarry Smith    Level: advanced
7947c6ae99SBarry Smith 
8047c6ae99SBarry Smith .keywords: distributed array, global to local, begin, coarse problem
8147c6ae99SBarry Smith 
82aa219208SBarry Smith .seealso: DMDAGlobalToNaturalEnd(), DMLocalToGlobalBegin(), DMDACreate2d(),
83aa219208SBarry Smith           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMDACreateNaturalVector()
8447c6ae99SBarry Smith @*/
857087cfbeSBarry Smith PetscErrorCode  DMDANaturalAllToGlobalCreate(DM da,VecScatter *scatter)
8647c6ae99SBarry Smith {
8747c6ae99SBarry Smith   PetscErrorCode ierr;
8847c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
8947c6ae99SBarry Smith   PetscInt       M,m = dd->Nlocal,start;
9047c6ae99SBarry Smith   IS             from,to;
9147c6ae99SBarry Smith   Vec            tmplocal,global;
9247c6ae99SBarry Smith   AO             ao;
9347c6ae99SBarry Smith 
9447c6ae99SBarry Smith   PetscFunctionBegin;
9547c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
9647c6ae99SBarry Smith   PetscValidPointer(scatter,2);
97aa219208SBarry Smith   ierr = DMDAGetAO(da,&ao);CHKERRQ(ierr);
9847c6ae99SBarry Smith 
9947c6ae99SBarry Smith   /* create the scatter context */
10047c6ae99SBarry Smith   ierr = MPI_Allreduce(&m,&M,1,MPIU_INT,MPI_SUM,((PetscObject)da)->comm);CHKERRQ(ierr);
10147c6ae99SBarry Smith   ierr = VecCreateMPIWithArray(((PetscObject)da)->comm,m,PETSC_DETERMINE,0,&global);CHKERRQ(ierr);
10247c6ae99SBarry Smith   ierr = VecGetOwnershipRange(global,&start,PETSC_NULL);CHKERRQ(ierr);
10347c6ae99SBarry Smith   ierr = ISCreateStride(((PetscObject)da)->comm,m,start,1,&from);CHKERRQ(ierr);
10447c6ae99SBarry Smith   ierr = AOPetscToApplicationIS(ao,from);CHKERRQ(ierr);
10547c6ae99SBarry Smith   ierr = ISCreateStride(((PetscObject)da)->comm,m,start,1,&to);CHKERRQ(ierr);
10647c6ae99SBarry Smith   ierr = VecCreateSeqWithArray(PETSC_COMM_SELF,M,0,&tmplocal);CHKERRQ(ierr);
10747c6ae99SBarry Smith   ierr = VecSetBlockSize(tmplocal,dd->w);CHKERRQ(ierr);
10847c6ae99SBarry Smith   ierr = VecSetBlockSize(global,dd->w);CHKERRQ(ierr);
10947c6ae99SBarry Smith   ierr = VecScatterCreate(tmplocal,from,global,to,scatter);CHKERRQ(ierr);
110fcfd50ebSBarry Smith   ierr = VecDestroy(&tmplocal);CHKERRQ(ierr);
111fcfd50ebSBarry Smith   ierr = VecDestroy(&global);CHKERRQ(ierr);
112fcfd50ebSBarry Smith   ierr = ISDestroy(&from);CHKERRQ(ierr);
113fcfd50ebSBarry Smith   ierr = ISDestroy(&to);CHKERRQ(ierr);
11447c6ae99SBarry Smith   PetscFunctionReturn(0);
11547c6ae99SBarry Smith }
11647c6ae99SBarry Smith 
117