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