Actual source code: dadestroy.c

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

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

  7: /* Logging support */
  8: PetscCookie DA_COOKIE = 0;
  9: PetscEvent  DA_GlobalToLocal = 0, DA_LocalToGlobal = 0, DA_LocalADFunction = 0;

 13: /*@C
 14:    DADestroy - Destroys a distributed array.

 16:    Collective on DA

 18:    Input Parameter:
 19: .  da - the distributed array to destroy 

 21:    Level: beginner

 23: .keywords: distributed array, destroy

 25: .seealso: DACreate1d(), DACreate2d(), DACreate3d()
 26: @*/
 27: PetscErrorCode DADestroy(DA da)
 28: {
 30:   PetscErrorCode i,cnt = 0;


 35:   for (i=0; i<DA_MAX_WORK_VECTORS; i++) {
 36:     if (da->localin[i])  {cnt++;}
 37:     if (da->globalin[i]) {cnt++;}
 38:   }

 40:   if (--da->refct - cnt > 0) return(0);
 41:   /*
 42:          Need this test because the da references the vectors that 
 43:      reference the da, so destroying the da calls destroy on the 
 44:      vectors that cause another destroy on the da
 45:   */
 46:   if (da->refct < 0) return(0);
 47:   da->refct = 0;

 49:   for (i=0; i<DA_MAX_WORK_VECTORS; i++) {
 50:     if (da->localout[i]) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Destroying a DA that has a local vector obtained with DAGetLocalVector()");
 51:     if (da->localin[i]) {VecDestroy(da->localin[i]);}
 52:     if (da->globalout[i]) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Destroying a DA that has a global vector obtained with DAGetGlobalVector()");
 53:     if (da->globalin[i]) {VecDestroy(da->globalin[i]);}
 54:   }

 56:   for (i=0; i<DA_MAX_AD_ARRAYS; i++) {
 57:     if (da->adstartghostedout[i]){
 58:       PetscFree(da->adstartghostedout[i]);
 59:     }
 60:     if (da->adstartghostedin[i]){
 61:       PetscFree(da->adstartghostedin[i]);
 62:     }
 63:     if (da->adstartout[i]){
 64:       PetscFree(da->adstartout[i]);
 65:     }
 66:     if (da->adstartin[i]){
 67:       PetscFree(da->adstartin[i]);
 68:     }
 69:   }
 70:   for (i=0; i<DA_MAX_AD_ARRAYS; i++) {
 71:     if (da->admfstartghostedout[i]){
 72:       PetscFree(da->admfstartghostedout[i]);
 73:     }
 74:     if (da->admfstartghostedin[i]){
 75:       PetscFree(da->admfstartghostedin[i]);
 76:     }
 77:     if (da->admfstartout[i]){
 78:       PetscFree(da->admfstartout[i]);
 79:     }
 80:     if (da->admfstartin[i]){
 81:       PetscFree(da->admfstartin[i]);
 82:     }
 83:   }
 84:   for (i=0; i<DA_MAX_WORK_ARRAYS; i++) {
 85:     if (da->startghostedout[i]){
 86:       PetscFree(da->startghostedout[i]);
 87:     }
 88:     if (da->startghostedin[i]){
 89:       PetscFree(da->startghostedin[i]);
 90:     }
 91:     if (da->startout[i]){
 92:       PetscFree(da->startout[i]);
 93:     }
 94:     if (da->startin[i]){
 95:       PetscFree(da->startin[i]);
 96:     }
 97:   }

 99:   /* if memory was published with AMS then destroy it */
100:   PetscObjectDepublish(da);

102:   PetscLogObjectDestroy(da);
103:   if (da->ltog)   {VecScatterDestroy(da->ltog);}
104:   if (da->gtol)   {VecScatterDestroy(da->gtol);}
105:   if (da->ltol)   {VecScatterDestroy(da->ltol);}
106:   if (da->natural){
107:     VecDestroy(da->natural);
108:   }
109:   if (da->gton) {
110:     VecScatterDestroy(da->gton);
111:   }

113:   if (da->ao) {
114:     AODestroy(da->ao);
115:   }
116:   ISLocalToGlobalMappingDestroy(da->ltogmap);
117:   ISLocalToGlobalMappingDestroy(da->ltogmapb);

119:   if (da->lx) {PetscFree(da->lx);}
120:   if (da->ly) {PetscFree(da->ly);}
121:   if (da->lz) {PetscFree(da->lz);}

123:   for (i=0; i<da->w; i++) {
124:     PetscStrfree(da->fieldname[i]);
125:   }
126:   PetscFree(da->fieldname);

128:   if (da->localcoloring) {
129:     ISColoringDestroy(da->localcoloring);
130:   }
131:   if (da->ghostedcoloring) {
132:     ISColoringDestroy(da->ghostedcoloring);
133:   }

135:   if (da->coordinates) {VecDestroy(da->coordinates);}
136:   if (da->ghosted_coordinates) {VecDestroy(da->ghosted_coordinates);}
137:   if (da->da_coordinates && da != da->da_coordinates) {DADestroy(da->da_coordinates);}

139:   if (da->dfill) {PetscFree(da->dfill);}
140:   if (da->ofill) {PetscFree(da->ofill);}
141:   if (da->e) {PetscFree(da->e);}

143:   PetscHeaderDestroy(da);
144:   return(0);
145: }

149: /*@C
150:    DAGetISLocalToGlobalMapping - Accesses the local-to-global mapping in a DA.

152:    Not Collective

154:    Input Parameter:
155: .  da - the distributed array that provides the mapping 

157:    Output Parameter:
158: .  ltog - the mapping

160:    Level: intermediate

162:    Notes:
163:    This mapping can them be used by VecSetLocalToGlobalMapping() or 
164:    MatSetLocalToGlobalMapping().

166:    Essentially the same data is returned in the form of an integer array
167:    with the routine DAGetGlobalIndices().

169: .keywords: distributed array, destroy

171: .seealso: DACreate1d(), DACreate2d(), DACreate3d(), VecSetLocalToGlobalMapping(),
172:           MatSetLocalToGlobalMapping(), DAGetGlobalIndices(), DAGetISLocalToGlobalMappingBlck()
173: @*/
174: PetscErrorCode DAGetISLocalToGlobalMapping(DA da,ISLocalToGlobalMapping *map)
175: {
179:   *map = da->ltogmap;
180:   return(0);
181: }

185: /*@C
186:    DAGetISLocalToGlobalMappingBlck - Accesses the local-to-global mapping in a DA.

188:    Not Collective

190:    Input Parameter:
191: .  da - the distributed array that provides the mapping 

193:    Output Parameter:
194: .  ltog - the mapping

196:    Level: intermediate

198:    Notes:
199:    This mapping can them be used by VecSetLocalToGlobalMappingBlock() or 
200:    MatSetLocalToGlobalMappingBlock().

202:    Essentially the same data is returned in the form of an integer array
203:    with the routine DAGetGlobalIndices().

205: .keywords: distributed array, destroy

207: .seealso: DACreate1d(), DACreate2d(), DACreate3d(), VecSetLocalToGlobalMapping(),
208:           MatSetLocalToGlobalMapping(), DAGetGlobalIndices(), DAGetISLocalToGlobalMapping()
209: @*/
210: PetscErrorCode DAGetISLocalToGlobalMappingBlck(DA da,ISLocalToGlobalMapping *map)
211: {
215:   *map = da->ltogmapb;
216:   return(0);
217: }