Debian Codegraph @

packages/seacas/applications/nem_slice/elb_groups.C

raw
1 /*
2 * Copyright(C) 1999-2021, 2023, 2024 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 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10 *----------------------------------------------------------------------------
11 * Functions contained in this file:
12 *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
13
14 #include "elb.h" // for Problem_Description, etc
15 #include "elb_elem.h" // for elem_name_from_enum
16 #include "elb_err.h" // for Gen_Error
17 #include "elb_groups.h"
18 #include "elb_util.h"
19 #include "vector_data.h"
20 #include <cstdio> // for sscanf, nullptr
21 #include <cstdlib> // for free, malloc
22 #include <cstring> // for strchr, strlen
23 #include <fmt/format.h>
24 #include <vector> // for vector
25
26 /*****************************************************************************/
27 namespace {
28 template <typename INT>
29 void scandescriptor(const char *d, INT *blkids, int n, int nblks, Problem_Description *prob);
30 template <typename INT>
31 void chgrp(int grp_id, size_t blk, INT *blkids, int nblks, Problem_Description *prob);
32 } // namespace
33 extern int ilog2i(size_t n);
34
35 /*****************************************************************************/
36
37 /*****************************************************************************/
38 /*****************************************************************************/
39 /*****************************************************************************/
40 /* Function parse_groups() begins:
41 *----------------------------------------------------------------------------
42 * This function will parse the group designation string and set up which
43 * element blocks will be in which groups.
44 *
45 * the group designator prob->groups follows these rules:
46 * - Blocks are grouped using the slash "/" character.
47 * - Ids are separated with white space, comma, or by the hyphen "-" character.
48 * - Any blocks not included in the list, are added to a separate group.
49 * - Duplicates in the list are permitted, but the last group to which a
50 * block is placed is where the block will go.
51 * - Block IDs not in the exodus file are quietly ignored.
52 *
53 * Examples.
54 * Assume block IDs= 1-20 31-45
55 *
56 * descriptor group1 group2 group3
57 * - "1-20" 1-20 31-45
58 * - "30-45 3/ 10-12" 3, 30-45 10,11,12 1,2,4-20
59 * - "1-20/40-45/5-10 21-41 1-4,11-20 42,43,45 5-10 31-41
60 *
61 *****************************************************************************/
62 template int parse_groups(Mesh_Description<int> *mesh, Problem_Description *prob);
63 template int parse_groups(Mesh_Description<int64_t> *mesh, Problem_Description *prob);
64
65 template <typename INT> int parse_groups(Mesh_Description<INT> *mesh, Problem_Description *prob)
66 {
67 /* prepare the group number array, and copy the element block counts */
68 prob->group_no.resize(mesh->num_el_blks, -1);
69
70 /* convert any comma's to blank spaces in the designator string */
71 for (size_t i = 0; i < strlen(prob->groups); i++) {
72 if (prob->groups[i] == ',') {
73 prob->groups[i] = ' ';
74
75 /* fill in the group identifier for each block */
76 }
77 }
78 char *id = prob->groups;
79 size_t i = 0;
80 do {
81 if (*id == '/') {
82 id++;
83 }
84 scandescriptor(id, Data(mesh->eb_ids), i, mesh->num_el_blks, prob);
85 id = strchr(id, '/');
86 i++;
87 } while (id != nullptr);
88 int last = i;
89
90 /* set any remaining blocks to new group */
91 int found = 0;
92 for (i = 0; i < mesh->num_el_blks; i++) {
93 if (prob->group_no[i] < 0) {
94 prob->group_no[i] = last;
95 found = 1;
96 }
97 }
98
99 if (found) {
100 last++;
101 }
102
103 prob->num_groups = last;
104
105 {
106 size_t first_el = 0;
107 fmt::print("\nNumber of blocks: {}\n", mesh->num_el_blks);
108 fmt::print("Block ID and associated groups:\n");
109 fmt::print(" block #elems group type\n");
110 for (i = 0; i < mesh->num_el_blks; i++) {
111 fmt::print("{:8d}{:8d}{:8d}{:8s}\n", (size_t)mesh->eb_ids[i], (size_t)mesh->eb_cnts[i],
112 prob->group_no[i], elem_name_from_enum(mesh->elem_type[first_el]));
113 first_el += mesh->eb_cnts[i];
114 }
115 fmt::print("There are {} groups of blocks\n", prob->num_groups);
116 }
117
118 /* finished with the group designator string */
119 delete[] prob->groups;
120
121 return 1;
122 }
123
124 /*****************************************************************************/
125 /*****************************************************************************/
126 /*****************************************************************************/
127 /* Function get_group_info() begins:
128 *----------------------------------------------------------------------------
129 * This function will get the information that is needed to break up the
130 * groups before passing them to Chaco.
131 * It generates the following:
132 * - an array of with the group number for each element
133 * - an array of elements per group
134 * - an array of processors to be used per group
135 * - the max number of vertecies and adjacencies for all of the groups
136 * this value is needed so that arrays used to pass information
137 * to Chaco can be allocated
138 *****************************************************************************/
139 template int get_group_info(Machine_Description *machine, Problem_Description *prob,
140 Mesh_Description<int> *mesh, Graph_Description<int> *graph,
141 int elem2grp[], int nprocg[], int nelemg[], size_t *max_vtx,
142 size_t *max_adj);
143 template int get_group_info(Machine_Description *machine, Problem_Description *prob,
144 Mesh_Description<int64_t> *mesh, Graph_Description<int64_t> *graph,
145 int elem2grp[], int nprocg[], int nelemg[], size_t *max_vtx,
146 size_t *max_adj);
147
148 template <typename INT>
149 int get_group_info(Machine_Description *machine, Problem_Description *prob,
150 Mesh_Description<INT> *mesh, Graph_Description<INT> *graph, int elem2grp[],
151 int nprocg[], int nelemg[], size_t *max_vtx, size_t *max_adj)
152 {
153 int nproc = 0;
154 std::vector<int> nadj_per_grp;
155
156 /* allocate array to hold adjacency counts, if necessary */
157 if (prob->alloc_graph == ELB_TRUE) {
158 nadj_per_grp.resize(prob->num_groups);
159 }
160
161 /* initialize the group counts arrays */
162 for (int i = 0; i < prob->num_groups; i++) {
163 nelemg[i] = 0;
164 }
165
166 /*
167 * fill the vertex2proc array with the group number of the individual
168 * elements, calculate how many elements in each group and determine
169 * how many adjacencies are in each group (if necessary).
170 */
171 INT sum = 0;
172 int iblk = 0;
173 for (size_t i = 0; i < prob->num_vertices; i++) {
174
175 /* figure out which element block this is */
176 if (sum == mesh->eb_cnts[iblk]) {
177 sum = 0;
178 iblk++;
179 }
180 sum++;
181
182 /*
183 * use negative numbers to specify the groups, in order to
184 * avoid having a problem with group 0, add 1 to the group
185 * number
186 */
187 elem2grp[i] = -(prob->group_no[iblk] + 1);
188
189 nelemg[prob->group_no[iblk]]++;
190
191 if (prob->alloc_graph == ELB_TRUE) {
192 nadj_per_grp[prob->group_no[iblk]] += graph->start[i + 1] - graph->start[i];
193 }
194 }
195
196 /*
197 * calculate how many processors to use for each group
198 * using method from the materials group, haven't really checked it
199 */
200 if (machine->type == MachineType::MESH) {
201 nproc = machine->procs_per_box;
202 }
203 else if (machine->type == MachineType::HCUBE) {
204 nproc = ilog2i(machine->procs_per_box);
205 }
206 for (int i = 0; i < prob->num_groups; i++) {
207 nprocg[i] = int((nproc * (nelemg[i] + 0.5F)) / static_cast<float>(prob->num_vertices));
208 if (nelemg[i] && !nprocg[i]) {
209 nprocg[i] = 1;
210 }
211 }
212
213 /*
214 * check to see if correct number of processors have been allocated
215 * and get the maximum number of vertices
216 */
217 sum = 0;
218 size_t j = 0;
219 *max_vtx = 0;
220 *max_adj = 0;
221 for (int i = 0; i < prob->num_groups; i++) {
222 sum += nprocg[i];
223 if (nprocg[i] > nprocg[j]) {
224 j = i;
225 *max_vtx = nelemg[j]; /* most processors implies most elements */
226 }
227
228 /* determine how large to make temporary arrays */
229 if (static_cast<size_t>(nelemg[i]) > *max_vtx) {
230 *max_vtx = nelemg[i];
231 }
232 if (prob->alloc_graph == ELB_TRUE) {
233 if (static_cast<size_t>(nadj_per_grp[i]) > *max_adj) {
234 *max_adj = nadj_per_grp[i];
235 }
236 }
237 }
238 if (sum != nproc) {
239 /* correct group with most processors (j determined above) */
240 nprocg[j] -= (sum - nproc);
241 if (nprocg[j] <= 0) {
242 Gen_Error(0, "Unable to balance # processors in get_group_info().");
243 return 0;
244 }
245 }
246 fmt::print("Load balance information\n");
247 for (int i = 0; i < prob->num_groups; i++) {
248 fmt::print("group[{}] #elements={:10d} #proc={}\n", i, nelemg[i], nprocg[i]);
249 }
250
251 return 1;
252 }
253
254 /**********************************************************************/
255 namespace {
256 template <typename INT>
257 void scandescriptor(const char *d, INT *blkids, int n, int nblks, Problem_Description *prob)
258 {
259 /* reads the descriptor up to the next "/" character. interprets the
260 descriptor. The ranges specified in the descriptor are then stored
261 in the grp array. */
262 int i;
263 int last = 0; /* last integer read */
264 int stop; /* stop value in a string range */
265 int qn; /* number of bytes read */
266 int c; /* integer index when spanning a range */
267
268 const char *p = d;
269 while (*p != '/' && *p != 0) {
270 int q = sscanf(p, "%d%n", &i, &qn);
271 if (q == 0 || i < 0) {
272 if (p[qn - 1] == '/') {
273 return;
274 }
275 if (i < 0) {
276 stop = -i;
277 for (c = last; c <= stop; c++) {
278 chgrp(n, c, blkids, nblks, prob);
279 }
280 }
281 else if (p[qn - 1] == '-') {
282 p += qn;
283 sscanf(p, "%d%n", &stop, &qn);
284 for (c = last; c <= stop; c++) {
285 chgrp(n, c, blkids, nblks, prob);
286 }
287 }
288 else {
289 /* check for minus sign */
290 for (c = 0; c < qn; c++) {
291 if (p[c] == '-') {
292 break;
293 }
294 }
295 if (c < qn) {
296 p += qn;
297 sscanf(p, "%d%n", &stop, &qn);
298 for (c = last; c <= stop; c++) {
299 chgrp(n, c, blkids, nblks, prob);
300 }
301 }
302 else {
303 fmt::print("Error reading descriptor '{}'\n", d);
304 fmt::print(" ");
305 for (c = 0; c < qn; c++) {
306 fmt::print(" ");
307 }
308 fmt::print("^\n");
309 return;
310 }
311 }
312 }
313 else {
314 last = i;
315 }
316 chgrp(n, i, blkids, nblks, prob);
317 p += qn;
318 }
319 }
320
321 /**********************************************************************/
322 /* changes the grp[] entry corresponding to block=blk to the value grp_id
323 * It uses the extern variables "nblks", "grp" and "blkid". only grp
324 * is altered.
325 *
326 * Not very efficient. To find the blk, it loops through all blks.
327 */
328 template <typename INT>
329 void chgrp(int grp_id, size_t blk, INT *blkids, int nblks, Problem_Description *prob)
330 {
331 for (int j = 0; j < nblks; j++) {
332 if (blkids[j] == (INT)blk) {
333 prob->group_no[j] = grp_id;
334 return;
335 }
336 }
337 }
338 } // namespace