almost done with port
[IRC.git] / Robust / src / Benchmarks / SingleTM / Yada / yada.java
1 /* 
2  * For the license of ssca2, please see ssca2/COPYRIGHT
3  * 
4  * ------------------------------------------------------------------------
5  * 
6  * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
7  * header of the files.
8  * 
9  * ------------------------------------------------------------------------
10  * 
11  * For the license of lib/rbtree.h and lib/rbtree.c, please see
12  * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
13  * 
14  * ------------------------------------------------------------------------
15  * 
16  * Unless otherwise noted, the following license applies to STAMP files:
17  * 
18  * Copyright (c) 2007, Stanford University
19  * All rights reserved.
20  * 
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions are
23  * met:
24  * 
25  *     * Redistributions of source code must retain the above copyright
26  *       notice, this list of conditions and the following disclaimer.
27  * 
28  *     * Redistributions in binary form must reproduce the above copyright
29  *       notice, this list of conditions and the following disclaimer in
30  *       the documentation and/or other materials provided with the
31  *       distribution.
32  * 
33  *     * Neither the name of Stanford University nor the names of its
34  *       contributors may be used to endorse or promote products derived
35  *       from this software without specific prior written permission.
36  * 
37  * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
38  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
40  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
41  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
47  * THE POSSIBILITY OF SUCH DAMAGE.
48  *
49  * =============================================================================
50  */
51
52 public class yada {
53   String global_inputPrefix;
54   int global_numThread;
55   double global_angleConstraint;
56   mesh  global_meshPtr;
57   heap  global_workHeapPtr;
58   int global_totalNumAdded;
59   int global_numProcess;
60   global global;
61
62   public yada() {
63     global_inputPrefix     = "";
64     global_numThread       = 1;
65     global_angleConstraint = 20.0;
66     global_totalNumAdded = 0;
67     global_numProcess    = 0;
68   }
69
70   public yada(mesh meshptr, heap heapptr, double angle, global g) {
71     global_meshPtr=meshptr;
72     global_workHeapPtr=heapptr;
73     global_angleConstraint=angle;
74     global=g;
75   }
76
77
78 /* =============================================================================
79  * displayUsage
80  * =============================================================================
81  */
82   public static void displayUsage () {
83     System.out.println("Usage: Yada [options]");
84     System.out.println("Options:                              (defaults)");
85     System.out.println("    a <FLT>   Min [a]ngle constraint  (20.0)");
86     System.out.println("    i <STR>   [i]nput name prefix     (\"\")");
87     System.out.println("    t <UINT>  Number of [t]hreads     (1L)");
88     System.exit(1);
89   }
90
91
92 /* =============================================================================
93  * parseArgs
94  * =============================================================================
95  */
96   public void parseArgs (String argv[]) {
97     for(int index=0;index>argv.length;index++) {
98       if (argv[index].equals("-a")) {
99         index++;
100         global_angleConstraint=Double.parseDouble(argv[index]);
101       } else if (argv[index].equals("-i")) {
102         index++;
103         global_inputprefix=argv[index];
104       } else if (argv[index].equals("-t")) {
105         index++;
106         global_numThread=Integer.parseInt(argv[index]);
107       } else {
108         displayUsage();
109         System.exit();
110       }
111     }
112 }
113
114
115 /* =============================================================================
116  * initializeWork
117  * =============================================================================
118  */
119   public static int initializeWork (heap workHeapPtr, mesh meshPtr) {
120     Random randomPtr = new Random();
121     randomPtr.seed(0);
122     meshPtr.mesh_shuffleBad(randomPtr);
123
124     int numBad = 0;
125     while (1) {
126         element elementPtr = mesh_getBad(meshPtr);
127         if (elementPtr==null) {
128           break;
129         }
130         numBad++;
131         boolean status = workHeapPtr.heap_insert(elementPtr);
132         yada.Assert(status);
133         elementPtr.element_setIsReferenced(true);
134     }
135     
136     return numBad;
137   }
138   
139   public static void Assert(boolean status) {
140   }
141 /* =============================================================================
142  * process
143  * =============================================================================
144  */
145   public static void process() {
146     heap workHeapPtr = global_workHeapPtr;
147     mesh meshPtr = global_meshPtr;
148     int totalNumAdded = 0;
149     int numProcess = 0;
150     region regionPtr = new region();
151     yada.Assert(regionPtr);
152
153     while (true) {
154         element elementPtr;
155         atomic {
156           elementPtr = (element) workHeapPtr.heap_remove();
157         }
158         if (elementPtr == null) {
159           break;
160         }
161         boolean isGarbage;
162         atomic {
163           isGarbage = elementPtr.element_isGarbage();
164         }
165
166         if (isGarbage) {
167             /*
168              * Handle delayed deallocation
169              */
170             continue;
171         }
172
173         int numAdded;
174         atomic {
175           regionPtr.region_clearBad();
176           numAdded = regionPtr.TMregion_refine(elementPtr, meshPtr);
177         }
178         atomic {
179           elementPtr.element_setIsReferenced(false);
180           isGarbage = elementPtr.element_isGarbage();
181         }
182
183         totalNumAdded += numAdded;
184         
185         atomic {
186           regionPtr.region_transferBad(workHeapPtr);
187         }
188         numProcess++;
189     }
190
191     atomic {
192       global.global_totalNumAdded=global.global_totalNumAdded + totalNumAdded;
193       global.global_numProcess=global.global_numProcess + numProcess;
194     }
195   }
196
197   public void run() {
198     Barrier.enterBarrier();
199     process()
200     Barrier.enterBarrier();
201   }
202   
203 /* =============================================================================
204  * main
205  * =============================================================================
206  */
207   public static void main(String[] argv) {
208     /*
209      * Initialization
210      */
211     yada y=new yada();
212     global g=new global();
213     y.global=g;
214     y.parseArgs(argv);
215     Barrier.setBarrier(y.global_numThread);
216     y.global_meshPtr = new mesh();
217     System.out.println("Angle constraint = "+ y.global_angleConstraint);
218     System.out.println("Reading input... ");
219     int initNumElement = y.global_meshPtr.mesh_read(global_inputPrefix);
220     System.out.println("done.");
221     y.global_workHeapPtr = new heap(1);
222
223     for(int i=1;i<global_numThread;i++) {
224       yada ychild=new yada(y.global_meshPtr, y.global_angleConstraint, y.global_angleConstraint, g);
225       ychild.start();
226     }
227
228     int initNumBadElement = y.global_workHeapPtr.initializeWork(y.global_meshPtr);
229
230     System.out.println("Initial number of mesh elements = "+ initNumElement);
231     System.out.println("Initial number of bad elements  = "+ initNumBadElement);
232     System.out.println("Starting triangulation...");
233
234     /*
235      * Run benchmark
236      */
237
238     long start=System.currentTimeMillis();
239     y.run();
240     long stop=System.currentTimeMillis();
241
242     System.out.println(" done.");
243     System.out.println("Elapsed time                    = "+(stop-start));
244
245     int finalNumElement = initNumElement + y.global.global_totalNumAdded;
246     System.out.println("Final mesh size                 = "+ finalNumElement);
247     System.out.println("Number of elements processed    = "+ y.global.global_numProcess);
248   }
249 }
250
251 /* =============================================================================
252  *
253  * End of ruppert.c
254  *
255  * =============================================================================
256  */