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