| 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 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
public class ConcurrentReadInputStreamD extends ConcurrentReadInputStream { |
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 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 |
|
| 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)); |
| 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 |
|
| 101 |
|
| 102 |
while(!depot.empty.isEmpty() && !shutdown){ |
| 103 |
|
| 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){ |
| 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 |
|
| 118 |
|
| 119 |
|
| 120 |
private final void addPoison(){ |
| 121 |
|
| 122 |
|
| 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 |
|
| 132 |
|
| 133 |
|
| 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){ |
| 159 |
|
| 160 |
{ |
| 161 |
ArrayList<Read> dummy=null; |
| 162 |
while(dummy==null && !shutdown){ |
| 163 |
try { |
| 164 |
dummy=depot.empty.take(); |
| 165 |
} catch (InterruptedException e) { |
| 166 |
|
| 167 |
e.printStackTrace(); |
| 168 |
if(shutdown){break;} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 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 |
|
| 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){ |
| 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){ |
| 208 |
{ |
| 209 |
ArrayList<Read> dummy=null; |
| 210 |
while(dummy==null && !shutdown){ |
| 211 |
try { |
| 212 |
dummy=depot.empty.take(); |
| 213 |
} catch (InterruptedException e) { |
| 214 |
|
| 215 |
e.printStackTrace(); |
| 216 |
if(shutdown){break;} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 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 |
|
| 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 |
|
| 237 |
|
| 238 |
|
| 239 |
protected void broadcast(ListNum<Read> ln){ |
| 240 |
if(!keepAll && ln.size()>0){ |
| 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 |
|
| 252 |
success=true; |
| 253 |
} catch (Exception e) { |
| 254 |
|
| 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 |
|
| 269 |
success=true; |
| 270 |
} catch (Exception e) { |
| 271 |
|
| 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 |
|
| 284 |
success=true; |
| 285 |
} catch (Exception e) { |
| 286 |
|
| 287 |
e.printStackTrace(); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 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 |
|
| 299 |
success=true; |
| 300 |
} catch (Exception e) { |
| 301 |
|
| 302 |
e.printStackTrace(); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 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 |
|
| 314 |
success=true; |
| 315 |
} catch (Exception e) { |
| 316 |
|
| 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 |
|
| 329 |
success=true; |
| 330 |
} catch (Exception e) { |
| 331 |
|
| 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 |
|
| 344 |
success=true; |
| 345 |
} catch (Exception e) { |
| 346 |
|
| 347 |
e.printStackTrace(); |
| 348 |
} |
| 349 |
} |
| 350 |
throw new RuntimeException("TODO"); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 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; |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 430 |
e.printStackTrace(); |
| 431 |
} |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 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 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 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 |
|
| 496 |
private final boolean paired; |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
public static boolean verbose=false; |
| 503 |
|
| 504 |
} |