| 1 |
package stream; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.concurrent.atomic.AtomicInteger; |
| 5 |
|
| 6 |
import shared.Shared; |
| 7 |
import structures.ListNum; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
public class ConcurrentReadOutputStreamD extends ConcurrentReadOutputStream{ |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public ConcurrentReadOutputStreamD(ConcurrentReadOutputStream cros_, boolean master_){ |
| 21 |
super(cros_==null ? null : cros_.ff1, cros_==null ? null : cros_.ff2); |
| 22 |
dest=cros_; |
| 23 |
master=master_; |
| 24 |
rank=Shared.MPI_RANK; |
| 25 |
ranks=Shared.MPI_NUM_RANKS; |
| 26 |
assert(master==(cros_!=null)); |
| 27 |
} |
| 28 |
|
| 29 |
@Override |
| 30 |
public synchronized void start(){ |
| 31 |
if(started){ |
| 32 |
System.err.println("Resetting output stream."); |
| 33 |
throw new RuntimeException(); |
| 34 |
} |
| 35 |
|
| 36 |
started=true; |
| 37 |
if(master){ |
| 38 |
terminatedCount.set(0); |
| 39 |
dest.start(); |
| 40 |
startThreads(); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
private void startThreads(){ |
| 45 |
assert(master); |
| 46 |
for(int i=0; i<ranks; i++){ |
| 47 |
if(i!=rank){ |
| 48 |
ListenThread lt=new ListenThread(i); |
| 49 |
lt.start(); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
@Override |
| 60 |
public synchronized void add(ArrayList<Read> list, long listnum){ |
| 61 |
if(master){ |
| 62 |
dest.add(list, listnum); |
| 63 |
}else{ |
| 64 |
unicast(list, listnum, 0); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public void close(){ |
| 70 |
if(master){ |
| 71 |
int count=terminatedCount.incrementAndGet(); |
| 72 |
while(count<ranks){ |
| 73 |
synchronized(terminatedCount){ |
| 74 |
count=terminatedCount.intValue(); |
| 75 |
if(count<ranks){ |
| 76 |
try { |
| 77 |
terminatedCount.wait(1000); |
| 78 |
} catch (InterruptedException e) { |
| 79 |
|
| 80 |
e.printStackTrace(); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
dest.close(); |
| 86 |
}else{ |
| 87 |
unicast(new ListNum<Read>(new ArrayList<Read>(1), -1), 0); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public void join(){ |
| 93 |
if(master){ |
| 94 |
dest.join(); |
| 95 |
broadcastJoin(true); |
| 96 |
}else{ |
| 97 |
boolean b=listenForJoin(); |
| 98 |
assert(b); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public synchronized void resetNextListID(){ |
| 104 |
if(master){ |
| 105 |
dest.resetNextListID(); |
| 106 |
terminatedCount.set(0); |
| 107 |
finishedSuccessfully=false; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public String fname(){ |
| 113 |
return ff1.name(); |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public boolean errorState(){ |
| 118 |
if(master){ |
| 119 |
return errorState || dest.errorState(); |
| 120 |
}else{ |
| 121 |
return errorState; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public boolean finishedSuccessfully(){ |
| 127 |
if(finishedSuccessfully){return true;} |
| 128 |
|
| 129 |
synchronized(this){ |
| 130 |
if(finishedSuccessfully){return true;} |
| 131 |
if(master){ |
| 132 |
finishedSuccessfully=dest.finishedSuccessfully(); |
| 133 |
broadcastFinishedSuccessfully(finishedSuccessfully); |
| 134 |
}else{ |
| 135 |
finishedSuccessfully=listenFinishedSuccessfully(); |
| 136 |
} |
| 137 |
} |
| 138 |
return finishedSuccessfully; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
private void unicast(ArrayList<Read> list, long listnum, int i) { |
| 146 |
unicast(new ListNum<Read>(list, listnum), i); |
| 147 |
} |
| 148 |
|
| 149 |
protected void unicast(ListNum<Read> ln, int i) { |
| 150 |
if(verbose){System.err.println("crosD "+(master?"master":"slave ")+": Unicasting reads to "+i+".");} |
| 151 |
assert(!master); |
| 152 |
|
| 153 |
boolean success=false; |
| 154 |
while(!success){ |
| 155 |
try { |
| 156 |
|
| 157 |
success=true; |
| 158 |
} catch (Exception e) { |
| 159 |
|
| 160 |
e.printStackTrace(); |
| 161 |
} |
| 162 |
} |
| 163 |
throw new RuntimeException("TODO"); |
| 164 |
} |
| 165 |
|
| 166 |
protected ListNum<Read> listen(int i){ |
| 167 |
if(verbose){System.err.println("crosD "+(master?"master":"slave ")+": Listening for reads from "+i+".");} |
| 168 |
assert(master); |
| 169 |
|
| 170 |
boolean success=false; |
| 171 |
while(!success){ |
| 172 |
try { |
| 173 |
|
| 174 |
success=true; |
| 175 |
} catch (Exception e) { |
| 176 |
|
| 177 |
e.printStackTrace(); |
| 178 |
} |
| 179 |
} |
| 180 |
throw new RuntimeException("TODO"); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
protected boolean listenFinishedSuccessfully() { |
| 188 |
if(verbose){System.err.println("crosD "+(master?"master":"slave ")+": listenFinishedSuccessfully.");} |
| 189 |
assert(!master); |
| 190 |
|
| 191 |
boolean success=false; |
| 192 |
while(!success){ |
| 193 |
try { |
| 194 |
|
| 195 |
success=true; |
| 196 |
} catch (Exception e) { |
| 197 |
|
| 198 |
e.printStackTrace(); |
| 199 |
} |
| 200 |
} |
| 201 |
throw new RuntimeException("TODO"); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
protected void broadcastFinishedSuccessfully(boolean b) { |
| 208 |
if(verbose){System.err.println("crosD "+(master?"master":"slave ")+": broadcastFinishedSuccessfully.");} |
| 209 |
assert(master); |
| 210 |
|
| 211 |
boolean success=false; |
| 212 |
while(!success){ |
| 213 |
try { |
| 214 |
|
| 215 |
success=true; |
| 216 |
} catch (Exception e) { |
| 217 |
|
| 218 |
e.printStackTrace(); |
| 219 |
} |
| 220 |
} |
| 221 |
throw new RuntimeException("TODO"); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
protected void broadcastJoin(boolean b) { |
| 226 |
if(verbose){System.err.println("crosD "+(master?"master":"slave ")+": broadcastJoin.");} |
| 227 |
assert(master); |
| 228 |
|
| 229 |
boolean success=false; |
| 230 |
while(!success){ |
| 231 |
try { |
| 232 |
|
| 233 |
success=true; |
| 234 |
} catch (Exception e) { |
| 235 |
|
| 236 |
e.printStackTrace(); |
| 237 |
} |
| 238 |
} |
| 239 |
throw new RuntimeException("TODO"); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
protected boolean listenForJoin() { |
| 244 |
if(verbose){System.err.println("crosD "+(master?"master":"slave ")+": listenForJoin.");} |
| 245 |
assert(!master); |
| 246 |
|
| 247 |
boolean success=false; |
| 248 |
while(!success){ |
| 249 |
try { |
| 250 |
|
| 251 |
success=true; |
| 252 |
} catch (Exception e) { |
| 253 |
|
| 254 |
e.printStackTrace(); |
| 255 |
} |
| 256 |
} |
| 257 |
throw new RuntimeException("TODO"); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
private class ListenThread extends Thread{ |
| 265 |
|
| 266 |
ListenThread(int sourceNum_){ |
| 267 |
sourceNum=sourceNum_; |
| 268 |
assert(sourceNum_!=rank); |
| 269 |
assert(sourceNum>=0 && sourceNum<ranks); |
| 270 |
} |
| 271 |
|
| 272 |
@Override |
| 273 |
public void run(){ |
| 274 |
assert(master); |
| 275 |
ListNum<Read> ln=listen(sourceNum); |
| 276 |
while(ln!=null && ln.id>=0){ |
| 277 |
dest.add(ln.list, ln.id); |
| 278 |
ln=listen(sourceNum); |
| 279 |
} |
| 280 |
final int count=terminatedCount.addAndGet(1); |
| 281 |
if(count>=ranks){ |
| 282 |
synchronized(terminatedCount){ |
| 283 |
terminatedCount.notify(); |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
final int sourceNum; |
| 289 |
|
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
@Override |
| 297 |
public ReadStreamWriter getRS1(){return master ? dest.getRS1() : null;} |
| 298 |
@Override |
| 299 |
public ReadStreamWriter getRS2(){return master ? dest.getRS2() : null;} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
protected final AtomicInteger terminatedCount=new AtomicInteger(0); |
| 306 |
protected final ConcurrentReadOutputStreamD thisPointer=this; |
| 307 |
|
| 308 |
|
| 309 |
protected ConcurrentReadOutputStream dest; |
| 310 |
protected final boolean master; |
| 311 |
protected final int rank, ranks; |
| 312 |
|
| 313 |
} |