xref: /petsc/src/dm/impls/da/dadestroy.c (revision 7453f77509fc006cbcc7de2dd772f60dc49feac5)
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 #undef __FUNCT__
12 #define __FUNCT__ "DMDestroy_Private"
13 /*
14    DMDestroy_Private - handles the work vectors created by DMGetGlobalVector() and DMGetLocalVector()
15 
16 */
17 PetscErrorCode  DMDestroy_Private(DM dm,PetscBool  *done)
18 {
19   PetscErrorCode ierr;
20   PetscErrorCode i,cnt = 0;
21 
22   PetscFunctionBegin;
23   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24   *done = PETSC_FALSE;
25 
26   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
27     if (dm->localin[i])  cnt++;
28     if (dm->globalin[i]) cnt++;
29   }
30 
31   if (--((PetscObject)dm)->refct - cnt > 0) PetscFunctionReturn(0);
32 
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) PetscFunctionReturn(0);
39   ((PetscObject)dm)->refct = 0;
40 
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     ierr = VecDestroy(&dm->localin[i]);CHKERRQ(ierr);
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     ierr = VecDestroy(&dm->globalin[i]);CHKERRQ(ierr);
46   }
47   ierr = ISLocalToGlobalMappingDestroy(&dm->ltogmap);CHKERRQ(ierr);
48 
49   *done = PETSC_TRUE;
50   PetscFunctionReturn(0);
51 }
52 
53 #undef __FUNCT__
54 #define __FUNCT__ "DMDestroy_DA"
55 PetscErrorCode  DMDestroy_DA(DM da)
56 {
57   PetscErrorCode ierr;
58   PetscErrorCode i;
59   DM_DA          *dd = (DM_DA*)da->data;
60 
61   PetscFunctionBegin;
62   /* destroy the external/common part */
63   for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
64     ierr = PetscFree(dd->startghostedout[i]);CHKERRQ(ierr);
65     ierr = PetscFree(dd->startghostedin[i]);CHKERRQ(ierr);
66     ierr = PetscFree(dd->startout[i]);CHKERRQ(ierr);
67     ierr = PetscFree(dd->startin[i]);CHKERRQ(ierr);
68   }
69 
70   ierr = VecScatterDestroy(&dd->gtol);CHKERRQ(ierr);
71   ierr = VecScatterDestroy(&dd->ltol);CHKERRQ(ierr);
72   ierr = VecDestroy(&dd->natural);CHKERRQ(ierr);
73   ierr = VecScatterDestroy(&dd->gton);CHKERRQ(ierr);
74   ierr = AODestroy(&dd->ao);CHKERRQ(ierr);
75   ierr = PetscFree(dd->aotype);CHKERRQ(ierr);
76 
77   ierr = PetscFree(dd->lx);CHKERRQ(ierr);
78   ierr = PetscFree(dd->ly);CHKERRQ(ierr);
79   ierr = PetscFree(dd->lz);CHKERRQ(ierr);
80 
81   ierr = PetscFree(dd->refine_x_hier);CHKERRQ(ierr);
82   ierr = PetscFree(dd->refine_y_hier);CHKERRQ(ierr);
83   ierr = PetscFree(dd->refine_z_hier);CHKERRQ(ierr);
84 
85   if (dd->fieldname) {
86     for (i=0; i<dd->w; i++) {
87       ierr = PetscFree(dd->fieldname[i]);CHKERRQ(ierr);
88     }
89     ierr = PetscFree(dd->fieldname);CHKERRQ(ierr);
90   }
91   if (dd->coordinatename) {
92     for (i=0; i<da->dim; i++) {
93       ierr = PetscFree(dd->coordinatename[i]);CHKERRQ(ierr);
94     }
95     ierr = PetscFree(dd->coordinatename);CHKERRQ(ierr);
96   }
97   ierr = ISColoringDestroy(&dd->localcoloring);CHKERRQ(ierr);
98   ierr = ISColoringDestroy(&dd->ghostedcoloring);CHKERRQ(ierr);
99 
100   ierr = PetscFree(dd->neighbors);CHKERRQ(ierr);
101   ierr = PetscFree(dd->dfill);CHKERRQ(ierr);
102   ierr = PetscFree(dd->ofill);CHKERRQ(ierr);
103   ierr = PetscFree(dd->ofillcols);CHKERRQ(ierr);
104   ierr = PetscFree(dd->e);CHKERRQ(ierr);
105 
106   /* ierr = PetscSectionDestroy(&dd->defaultGlobalSection);CHKERRQ(ierr); */
107   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
108   ierr = PetscFree(dd);CHKERRQ(ierr);
109   PetscFunctionReturn(0);
110 }
111