Debian Codegraph @

packages/seacas/libraries/chaco/klspiff/compress_ewgts.c

raw
1 /*
2 * Copyright(C) 1999-2020 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 "smalloc.h" // for sfree, smalloc
10 #include "structs.h" // for vtx_data
11 #include <stdio.h> // for NULL, printf
12
13 static float *old_ewgts; /* space for old edge weights */
14
15 void compress_ewgts(struct vtx_data **graph, /* list of graph info for each vertex */
16 int nvtxs, /* number of vertices in graph */
17 int nedges, /* number of edges in graph */
18 double ewgt_max, /* largest edge weight */
19 int using_ewgts /* are edge weights being used? */
20 )
21 {
22 extern double EWGT_RATIO_MAX; /* max allowed ewgt/nvtxs */
23 float *old_ewptr; /* loops through old edge weights */
24 float *new_ewptr; /* loops through old edge weights */
25 float *self_ptr; /* points to self edge in new_ewgts */
26 float *new_ewgts; /* space for new edge weights */
27 int ewgt; /* new edge weight value */
28 double sum; /* sum of all the new edge weights */
29 double ratio; /* amount to reduce edge weights */
30 int i, j; /* loop counter */
31
32 /* Check easy cases first. */
33 if (!using_ewgts) {
34 old_ewgts = NULL;
35 }
36 else if (ewgt_max < EWGT_RATIO_MAX * nvtxs) {
37 /* If not too heavy, leave it alone. */
38 old_ewgts = NULL;
39 printf("In compress_ewgts, but not too heavy, ewgt_max = %g, nvtxs = %d\n", ewgt_max, nvtxs);
40 }
41
42 else { /* Otherwise, compress edge weights. */
43 /* Allocate space for new edge weights */
44 old_ewgts = graph[1]->ewgts;
45 new_ewgts = smalloc((2 * nedges + nvtxs) * sizeof(float));
46 ratio = (EWGT_RATIO_MAX * nvtxs) / ewgt_max;
47 printf("In compress_ewgts, ewgt_max = %g, nvtxs = %d, ratio = %e\n", ewgt_max, nvtxs, ratio);
48 old_ewptr = old_ewgts;
49 new_ewptr = new_ewgts;
50 for (i = 1; i <= nvtxs; i++) {
51 self_ptr = new_ewptr++;
52 old_ewptr++;
53 sum = 0;
54 for (j = graph[i]->nedges - 1; j; j--) {
55 ewgt = (int)(*(old_ewptr++) * ratio + 1.0);
56 *(new_ewptr++) = (float)ewgt;
57 sum += (float)ewgt;
58 }
59 *self_ptr = -sum;
60 graph[i]->ewgts = self_ptr;
61 }
62 }
63 }
64
65 void restore_ewgts(struct vtx_data **graph, /* list of graph info for each vertex */
66 int nvtxs /* number of vertices in graph */
67 )
68 {
69 int i; /* loop counter */
70
71 /* Check easy case first. */
72 if (old_ewgts == NULL) {
73 return;
74 }
75 /* otherwise, compress edge weights. */
76 sfree(graph[1]->ewgts);
77 for (i = 1; i <= nvtxs; i++) {
78 graph[i]->ewgts = old_ewgts;
79 old_ewgts += graph[i]->nedges;
80 }
81 old_ewgts = NULL;
82 }