57c06a0f5f20e0dfe2eda06a0359afa0b6752c2e
[IRC.git] / Robust / src / Benchmarks / SingleTM / Labyrinth / Labyrinth.java
1 /* =============================================================================
2  *
3  * Labyrinth.java
4  *
5  * =============================================================================
6  *
7  * Copyright (C) Stanford University, 2006.  All Rights Reserved.
8  * Author: Chi Cao Minh
9  *
10  * =============================================================================
11  *
12  * For the license of bayes/sort.h and bayes/sort.c, please see the header
13  * of the files.
14  * 
15  * ------------------------------------------------------------------------
16  * 
17  * For the license of kmeans, please see kmeans/LICENSE.kmeans
18  * 
19  * ------------------------------------------------------------------------
20  * 
21  * For the license of ssca2, please see ssca2/COPYRIGHT
22  * 
23  * ------------------------------------------------------------------------
24  * 
25  * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
26  * header of the files.
27  * 
28  * ------------------------------------------------------------------------
29  * 
30  * For the license of lib/rbtree.h and lib/rbtree.c, please see
31  * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
32  * 
33  * ------------------------------------------------------------------------
34  * 
35  * Unless otherwise noted, the following license applies to STAMP files:
36  * 
37  * Copyright (c) 2007, Stanford University
38  * All rights reserved.
39  * 
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions are
42  * met:
43  * 
44  *     * Redistributions of source code must retain the above copyright
45  *       notice, this list of conditions and the following disclaimer.
46  * 
47  *     * Redistributions in binary form must reproduce the above copyright
48  *       notice, this list of conditions and the following disclaimer in
49  *       the documentation and/or other materials provided with the
50  *       distribution.
51  * 
52  *     * Neither the name of Stanford University nor the names of its
53  *       contributors may be used to endorse or promote products derived
54  *       from this software without specific prior written permission.
55  * 
56  * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
57  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
66  * THE POSSIBILITY OF SUCH DAMAGE.
67  *
68  * =============================================================================
69  */
70
71 public class Labyrinth extends Thread{
72
73     static String global_inputFile;
74     static boolean global_doPrint;
75     int numThread;
76     int bendCost;
77     int xCost;
78     int yCost;
79     int zCost;
80
81
82     // For threads
83     int threadID;
84     Solve_Arg routerArg;
85
86     private void setDefaultParams() {
87
88         /* default values */
89         global_inputFile = null;
90         global_doPrint = false;
91         bendCost = 1;
92         xCost = 1;
93         yCost = 1;
94         zCost = 2;
95         numThread = 1;
96     }
97
98     private void parseArg(String[] argv) {
99         int i=0;
100         String arg;
101         boolean opterr = false;
102
103
104         setDefaultParams();
105
106         while (i < argv.length) {
107
108             if(argv[i].charAt(0) == '-' ) {
109                 arg = argv[i++];
110                 // check options
111                 if(arg.equals("-b")) {
112                     bendCost = Integer.parseInt(argv[i++]);
113                 }
114                 else if(arg.equals("-x")) {
115                     xCost = Integer.parseInt(argv[i++]);
116                     }
117                 else if(arg.equals("-y")) {
118                     yCost = Integer.parseInt(argv[i++]);
119                     }
120                 else if(arg.equals("-z")) {
121                     zCost = Integer.parseInt(argv[i++]);
122                     }
123                 else if(arg.equals("-t")) {
124                         numThread = Integer.parseInt(argv[i++]);
125                 }
126                 else if(arg.equals("-i")) {
127                     global_inputFile = argv[i++];
128                     }
129                 else if(arg.equals("-p")) {
130                         global_doPrint = true;
131                 }
132                 else {
133                     System.out.println("Non-option argument: " + argv[i]);
134                     opterr = true;
135                 }   
136             
137             }
138         }
139         if(opterr) {
140             displayUsage();
141             System.exit(1);
142         }
143     }
144
145     public Labyrinth(String[] argv)
146     {     
147         parseArg(argv);
148     }
149
150
151     public Labyrinth(int myID,Solve_Arg rArg)
152     {
153         threadID = myID;
154         routerArg = rArg;
155     }
156
157     public void run() {
158       Barrier.enterBarrier();
159       Router.solve(routerArg);
160       Barrier.enterBarrier();
161     }
162
163     public void displayUsage() 
164     {
165         System.out.println("Usage: Labyrinth [options]");
166         System.out.println("Options:");
167         System.out.println("    b <INT>     bend cost");
168         System.out.println("    i <FILE>    input file name");
169         System.out.println("    p           print routed maze");
170         System.out.println("    t <INT>     Number of threads");
171         System.out.println("    x <INT>     x movement cost");
172         System.out.println("    y <INT>     y movement cost");
173         System.out.println("    z <INT>     z movement cost");
174     }
175         
176
177     public static void main(String[] argv) 
178     {
179         /*
180          * Initailization
181          */
182
183         Labyrinth labyrinth = new Labyrinth(argv);
184
185         Barrier.setBarrier(labyrinth.numThread);
186
187         Maze mazePtr = Maze.alloc();
188
189         int numPathToRoute =  mazePtr.readMaze(labyrinth.global_inputFile);
190
191         Router routerPtr = Router.alloc(labyrinth.xCost,labyrinth.yCost,
192                                         labyrinth.zCost,labyrinth.bendCost);
193
194         List_t pathVectorListPtr = List_t.alloc(0);     // list_t.alloc(null)
195         /*
196          * Run transactions
197          */
198         Solve_Arg routerArg = new Solve_Arg(routerPtr,mazePtr,pathVectorListPtr);
199
200         /* Create and start thread */
201
202         Labyrinth[] lb = new Labyrinth[labyrinth.numThread];
203
204         for(int i = 1; i<labyrinth.numThread;i++) {
205             lb[i] = new Labyrinth(i,routerArg);
206         }
207
208         long start = System.currentTimeMillis();
209
210         for(int i = 1; i<labyrinth.numThread;i++) {
211             lb[i].start();
212         }
213
214         Barrier.enterBarrier();
215         Router.solve(routerArg);        
216         Barrier.enterBarrier();
217
218         /* End of Solve */
219         long finish = System.currentTimeMillis();
220
221
222         int numPathRouted = 0;
223         List_Iter it = new List_Iter();
224
225         it.reset(pathVectorListPtr);
226         while(it.hasNext(pathVectorListPtr)) {
227             Vector_t pathVectorPtr = (Vector_t)it.next(pathVectorListPtr);
228             numPathRouted += pathVectorPtr.vector_getSize();
229         }
230
231         float elapsed = ((float)finish-(float)start)/1000;
232
233         System.out.println("Paths routed    = " + numPathRouted);
234         System.out.println("Elapsed time    = " + elapsed);
235
236         boolean stats = mazePtr.checkPaths(pathVectorListPtr,labyrinth.global_doPrint);
237         if(!stats)
238         {
239             System.out.println("Verification not passed");
240             
241         }
242         else 
243             System.out.println("Verification passed.");
244         System.out.println("Finished");    
245     }
246 }
247
248 /* =============================================================================
249  *
250  * End of labyrinth.c
251  *
252  * =============================================================================
253  */