xref: /petsc/src/dm/impls/da/daindex.c (revision 8ea3bf28dab5b469bf97b96160a139e62b44e343)
147c6ae99SBarry Smith 
247c6ae99SBarry Smith /*
347c6ae99SBarry Smith   Code for manipulating distributed regular arrays in parallel.
447c6ae99SBarry Smith */
547c6ae99SBarry Smith 
64035e84dSBarry Smith #include <petsc-private/dmdaimpl.h>    /*I   "petscdmda.h"   I*/
747c6ae99SBarry Smith 
847c6ae99SBarry Smith #undef __FUNCT__
9aa219208SBarry Smith #define __FUNCT__ "DMDAGetGlobalIndices"
1047c6ae99SBarry Smith /*@C
11aa219208SBarry Smith    DMDAGetGlobalIndices - Returns the global node number of all local nodes,
1247c6ae99SBarry Smith    including ghost nodes.
1347c6ae99SBarry Smith 
1447c6ae99SBarry Smith    Not Collective
1547c6ae99SBarry Smith 
1647c6ae99SBarry Smith    Input Parameter:
1747c6ae99SBarry Smith .  da - the distributed array
1847c6ae99SBarry Smith 
1947c6ae99SBarry Smith    Output Parameters:
200298fd71SBarry Smith +  n - the number of local elements, including ghost nodes (or NULL)
2147c6ae99SBarry Smith -  idx - the global indices
2247c6ae99SBarry Smith 
2347c6ae99SBarry Smith    Level: intermediate
2447c6ae99SBarry Smith 
2547c6ae99SBarry Smith    Note:
26aa219208SBarry Smith    For DMDA_STENCIL_STAR stencils the inactive corner ghost nodes are also included
2747c6ae99SBarry Smith    in the list of local indices (even though those nodes are not updated
28aa219208SBarry Smith    during calls to DMDAXXXToXXX().
2947c6ae99SBarry Smith 
3047c6ae99SBarry Smith    Essentially the same data is returned in the form of a local-to-global mapping
31*8ea3bf28SBarry Smith    with the routine DMDAGetISLocalToGlobalMapping(), that is the recommended interface.
32*8ea3bf28SBarry Smith 
33*8ea3bf28SBarry Smith    You must call DMDARestoreGlobalIndices() after you are finished using the indices
3447c6ae99SBarry Smith 
3547c6ae99SBarry Smith    Fortran Note:
3647c6ae99SBarry Smith    This routine is used differently from Fortran
3747c6ae99SBarry Smith .vb
389a42bb27SBarry Smith         DM          da
3947c6ae99SBarry Smith         integer     n,da_array(1)
4047c6ae99SBarry Smith         PetscOffset i_da
4147c6ae99SBarry Smith         integer     ierr
42aa219208SBarry Smith         call DMDAGetGlobalIndices(da,n,da_array,i_da,ierr)
4347c6ae99SBarry Smith 
4447c6ae99SBarry Smith    C Access first local entry in list
4547c6ae99SBarry Smith         value = da_array(i_da + 1)
4647c6ae99SBarry Smith .ve
4747c6ae99SBarry Smith 
4847c6ae99SBarry Smith    See the <A href="../../docs/manual.pdf#nameddest=ch_fortran">Fortran chapter</A> of the users manual for details.
4947c6ae99SBarry Smith 
5047c6ae99SBarry Smith .keywords: distributed array, get, global, indices, local-to-global
5147c6ae99SBarry Smith 
52*8ea3bf28SBarry Smith .seealso: DMDACreate2d(), DMDAGetGhostCorners(), DMDAGetCorners(), DMLocalToGlobalBegin(), DMDARestoreGlobalIndices()
5399f0b487SRichard Tran Mills           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToLocalBegin(), DMDAGetAO(), DMDAGetGlobalIndicesF90()
5499f0b487SRichard Tran Mills           DMDAGetISLocalToGlobalMapping(), DMDACreate3d(), DMDACreate1d(), DMLocalToLocalEnd(), DMDAGetOwnershipRanges()
5547c6ae99SBarry Smith @*/
56*8ea3bf28SBarry Smith PetscErrorCode  DMDAGetGlobalIndices(DM da,PetscInt *n,const PetscInt *idx[])
5747c6ae99SBarry Smith {
58*8ea3bf28SBarry Smith   PetscErrorCode ierr;
5947c6ae99SBarry Smith 
6047c6ae99SBarry Smith   PetscFunctionBegin;
6147c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
62*8ea3bf28SBarry Smith   if (n) {
63*8ea3bf28SBarry Smith     ierr = ISLocalToGlobalMappingGetSize(da->ltogmap,n);CHKERRQ(ierr);
64*8ea3bf28SBarry Smith   }
65*8ea3bf28SBarry Smith   if (idx) {
66*8ea3bf28SBarry Smith     ierr = ISLocalToGlobalMappingGetIndices(da->ltogmap,idx);CHKERRQ(ierr);
67*8ea3bf28SBarry Smith   }
6847c6ae99SBarry Smith   PetscFunctionReturn(0);
6947c6ae99SBarry Smith }
7047c6ae99SBarry Smith 
7147c6ae99SBarry Smith #undef __FUNCT__
72aa219208SBarry Smith #define __FUNCT__ "DMDAGetNatural_Private"
7347c6ae99SBarry Smith /*
7447c6ae99SBarry Smith    Gets the natural number for each global number on the process.
7547c6ae99SBarry Smith 
76aa219208SBarry Smith    Used by DMDAGetAO() and DMDAGlobalToNatural_Create()
7747c6ae99SBarry Smith */
78aa219208SBarry Smith PetscErrorCode DMDAGetNatural_Private(DM da,PetscInt *outNlocal,IS *isnatural)
7947c6ae99SBarry Smith {
8047c6ae99SBarry Smith   PetscErrorCode ierr;
8147c6ae99SBarry Smith   PetscInt       Nlocal,i,j,k,*lidx,lict = 0;
8247c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
8347c6ae99SBarry Smith 
8447c6ae99SBarry Smith   PetscFunctionBegin;
8547c6ae99SBarry Smith   Nlocal = (dd->xe-dd->xs);
868865f1eaSKarl Rupp   if (dd->dim > 1) Nlocal *= (dd->ye-dd->ys);
878865f1eaSKarl Rupp   if (dd->dim > 2) Nlocal *= (dd->ze-dd->zs);
8847c6ae99SBarry Smith 
8947c6ae99SBarry Smith   ierr = PetscMalloc(Nlocal*sizeof(PetscInt),&lidx);CHKERRQ(ierr);
9047c6ae99SBarry Smith 
9147c6ae99SBarry Smith   if (dd->dim == 1) {
9247c6ae99SBarry Smith     for (i=dd->xs; i<dd->xe; i++) {
9347c6ae99SBarry Smith       /*  global number in natural ordering */
9447c6ae99SBarry Smith       lidx[lict++] = i;
9547c6ae99SBarry Smith     }
9647c6ae99SBarry Smith   } else if (dd->dim == 2) {
9747c6ae99SBarry Smith     for (j=dd->ys; j<dd->ye; j++) {
9847c6ae99SBarry Smith       for (i=dd->xs; i<dd->xe; i++) {
9947c6ae99SBarry Smith         /*  global number in natural ordering */
10047c6ae99SBarry Smith         lidx[lict++] = i + j*dd->M*dd->w;
10147c6ae99SBarry Smith       }
10247c6ae99SBarry Smith     }
10347c6ae99SBarry Smith   } else if (dd->dim == 3) {
10447c6ae99SBarry Smith     for (k=dd->zs; k<dd->ze; k++) {
10547c6ae99SBarry Smith       for (j=dd->ys; j<dd->ye; j++) {
10647c6ae99SBarry Smith         for (i=dd->xs; i<dd->xe; i++) {
10747c6ae99SBarry Smith           lidx[lict++] = i + j*dd->M*dd->w + k*dd->M*dd->N*dd->w;
10847c6ae99SBarry Smith         }
10947c6ae99SBarry Smith       }
11047c6ae99SBarry Smith     }
11147c6ae99SBarry Smith   }
11247c6ae99SBarry Smith   *outNlocal = Nlocal;
113ce94432eSBarry Smith   ierr       = ISCreateGeneral(PetscObjectComm((PetscObject)da),Nlocal,lidx,PETSC_OWN_POINTER,isnatural);CHKERRQ(ierr);
11447c6ae99SBarry Smith   PetscFunctionReturn(0);
11547c6ae99SBarry Smith }
11647c6ae99SBarry Smith 
11747c6ae99SBarry Smith #undef __FUNCT__
118*8ea3bf28SBarry Smith #define __FUNCT__ "DMDARestoreGlobalIndices"
119*8ea3bf28SBarry Smith /*@C
120*8ea3bf28SBarry Smith    DMDARestoreGlobalIndices - Restores the global node number of all local nodes,   including ghost nodes.
121*8ea3bf28SBarry Smith 
122*8ea3bf28SBarry Smith    Not Collective
123*8ea3bf28SBarry Smith 
124*8ea3bf28SBarry Smith    Input Parameter:
125*8ea3bf28SBarry Smith .  da - the distributed array
126*8ea3bf28SBarry Smith 
127*8ea3bf28SBarry Smith    Output Parameters:
128*8ea3bf28SBarry Smith +  n - the number of local elements, including ghost nodes (or NULL)
129*8ea3bf28SBarry Smith -  idx - the global indices
130*8ea3bf28SBarry Smith 
131*8ea3bf28SBarry Smith    Level: intermediate
132*8ea3bf28SBarry Smith 
133*8ea3bf28SBarry Smith    Note:
134*8ea3bf28SBarry Smith    For DMDA_STENCIL_STAR stencils the inactive corner ghost nodes are also included
135*8ea3bf28SBarry Smith    in the list of local indices (even though those nodes are not updated
136*8ea3bf28SBarry Smith    during calls to DMDAXXXToXXX().
137*8ea3bf28SBarry Smith 
138*8ea3bf28SBarry Smith    Essentially the same data is returned in the form of a local-to-global mapping
139*8ea3bf28SBarry Smith    with the routine DMDAGetISLocalToGlobalMapping();
140*8ea3bf28SBarry Smith 
141*8ea3bf28SBarry Smith    Fortran Note:
142*8ea3bf28SBarry Smith    This routine is used differently from Fortran
143*8ea3bf28SBarry Smith .vb
144*8ea3bf28SBarry Smith         DM          da
145*8ea3bf28SBarry Smith         integer     n,da_array(1)
146*8ea3bf28SBarry Smith         PetscOffset i_da
147*8ea3bf28SBarry Smith         integer     ierr
148*8ea3bf28SBarry Smith         call DMDAGetGlobalIndices(da,n,da_array,i_da,ierr)
149*8ea3bf28SBarry Smith 
150*8ea3bf28SBarry Smith    C Access first local entry in list
151*8ea3bf28SBarry Smith         value = da_array(i_da + 1)
152*8ea3bf28SBarry Smith .ve
153*8ea3bf28SBarry Smith 
154*8ea3bf28SBarry Smith    See the <A href="../../docs/manual.pdf#nameddest=ch_fortran">Fortran chapter</A> of the users manual for details.
155*8ea3bf28SBarry Smith 
156*8ea3bf28SBarry Smith .keywords: distributed array, get, global, indices, local-to-global
157*8ea3bf28SBarry Smith 
158*8ea3bf28SBarry Smith .seealso: DMDACreate2d(), DMDAGetGhostCorners(), DMDAGetCorners(), DMLocalToGlobalBegin()
159*8ea3bf28SBarry Smith           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToLocalBegin(), DMDAGetAO(), DMDAGetGlobalIndicesF90()
160*8ea3bf28SBarry Smith           DMDAGetISLocalToGlobalMapping(), DMDACreate3d(), DMDACreate1d(), DMLocalToLocalEnd(), DMDAGetOwnershipRanges()
161*8ea3bf28SBarry Smith @*/
162*8ea3bf28SBarry Smith PetscErrorCode  DMDARestoreGlobalIndices(DM da,PetscInt *n,const PetscInt *idx[])
163*8ea3bf28SBarry Smith {
164*8ea3bf28SBarry Smith   PetscErrorCode ierr;
165*8ea3bf28SBarry Smith 
166*8ea3bf28SBarry Smith   PetscFunctionBegin;
167*8ea3bf28SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
168*8ea3bf28SBarry Smith   if (idx) {
169*8ea3bf28SBarry Smith     ierr = ISLocalToGlobalMappingRestoreIndices(da->ltogmap,idx);CHKERRQ(ierr);
170*8ea3bf28SBarry Smith   }
171*8ea3bf28SBarry Smith   PetscFunctionReturn(0);
172*8ea3bf28SBarry Smith }
173*8ea3bf28SBarry Smith 
174*8ea3bf28SBarry Smith #undef __FUNCT__
175aa219208SBarry Smith #define __FUNCT__ "DMDAGetAO"
17647c6ae99SBarry Smith /*@
177aa219208SBarry Smith    DMDAGetAO - Gets the application ordering context for a distributed array.
17847c6ae99SBarry Smith 
179aa219208SBarry Smith    Collective on DMDA
18047c6ae99SBarry Smith 
18147c6ae99SBarry Smith    Input Parameter:
18247c6ae99SBarry Smith .  da - the distributed array
18347c6ae99SBarry Smith 
18447c6ae99SBarry Smith    Output Parameters:
185aa219208SBarry Smith .  ao - the application ordering context for DMDAs
18647c6ae99SBarry Smith 
18747c6ae99SBarry Smith    Level: intermediate
18847c6ae99SBarry Smith 
18947c6ae99SBarry Smith    Notes:
19047c6ae99SBarry Smith    In this case, the AO maps to the natural grid ordering that would be used
191aa219208SBarry Smith    for the DMDA if only 1 processor were employed (ordering most rapidly in the
19247c6ae99SBarry Smith    x-direction, then y, then z).  Multiple degrees of freedom are numbered
19347c6ae99SBarry Smith    for each node (rather than 1 component for the whole grid, then the next
19447c6ae99SBarry Smith    component, etc.)
19547c6ae99SBarry Smith 
19647c6ae99SBarry Smith .keywords: distributed array, get, global, indices, local-to-global
19747c6ae99SBarry Smith 
198aa219208SBarry Smith .seealso: DMDACreate2d(), DMDAGetGhostCorners(), DMDAGetCorners(), DMDALocalToGlocal()
19999f0b487SRichard Tran Mills           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToLocalBegin(), DMLocalToLocalEnd(), DMDAGetGlobalIndices(), DMDAGetOwnershipRanges(),
20047c6ae99SBarry Smith           AO, AOPetscToApplication(), AOApplicationToPetsc()
20147c6ae99SBarry Smith @*/
2027087cfbeSBarry Smith PetscErrorCode  DMDAGetAO(DM da,AO *ao)
20347c6ae99SBarry Smith {
20447c6ae99SBarry Smith   DM_DA *dd = (DM_DA*)da->data;
20547c6ae99SBarry Smith 
20647c6ae99SBarry Smith   PetscFunctionBegin;
20747c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
20847c6ae99SBarry Smith   PetscValidPointer(ao,2);
20947c6ae99SBarry Smith 
21047c6ae99SBarry Smith   /*
21147c6ae99SBarry Smith      Build the natural ordering to PETSc ordering mappings.
21247c6ae99SBarry Smith   */
21347c6ae99SBarry Smith   if (!dd->ao) {
21447c6ae99SBarry Smith     IS             ispetsc,isnatural;
21547c6ae99SBarry Smith     PetscErrorCode ierr;
21647c6ae99SBarry Smith     PetscInt       Nlocal;
21747c6ae99SBarry Smith 
218aa219208SBarry Smith     ierr = DMDAGetNatural_Private(da,&Nlocal,&isnatural);CHKERRQ(ierr);
219ce94432eSBarry Smith     ierr = ISCreateStride(PetscObjectComm((PetscObject)da),Nlocal,dd->base,1,&ispetsc);CHKERRQ(ierr);
22047c6ae99SBarry Smith     ierr = AOCreateBasicIS(isnatural,ispetsc,&dd->ao);CHKERRQ(ierr);
2213bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)da,(PetscObject)dd->ao);CHKERRQ(ierr);
222fcfd50ebSBarry Smith     ierr = ISDestroy(&ispetsc);CHKERRQ(ierr);
223fcfd50ebSBarry Smith     ierr = ISDestroy(&isnatural);CHKERRQ(ierr);
22447c6ae99SBarry Smith   }
22547c6ae99SBarry Smith   *ao = dd->ao;
22647c6ae99SBarry Smith   PetscFunctionReturn(0);
22747c6ae99SBarry Smith }
22847c6ae99SBarry Smith 
22947c6ae99SBarry Smith /*MC
230aa219208SBarry Smith     DMDAGetGlobalIndicesF90 - Returns a Fortran90 pointer to the list of
23147c6ae99SBarry Smith     global indices (global node number of all local nodes, including
23247c6ae99SBarry Smith     ghost nodes).
23347c6ae99SBarry Smith 
23447c6ae99SBarry Smith     Synopsis:
235aa219208SBarry Smith     DMDAGetGlobalIndicesF90(DM da,integer n,{integer, pointer :: idx(:)},integer ierr)
23647c6ae99SBarry Smith 
23747c6ae99SBarry Smith     Not Collective
23847c6ae99SBarry Smith 
23947c6ae99SBarry Smith     Input Parameter:
24047c6ae99SBarry Smith .   da - the distributed array
24147c6ae99SBarry Smith 
24247c6ae99SBarry Smith     Output Parameters:
2430298fd71SBarry Smith +   n - the number of local elements, including ghost nodes (or NULL)
24447c6ae99SBarry Smith .   idx - the Fortran90 pointer to the global indices
24547c6ae99SBarry Smith -   ierr - error code
24647c6ae99SBarry Smith 
24747c6ae99SBarry Smith     Level: intermediate
24847c6ae99SBarry Smith 
249*8ea3bf28SBarry Smith .keywords: distributed array, get, global, indices, local-to-global, f90
250*8ea3bf28SBarry Smith 
251*8ea3bf28SBarry Smith .seealso: DMDAGetGlobalIndices(), DMDARestoreGlobalIndicesF90(), DMDARestoreGlobalIndices()
252*8ea3bf28SBarry Smith M*/
253*8ea3bf28SBarry Smith 
254*8ea3bf28SBarry Smith /*MC
255*8ea3bf28SBarry Smith     DMDARestoreGlobalIndicesF90 - Returns a Fortran90 pointer to the list of
256*8ea3bf28SBarry Smith     global indices (global node number of all local nodes, including
257*8ea3bf28SBarry Smith     ghost nodes).
258*8ea3bf28SBarry Smith 
259*8ea3bf28SBarry Smith     Synopsis:
260*8ea3bf28SBarry Smith     DMDARestoreGlobalIndicesF90(DM da,integer n,{integer, pointer :: idx(:)},integer ierr)
261*8ea3bf28SBarry Smith 
262*8ea3bf28SBarry Smith     Not Collective
263*8ea3bf28SBarry Smith 
264*8ea3bf28SBarry Smith     Input Parameter:
265*8ea3bf28SBarry Smith .   da - the distributed array
266*8ea3bf28SBarry Smith 
267*8ea3bf28SBarry Smith     Output Parameters:
268*8ea3bf28SBarry Smith +   n - the number of local elements, including ghost nodes (or NULL)
269*8ea3bf28SBarry Smith .   idx - the Fortran90 pointer to the global indices
270*8ea3bf28SBarry Smith -   ierr - error code
271*8ea3bf28SBarry Smith 
272*8ea3bf28SBarry Smith     Level: intermediate
27347c6ae99SBarry Smith 
27447c6ae99SBarry Smith .keywords: distributed array, get, global, indices, local-to-global, f90
27547c6ae99SBarry Smith 
276*8ea3bf28SBarry Smith .seealso: DMDARestoreGlobalIndices(), DMDAGetGlobalIndicesF90(), DMDAGetGlobalIndices()
27747c6ae99SBarry Smith M*/
278*8ea3bf28SBarry Smith 
279