xref: /petsc/src/dm/impls/da/daindex.c (revision b45d2f2cb7e031d9c0de5873eca80614ca7b863b)
147c6ae99SBarry Smith 
247c6ae99SBarry Smith /*
347c6ae99SBarry Smith   Code for manipulating distributed regular arrays in parallel.
447c6ae99SBarry Smith */
547c6ae99SBarry Smith 
6*b45d2f2cSJed Brown #include <petsc-private/daimpl.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:
2047c6ae99SBarry Smith +  n - the number of local elements, including ghost nodes (or PETSC_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
31aa219208SBarry Smith    with the routine DMDAGetISLocalToGlobalMapping();
3247c6ae99SBarry Smith 
3347c6ae99SBarry Smith    Fortran Note:
3447c6ae99SBarry Smith    This routine is used differently from Fortran
3547c6ae99SBarry Smith .vb
369a42bb27SBarry Smith         DM          da
3747c6ae99SBarry Smith         integer     n,da_array(1)
3847c6ae99SBarry Smith         PetscOffset i_da
3947c6ae99SBarry Smith         integer     ierr
40aa219208SBarry Smith         call DMDAGetGlobalIndices(da,n,da_array,i_da,ierr)
4147c6ae99SBarry Smith 
4247c6ae99SBarry Smith    C Access first local entry in list
4347c6ae99SBarry Smith         value = da_array(i_da + 1)
4447c6ae99SBarry Smith .ve
4547c6ae99SBarry Smith 
4647c6ae99SBarry Smith    See the <A href="../../docs/manual.pdf#nameddest=ch_fortran">Fortran chapter</A> of the users manual for details.
4747c6ae99SBarry Smith 
4847c6ae99SBarry Smith .keywords: distributed array, get, global, indices, local-to-global
4947c6ae99SBarry Smith 
50aa219208SBarry Smith .seealso: DMDACreate2d(), DMDAGetGhostCorners(), DMDAGetCorners(), DMLocalToGlobalBegin()
51aa219208SBarry Smith           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMDALocalToLocalBegin(), DMDAGetAO(), DMDAGetGlobalIndicesF90()
52aa219208SBarry Smith           DMDAGetISLocalToGlobalMapping(), DMDACreate3d(), DMDACreate1d(), DMDALocalToLocalEnd(), DMDAGetOwnershipRanges()
5347c6ae99SBarry Smith @*/
547087cfbeSBarry Smith PetscErrorCode  DMDAGetGlobalIndices(DM da,PetscInt *n,PetscInt **idx)
5547c6ae99SBarry Smith {
5647c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
5747c6ae99SBarry Smith 
5847c6ae99SBarry Smith   PetscFunctionBegin;
5947c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
6047c6ae99SBarry Smith   if (n)   *n   = dd->Nl;
6147c6ae99SBarry Smith   if (idx) *idx = dd->idx;
6247c6ae99SBarry Smith   PetscFunctionReturn(0);
6347c6ae99SBarry Smith }
6447c6ae99SBarry Smith 
6547c6ae99SBarry Smith #undef __FUNCT__
66aa219208SBarry Smith #define __FUNCT__ "DMDAGetNatural_Private"
6747c6ae99SBarry Smith /*
6847c6ae99SBarry Smith    Gets the natural number for each global number on the process.
6947c6ae99SBarry Smith 
70aa219208SBarry Smith    Used by DMDAGetAO() and DMDAGlobalToNatural_Create()
7147c6ae99SBarry Smith */
72aa219208SBarry Smith PetscErrorCode DMDAGetNatural_Private(DM da,PetscInt *outNlocal,IS *isnatural)
7347c6ae99SBarry Smith {
7447c6ae99SBarry Smith   PetscErrorCode ierr;
7547c6ae99SBarry Smith   PetscInt       Nlocal,i,j,k,*lidx,lict = 0;
7647c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
7747c6ae99SBarry Smith 
7847c6ae99SBarry Smith   PetscFunctionBegin;
7947c6ae99SBarry Smith   Nlocal = (dd->xe-dd->xs);
8047c6ae99SBarry Smith   if (dd->dim > 1) {
8147c6ae99SBarry Smith     Nlocal *= (dd->ye-dd->ys);
8247c6ae99SBarry Smith   }
8347c6ae99SBarry Smith   if (dd->dim > 2) {
8447c6ae99SBarry Smith     Nlocal *= (dd->ze-dd->zs);
8547c6ae99SBarry Smith   }
8647c6ae99SBarry Smith 
8747c6ae99SBarry Smith   ierr = PetscMalloc(Nlocal*sizeof(PetscInt),&lidx);CHKERRQ(ierr);
8847c6ae99SBarry Smith 
8947c6ae99SBarry Smith   if (dd->dim == 1) {
9047c6ae99SBarry Smith     for (i=dd->xs; i<dd->xe; i++) {
9147c6ae99SBarry Smith       /*  global number in natural ordering */
9247c6ae99SBarry Smith       lidx[lict++] = i;
9347c6ae99SBarry Smith     }
9447c6ae99SBarry Smith   } else if (dd->dim == 2) {
9547c6ae99SBarry Smith     for (j=dd->ys; j<dd->ye; j++) {
9647c6ae99SBarry Smith       for (i=dd->xs; i<dd->xe; i++) {
9747c6ae99SBarry Smith 	/*  global number in natural ordering */
9847c6ae99SBarry Smith 	lidx[lict++] = i + j*dd->M*dd->w;
9947c6ae99SBarry Smith       }
10047c6ae99SBarry Smith     }
10147c6ae99SBarry Smith   } else if (dd->dim == 3) {
10247c6ae99SBarry Smith     for (k=dd->zs; k<dd->ze; k++) {
10347c6ae99SBarry Smith       for (j=dd->ys; j<dd->ye; j++) {
10447c6ae99SBarry Smith 	for (i=dd->xs; i<dd->xe; i++) {
10547c6ae99SBarry Smith 	  lidx[lict++] = i + j*dd->M*dd->w + k*dd->M*dd->N*dd->w;
10647c6ae99SBarry Smith 	}
10747c6ae99SBarry Smith       }
10847c6ae99SBarry Smith     }
10947c6ae99SBarry Smith   }
11047c6ae99SBarry Smith   *outNlocal = Nlocal;
11147c6ae99SBarry Smith   ierr = ISCreateGeneral(((PetscObject)da)->comm,Nlocal,lidx,PETSC_OWN_POINTER,isnatural);CHKERRQ(ierr);
11247c6ae99SBarry Smith   PetscFunctionReturn(0);
11347c6ae99SBarry Smith }
11447c6ae99SBarry Smith 
11547c6ae99SBarry Smith #undef __FUNCT__
116aa219208SBarry Smith #define __FUNCT__ "DMDAGetAO"
11747c6ae99SBarry Smith /*@
118aa219208SBarry Smith    DMDAGetAO - Gets the application ordering context for a distributed array.
11947c6ae99SBarry Smith 
120aa219208SBarry Smith    Collective on DMDA
12147c6ae99SBarry Smith 
12247c6ae99SBarry Smith    Input Parameter:
12347c6ae99SBarry Smith .  da - the distributed array
12447c6ae99SBarry Smith 
12547c6ae99SBarry Smith    Output Parameters:
126aa219208SBarry Smith .  ao - the application ordering context for DMDAs
12747c6ae99SBarry Smith 
12847c6ae99SBarry Smith    Level: intermediate
12947c6ae99SBarry Smith 
13047c6ae99SBarry Smith    Notes:
13147c6ae99SBarry Smith    In this case, the AO maps to the natural grid ordering that would be used
132aa219208SBarry Smith    for the DMDA if only 1 processor were employed (ordering most rapidly in the
13347c6ae99SBarry Smith    x-direction, then y, then z).  Multiple degrees of freedom are numbered
13447c6ae99SBarry Smith    for each node (rather than 1 component for the whole grid, then the next
13547c6ae99SBarry Smith    component, etc.)
13647c6ae99SBarry Smith 
13747c6ae99SBarry Smith .keywords: distributed array, get, global, indices, local-to-global
13847c6ae99SBarry Smith 
139aa219208SBarry Smith .seealso: DMDACreate2d(), DMDAGetGhostCorners(), DMDAGetCorners(), DMDALocalToGlocal()
140aa219208SBarry Smith           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMDALocalToLocalBegin(), DMDALocalToLocalEnd(), DMDAGetGlobalIndices(), DMDAGetOwnershipRanges(),
14147c6ae99SBarry Smith           AO, AOPetscToApplication(), AOApplicationToPetsc()
14247c6ae99SBarry Smith @*/
1437087cfbeSBarry Smith PetscErrorCode  DMDAGetAO(DM da,AO *ao)
14447c6ae99SBarry Smith {
14547c6ae99SBarry Smith   DM_DA *dd = (DM_DA*)da->data;
14647c6ae99SBarry Smith 
14747c6ae99SBarry Smith   PetscFunctionBegin;
14847c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
14947c6ae99SBarry Smith   PetscValidPointer(ao,2);
15047c6ae99SBarry Smith 
15147c6ae99SBarry Smith   /*
15247c6ae99SBarry Smith      Build the natural ordering to PETSc ordering mappings.
15347c6ae99SBarry Smith   */
15447c6ae99SBarry Smith   if (!dd->ao) {
15547c6ae99SBarry Smith     IS             ispetsc,isnatural;
15647c6ae99SBarry Smith     PetscErrorCode ierr;
15747c6ae99SBarry Smith     PetscInt       Nlocal;
15847c6ae99SBarry Smith 
159aa219208SBarry Smith     ierr = DMDAGetNatural_Private(da,&Nlocal,&isnatural);CHKERRQ(ierr);
16047c6ae99SBarry Smith     ierr = ISCreateStride(((PetscObject)da)->comm,Nlocal,dd->base,1,&ispetsc);CHKERRQ(ierr);
16147c6ae99SBarry Smith     ierr = AOCreateBasicIS(isnatural,ispetsc,&dd->ao);CHKERRQ(ierr);
16247c6ae99SBarry Smith     ierr = PetscLogObjectParent(da,dd->ao);CHKERRQ(ierr);
163fcfd50ebSBarry Smith     ierr = ISDestroy(&ispetsc);CHKERRQ(ierr);
164fcfd50ebSBarry Smith     ierr = ISDestroy(&isnatural);CHKERRQ(ierr);
16547c6ae99SBarry Smith   }
16647c6ae99SBarry Smith   *ao = dd->ao;
16747c6ae99SBarry Smith   PetscFunctionReturn(0);
16847c6ae99SBarry Smith }
16947c6ae99SBarry Smith 
17047c6ae99SBarry Smith /*MC
171aa219208SBarry Smith     DMDAGetGlobalIndicesF90 - Returns a Fortran90 pointer to the list of
17247c6ae99SBarry Smith     global indices (global node number of all local nodes, including
17347c6ae99SBarry Smith     ghost nodes).
17447c6ae99SBarry Smith 
17547c6ae99SBarry Smith     Synopsis:
176aa219208SBarry Smith     DMDAGetGlobalIndicesF90(DM da,integer n,{integer, pointer :: idx(:)},integer ierr)
17747c6ae99SBarry Smith 
17847c6ae99SBarry Smith     Not Collective
17947c6ae99SBarry Smith 
18047c6ae99SBarry Smith     Input Parameter:
18147c6ae99SBarry Smith .   da - the distributed array
18247c6ae99SBarry Smith 
18347c6ae99SBarry Smith     Output Parameters:
18447c6ae99SBarry Smith +   n - the number of local elements, including ghost nodes (or PETSC_NULL)
18547c6ae99SBarry Smith .   idx - the Fortran90 pointer to the global indices
18647c6ae99SBarry Smith -   ierr - error code
18747c6ae99SBarry Smith 
18847c6ae99SBarry Smith     Level: intermediate
18947c6ae99SBarry Smith 
19047c6ae99SBarry Smith     Notes:
19147c6ae99SBarry Smith      Not yet supported for all F90 compilers
19247c6ae99SBarry Smith 
19347c6ae99SBarry Smith .keywords: distributed array, get, global, indices, local-to-global, f90
19447c6ae99SBarry Smith 
195aa219208SBarry Smith .seealso: DMDAGetGlobalIndices()
19647c6ae99SBarry Smith M*/
197