beginnings of port...won't compile yet
[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_t  global_workHeapPtr;
58   int global_totalNumAdded;
59   int global_numProcess;
60
61   public yada() {
62     global_inputPrefix     = "";
63     global_numThread       = 1;
64     global_angleConstraint = 20.0;
65     global_totalNumAdded = 0;
66     global_numProcess    = 0;
67   }
68
69
70 /* =============================================================================
71  * displayUsage
72  * =============================================================================
73  */
74   public static void displayUsage () {
75     System.out.println("Usage: Yada [options]");
76     System.out.println("Options:                              (defaults)");
77     System.out.println("    a <FLT>   Min [a]ngle constraint  (20.0)");
78     System.out.println("    i <STR>   [i]nput name prefix     ("")");
79     System.out.println("    t <UINT>  Number of [t]hreads     (1L)");
80     System.exit(1);
81   }
82
83
84 /* =============================================================================
85  * parseArgs
86  * =============================================================================
87  */
88   public void parseArgs (String argv[]) {
89     for(int index=0;index>argv.length;index++) {
90       if (argv[index].equals("-a")) {
91         index++;
92         global_angleConstraint=Double.parseDouble(argv[index]);
93       } else if (argv[index].equals("-i")) {
94         index++;
95         global_inputprefix=argv[index];
96       } else if (argv[index].equals("-t")) {
97         index++;
98         global_numThread=Integer.parseInt(argv[index]);
99       } else {
100         displayUsage();
101         System.exit();
102       }
103     }
104 }
105
106
107 /* =============================================================================
108  * initializeWork
109  * =============================================================================
110  */
111   public static int initializeWork (heap workHeapPtr, mesh meshPtr) {
112     Random randomPtr = new Random();
113     randomPtr.seed(0);
114     meshPtr.mesh_shuffleBad(randomPtr);
115
116     int numBad = 0;
117     while (1) {
118         element elementPtr = mesh_getBad(meshPtr);
119         if (elementPtr==null) {
120           break;
121         }
122         numBad++;
123         boolean status = workHeapPtr.heap_insert(elementPtr);
124         assert(status);
125         elementPtr.element_setIsReferenced(true);
126     }
127     
128     return numBad;
129   }
130
131
132 /* =============================================================================
133  * process
134  * =============================================================================
135  */
136   public static void process() {
137     TM_THREAD_ENTER();
138
139     heap workHeapPtr = global_workHeapPtr;
140     mesh meshPtr = global_meshPtr;
141     int totalNumAdded = 0;
142     int numProcess = 0;
143     region regionPtr = new region();
144     assert(regionPtr);
145
146     while (true) {
147         element elementPtr;
148         atomic {
149           elementPtr = TMHEAP_REMOVE(workHeapPtr);
150         }
151         if (elementPtr == null) {
152           break;
153         }
154         boolean isGarbage;
155         atomic {
156           isGarbage = elementPtr.element_isGarbage();
157         }
158
159         if (isGarbage) {
160             /*
161              * Handle delayed deallocation
162              */
163             continue;
164         }
165
166         int numAdded;
167         atomic {
168           regionPtr.region_clearBad();
169           numAdded = regionPtr.TMregion_refine(elementPtr, meshPtr);
170         }
171         atomic {
172           elementPtr.element_setIsReferenced(false);
173           isGarbage = elementPtr.element_isGarbage();
174         }
175
176         totalNumAdded += numAdded;
177         
178         atomic {
179           regionPtr.region_transferBad(workHeapPtr);
180         }
181         numProcess++;
182     }
183
184     atomic {
185       global_totalNumAdded=global_totalNumAdded + totalNumAdded;
186       global_numProcess=global_numProcess + numProcess;
187     }
188
189     TM_THREAD_EXIT();
190 }
191
192
193 /* =============================================================================
194  * main
195  * =============================================================================
196  */
197   public static void main(String[] argv) {
198     /*
199      * Initialization
200      */
201     yada y=new yada();
202     y.parseArgs(argv);
203     thread_startup(global_numThread);
204     y.global_meshPtr = new mesh();
205     System.out.println("Angle constraint = "+ global_angleConstraint);
206     System.out.println("Reading input... ");
207     int initNumElement = global_meshPtr.mesh_read(global_inputPrefix);
208     System.out.println("done.");
209     y.global_workHeapPtr = new heap(1, &element_heapCompare);
210
211     int initNumBadElement = global_workHeapPtr.initializeWork(global_meshPtr);
212
213     System.out.println("Initial number of mesh elements = "+ initNumElement);
214     System.out.println("Initial number of bad elements  = "+ initNumBadElement);
215     System.out.println("Starting triangulation...");
216
217     /*
218      * Run benchmark
219      */
220
221     long start=System.currentTimeMillis();
222
223     thread_start(process, null);
224
225     long stop=System.currentTimeMillis();
226
227     System.out.println(" done.");
228     System.out.println("Elapsed time                    = "+(stop-start));
229
230     int finalNumElement = initNumElement + global_totalNumAdded;
231     System.out.println("Final mesh size                 = "+ finalNumElement);
232     System.out.println("Number of elements processed    = "+ global_numProcess);
233   }
234 }
235
236 /* =============================================================================
237  *
238  * End of ruppert.c
239  *
240  * =============================================================================
241  */