xref: /petsc/src/dm/impls/da/dagtona.c (revision 47c6ae997ffd1b2afd66b6474dff5950ae8613d1)
1*47c6ae99SBarry Smith #define PETSCDM_DLL
2*47c6ae99SBarry Smith 
3*47c6ae99SBarry Smith /*
4*47c6ae99SBarry Smith      Tools to help solve the coarse grid problem redundantly.
5*47c6ae99SBarry Smith   Provides two scatter contexts that (1) map from the usual global vector
6*47c6ae99SBarry Smith   to all processors the entire vector in NATURAL numbering and (2)
7*47c6ae99SBarry Smith   from the entire vector on each processor in natural numbering extracts
8*47c6ae99SBarry Smith   out this processors piece in GLOBAL numbering
9*47c6ae99SBarry Smith */
10*47c6ae99SBarry Smith 
11*47c6ae99SBarry Smith #include "private/daimpl.h"    /*I   "petscda.h"   I*/
12*47c6ae99SBarry Smith 
13*47c6ae99SBarry Smith #undef __FUNCT__
14*47c6ae99SBarry Smith #define __FUNCT__ "DAGlobalToNaturalAllCreate"
15*47c6ae99SBarry Smith /*@
16*47c6ae99SBarry Smith    DAGlobalToNaturalAllCreate - Creates a scatter context that maps from the
17*47c6ae99SBarry Smith      global vector the entire vector to each processor in natural numbering
18*47c6ae99SBarry Smith 
19*47c6ae99SBarry Smith    Collective on DA
20*47c6ae99SBarry Smith 
21*47c6ae99SBarry Smith    Input Parameter:
22*47c6ae99SBarry Smith .  da - the distributed array context
23*47c6ae99SBarry Smith 
24*47c6ae99SBarry Smith    Output Parameter:
25*47c6ae99SBarry Smith .  scatter - the scatter context
26*47c6ae99SBarry Smith 
27*47c6ae99SBarry Smith    Level: advanced
28*47c6ae99SBarry Smith 
29*47c6ae99SBarry Smith .keywords: distributed array, global to local, begin, coarse problem
30*47c6ae99SBarry Smith 
31*47c6ae99SBarry Smith .seealso: DAGlobalToNaturalEnd(), DALocalToGlobal(), DACreate2d(),
32*47c6ae99SBarry Smith           DAGlobalToLocalBegin(), DAGlobalToLocalEnd(), DACreateNaturalVector()
33*47c6ae99SBarry Smith @*/
34*47c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DAGlobalToNaturalAllCreate(DA da,VecScatter *scatter)
35*47c6ae99SBarry Smith {
36*47c6ae99SBarry Smith   PetscErrorCode ierr;
37*47c6ae99SBarry Smith   PetscInt       N;
38*47c6ae99SBarry Smith   IS             from,to;
39*47c6ae99SBarry Smith   Vec            tmplocal,global;
40*47c6ae99SBarry Smith   AO             ao;
41*47c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
42*47c6ae99SBarry Smith 
43*47c6ae99SBarry Smith   PetscFunctionBegin;
44*47c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
45*47c6ae99SBarry Smith   PetscValidPointer(scatter,2);
46*47c6ae99SBarry Smith   ierr = DAGetAO(da,&ao);CHKERRQ(ierr);
47*47c6ae99SBarry Smith 
48*47c6ae99SBarry Smith   /* create the scatter context */
49*47c6ae99SBarry Smith   ierr = VecCreateMPIWithArray(((PetscObject)da)->comm,dd->Nlocal,PETSC_DETERMINE,0,&global);CHKERRQ(ierr);
50*47c6ae99SBarry Smith   ierr = VecGetSize(global,&N);CHKERRQ(ierr);
51*47c6ae99SBarry Smith   ierr = ISCreateStride(((PetscObject)da)->comm,N,0,1,&to);CHKERRQ(ierr);
52*47c6ae99SBarry Smith   ierr = AOPetscToApplicationIS(ao,to);CHKERRQ(ierr);
53*47c6ae99SBarry Smith   ierr = ISCreateStride(((PetscObject)da)->comm,N,0,1,&from);CHKERRQ(ierr);
54*47c6ae99SBarry Smith   ierr = VecCreateSeqWithArray(PETSC_COMM_SELF,N,0,&tmplocal);CHKERRQ(ierr);
55*47c6ae99SBarry Smith   ierr = VecSetBlockSize(tmplocal,dd->w);CHKERRQ(ierr);
56*47c6ae99SBarry Smith   ierr = VecSetBlockSize(global,dd->w);CHKERRQ(ierr);
57*47c6ae99SBarry Smith   ierr = VecScatterCreate(global,from,tmplocal,to,scatter);CHKERRQ(ierr);
58*47c6ae99SBarry Smith   ierr = VecDestroy(tmplocal);CHKERRQ(ierr);
59*47c6ae99SBarry Smith   ierr = VecDestroy(global);CHKERRQ(ierr);
60*47c6ae99SBarry Smith   ierr = ISDestroy(from);CHKERRQ(ierr);
61*47c6ae99SBarry Smith   ierr = ISDestroy(to);CHKERRQ(ierr);
62*47c6ae99SBarry Smith   PetscFunctionReturn(0);
63*47c6ae99SBarry Smith }
64*47c6ae99SBarry Smith 
65*47c6ae99SBarry Smith #undef __FUNCT__
66*47c6ae99SBarry Smith #define __FUNCT__ "DANaturalAllToGlobalCreate"
67*47c6ae99SBarry Smith /*@
68*47c6ae99SBarry Smith    DANaturalAllToGlobalCreate - Creates a scatter context that maps from a copy
69*47c6ae99SBarry Smith      of the entire vector on each processor to its local part in the global vector.
70*47c6ae99SBarry Smith 
71*47c6ae99SBarry Smith    Collective on DA
72*47c6ae99SBarry Smith 
73*47c6ae99SBarry Smith    Input Parameter:
74*47c6ae99SBarry Smith .  da - the distributed array context
75*47c6ae99SBarry Smith 
76*47c6ae99SBarry Smith    Output Parameter:
77*47c6ae99SBarry Smith .  scatter - the scatter context
78*47c6ae99SBarry Smith 
79*47c6ae99SBarry Smith    Level: advanced
80*47c6ae99SBarry Smith 
81*47c6ae99SBarry Smith .keywords: distributed array, global to local, begin, coarse problem
82*47c6ae99SBarry Smith 
83*47c6ae99SBarry Smith .seealso: DAGlobalToNaturalEnd(), DALocalToGlobal(), DACreate2d(),
84*47c6ae99SBarry Smith           DAGlobalToLocalBegin(), DAGlobalToLocalEnd(), DACreateNaturalVector()
85*47c6ae99SBarry Smith @*/
86*47c6ae99SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DANaturalAllToGlobalCreate(DA da,VecScatter *scatter)
87*47c6ae99SBarry Smith {
88*47c6ae99SBarry Smith   PetscErrorCode ierr;
89*47c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
90*47c6ae99SBarry Smith   PetscInt       M,m = dd->Nlocal,start;
91*47c6ae99SBarry Smith   IS             from,to;
92*47c6ae99SBarry Smith   Vec            tmplocal,global;
93*47c6ae99SBarry Smith   AO             ao;
94*47c6ae99SBarry Smith 
95*47c6ae99SBarry Smith   PetscFunctionBegin;
96*47c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
97*47c6ae99SBarry Smith   PetscValidPointer(scatter,2);
98*47c6ae99SBarry Smith   ierr = DAGetAO(da,&ao);CHKERRQ(ierr);
99*47c6ae99SBarry Smith 
100*47c6ae99SBarry Smith   /* create the scatter context */
101*47c6ae99SBarry Smith   ierr = MPI_Allreduce(&m,&M,1,MPIU_INT,MPI_SUM,((PetscObject)da)->comm);CHKERRQ(ierr);
102*47c6ae99SBarry Smith   ierr = VecCreateMPIWithArray(((PetscObject)da)->comm,m,PETSC_DETERMINE,0,&global);CHKERRQ(ierr);
103*47c6ae99SBarry Smith   ierr = VecGetOwnershipRange(global,&start,PETSC_NULL);CHKERRQ(ierr);
104*47c6ae99SBarry Smith   ierr = ISCreateStride(((PetscObject)da)->comm,m,start,1,&from);CHKERRQ(ierr);
105*47c6ae99SBarry Smith   ierr = AOPetscToApplicationIS(ao,from);CHKERRQ(ierr);
106*47c6ae99SBarry Smith   ierr = ISCreateStride(((PetscObject)da)->comm,m,start,1,&to);CHKERRQ(ierr);
107*47c6ae99SBarry Smith   ierr = VecCreateSeqWithArray(PETSC_COMM_SELF,M,0,&tmplocal);CHKERRQ(ierr);
108*47c6ae99SBarry Smith   ierr = VecSetBlockSize(tmplocal,dd->w);CHKERRQ(ierr);
109*47c6ae99SBarry Smith   ierr = VecSetBlockSize(global,dd->w);CHKERRQ(ierr);
110*47c6ae99SBarry Smith   ierr = VecScatterCreate(tmplocal,from,global,to,scatter);CHKERRQ(ierr);
111*47c6ae99SBarry Smith   ierr = VecDestroy(tmplocal);CHKERRQ(ierr);
112*47c6ae99SBarry Smith   ierr = VecDestroy(global);CHKERRQ(ierr);
113*47c6ae99SBarry Smith   ierr = ISDestroy(from);CHKERRQ(ierr);
114*47c6ae99SBarry Smith   ierr = ISDestroy(to);CHKERRQ(ierr);
115*47c6ae99SBarry Smith   PetscFunctionReturn(0);
116*47c6ae99SBarry Smith }
117*47c6ae99SBarry Smith 
118