xref: /petsc/src/dm/impls/da/dadestroy.c (revision d372ba47371068bdce79d7d3cf159aa0df7e4cba)
1 
2 /*
3   Code for manipulating distributed regular arrays in parallel.
4 */
5 
6 #include <private/daimpl.h>    /*I   "petscdmda.h"   I*/
7 
8 /* Logging support */
9 PetscClassId  ADDA_CLASSID;
10 PetscLogEvent DMDA_LocalADFunction;
11 
12 #undef __FUNCT__
13 #define __FUNCT__ "DMDestroy_Private"
14 /*
15    DMDestroy_Private - handles the work vectors created by DMGetGlobalVector() and DMGetLocalVector()
16 
17 */
18 PetscErrorCode  DMDestroy_Private(DM dm,PetscBool  *done)
19 {
20   PetscErrorCode ierr;
21   PetscErrorCode i,cnt = 0;
22 
23   PetscFunctionBegin;
24   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25   *done = PETSC_FALSE;
26 
27   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
28     if (dm->localin[i])  {cnt++;}
29     if (dm->globalin[i]) {cnt++;}
30   }
31 
32   if (--((PetscObject)dm)->refct - cnt > 0) PetscFunctionReturn(0);
33 
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) PetscFunctionReturn(0);
40   ((PetscObject)dm)->refct = 0;
41 
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     ierr = VecDestroy(&dm->localin[i]);CHKERRQ(ierr);
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     ierr = VecDestroy(&dm->globalin[i]);CHKERRQ(ierr);
47   }
48   ierr = ISLocalToGlobalMappingDestroy(&dm->ltogmap);CHKERRQ(ierr);
49   ierr = ISLocalToGlobalMappingDestroy(&dm->ltogmapb);CHKERRQ(ierr);
50 
51   *done = PETSC_TRUE;
52   PetscFunctionReturn(0);
53 }
54 
55 #undef __FUNCT__
56 #define __FUNCT__ "DMDestroy_DA"
57 PetscErrorCode  DMDestroy_DA(DM da)
58 {
59   PetscErrorCode ierr;
60   PetscErrorCode i;
61   DM_DA          *dd = (DM_DA*)da->data;
62 
63   PetscFunctionBegin;
64   /* destroy the external/common part */
65   for (i=0; i<DMDA_MAX_AD_ARRAYS; i++) {
66     ierr = PetscFree(dd->adstartghostedout[i]);CHKERRQ(ierr);
67     ierr = PetscFree(dd->adstartghostedin[i]);CHKERRQ(ierr);
68     ierr = PetscFree(dd->adstartout[i]);CHKERRQ(ierr);
69     ierr = PetscFree(dd->adstartin[i]);CHKERRQ(ierr);
70   }
71   for (i=0; i<DMDA_MAX_AD_ARRAYS; i++) {
72     ierr = PetscFree(dd->admfstartghostedout[i]);CHKERRQ(ierr);
73     ierr = PetscFree(dd->admfstartghostedin[i]);CHKERRQ(ierr);
74     ierr = PetscFree(dd->admfstartout[i]);CHKERRQ(ierr);
75     ierr = PetscFree(dd->admfstartin[i]);CHKERRQ(ierr);
76   }
77   for (i=0; i<DMDA_MAX_WORK_ARRAYS; i++) {
78     ierr = PetscFree(dd->startghostedout[i]);CHKERRQ(ierr);
79     ierr = PetscFree(dd->startghostedin[i]);CHKERRQ(ierr);
80     ierr = PetscFree(dd->startout[i]);CHKERRQ(ierr);
81     ierr = PetscFree(dd->startin[i]);CHKERRQ(ierr);
82   }
83 
84   /* if memory was published with AMS then destroy it */
85   ierr = PetscObjectDepublish(da);CHKERRQ(ierr);
86 
87   ierr = VecScatterDestroy(&dd->ltog);CHKERRQ(ierr);
88   ierr = VecScatterDestroy(&dd->gtol);CHKERRQ(ierr);
89   ierr = VecScatterDestroy(&dd->ltol);CHKERRQ(ierr);
90   ierr = VecDestroy(&dd->natural);CHKERRQ(ierr);
91   ierr = VecScatterDestroy(&dd->gton);CHKERRQ(ierr);
92   ierr = AODestroy(&dd->ao);CHKERRQ(ierr);
93 
94   ierr = PetscFree(dd->idx);CHKERRQ(ierr);
95   ierr = PetscFree(dd->lx);CHKERRQ(ierr);
96   ierr = PetscFree(dd->ly);CHKERRQ(ierr);
97   ierr = PetscFree(dd->lz);CHKERRQ(ierr);
98 
99   if (dd->fieldname) {
100     for (i=0; i<dd->w; i++) {
101       ierr = PetscFree(dd->fieldname[i]);CHKERRQ(ierr);
102     }
103     ierr = PetscFree(dd->fieldname);CHKERRQ(ierr);
104   }
105   ierr = ISColoringDestroy(&dd->localcoloring);CHKERRQ(ierr);
106   ierr = ISColoringDestroy(&dd->ghostedcoloring);CHKERRQ(ierr);
107 
108   ierr = VecDestroy(&dd->coordinates);CHKERRQ(ierr);
109   ierr = VecDestroy(&dd->ghosted_coordinates);CHKERRQ(ierr);
110   ierr = DMDestroy(&dd->da_coordinates);CHKERRQ(ierr);
111 
112   ierr = PetscFree(dd->neighbors);CHKERRQ(ierr);
113   ierr = PetscFree(dd->dfill);CHKERRQ(ierr);
114   ierr = PetscFree(dd->ofill);CHKERRQ(ierr);
115   ierr = PetscFree(dd->e);CHKERRQ(ierr);
116   PetscFunctionReturn(0);
117 }
118