2: /*
3: Code for manipulating distributed regular arrays in parallel.
4: */
6: #include <petsc-private/dmdaimpl.h> /*I "petscdmda.h" I*/
8: /* Logging support */
9: PetscLogEvent DMDA_LocalADFunction;
13: /*
14: DMDestroy_Private - handles the work vectors created by DMGetGlobalVector() and DMGetLocalVector()
16: */
17: PetscErrorCode DMDestroy_Private(DM dm,PetscBool *done)
18: {
20: PetscErrorCode i,cnt = 0;
24: *done = PETSC_FALSE;
26: for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
27: if (dm->localin[i]) cnt++;
28: if (dm->globalin[i]) cnt++;
29: }
31: if (--((PetscObject)dm)->refct - cnt > 0) return(0);
33: /*
34: Need this test because the dm references the vectors that
35: reference the dm, so destroying the dm calls destroy on the
36: vectors that cause another destroy on the dm
37: */
38: if (((PetscObject)dm)->refct < 0) return(0);
39: ((PetscObject)dm)->refct = 0;
41: for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
42: if (dm->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
43: VecDestroy(&dm->localin[i]);
44: if (dm->globalout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a global vector obtained with DMGetGlobalVector()");
45: VecDestroy(&dm->globalin[i]);
46: }
47: ISLocalToGlobalMappingDestroy(&dm->ltogmap);
49: *done = PETSC_TRUE;
50: return(0);
51: }
55: PetscErrorCode DMDestroy_DA(DM da)
56: {
58: PetscErrorCode i;
59: DM_DA *dd = (DM_DA*)da->data;
62: /* destroy the external/common part */
63: for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
64: PetscFree(dd->startghostedout[i]);
65: PetscFree(dd->startghostedin[i]);
66: PetscFree(dd->startout[i]);
67: PetscFree(dd->startin[i]);
68: }
70: VecScatterDestroy(&dd->gtol);
71: VecScatterDestroy(&dd->ltol);
72: VecDestroy(&dd->natural);
73: VecScatterDestroy(&dd->gton);
74: AODestroy(&dd->ao);
76: PetscFree(dd->lx);
77: PetscFree(dd->ly);
78: PetscFree(dd->lz);
80: if (dd->fieldname) {
81: for (i=0; i<dd->w; i++) {
82: PetscFree(dd->fieldname[i]);
83: }
84: PetscFree(dd->fieldname);
85: }
86: if (dd->coordinatename) {
87: for (i=0; i<dd->dim; i++) {
88: PetscFree(dd->coordinatename[i]);
89: }
90: PetscFree(dd->coordinatename);
91: }
92: ISColoringDestroy(&dd->localcoloring);
93: ISColoringDestroy(&dd->ghostedcoloring);
95: PetscFree(dd->neighbors);
96: PetscFree(dd->dfill);
97: PetscFree(dd->ofill);
98: PetscFree(dd->ofillcols);
99: PetscFree(dd->e);
101: /* PetscSectionDestroy(&dd->defaultGlobalSection); */
102: /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
103: PetscFree(dd);
104: return(0);
105: }