Actual source code: daltol.c

  1: /*
  2:   Code for manipulating distributed regular arrays in parallel.
  3: */

 5:  #include src/dm/da/daimpl.h

  9: /*
 10:    DALocalToLocalCreate - Creates the local to local scatter

 12:    Collective on DA

 14:    Input Parameter:
 15: .  da - the distributed array

 17: */
 18: PetscErrorCode DALocalToLocalCreate(DA da)
 19: {
 21:   PetscInt *idx,left,j,count,up,down,i,bottom,top,k;


 26:   if (da->ltol) return(0);
 27:   /* 
 28:      We simply remap the values in the from part of 
 29:      global to local to read from an array with the ghost values 
 30:      rather then from the plain array.
 31:   */
 32:   VecScatterCopy(da->gtol,&da->ltol);
 33:   PetscLogObjectParent(da,da->ltol);
 34:   if (da->dim == 1) {
 35:     left = da->xs - da->Xs;
 36:     PetscMalloc((da->xe-da->xs)*sizeof(PetscInt),&idx);
 37:     for (j=0; j<da->xe-da->xs; j++) {
 38:       idx[j] = left + j;
 39:     }
 40:   } else if (da->dim == 2) {
 41:     left  = da->xs - da->Xs; down  = da->ys - da->Ys; up    = down + da->ye-da->ys;
 42:     PetscMalloc((da->xe-da->xs)*(up - down)*sizeof(PetscInt),&idx);
 43:     count = 0;
 44:     for (i=down; i<up; i++) {
 45:       for (j=0; j<da->xe-da->xs; j++) {
 46:         idx[count++] = left + i*(da->Xe-da->Xs) + j;
 47:       }
 48:     }
 49:   } else if (da->dim == 3) {
 50:     left   = da->xs - da->Xs;
 51:     bottom = da->ys - da->Ys; top = bottom + da->ye-da->ys ;
 52:     down   = da->zs - da->Zs; up  = down + da->ze-da->zs;
 53:     count  = (da->xe-da->xs)*(top-bottom)*(up-down);
 54:     PetscMalloc(count*sizeof(PetscInt),&idx);
 55:     count  = 0;
 56:     for (i=down; i<up; i++) {
 57:       for (j=bottom; j<top; j++) {
 58:         for (k=0; k<da->xe-da->xs; k++) {
 59:           idx[count++] = (left+j*(da->Xe-da->Xs))+i*(da->Xe-da->Xs)*(da->Ye-da->Ys) + k;
 60:         }
 61:       }
 62:     }
 63:   } else SETERRQ1(PETSC_ERR_ARG_CORRUPT,"DA has invalid dimension %D",da->dim);

 65:   VecScatterRemap(da->ltol,idx,PETSC_NULL);
 66:   PetscFree(idx);
 67:   return(0);
 68: }

 72: /*@
 73:    DALocalToLocalBegin - Maps from a local vector (including ghost points
 74:    that contain irrelevant values) to another local vector where the ghost
 75:    points in the second are set correctly. Must be followed by DALocalToLocalEnd().

 77:    Collective on DA and Vec

 79:    Input Parameters:
 80: +  da - the distributed array context
 81: .  g - the original local vector
 82: -  mode - one of INSERT_VALUES or ADD_VALUES

 84:    Output Parameter:
 85: .  l  - the local vector with correct ghost values

 87:    Level: intermediate

 89:    Notes:
 90:    The local vectors used here need not be the same as those
 91:    obtained from DACreateLocalVector(), BUT they
 92:    must have the same parallel data layout; they could, for example, be 
 93:    obtained with VecDuplicate() from the DA originating vectors.

 95: .keywords: distributed array, local-to-local, begin

 97: .seealso: DALocalToLocalEnd(), DALocalToGlobal()
 98: @*/
 99: PetscErrorCode DALocalToLocalBegin(DA da,Vec g,InsertMode mode,Vec l)
100: {

105:   if (!da->ltol) {
106:     DALocalToLocalCreate(da);
107:   }
108:   VecScatterBegin(g,l,mode,SCATTER_FORWARD,da->ltol);
109:   return(0);
110: }

114: /*@
115:    DALocalToLocalEnd - Maps from a local vector (including ghost points
116:    that contain irrelevant values) to another local vector where the ghost
117:    points in the second are set correctly.  Must be preceeded by 
118:    DALocalToLocalBegin().

120:    Collective on DA and Vec

122:    Input Parameters:
123: +  da - the distributed array context
124: .  g - the original local vector
125: -  mode - one of INSERT_VALUES or ADD_VALUES

127:    Output Parameter:
128: .  l  - the local vector with correct ghost values

130:    Level: intermediate

132:    Note:
133:    The local vectors used here need not be the same as those
134:    obtained from DACreateLocalVector(), BUT they
135:    must have the same parallel data layout; they could, for example, be 
136:    obtained with VecDuplicate() from the DA originating vectors.

138: .keywords: distributed array, local-to-local, end

140: .seealso: DALocalToLocalBegin(), DALocalToGlobal()
141: @*/
142: PetscErrorCode DALocalToLocalEnd(DA da,Vec g,InsertMode mode,Vec l)
143: {

150:   VecScatterEnd(g,l,mode,SCATTER_FORWARD,da->ltol);
151:   return(0);
152: }