Debian Codegraph @

packages/seacas/libraries/chaco/main/interface.c

raw
1 /*
2 * Copyright(C) 1999-2020, 2022, 2023 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8
9 #include "chaco.h"
10 #include "defs.h" // for FALSE, TRUE
11 #include "prototypes.h"
12 #include "smalloc.h" // for sfree, smalloc_ret
13 #include "structs.h"
14 #include <stdio.h> // for NULL, printf, fclose, fopen, etc
15 #include <stdlib.h> // for free
16
17 extern int submain(struct vtx_data **graph, int nvtxs, int nedges, int using_vwgts, int using_ewgts,
18 int igeom, float **coords, char *outassignname, char *outfilename,
19 int *assignment, double *goal, int architecture, int ndims_tot, int mesh_dims[3],
20 int global_method, int local_method, int rqi_flag, int vmax, int ndims,
21 double eigtol, long seed);
22
23 int Using_Main = FALSE; /* Is main routine being called? */
24
25 int INTER_FACE(int nvtxs, /* number of vertices in full graph */
26 int *start, /* start of edge list for each vertex */
27 int *adjacency, /* edge list data */
28 int *vwgts, /* weights for all vertices */
29 float *ewgts, /* weights for all edges */
30 float *x, float *y, float *z, /* coordinates for inertial method */
31 char *outassignname, /* name of assignment output file */
32 char *outfilename, /* output file name */
33 int *assignment, /* set number of each vtx (length n) */
34 int architecture, /* 0 => hypercube, d => d-dimensional mesh */
35 int ndims_tot, /* total number of cube dimensions to divide */
36 int mesh_dims[3], /* dimensions of mesh of processors */
37 double *goal, /* desired set sizes for each set */
38 int global_method, /* global partitioning algorithm */
39 int local_method, /* local partitioning algorithm */
40 int rqi_flag, /* should I use RQI/Symmlq eigensolver? */
41 int vmax, /* how many vertices to coarsen down to? */
42 int ndims, /* number of eigenvectors (2^d sets) */
43 double eigtol, /* tolerance on eigenvectors */
44 long seed /* for random graph mutations */
45 )
46 {
47 extern char *PARAMS_FILENAME; /* name of file with parameter updates */
48 extern int MAKE_VWGTS; /* make vertex weights equal to degrees? */
49 extern int MATCH_TYPE; /* matching routine to use */
50 extern int FREE_GRAPH; /* free graph data structure after reformat? */
51 extern int DEBUG_PARAMS; /* debug flag for reading parameters */
52 extern int DEBUG_TRACE; /* trace main execution path */
53 extern double start_time; /* time routine is entered */
54 extern double reformat_time; /* time spent reformatting graph */
55 FILE *params_file = NULL; /* file for reading new parameters */
56 struct vtx_data **graph; /* graph data structure */
57 double vwgt_sum; /* sum of vertex weights */
58 double time; /* timing variable */
59 float **coords; /* coordinates for vertices if used */
60 int *vptr; /* loops through vertex weights */
61 int flag; /* return code from balance */
62 int nedges; /* number of edges in graph */
63 int using_vwgts; /* are vertex weights being used? */
64 int using_ewgts; /* are edge weights being used? */
65 int nsets_tot = 0; /* total number of sets being created */
66 int igeom; /* geometric dimension for inertial method */
67 int default_goal; /* using default goals? */
68 int i; /* loop counter */
69 double seconds(void);
70
71 if (DEBUG_TRACE > 0) {
72 printf("<Entering INTER_FACE>\n");
73 }
74
75 flag = 0;
76 graph = NULL;
77 coords = NULL;
78
79 if (!Using_Main) { /* If not using main, need to read parameters file. */
80 start_time = seconds();
81 params_file = fopen(PARAMS_FILENAME, "r");
82 if (params_file == NULL && DEBUG_PARAMS > 1) {
83 printf("Parameter file `%s' not found; using default parameters.\n", PARAMS_FILENAME);
84 }
85 read_params(params_file);
86 }
87
88 if (goal == NULL) { /* If not passed in, default goals have equal set sizes. */
89 default_goal = TRUE;
90 if (architecture == 0) {
91 nsets_tot = 1 << ndims_tot;
92 }
93 else if (architecture == 1) {
94 nsets_tot = mesh_dims[0];
95 }
96 else if (architecture == 2) {
97 nsets_tot = mesh_dims[0] * mesh_dims[1];
98 }
99 else if (architecture > 2) {
100 nsets_tot = mesh_dims[0] * mesh_dims[1] * mesh_dims[2];
101 }
102
103 if (MAKE_VWGTS && start != NULL) {
104 vwgt_sum = start[nvtxs] - start[0] + nvtxs;
105 }
106 else if (vwgts == NULL) {
107 vwgt_sum = nvtxs;
108 }
109 else {
110 vwgt_sum = 0;
111 vptr = vwgts;
112 for (i = nvtxs; i; i--) {
113 vwgt_sum += *(vptr++);
114 }
115 }
116
117 if (nsets_tot > 0) {
118 vwgt_sum /= nsets_tot;
119 }
120 goal = smalloc_ret(nsets_tot * sizeof(double));
121 if (goal == NULL) {
122 strout("\nERROR: No room to make goals.\n");
123 flag = 1;
124 goto skip;
125 }
126 for (i = 0; i < nsets_tot; i++) {
127 goal[i] = vwgt_sum;
128 }
129 }
130 else {
131 default_goal = FALSE;
132 }
133
134 if (MAKE_VWGTS) {
135 /* Generate vertex weights equal to degree of node. */
136 if (vwgts != NULL) {
137 strout("WARNING: Vertex weights being overwritten by vertex degrees.");
138 }
139 vwgts = smalloc_ret(nvtxs * sizeof(int));
140 if (vwgts == NULL) {
141 strout("\nERROR: No room to make vertex weights.\n");
142 flag = 1;
143 goto skip;
144 }
145 if (start != NULL) {
146 for (i = 0; i < nvtxs; i++) {
147 vwgts[i] = 1 + start[i + 1] - start[i];
148 }
149 }
150 else {
151 for (i = 0; i < nvtxs; i++) {
152 vwgts[i] = 1;
153 }
154 }
155 }
156
157 using_vwgts = (vwgts != NULL);
158 using_ewgts = (ewgts != NULL);
159
160 if (start != NULL || vwgts != NULL) { /* Reformat into our data structure. */
161 time = seconds();
162 flag = reformat(start, adjacency, nvtxs, &nedges, vwgts, ewgts, &graph);
163
164 if (flag) {
165 strout("\nERROR: No room to reformat graph.\n");
166 goto skip;
167 }
168
169 reformat_time += seconds() - time;
170 }
171 else {
172 nedges = 0;
173 }
174
175 if (FREE_GRAPH) { /* Free old graph data structures. */
176 sfree(start);
177 start = NULL;
178 sfree(adjacency);
179 adjacency = NULL;
180 sfree(vwgts);
181 vwgts = NULL;
182 sfree(ewgts);
183 ewgts = NULL;
184 }
185
186 if (global_method == 3 ||
187 (MATCH_TYPE == 5 && (global_method == 1 || (global_method == 2 && rqi_flag)))) {
188 if (x == NULL) {
189 igeom = 0;
190 }
191 else { /* Set up coordinate data structure. */
192 coords = smalloc_ret(3 * sizeof(float *));
193 if (coords == NULL) {
194 strout("\nERROR: No room to make coordinate array.\n");
195 flag = 1;
196 goto skip;
197 }
198 /* Minus 1's are to allow remainder of program to index with 1. */
199 coords[0] = x - 1;
200 igeom = 1;
201 if (y != NULL) {
202 coords[1] = y - 1;
203 igeom = 2;
204 if (z != NULL) {
205 coords[2] = z - 1;
206 igeom = 3;
207 }
208 }
209 }
210 }
211 else {
212 igeom = 0;
213 }
214
215 /* Subtract from assignment to allow code to index from 1. */
216 assignment = assignment - 1;
217 flag = submain(graph, nvtxs, nedges, using_vwgts, using_ewgts, igeom, coords, outassignname,
218 outfilename, assignment, goal, architecture, ndims_tot, mesh_dims, global_method,
219 local_method, rqi_flag, vmax, ndims, eigtol, seed);
220
221 skip:
222 sfree(coords);
223
224 if (default_goal) {
225 sfree(goal);
226 }
227
228 if (graph != NULL) {
229 free_graph(graph);
230 }
231
232 if (flag && FREE_GRAPH) {
233 sfree(start);
234 sfree(adjacency);
235 sfree(vwgts);
236 sfree(ewgts);
237 }
238
239 if (!Using_Main && params_file != NULL) {
240 fclose(params_file);
241 }
242
243 return (flag);
244 }