Actual source code: daltol.c

      1: #define PETSCDM_DLL

      3: /*
      4:   Code for manipulating distributed regular arrays in parallel.
      5: */

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

     11: /*
     12:    DALocalToLocalCreate - Creates the local to local scatter

     14:    Collective on DA

     16:    Input Parameter:
     17: .  da - the distributed array

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

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

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

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

     79:    Collective on DA and Vec

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

     86:    Output Parameter:
     87: .  l  - the local vector with correct ghost values

     89:    Level: intermediate

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

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

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

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

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

    122:    Collective on DA and Vec

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

    129:    Output Parameter:
    130: .  l  - the local vector with correct ghost values

    132:    Level: intermediate

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

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

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

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