Labyrinth benchmakr
[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     Router.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         setDefaultParams();
104
105         while (i < args.length && argv[i].startswith("-")) {
106             arg = argv[i++];
107
108             // check options
109             if(arg.equals("-b")) {
110                 bendCost = Integer.parseInt(argv[i++]);
111             }
112             else if(arg.equals("-x")) {
113                 xCost = Integer.parseInt(argv[i++]);
114             }
115             else if(arg.equals("-y")) {
116                 yCost = Integer.parseInt(argv[i++]);
117             }
118             else if(arg.equals("-z")) {
119                 zCost = Integer.parseInt(argv[i++]);
120             }
121             else if(arg.equals("-t")) {
122                 numThread = Integer.parseInt(argv[i++]);
123             }
124             else if(arg.equals("-i")) {
125                 global_inputFile = argv[i++];
126             }
127             else if(arg.equals("-p")) {
128                 global_doPrint = true;
129             }
130             else {
131                 System.err.println("Non-option argument: " + argv[i]);
132                 opterr = true;
133             }
134         }
135
136         if(opterr) {
137             displayUsage();
138             System.out.exit(1);
139         }
140     }
141
142     public Labyrinth(String[] argv)
143     {     
144         parseArg(argv);
145     }
146
147
148     public Labyrinth(int myID,Router.Arg rArg)
149     {
150         threadID = myID;
151         routerArg = rArg;
152     }
153
154     public void run() {
155         while(true) {
156             Barrier.enterBarrier();
157             Router.solve(routerArg);
158             Barrier.enterBarrier();
159         }
160     }
161
162     public static void main(String[] argv) 
163     {
164         /*
165          * Initailization
166          */
167         Labyrinth labyrinth = new Labyrinth(argv);
168         
169         Barrier.setBarrier(laybyrinth.numThread);
170
171         Maze mazePtr = Maze.alloc();
172
173         int numPathToRoute =  mazePtr.read(global_inputFile);
174         
175         Router routerPtr = Router.alloc(laybyrinth.xCost,laybyrinth.yCost,
176                                         laybyrinth.zCost,laybyrinth.bendCost);
177
178         List_t pathVectorListPtr = List_t.alloc(0);     // list_t.alloc(null)
179
180         /*
181          * Run transactions
182          */
183         Solve_arg routerArg = new Solve_arg(routerPtr,mazePtr,pathVectorListPtr);
184
185
186         /* Create and start thread */
187         Layrinth[] lb = new Labyrinth[numThread];
188
189         for(int i = 1; i<numThread;i++) {
190             lb[i] = new Labyrinth(i,routerArg);
191         }
192
193         for(int i = 1; i<numThread;i++) {
194             ib[i].start();
195         }
196
197         /* End of Solve */
198
199         int numPathRouted = 0;
200         List_Iter it = new List_Iter();
201
202         it.reset(pathVectorListPtr);
203         while(it.hasNext(pathVectorListPtr)) {
204             Vector_t pathVectorPtr = (Vector_t)it.next(pathVectorListPtr);
205             numPathRouted += pathVectorPtr.getSize();
206         }
207
208         System.out.println("Paths routed    = " + numPaathRouted);
209
210         boolean stats = mazePtr.checkPaths(pathVectorListPtr,global_doPrint);
211         if(!stats)
212         {
213             System.out.println("Verification not passed");
214             
215         }
216         else 
217             System.out.println("Verification passed.");
218         System.out.println("Finished");    
219     }
220 }
221
222 /* =============================================================================
223  *
224  * End of labyrinth.c
225  *
226  * =============================================================================
227  */