1 /* Copyright 2004,2007 ENSEIRB, INRIA & CNRS
2 **
3 ** This file is part of the Scotch software package for static mapping,
4 ** graph partitioning and sparse matrix ordering.
5 **
6 ** This software is governed by the CeCILL-C license under French law
7 ** and abiding by the rules of distribution of free software. You can
8 ** use, modify and/or redistribute the software under the terms of the
9 ** CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
10 ** URL: "http://www.cecill.info".
11 **
12 ** As a counterpart to the access to the source code and rights to copy,
13 ** modify and redistribute granted by the license, users are provided
14 ** only with a limited warranty and the software's author, the holder of
15 ** the economic rights, and the successive licensors have only limited
16 ** liability.
17 **
18 ** In this respect, the user's attention is drawn to the risks associated
19 ** with loading, using, modifying and/or developing or reproducing the
20 ** software by the user in light of its specific status of free software,
21 ** that may mean that it is complicated to manipulate, and that also
22 ** therefore means that it is reserved for developers and experienced
23 ** professionals having in-depth computer knowledge. Users are therefore
24 ** encouraged to load and test the software's suitability as regards
25 ** their requirements in conditions enabling the security of their
26 ** systems and/or data to be ensured and, more generally, to use and
27 ** operate it in the same conditions as regards security.
28 **
29 ** The fact that you are presently reading this means that you have had
30 ** knowledge of the CeCILL-C license and that you accept its terms.
31 */
32 /************************************************************/
33 /**                                                        **/
34 /**   NAME       : mesh.c                                  **/
35 /**                                                        **/
36 /**   AUTHOR     : Francois PELLEGRINI                     **/
37 /**                                                        **/
38 /**   FUNCTION   : This module handles the source mesh     **/
39 /**                functions.                              **/
40 /**                                                        **/
41 /**   DATES      : # Version 4.0  : from : 29 dec 2001     **/
42 /**                                 to     05 may 2004     **/
43 /**                                                        **/
44 /************************************************************/
45 
46 /*
47 **  The defines and includes.
48 */
49 
50 #define MESH
51 
52 #include "module.h"
53 #include "common.h"
54 #include "graph.h"
55 #include "mesh.h"
56 
57 /****************************************/
58 /*                                      */
59 /* These routines handle source meshes. */
60 /*                                      */
61 /****************************************/
62 
63 /* This routine initializes a source mesh
64 ** structure.
65 ** It returns:
66 ** - 0  : in all cases.
67 */
68 
69 int
meshInit(Mesh * const meshptr)70 meshInit (
71 Mesh * const                meshptr)
72 {
73   memSet (meshptr, 0, sizeof (Mesh));             /* Initialize mesh fields      */
74   meshptr->flagval = MESHFREETABS;                /* By default, free all arrays */
75 
76   return (0);
77 }
78 
79 /* This routine frees a source mesh structure.
80 ** It returns:
81 ** - VOID  : in all cases.
82 */
83 
84 void
meshExit(Mesh * const meshptr)85 meshExit (
86 Mesh * const                meshptr)
87 {
88   meshFree (meshptr);                             /* Exit mesh data */
89 
90 #ifdef SCOTCH_DEBUG_MESH2
91   memSet (meshptr, ~0, sizeof (Mesh));            /* Purge mesh fields */
92 #endif /* SCOTCH_DEBUG_MESH2 */
93 }
94 
95 /* This routine frees the mesh data. Because
96 ** vertex load arrays are either passed by
97 ** the user, or grouped with other arrays,
98 ** they are not considered for explicit
99 ** freeing. This is also much simpler, as
100 ** load arrays can be grouped or not.
101 ** It returns:
102 ** - VOID  : in all cases.
103 */
104 
105 void
meshFree(Mesh * const meshptr)106 meshFree (
107 Mesh * const                meshptr)
108 {
109   if (((meshptr->flagval & MESHFREEEDGE) != 0) && /* If edgetab must be freed */
110       (meshptr->edgetax != NULL))                 /* And if it exists          */
111     memFree (meshptr->edgetax + meshptr->baseval); /* Free it                  */
112 
113   if ((meshptr->flagval & MESHFREEVEND) != 0) {   /* If vendtab must be freed                                    */
114     if ((meshptr->vendtax != NULL) &&             /* If vendtab is distinct from verttab                         */
115         (meshptr->vendtax != meshptr->verttax + 1) && /* (if vertex arrays grouped, vendtab not distinct anyway) */
116         ((meshptr->flagval & MESHVERTGROUP) == 0))
117       memFree (meshptr->vendtax + meshptr->baseval); /* Then free vendtab */
118   }
119   if ((meshptr->flagval & MESHFREEVERT) != 0) {   /* If verttab must be freed                             */
120     if (meshptr->verttax != NULL)                 /* Free verttab anyway, as it is the array group leader */
121       memFree (meshptr->verttax + meshptr->baseval);
122   }
123 #ifdef SCOTCH_DEBUG_MESH2
124   if ((meshptr->flagval & MESHFREEVNUM) != 0) {   /* If vnumtab must be freed         */
125     if ((meshptr->vnumtax != NULL) &&             /* And is not in vertex array group */
126         ((meshptr->flagval & MESHVERTGROUP) == 0))
127       errorPrint ("meshFree: vnumtab should never be freed as its base may vary according to creation routines");
128   }
129 #endif /* SCOTCH_DEBUG_MESH2 */
130   if ((meshptr->flagval & MESHFREEOTHR) != 0) {   /* If other arrays must be freed */
131     if (meshptr->vlbltax != NULL)
132       memFree (meshptr->vlbltax + meshptr->baseval);
133   }
134 
135 #ifdef SCOTCH_DEBUG_MESH2
136   memSet (meshptr, ~0, sizeof (Mesh));            /* Purge mesh fields */
137 #endif /* SCOTCH_DEBUG_MESH2 */
138 }
139 
140 /* This routine sets the base of the given
141 ** mesh to the given base value, and returns
142 ** the old base value.
143 ** It returns:
144 ** - old base value : in all cases.
145 */
146 
147 Gnum
meshBase(Mesh * const meshptr,const Gnum baseval)148 meshBase (
149 Mesh * const                meshptr,
150 const Gnum                  baseval)
151 {
152   Gnum                baseold;                    /* Old base value  */
153   Gnum                baseadj;                    /* Base adjustment */
154   Gnum                vertnum;
155   Gnum                edgenum;
156 
157   if (meshptr->baseval == baseval)                /* If nothing to do */
158     return (baseval);
159 
160   baseold = meshptr->baseval;                     /* Record old base value */
161   baseadj = baseval - baseold;                    /* Compute adjustment    */
162 
163   for (vertnum = meshptr->baseval; vertnum < (meshptr->velmnbr + meshptr->vnodnbr + meshptr->baseval); vertnum ++) {
164     for (edgenum = meshptr->verttax[vertnum]; edgenum < meshptr->vendtax[vertnum]; edgenum ++)
165       meshptr->edgetax[edgenum] += baseadj;
166     meshptr->verttax[vertnum] += baseadj;
167   }
168   if (meshptr->vendtax != meshptr->verttax + 1) { /* If distinct vertex end array */
169     for (vertnum = meshptr->baseval; vertnum < (meshptr->velmnbr + meshptr->vnodnbr + meshptr->baseval); vertnum ++)
170       meshptr->vendtax[vertnum] += baseadj;
171   }
172   else                                            /* If same vertex end array (of size +1)                               */
173     meshptr->verttax[meshptr->velmnbr + meshptr->vnodnbr + meshptr->baseval] += baseadj; /* Adjust last entry of verttab */
174 
175   meshptr->verttax -= baseadj;                    /* Adjust array accesses */
176   meshptr->vendtax -= baseadj;
177   meshptr->edgetax -= baseadj;
178 
179   if (meshptr->vnumtax != NULL)
180     meshptr->vnumtax -= baseadj;
181   if (meshptr->vlbltax != NULL)
182     meshptr->vlbltax -= baseadj;
183 
184   meshptr->baseval  = baseval;                    /* Set new base value    */
185   meshptr->velmbas += baseadj;                    /* Adjust mesh parameter */
186   meshptr->velmnnd += baseadj;
187   meshptr->vnodbas += baseadj;
188   meshptr->vnodnnd += baseadj;
189 
190   return (baseold);                               /* Return old base value */
191 }
192