Debian Codegraph @

current/stream/ConcurrentReadInputStreamD.java

raw symbols SCIP
1 package stream;
2
3 import java.util.ArrayList;
4 import java.util.concurrent.TimeUnit;
5
6 import shared.Shared;
7 import structures.ListNum;
8
9 /**
10 * This class is designed for distributed environments.
11 * The 'master' reads from the filesystem, creates reads, and broadcasts them.
12 * The 'slaves' listen for broadcasts.
13 * @author Brian Bushnell
14 * @date Oct 7, 2014
15 *
16 */
17 public class ConcurrentReadInputStreamD extends ConcurrentReadInputStream {
18
19 /*--------------------------------------------------------------*/
20 /*---------------- Initialization ----------------*/
21 /*--------------------------------------------------------------*/
22
23 public ConcurrentReadInputStreamD(ConcurrentReadInputStream cris_, boolean master_, boolean keepAll_){
24 super(cris_.fname());
25 source=cris_;
26 master=master_;
27 rank=Shared.MPI_RANK;
28 ranks=Shared.MPI_NUM_RANKS;
29 depot=new ConcurrentDepot<Read>(BUF_LEN, NUM_BUFFS);
30 assert(master==(cris_!=null));
31
32 if(master){
33 paired=source.paired();
34 broadcastPaired(paired);
35 keepAll=keepAll_;
36 broadcastKeepall(keepAll);
37 }else{
38 paired=listenPaired();
39 keepAll=listenKeepall();
40 }
41
42 }
43
44 /*--------------------------------------------------------------*/
45 /*---------------- Outer Methods ----------------*/
46 /*--------------------------------------------------------------*/
47
48 @Override
49 public synchronized ListNum<Read> nextList() {
50 ArrayList<Read> list=null;
51 if(verbose){System.err.println("crisD: **************** nextList() was called; shutdown="+shutdown+", depot.full="+depot.full.size());}
52 while(list==null){
53 if(shutdown){
54 if(verbose){System.err.println("crisD: **************** nextList() returning null; shutdown="+shutdown+", depot.full="+depot.full.size());}
55 return null;
56 }
57 try {
58 list=depot.full.take();
59 assert(list!=null);
60 } catch (InterruptedException e) {
61 // TODO Auto-generated catch block
62 e.printStackTrace();
63 }
64 }
65
66 if(verbose){System.err.println("crisD: **************** nextList() returning list of size "+list.size()+"; shutdown="+shutdown+", depot.full="+depot.full.size());}
67 ListNum<Read> ln=new ListNum<Read>(list, listnum);
68 listnum++;
69 return ln;
70 }
71
72 @Override
73 public void returnList(long listNumber, boolean poison){
74 if(poison){
75 if(verbose){System.err.println("crisD: A: Adding empty list to full.");}
76 depot.full.add(new ArrayList<Read>(0));
77 }else{
78 if(verbose){System.err.println("crisD: A: Adding empty list to empty.");}
79 depot.empty.add(new ArrayList<Read>(BUF_LEN)); //Technically this could be a length-0 list since it is never used.
80 }
81 }
82
83 @Override
84 public void run() {
85 synchronized(running){
86 assert(!running[0]) : "This cris was started by multiple threads.";
87 running[0]=true;
88 }
89 if(verbose){System.err.println("crisD: cris started.");}
90 threads=new Thread[] {Thread.currentThread()};
91
92 if(master){
93 readLists_master();
94 }else{
95 readLists_slave();
96 }
97
98 addPoison();
99
100 //End thread
101
102 while(!depot.empty.isEmpty() && !shutdown){
103 // System.out.println("crisD: Ending");
104 if(verbose){System.err.println("crisD: B: Adding empty lists to full.");}
105 depot.full.add(depot.empty.poll());
106 }
107 if(verbose){System.err.println("crisD: cris thread syncing before shutdown.");}
108
109 synchronized(running){//TODO Note: for some reason syncing on 'this' instead of 'running' causes a hang. Something else must be syncing improperly on this.
110 assert(running[0]);
111 running[0]=false;
112 }
113 if(verbose){System.err.println("crisD: cris thread terminated. Final depot size: "+depot.full.size()+", "+depot.empty.size());}
114 }
115
116 /*--------------------------------------------------------------*/
117 /*---------------- Inner Methods ----------------*/
118 /*--------------------------------------------------------------*/
119
120 private final void addPoison(){
121 //System.err.println("crisD: Adding poison.");
122 //Add poison pills
123 if(verbose){System.err.println("crisD: C: Adding poison to full.");}
124 depot.full.add(new ArrayList<Read>());
125 for(int i=1; i<depot.bufferCount; i++){
126 ArrayList<Read> list=null;
127 while(list==null){
128 try {
129 list=depot.empty.poll(1000, TimeUnit.MILLISECONDS);
130 } catch (InterruptedException e) {
131 // TODO Auto-generated catch block
132 // System.err.println("crisD: Do not be alarmed by the following error message:");
133 // e.printStackTrace();
134 if(shutdown){
135 i=depot.bufferCount;
136 break;
137 }
138 }
139 }
140 if(list!=null){
141 if(verbose){System.err.println("crisD: D: Adding list("+list.size()+") to full.");}
142 depot.full.add(list);
143 }
144 }
145 if(verbose){System.err.println("crisD: Added poison.");}
146 }
147
148 private final void readLists_master(){
149
150 if(verbose){System.err.println("crisD: Entered readLists_master().");}
151 ListNum<Read> lnForUnicastShutdown=null;
152 for(ListNum<Read> ln=source.nextList(); !shutdown && ln.list!=null; ln=source.nextList()){
153 final ArrayList<Read> reads=ln.list;
154 final int count=(reads==null ? 0 : reads.size());
155
156 if(verbose){System.err.println("crisD: Master fetched "+count+" reads.");}
157
158 if(keepAll || count==0 || (ln.id%ranks)==rank){//Decide whether to process this list
159
160 {
161 ArrayList<Read> dummy=null;
162 while(dummy==null && !shutdown){
163 try {
164 dummy=depot.empty.take();
165 } catch (InterruptedException e) {
166 // TODO Auto-generated catch block
167 e.printStackTrace();
168 if(shutdown){break;}
169 }
170 }
171 // if(shutdown){break;}
172 }
173
174 try {
175 depot.full.put(reads);
176 if(verbose){System.err.println("crisD: Master added reads to depot.");}
177 } catch (InterruptedException e) {
178 // TODO Auto-generated catch block
179 e.printStackTrace();
180 }
181 }
182 broadcast(ln);
183 lnForUnicastShutdown=ln;
184 if(verbose){System.err.println("crisD: Master broadcasted.");}
185 source.returnList(ln.id, count<1);
186 if(verbose){System.err.println("crisD: Master returned a list.");}
187 if(count<1){break;}
188 }
189 if(!keepAll){//Shutdown all slaves if unicasting
190 for(int i=1; i<ranks; i++){
191 unicast(lnForUnicastShutdown, i);
192 }
193 }
194 if(verbose){System.err.println("crisD: Finished readLists_master().");}
195 }
196
197 private final void readLists_slave(){
198
199 if(verbose){System.err.println("crisD: Entered readLists_slave().");}
200 for(ListNum<Read> ln=listen(); !shutdown && ln!=null; ln=listen()){
201
202 final ArrayList<Read> reads=ln.list;
203 final int count=(reads==null ? 0 : reads.size());
204
205 if(verbose){System.err.println("crisD: Slave fetched "+count+" reads.");}
206
207 if(keepAll || count==0 || (ln.id%ranks)==rank){//Decide whether to process this list
208 {
209 ArrayList<Read> dummy=null;
210 while(dummy==null && !shutdown){
211 try {
212 dummy=depot.empty.take();
213 } catch (InterruptedException e) {
214 // TODO Auto-generated catch block
215 e.printStackTrace();
216 if(shutdown){break;}
217 }
218 }
219 // if(shutdown){break;}
220 }
221
222 try {
223 depot.full.put(reads);
224 if(verbose){System.err.println("crisD: Slave added reads to depot.");}
225 } catch (InterruptedException e) {
226 // TODO Auto-generated catch block
227 e.printStackTrace();
228 }
229 }
230 if(count<1){break;}
231 }
232 if(verbose){System.err.println("crisD: Finished readLists_slave().");}
233 }
234
235 /*--------------------------------------------------------------*/
236 /*---------------- Concurrency Methods ----------------*/
237 /*--------------------------------------------------------------*/
238
239 protected void broadcast(ListNum<Read> ln){
240 if(!keepAll && ln.size()>0){//Decide how to send this list
241 final int toRank=(int)(ln.id%ranks);
242 unicast(ln, toRank);
243 return;
244 }
245
246 if(verbose){System.err.println("crisD "+(master?"master":"slave ")+": Broadcasting reads.");}
247
248 boolean success=false;
249 while(!success && !shutdown){
250 try {
251 //Do some MPI stuff
252 success=true;
253 } catch (Exception e) {
254 // TODO Auto-generated catch block
255 e.printStackTrace();
256 }
257 }
258 throw new RuntimeException("TODO");
259 }
260
261 protected void unicast(ListNum<Read> ln, final int toRank){
262 if(toRank==rank){return;}
263 if(verbose){System.err.println("crisD "+(master?"master":"slave ")+": Unicasting reads to "+toRank+".");}
264
265 boolean success=false;
266 while(!success && !shutdown){
267 try {
268 //Do some MPI stuff
269 success=true;
270 } catch (Exception e) {
271 // TODO Auto-generated catch block
272 e.printStackTrace();
273 }
274 }
275 throw new RuntimeException("TODO");
276 }
277
278 protected void broadcastPaired(boolean b){
279 if(verbose){System.err.println("crisD "+(master?"master":"slave ")+": Broadcasting pairing status.");}
280 boolean success=false;
281 while(!success && !shutdown){
282 try {
283 //Do some MPI stuff
284 success=true;
285 } catch (Exception e) {
286 // TODO Auto-generated catch block
287 e.printStackTrace();
288 }
289 }
290 // throw new RuntimeException("TODO");
291 }
292
293 protected void broadcastKeepall(boolean b){
294 if(verbose){System.err.println("crisD "+(master?"master":"slave ")+": Broadcasting keepAll status.");}
295 boolean success=false;
296 while(!success && !shutdown){
297 try {
298 //Do some MPI stuff
299 success=true;
300 } catch (Exception e) {
301 // TODO Auto-generated catch block
302 e.printStackTrace();
303 }
304 }
305 // throw new RuntimeException("TODO");
306 }
307
308 protected ListNum<Read> listen(){
309 if(verbose){System.err.println("crisD "+(master?"master":"slave ")+": Listening to "+0+" for reads.");}
310 boolean success=false;
311 while(!success && !shutdown){
312 try {
313 //Do some MPI stuff
314 success=true;
315 } catch (Exception e) {
316 // TODO Auto-generated catch block
317 e.printStackTrace();
318 }
319 }
320 throw new RuntimeException("TODO");
321 }
322
323 protected boolean listenPaired(){
324 if(verbose){System.err.println("crisD "+(master?"master":"slave ")+": Listening to "+0+" for pairing status.");}
325 boolean success=false;
326 while(!success && !shutdown){
327 try {
328 //Do some MPI stuff
329 success=true;
330 } catch (Exception e) {
331 // TODO Auto-generated catch block
332 e.printStackTrace();
333 }
334 }
335 throw new RuntimeException("TODO");
336 }
337
338 protected boolean listenKeepall(){
339 if(verbose){System.err.println("crisD "+(master?"master":"slave ")+": Listening to "+0+" for keepAll status.");}
340 boolean success=false;
341 while(!success && !shutdown){
342 try {
343 //Do some MPI stuff
344 success=true;
345 } catch (Exception e) {
346 // TODO Auto-generated catch block
347 e.printStackTrace();
348 }
349 }
350 throw new RuntimeException("TODO");
351 }
352
353 /*--------------------------------------------------------------*/
354 /*---------------- Termination ----------------*/
355 /*--------------------------------------------------------------*/
356
357 @Override
358 public void shutdown(){
359 if(verbose){System.out.println("crisD: Called shutdown.");}
360
361 shutdown=true;
362 if(!shutdown){
363
364 if(master){
365 source.shutdown();
366 }
367 for(Thread t : threads){
368 if(t!=null && t.isAlive()){
369 t.interrupt();
370 }
371 }
372 }
373 }
374
375 @Override
376 public synchronized void restart(){
377 shutdown=false;
378 depot=new ConcurrentDepot<Read>(BUF_LEN, NUM_BUFFS);
379 basesIn=0;
380 readsIn=0;
381 listnum=0; //Added Oct 9, 2014
382 if(master){
383 source.restart();
384 }
385 }
386
387 @Override
388 public synchronized void close(){
389 shutdown();
390
391 if(master){
392 source.close();
393 }else{
394
395 }
396
397 if(threads!=null && threads[0]!=null && threads[0].isAlive()){
398
399 while(threads[0].isAlive()){
400 // System.out.println("crisD: B");
401 ArrayList<Read> list=null;
402 for(int i=0; i<1000 && list==null && threads[0].isAlive(); i++){
403 try {
404 list=depot.full.poll(200, TimeUnit.MILLISECONDS);
405 } catch (InterruptedException e) {
406 // TODO Auto-generated catch block
407 System.err.println("crisD: Do not be alarmed by the following error message:");
408 e.printStackTrace();
409 break;
410 }
411 }
412
413 if(list!=null){
414 list.clear();
415 depot.empty.add(list);
416 }
417
418 // System.out.println("crisD: isAlive? "+threads[0].isAlive());
419 }
420
421 }
422
423 if(threads!=null){
424 for(int i=1; i<threads.length; i++){
425 while(threads[i]!=null && threads[i].isAlive()){
426 try {
427 threads[i].join();
428 } catch (InterruptedException e) {
429 // TODO Auto-generated catch block
430 e.printStackTrace();
431 }
432 }
433 }
434 }
435
436 }
437
438 /*--------------------------------------------------------------*/
439 /*---------------- Getters ----------------*/
440 /*--------------------------------------------------------------*/
441
442 @Override
443 public boolean paired() {return paired;}
444
445 @Override
446 public boolean verbose(){return verbose;}
447
448 @Override
449 public void setSampleRate(float rate, long seed){
450 if(master){source.setSampleRate(rate, seed);}
451 }
452
453 @Override
454 public long basesIn(){return basesIn;}
455 @Override
456 public long readsIn(){return readsIn;}
457
458 @Override
459 public boolean errorState(){
460 if(master){return errorState|source.errorState();}
461 return errorState;
462 }
463
464 @Override
465 public Object[] producers(){
466 if(master){return source.producers();}
467 return null;
468 }
469
470 /*--------------------------------------------------------------*/
471 /*---------------- Fields ----------------*/
472 /*--------------------------------------------------------------*/
473
474 /** Wrapped source of reads. Null for slaves. */
475 private ConcurrentReadInputStream source;
476 private final boolean master;
477 protected final boolean keepAll;
478 protected final int rank, ranks;
479
480 private boolean errorState=false;
481
482 private boolean[] running=new boolean[] {false};
483
484 private boolean shutdown=false;
485
486 private ConcurrentDepot<Read> depot;
487
488 private Thread[] threads;
489
490 private long basesIn=0;
491 private long readsIn=0;
492
493 private long listnum=0;
494
495 /** This should be set in the first broadcast */
496 private final boolean paired;
497
498 /*--------------------------------------------------------------*/
499 /*---------------- Static Fields ----------------*/
500 /*--------------------------------------------------------------*/
501
502 public static boolean verbose=false;
503
504 }