1 /*  testSmooth4.c  */
2 
3 #include "../GPart.h"
4 #include "../../DSTree.h"
5 #include "../../MSMD.h"
6 #include "../../BKL.h"
7 #include "../../ETree.h"
8 #include "../../Perm.h"
9 #include "../../IV.h"
10 #include "../../timings.h"
11 
12 /*--------------------------------------------------------------------*/
13 int
main(int argc,char * argv[])14 main ( int argc, char *argv[] )
15 /*
16    ------------------------------------------
17    (1) read in a Graph,
18    (2) read in a 2-set partition
19    (3) smooth the separator
20    (4) optionally write out new partition
21 
22    created -- 96oct21, cca
23    ------------------------------------------
24 */
25 {
26 char        *inGraphFileName, *inIVfileName,
27             *msgFileName, *outIVfileName ;
28 double      alpha, cost, t1, t2 ;
29 FILE        *msgFile ;
30 GPart       *gpart ;
31 Graph       *graph ;
32 int         ierr, msglvl, option, nvtx, rc ;
33 IV          *tagsIV ;
34 
35 if ( argc != 8 ) {
36    fprintf(stdout,
37       "\n\n usage : %s msglvl msgFile inGraphFile inIVfile "
38       "\n         option alpha outIVfile"
39       "\n    msglvl      -- message level"
40       "\n    msgFile     -- message file"
41       "\n    inGraphFile -- input file, must be *.graphf or *.graphb"
42       "\n    inIVFile    -- input file, must be *.ivf or *.ivb"
43       "\n    option      -- smoothing option"
44       "\n       1 --> bipartite 2-layers"
45       "\n       2 --> non-bipartite 2-layers"
46       "\n       k > 2 --> k/2 layers on each side"
47       "\n    alpha       -- cost function parameter"
48       "\n    outIVFile   -- output file, must be *.ivf or *.ivb"
49       "\n", argv[0]) ;
50    return(0) ;
51 }
52 msglvl = atoi(argv[1]) ;
53 msgFileName = argv[2] ;
54 if ( strcmp(msgFileName, "stdout") == 0 ) {
55    msgFile = stdout ;
56 } else if ( (msgFile = fopen(msgFileName, "a")) == NULL ) {
57    fprintf(stderr, "\n fatal error in %s"
58            "\n unable to open file %s\n",
59            argv[0], msgFileName) ;
60    return(-1) ;
61 }
62 inGraphFileName = argv[3] ;
63 inIVfileName    = argv[4] ;
64 option          = atoi(argv[5]) ;
65 alpha           = atof(argv[6]) ;
66 outIVfileName   = argv[7] ;
67 fprintf(msgFile,
68         "\n %s : "
69         "\n msglvl      -- %d"
70         "\n msgFile     -- %s"
71         "\n inGraphFile -- %s"
72         "\n inIVfile    -- %s"
73         "\n option      -- %d"
74         "\n alpha       -- %f"
75         "\n outIVfile   -- %s"
76         "\n", argv[0], msglvl,
77         msgFileName, inGraphFileName, inIVfileName,
78         option, alpha, outIVfileName) ;
79 fflush(msgFile) ;
80 /*
81    ------------------------
82    read in the Graph object
83    ------------------------
84 */
85 if ( strcmp(inGraphFileName, "none") == 0 ) {
86    fprintf(msgFile, "\n no file to read from") ;
87    exit(0) ;
88 }
89 MARKTIME(t1) ;
90 graph = Graph_new() ;
91 Graph_setDefaultFields(graph) ;
92 if ( (rc = Graph_readFromFile(graph, inGraphFileName)) != 1 ) {
93    fprintf(msgFile, "\n return value %d from Graph_readFromFile(%p,%s)",
94         rc, graph, inGraphFileName) ;
95    exit(-1) ;
96 }
97 MARKTIME(t2) ;
98 fprintf(msgFile, "\n CPU %9.5f : read in graph from file %s",
99         t2 - t1, inGraphFileName) ;
100 nvtx = graph->nvtx ;
101 if ( msglvl < 4 ) {
102    Graph_writeStats(graph, msgFile) ;
103    fflush(msgFile) ;
104 } else {
105    Graph_writeForHumanEye(graph, msgFile) ;
106    fflush(msgFile) ;
107 }
108 /*
109    ------------------------
110    read in the IV object
111    ------------------------
112 */
113 if ( strcmp(inIVfileName, "none") == 0 ) {
114    fprintf(msgFile, "\n no file to read from") ;
115    exit(0) ;
116 }
117 MARKTIME(t1) ;
118 tagsIV = IV_new() ;
119 IV_setDefaultFields(tagsIV) ;
120 if ( (rc = IV_readFromFile(tagsIV, inIVfileName)) != 1 ) {
121    fprintf(msgFile, "\n return value %d from IV_readFromFile(%p,%s)",
122         rc, tagsIV, inIVfileName) ;
123    exit(-1) ;
124 }
125 MARKTIME(t2) ;
126 fprintf(msgFile, "\n CPU %9.5f : read in tagsIV from file %s",
127         t2 - t1, inIVfileName) ;
128 nvtx = IV_size(tagsIV) ;
129 if ( msglvl < 4 ) {
130    IV_writeStats(tagsIV, msgFile) ;
131    fflush(msgFile) ;
132 } else {
133    IV_writeForHumanEye(tagsIV, msgFile) ;
134    fflush(msgFile) ;
135 }
136 /*
137    -----------------------
138    create the GPart object
139    -----------------------
140 */
141 MARKTIME(t1) ;
142 gpart = GPart_new() ;
143 GPart_init(gpart, graph) ;
144 GPart_setMessageInfo(gpart, msglvl, msgFile) ;
145 IV_copy(&gpart->compidsIV, tagsIV) ;
146 GPart_setCweights(gpart) ;
147 MARKTIME(t2) ;
148 fprintf(msgFile, "\n CPU %9.5f : GPart initialized", t2 - t1) ;
149 fprintf(msgFile, "\n cweights : ") ;
150 IVfp80(msgFile, 1 + gpart->ncomp, IV_entries(&gpart->cweightsIV),
151        30, &ierr) ;
152 fflush(msgFile) ;
153 if ( msglvl > 0 ) {
154    int   wB, wS, wW ;
155    int   *cweights = IV_entries(&gpart->cweightsIV) ;
156 
157    fprintf(msgFile, "\n initial component weights :") ;
158    IVfp80(msgFile, 1+gpart->ncomp, cweights, 20, &ierr) ;
159    wS = cweights[0] ;
160    wB = cweights[1] ;
161    wW = cweights[2] ;
162    if ( wB < wW ) {
163       wB = wW ;
164       wW = cweights[1] ;
165    }
166    cost = wS*(1. + (alpha*wB)/wW) ;
167    fprintf(msgFile,
168    "\n initial |S| = %d , balance = %6.3f , cpu = %8.3f , cost = %8.1f\n",
169            wS, ((double) wB)/wW, t2 - t1, cost) ;
170    fflush(msgFile) ;
171 }
172 /*
173    -------------------------------------------
174    check that we have a valid vertex separator
175    -------------------------------------------
176 */
177 if ( 1 != GPart_validVtxSep(gpart) ) {
178    fprintf(msgFile, "\n\n WHOA : separator is not valid"
179            "\n\n WHOA : separator is not valid\n\n") ;
180    fflush(msgFile) ;
181 }
182 /*
183    --------------------
184    smooth the separator
185    --------------------
186 */
187 MARKTIME(t1) ;
188 if ( option <= 2 ) {
189    if ( msglvl > 1 ) {
190       fprintf(msgFile,
191               "\n calling GPart_smoothBy2layers with option %d",
192               option) ;
193       fflush(stdout) ;
194    }
195    GPart_smoothBy2layers(gpart, option, alpha) ;
196 } else {
197    if ( msglvl > 1 ) {
198       fprintf(msgFile,
199               "\n calling GPart_smoothBisector with option %d",
200               option/2) ;
201       fflush(stdout) ;
202    }
203    GPart_smoothBisector(gpart, option/2, alpha) ;
204 }
205 MARKTIME(t2) ;
206 fprintf(msgFile, "\n\n CPU %9.5f : improve bisector", t2 - t1) ;
207 if ( msglvl > 0 ) {
208    int   wB, wS, wW ;
209    int   *cweights = IV_entries(&gpart->cweightsIV) ;
210 
211    fprintf(msgFile, "\n final component weights :") ;
212    IVfp80(msgFile, 1+gpart->ncomp, cweights, 20, &ierr) ;
213    wS = cweights[0] ;
214    wB = cweights[1] ;
215    wW = cweights[2] ;
216    if ( wB < wW ) {
217       wB = wW ;
218       wW = cweights[1] ;
219    }
220    cost = wS*(1. + (alpha*wB)/wW) ;
221    fprintf(msgFile,
222    "\n final |S| = %d , balance = %6.3f , cpu = %8.3f , cost = %8.1f\n",
223            wS, ((double) wB)/wW, t2 - t1, cost) ;
224    fflush(msgFile) ;
225 }
226 /*
227    -------------------------------------------
228    check that we have a valid vertex separator
229    -------------------------------------------
230 */
231 if ( 1 != GPart_validVtxSep(gpart) ) {
232    fprintf(msgFile, "\n\n WHOA : separator is not valid"
233            "\n\n WHOA : separator is not valid\n\n") ;
234    fflush(msgFile) ;
235 }
236 /*
237    ------------------------------------------
238    optionally write the tags file out to disk
239    ------------------------------------------
240 */
241 if ( strcmp(outIVfileName, "none") != 0 ) {
242    IV_writeToFile(&gpart->compidsIV, outIVfileName) ;
243 }
244 /*
245    ------------------------
246    free the data structures
247    ------------------------
248 */
249 GPart_free(gpart) ;
250 Graph_free(graph) ;
251 IV_free(tagsIV) ;
252 
253 fclose(msgFile) ;
254 
255 return(1) ; }
256 
257 /*--------------------------------------------------------------------*/
258