2: /*
3: Code for manipulating distributed regular arrays in parallel.
4: */
6: #include <private/daimpl.h> /*I "petscdmda.h" I*/
8: /* Logging support */
9: PetscClassId ADDA_CLASSID;
10: PetscLogEvent DMDA_LocalADFunction;
14: /*
15: DMDestroy_Private - handles the work vectors created by DMGetGlobalVector() and DMGetLocalVector()
17: */
18: PetscErrorCode DMDestroy_Private(DM dm,PetscBool *done)
19: {
21: PetscErrorCode i,cnt = 0;
25: *done = PETSC_FALSE;
27: for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
28: if (dm->localin[i]) {cnt++;}
29: if (dm->globalin[i]) {cnt++;}
30: }
32: if (--((PetscObject)dm)->refct - cnt > 0) return(0);
34: /*
35: Need this test because the dm references the vectors that
36: reference the dm, so destroying the dm calls destroy on the
37: vectors that cause another destroy on the dm
38: */
39: if (((PetscObject)dm)->refct < 0) return(0);
40: ((PetscObject)dm)->refct = 0;
42: for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
43: if (dm->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
44: VecDestroy(&dm->localin[i]);
45: if (dm->globalout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a global vector obtained with DMGetGlobalVector()");
46: VecDestroy(&dm->globalin[i]);
47: }
48: ISLocalToGlobalMappingDestroy(&dm->ltogmap);
49: ISLocalToGlobalMappingDestroy(&dm->ltogmapb);
51: *done = PETSC_TRUE;
52: return(0);
53: }
57: PetscErrorCode DMDestroy_DA(DM da)
58: {
60: PetscErrorCode i;
61: DM_DA *dd = (DM_DA*)da->data;
64: /* destroy the external/common part */
65: for (i=0; i<DMDA_MAX_AD_ARRAYS; i++) {
66: PetscFree(dd->adstartghostedout[i]);
67: PetscFree(dd->adstartghostedin[i]);
68: PetscFree(dd->adstartout[i]);
69: PetscFree(dd->adstartin[i]);
70: }
71: for (i=0; i<DMDA_MAX_AD_ARRAYS; i++) {
72: PetscFree(dd->admfstartghostedout[i]);
73: PetscFree(dd->admfstartghostedin[i]);
74: PetscFree(dd->admfstartout[i]);
75: PetscFree(dd->admfstartin[i]);
76: }
77: for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
78: PetscFree(dd->startghostedout[i]);
79: PetscFree(dd->startghostedin[i]);
80: PetscFree(dd->startout[i]);
81: PetscFree(dd->startin[i]);
82: }
84: /* if memory was published with AMS then destroy it */
85: PetscObjectDepublish(da);
87: VecScatterDestroy(&dd->ltog);
88: VecScatterDestroy(&dd->gtol);
89: VecScatterDestroy(&dd->ltol);
90: VecDestroy(&dd->natural);
91: VecScatterDestroy(&dd->gton);
92: AODestroy(&dd->ao);
94: PetscFree(dd->idx);
95: PetscFree(dd->lx);
96: PetscFree(dd->ly);
97: PetscFree(dd->lz);
99: if (dd->fieldname) {
100: for (i=0; i<dd->w; i++) {
101: PetscFree(dd->fieldname[i]);
102: }
103: PetscFree(dd->fieldname);
104: }
105: ISColoringDestroy(&dd->localcoloring);
106: ISColoringDestroy(&dd->ghostedcoloring);
108: VecDestroy(&dd->coordinates);
109: VecDestroy(&dd->ghosted_coordinates);
110: DMDestroy(&dd->da_coordinates);
112: PetscFree(dd->neighbors);
113: PetscFree(dd->dfill);
114: PetscFree(dd->ofill);
115: PetscFree(dd->e);
116: return(0);
117: }