dfj version of delaunay refinement executes the original algorithm with workers=1...
[IRC.git] / Robust / src / Benchmarks / oooJava / DelaunayRefinement-orig-algo / SerialDelaunayRefinement.java
1 /*
2 Lonestar Benchmark Suite for irregular applications that exhibit 
3 amorphous data-parallelism.
4
5 Center for Grid and Distributed Computing
6 The University of Texas at Austin
7
8 Copyright (C) 2007, 2008, 2009 The University of Texas at Austin
9
10 Licensed under the Eclipse Public License, Version 1.0 (the "License");
11 you may not use this file except in compliance with the License.
12 You may obtain a copy of the License at
13
14 http://www.eclipse.org/legal/epl-v10.html
15
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
21
22 File: UndirectedEdgeGraph.java 
23
24 */
25
26 public class SerialDelaunayRefinement {  
27   public boolean isFirstRun;
28   public SerialDelaunayRefinement() {
29     isFirstRun = true;
30   }
31
32   public static void main(String args[]) {
33     SerialDelaunayRefinement sdr = new SerialDelaunayRefinement();
34     sdr.runMain(args);
35   }
36   
37   public void runMain(String args[]) {
38     long runtime = 0;
39     //Numbers below are Long.Max_Value
40     long lasttime = 0x7fffffffffffffffL;
41     long mintime = 0x7fffffffffffffffL;
42     for (long run = 0; ((run < 3) || Math.abs(lasttime - runtime) * 64 > Math.min(lasttime, runtime)) && run < 7; run++) {
43       runtime = run(args);
44       if (runtime < mintime) {
45         mintime = runtime;
46       }
47       //System.exit( 0 ); // GC STALLS FOREVER????
48       System.gc();
49     }
50
51     System.out.println("minimum runtime: " + mintime + " ms");
52     System.out.println("");
53   }
54   
55
56   public long run(String args[]) {
57     if (isFirstRun) {
58       System.out.println();
59       System.out.println("Lonestar Benchmark Suite v2.1");
60       System.out.println("Copyright (C) 2007, 2008, 2009 The University of Texas at Austin");
61       System.out.println("http://iss.ices.utexas.edu/lonestar/");
62       System.out.println();
63       System.out.println("application: Delaunay Mesh Refinement (serial version)");
64       System.out.println("Refines a Delaunay triangulation mesh such that no angle");
65       System.out.println("in the mesh is less than 30 degrees");
66       System.out.println("http://iss.ices.utexas.edu/lonestar/delaunayrefinement.html");
67       System.out.println();
68     }
69     if (args.length < 1) {
70       System.out.println("Arguments: <input file> [verify]");
71       System.exit(-1);
72     }
73
74     EdgeGraph mesh = new UndirectedEdgeGraph();
75
76     Mesh m = new Mesh();
77     m.read(mesh, args[0]);
78
79     //treat LinkedList as a stack
80     Stack worklist = new Stack();
81     //    LinkedList worklist = new LinkedList();
82
83     // worklist.addAll(Mesh.getBad(mesh));
84     HashMapIterator it = m.getBad(mesh).iterator();
85     while (it.hasNext()) {
86       worklist.push(it.next());
87     }
88
89     Cavity cavity = new Cavity(mesh);
90     if (isFirstRun) {
91       System.out.println("configuration: " + mesh.getNumNodes() + " total triangles, " + worklist.size() + " bad triangles");
92       System.out.println();
93     }
94
95     System.gc();
96
97     
98     int zzz = 0;
99     
100
101 //    long id = Time.getNewTimeId();
102     long startTime = System.currentTimeMillis();
103
104     while (!worklist.isEmpty()) {
105       Node bad_element = (Node) worklist.pop();
106 //      System.out.println("Bad Node"+ ((Element)mesh.getNodeData(bad_element)).toString());
107       if (bad_element != null && mesh.containsNode(bad_element)) {
108         cavity.initialize(bad_element);
109         cavity.build();
110         cavity.update();
111
112
113         //boolean printChange = true; //(zzz % 10 == 0);
114         
115         //remove old data
116         //if( printChange ) {
117         //  System.out.println( "\n\n\nbad tri: "+mesh.getNodeData( bad_element ) );
118         //  System.out.println( "\npre nodes: " );
119         //}
120         Node node;
121         for (Iterator iterator = cavity.getPre().getNodes().iterator(); iterator.hasNext();) {
122           node = (Node) iterator.next();
123           //if( printChange ) {
124           //  System.out.println( "  "+mesh.getNodeData( node ) );
125           //}          
126           mesh.removeNode(node);
127         }
128
129         //add new data
130         //if( printChange ) {
131         //  System.out.println( "post nodes: " );
132         //}
133         for (Iterator iterator1 = cavity.getPost().getNodes().iterator(); iterator1.hasNext();) {
134           node = (Node) iterator1.next();
135           //if( printChange ) {
136           //  System.out.println( "  "+mesh.getNodeData( node ) );
137           //}          
138           mesh.addNode(node);
139         }
140
141         //if( printChange ) {
142         //  System.out.println( "post edges: " );
143         //}
144         Edge_d edge;
145         for (Iterator iterator2 = cavity.getPost().getEdges().iterator(); iterator2.hasNext();) {
146           edge = (Edge_d) iterator2.next();
147           //if( printChange ) {
148           //  System.out.println( "  "+mesh.getEdgeData( edge ) );
149           //}          
150           mesh.addEdge(edge);
151         }
152
153         // worklist.addAll(cavity.getPost().newBad(mesh));
154         it = cavity.getPost().newBad(mesh).iterator();
155         while (it.hasNext()) {
156           worklist.push((Node)it.next());
157         }
158
159         if (mesh.containsNode(bad_element)) {
160           worklist.push((Node) bad_element);
161         }
162       } else {
163         //System.out.println( "\n\n\nthis tri no longer a concern: "+mesh.getNodeData( bad_element ) );
164       }
165
166       //++zzz;
167       //System.out.println( "\n\ntris="+mesh.getNumNodes()+
168       //                    " [wl="+worklist.size()+"]");
169       //if( zzz == 10 ) { System.exit( 0 ); }
170     }
171     long time = System.currentTimeMillis() - startTime;
172     System.out.println("runtime: " + time + " ms");
173     //TODO note how we only verify on first run...
174     if (isFirstRun && args.length > 1) {
175       verify(mesh);
176     }
177     isFirstRun = false;
178     return time;
179   }
180
181   public void verify(EdgeGraph result) {
182     //Put in cuz of static issues.
183     Mesh m = new Mesh();
184     if (!m.verify(result)) {
185 //      throw new IllegalStateException("refinement failed.");
186       System.out.println("Refinement Failed.");
187       System.exit(-1);
188     }
189     
190     int size = m.getBad(result).size();
191     if (size != 0) {
192       System.out.println("refinement failed\nstill have "+size+" bad triangles left.\n");
193       System.exit(-1);
194     } else {
195       System.out.println("OK");
196       return;
197     }
198   }
199 }