xref: /petsc/src/dm/impls/da/daindex.c (revision 9a42bb27a39f0cdf3306a1e22d33cd9809484eaa)
147c6ae99SBarry Smith #define PETSCDM_DLL
247c6ae99SBarry Smith 
347c6ae99SBarry Smith /*
447c6ae99SBarry Smith   Code for manipulating distributed regular arrays in parallel.
547c6ae99SBarry Smith */
647c6ae99SBarry Smith 
747c6ae99SBarry Smith #include "private/daimpl.h"    /*I   "petscda.h"   I*/
847c6ae99SBarry Smith 
947c6ae99SBarry Smith #undef __FUNCT__
1047c6ae99SBarry Smith #define __FUNCT__ "DAGetGlobalIndices"
1147c6ae99SBarry Smith /*@C
1247c6ae99SBarry Smith    DAGetGlobalIndices - Returns the global node number of all local nodes,
1347c6ae99SBarry Smith    including ghost nodes.
1447c6ae99SBarry Smith 
1547c6ae99SBarry Smith    Not Collective
1647c6ae99SBarry Smith 
1747c6ae99SBarry Smith    Input Parameter:
1847c6ae99SBarry Smith .  da - the distributed array
1947c6ae99SBarry Smith 
2047c6ae99SBarry Smith    Output Parameters:
2147c6ae99SBarry Smith +  n - the number of local elements, including ghost nodes (or PETSC_NULL)
2247c6ae99SBarry Smith -  idx - the global indices
2347c6ae99SBarry Smith 
2447c6ae99SBarry Smith    Level: intermediate
2547c6ae99SBarry Smith 
2647c6ae99SBarry Smith    Note:
2747c6ae99SBarry Smith    For DA_STENCIL_STAR stencils the inactive corner ghost nodes are also included
2847c6ae99SBarry Smith    in the list of local indices (even though those nodes are not updated
2947c6ae99SBarry Smith    during calls to DAXXXToXXX().
3047c6ae99SBarry Smith 
3147c6ae99SBarry Smith    Essentially the same data is returned in the form of a local-to-global mapping
3247c6ae99SBarry Smith    with the routine DAGetISLocalToGlobalMapping();
3347c6ae99SBarry Smith 
3447c6ae99SBarry Smith    Fortran Note:
3547c6ae99SBarry Smith    This routine is used differently from Fortran
3647c6ae99SBarry Smith .vb
37*9a42bb27SBarry Smith         DM          da
3847c6ae99SBarry Smith         integer     n,da_array(1)
3947c6ae99SBarry Smith         PetscOffset i_da
4047c6ae99SBarry Smith         integer     ierr
4147c6ae99SBarry Smith         call DAGetGlobalIndices(da,n,da_array,i_da,ierr)
4247c6ae99SBarry Smith 
4347c6ae99SBarry Smith    C Access first local entry in list
4447c6ae99SBarry Smith         value = da_array(i_da + 1)
4547c6ae99SBarry Smith .ve
4647c6ae99SBarry Smith 
4747c6ae99SBarry Smith    See the <A href="../../docs/manual.pdf#nameddest=ch_fortran">Fortran chapter</A> of the users manual for details.
4847c6ae99SBarry Smith 
4947c6ae99SBarry Smith .keywords: distributed array, get, global, indices, local-to-global
5047c6ae99SBarry Smith 
51*9a42bb27SBarry Smith .seealso: DACreate2d(), DAGetGhostCorners(), DAGetCorners(), DMLocalToGlobalBegin()
52*9a42bb27SBarry Smith           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DALocalToLocalBegin(), DAGetAO(), DAGetGlobalIndicesF90()
5347c6ae99SBarry Smith           DAGetISLocalToGlobalMapping(), DACreate3d(), DACreate1d(), DALocalToLocalEnd(), DAGetOwnershipRanges()
5447c6ae99SBarry Smith @*/
55*9a42bb27SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DAGetGlobalIndices(DM da,PetscInt *n,PetscInt **idx)
5647c6ae99SBarry Smith {
5747c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
5847c6ae99SBarry Smith 
5947c6ae99SBarry Smith   PetscFunctionBegin;
6047c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
6147c6ae99SBarry Smith   if (n)   *n   = dd->Nl;
6247c6ae99SBarry Smith   if (idx) *idx = dd->idx;
6347c6ae99SBarry Smith   PetscFunctionReturn(0);
6447c6ae99SBarry Smith }
6547c6ae99SBarry Smith 
6647c6ae99SBarry Smith #undef __FUNCT__
6747c6ae99SBarry Smith #define __FUNCT__ "DAGetNatural_Private"
6847c6ae99SBarry Smith /*
6947c6ae99SBarry Smith    Gets the natural number for each global number on the process.
7047c6ae99SBarry Smith 
7147c6ae99SBarry Smith    Used by DAGetAO() and DAGlobalToNatural_Create()
7247c6ae99SBarry Smith */
73*9a42bb27SBarry Smith PetscErrorCode DAGetNatural_Private(DM da,PetscInt *outNlocal,IS *isnatural)
7447c6ae99SBarry Smith {
7547c6ae99SBarry Smith   PetscErrorCode ierr;
7647c6ae99SBarry Smith   PetscInt       Nlocal,i,j,k,*lidx,lict = 0;
7747c6ae99SBarry Smith   DM_DA          *dd = (DM_DA*)da->data;
7847c6ae99SBarry Smith 
7947c6ae99SBarry Smith   PetscFunctionBegin;
8047c6ae99SBarry Smith   Nlocal = (dd->xe-dd->xs);
8147c6ae99SBarry Smith   if (dd->dim > 1) {
8247c6ae99SBarry Smith     Nlocal *= (dd->ye-dd->ys);
8347c6ae99SBarry Smith   }
8447c6ae99SBarry Smith   if (dd->dim > 2) {
8547c6ae99SBarry Smith     Nlocal *= (dd->ze-dd->zs);
8647c6ae99SBarry Smith   }
8747c6ae99SBarry Smith 
8847c6ae99SBarry Smith   ierr = PetscMalloc(Nlocal*sizeof(PetscInt),&lidx);CHKERRQ(ierr);
8947c6ae99SBarry Smith 
9047c6ae99SBarry Smith   if (dd->dim == 1) {
9147c6ae99SBarry Smith     for (i=dd->xs; i<dd->xe; i++) {
9247c6ae99SBarry Smith       /*  global number in natural ordering */
9347c6ae99SBarry Smith       lidx[lict++] = i;
9447c6ae99SBarry Smith     }
9547c6ae99SBarry Smith   } else if (dd->dim == 2) {
9647c6ae99SBarry Smith     for (j=dd->ys; j<dd->ye; j++) {
9747c6ae99SBarry Smith       for (i=dd->xs; i<dd->xe; i++) {
9847c6ae99SBarry Smith 	/*  global number in natural ordering */
9947c6ae99SBarry Smith 	lidx[lict++] = i + j*dd->M*dd->w;
10047c6ae99SBarry Smith       }
10147c6ae99SBarry Smith     }
10247c6ae99SBarry Smith   } else if (dd->dim == 3) {
10347c6ae99SBarry Smith     for (k=dd->zs; k<dd->ze; k++) {
10447c6ae99SBarry Smith       for (j=dd->ys; j<dd->ye; j++) {
10547c6ae99SBarry Smith 	for (i=dd->xs; i<dd->xe; i++) {
10647c6ae99SBarry Smith 	  lidx[lict++] = i + j*dd->M*dd->w + k*dd->M*dd->N*dd->w;
10747c6ae99SBarry Smith 	}
10847c6ae99SBarry Smith       }
10947c6ae99SBarry Smith     }
11047c6ae99SBarry Smith   }
11147c6ae99SBarry Smith   *outNlocal = Nlocal;
11247c6ae99SBarry Smith   ierr = ISCreateGeneral(((PetscObject)da)->comm,Nlocal,lidx,PETSC_OWN_POINTER,isnatural);CHKERRQ(ierr);
11347c6ae99SBarry Smith   PetscFunctionReturn(0);
11447c6ae99SBarry Smith }
11547c6ae99SBarry Smith 
11647c6ae99SBarry Smith #undef __FUNCT__
11747c6ae99SBarry Smith #define __FUNCT__ "DAGetAO"
11847c6ae99SBarry Smith /*@
11947c6ae99SBarry Smith    DAGetAO - Gets the application ordering context for a distributed array.
12047c6ae99SBarry Smith 
12147c6ae99SBarry Smith    Collective on DA
12247c6ae99SBarry Smith 
12347c6ae99SBarry Smith    Input Parameter:
12447c6ae99SBarry Smith .  da - the distributed array
12547c6ae99SBarry Smith 
12647c6ae99SBarry Smith    Output Parameters:
12747c6ae99SBarry Smith .  ao - the application ordering context for DAs
12847c6ae99SBarry Smith 
12947c6ae99SBarry Smith    Level: intermediate
13047c6ae99SBarry Smith 
13147c6ae99SBarry Smith    Notes:
13247c6ae99SBarry Smith    In this case, the AO maps to the natural grid ordering that would be used
13347c6ae99SBarry Smith    for the DA if only 1 processor were employed (ordering most rapidly in the
13447c6ae99SBarry Smith    x-direction, then y, then z).  Multiple degrees of freedom are numbered
13547c6ae99SBarry Smith    for each node (rather than 1 component for the whole grid, then the next
13647c6ae99SBarry Smith    component, etc.)
13747c6ae99SBarry Smith 
13847c6ae99SBarry Smith .keywords: distributed array, get, global, indices, local-to-global
13947c6ae99SBarry Smith 
14047c6ae99SBarry Smith .seealso: DACreate2d(), DAGetGhostCorners(), DAGetCorners(), DALocalToGlocal()
141*9a42bb27SBarry Smith           DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DALocalToLocalBegin(), DALocalToLocalEnd(), DAGetGlobalIndices(), DAGetOwnershipRanges(),
14247c6ae99SBarry Smith           AO, AOPetscToApplication(), AOApplicationToPetsc()
14347c6ae99SBarry Smith @*/
144*9a42bb27SBarry Smith PetscErrorCode PETSCDM_DLLEXPORT DAGetAO(DM da,AO *ao)
14547c6ae99SBarry Smith {
14647c6ae99SBarry Smith   DM_DA *dd = (DM_DA*)da->data;
14747c6ae99SBarry Smith 
14847c6ae99SBarry Smith   PetscFunctionBegin;
14947c6ae99SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
15047c6ae99SBarry Smith   PetscValidPointer(ao,2);
15147c6ae99SBarry Smith 
15247c6ae99SBarry Smith   /*
15347c6ae99SBarry Smith      Build the natural ordering to PETSc ordering mappings.
15447c6ae99SBarry Smith   */
15547c6ae99SBarry Smith   if (!dd->ao) {
15647c6ae99SBarry Smith     IS             ispetsc,isnatural;
15747c6ae99SBarry Smith     PetscErrorCode ierr;
15847c6ae99SBarry Smith     PetscInt       Nlocal;
15947c6ae99SBarry Smith 
16047c6ae99SBarry Smith     ierr = DAGetNatural_Private(da,&Nlocal,&isnatural);CHKERRQ(ierr);
16147c6ae99SBarry Smith     ierr = ISCreateStride(((PetscObject)da)->comm,Nlocal,dd->base,1,&ispetsc);CHKERRQ(ierr);
16247c6ae99SBarry Smith     ierr = AOCreateBasicIS(isnatural,ispetsc,&dd->ao);CHKERRQ(ierr);
16347c6ae99SBarry Smith     ierr = PetscLogObjectParent(da,dd->ao);CHKERRQ(ierr);
16447c6ae99SBarry Smith     ierr = ISDestroy(ispetsc);CHKERRQ(ierr);
16547c6ae99SBarry Smith     ierr = ISDestroy(isnatural);CHKERRQ(ierr);
16647c6ae99SBarry Smith   }
16747c6ae99SBarry Smith   *ao = dd->ao;
16847c6ae99SBarry Smith   PetscFunctionReturn(0);
16947c6ae99SBarry Smith }
17047c6ae99SBarry Smith 
17147c6ae99SBarry Smith /*MC
17247c6ae99SBarry Smith     DAGetGlobalIndicesF90 - Returns a Fortran90 pointer to the list of
17347c6ae99SBarry Smith     global indices (global node number of all local nodes, including
17447c6ae99SBarry Smith     ghost nodes).
17547c6ae99SBarry Smith 
17647c6ae99SBarry Smith     Synopsis:
177*9a42bb27SBarry Smith     DAGetGlobalIndicesF90(DM da,integer n,{integer, pointer :: idx(:)},integer ierr)
17847c6ae99SBarry Smith 
17947c6ae99SBarry Smith     Not Collective
18047c6ae99SBarry Smith 
18147c6ae99SBarry Smith     Input Parameter:
18247c6ae99SBarry Smith .   da - the distributed array
18347c6ae99SBarry Smith 
18447c6ae99SBarry Smith     Output Parameters:
18547c6ae99SBarry Smith +   n - the number of local elements, including ghost nodes (or PETSC_NULL)
18647c6ae99SBarry Smith .   idx - the Fortran90 pointer to the global indices
18747c6ae99SBarry Smith -   ierr - error code
18847c6ae99SBarry Smith 
18947c6ae99SBarry Smith     Level: intermediate
19047c6ae99SBarry Smith 
19147c6ae99SBarry Smith     Notes:
19247c6ae99SBarry Smith      Not yet supported for all F90 compilers
19347c6ae99SBarry Smith 
19447c6ae99SBarry Smith .keywords: distributed array, get, global, indices, local-to-global, f90
19547c6ae99SBarry Smith 
19647c6ae99SBarry Smith .seealso: DAGetGlobalIndices()
19747c6ae99SBarry Smith M*/
198