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
|
package ml;
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.ArrayBlockingQueue;
import shared.Tools;
class WorkerThread extends Thread implements Comparable<WorkerThread> {
//Constructor
WorkerThread(final int tid_, final ArrayBlockingQueue<JobData> wq, /*final Object LOCK_, */final float cutoffForEvaluation_){
tid=tid_;
jobQueue=wq;
// LOCK=LOCK_;
cutoffForEvaluation=cutoffForEvaluation_;
}
//Called by start()
@Override
public void run(){
//Do anything necessary prior to processing
//Process the samples
processInner();
//Do anything necessary after processing
//Indicate successful exit status
success=true;
// System.err.println("Worker Done.");
}
/** Iterate through the lines */
void processInner(){
while(true) {
// synchronized(LOCK) {
// synchronized(this) {
tprof.reset();
// if(net!=null) {net.clear();} //Not a good place since master could read it
clearStats();
tprof.log();//0
// }
// }
final JobData job=getJob();
tprof.log();//1: 47855878
if(job==JobData.POISON) {break;}
synchronized(this) {
prepareForWork(job);
assert(net!=null);
tprof.log();//2: 209349
if(job.sort){sortSamples(job);}
tprof.log();//3: 0/1000000?
synchronized(net) {
int processed=processSamples(job.weightMult);
tprof.log();//4: 7417842/8404496
sendResults(processed, job);
if(job.setLock!=null) {job.setLock.readLock().unlock();}
this.job=null;
tprof.log();//4: 45718/39325
}
}
}
// System.err.println("Worker "+tid+" finished");
}
// SITT:
// W 464 47855878 209349 ? 7417842 45718 0
//
// SITF:
// W 473 44546439 212696 ? 8404496 39325 0
JobData getJob() {
JobData job=null;
while(job==null) {
try {
job=jobQueue.take();//Could process any network here, with the same dimensions
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.err.println("Thread "+tid+" got "+token.seed+"; "+(token==poison)+", size="+workerQueue.size());
}
//System.err.println("W"+tid+" got permission for epoch e="+epoch+", ce="+currentEpoch);
return job;
}
void prepareForWork(JobData job) {
epoch=job.epoch;
backprop=job.backprop;
maxSamples=job.maxSamples;
samples=job.list;
this.job=job;
if(job.setLock!=null) {job.setLock.readLock().lock();}
assert(samples==null || maxSamples<=samples.size());
if(maxSamples<1) {
assert(false) : job.maxSamples+", "+samples.size()+", "+epoch;
return;
}
if(job.doCopy) {//TODO: If job size is zero, a null net can be returned
// assert(Trainer.copyNetInWorkerThread);//Not currently true for scanner threads
assert(job.mutableNet==null);
net=job.immutableNet.copy(false);//Works, but the reason is uncertain
net.transpose();
}else{
// synchronized(net) {//Does not work; a new net is needed
// assert(false);
// net.setFrom(job.net, false);
// net.clear();
// }
net=job.mutableNet;
synchronized(net) {
assert(Trainer.setNetInWorkerThread==(job.immutableNet!=null)) : Trainer.setNetInWorkerThread;
if(job.immutableNet!=null) {
net.setFrom(job.immutableNet, false);
}
net.clear();
net.transpose();
}
}
assert(net!=null);
// }
// net.setFrom(job.net, false);
}
int processSamples(float weightMult) {
// System.err.println("processEpoch()");
int samplesProcessed=0;
//System.err.println("W"+tid+" starts processing samples for "+epoch);
// for(int pos=tid; pos<maxSampleT && pos<samples.length; pos+=threads) {
// final Sample s=samples[pos];
// synchronized(s) {//Syncing here on LOCK solves nondeterminism...
// processSample(s, backprop);
// samplesProcessed++;
// }
// }
//
// for(final Sample s : samples) {
// synchronized(s) {//Syncing here on LOCK solves nondeterminism...
// processSample(s, backprop);
// samplesProcessed++;
// }
// }
if(job.set!=null) {
for(int i=job.jid; i<maxSamples; i+=job.jobsPerEpoch) {
Sample s=job.set[i];
synchronized(s) {
processSample(s, backprop, weightMult);
samplesProcessed++;
}
}
}else {
for(int i=0; i<maxSamples; i++) {
Sample s=samples.get(i);
synchronized(s) {
processSample(s, backprop, weightMult);
samplesProcessed++;
}
}
}
return samplesProcessed;
}
void sendResults(int samplesProcessed, JobData job) {
assert(maxSamples==samplesProcessed || samples==null) : maxSamples+", "+samplesProcessed+", "+(samples==null ? job.set.length : samples.size());
JobResults jr=new JobResults(maxSamples>0 ? net : null, epoch, samplesProcessed, tid, job.jid,
errorSum, weightedErrorSum, tpSum, tnSum, fpSum, fnSum);
net=null;//This is necessary
job.jobResultsQueue.add(jr);
}
/**
* Process a sample.
* @param line sample number
*/
void processSample(final Sample sample, boolean backprop, float weightMult){
sample.setEpoch(Tools.max(epoch, sample.epoch()));
sample.setLastTID(tid);
net.processSample(sample, backprop, weightMult);
addToStats(sample);
sample.setPivot();
linesProcessedT++;
}
void sortSamples(JobData job) {
// if(true) {return;}
// positive.clear();
// negative.clear();
assert(positive.size()==0);
assert(negative.size()==0);
for(Sample s : samples) {
// if(s.epoch()<2) {s.setPivot();}
s.setPivot();
assert(s.checkPivot()) : job;
if(s.positive) {positive.add(s);}
else {negative.add(s);}
}
Collections.sort(positive);
Collections.sort(negative);
// assert(false) : positive.size()+", "+negative.size();
final int limit=samples.size();
samples.clear();
int ppos=0, npos=0;
while(samples.size()<limit) {
if(npos<negative.size()) {
samples.add(negative.get(npos));
npos++;
}
if(ppos<positive.size()) {
samples.add(positive.get(ppos));
ppos++;
}
}
assert(limit==samples.size());
assert(limit==positive.size()+negative.size());
assert(ppos==positive.size());
assert(npos==negative.size());
positive.clear();
negative.clear();//Avoids dangling references
}
void addToStats(Sample s) {
for(int i=0; i<s.result.length; i++){
// boolean goal=(s.goal[0]>=Trainer.booleanCutoffGoal);
boolean goal=(s.goal[0]>=cutoffForEvaluation);
boolean pred=(s.result[0]>=cutoffForEvaluation);
tpSum+=(goal && pred) ? 1 : 0;
tnSum+=(!goal && !pred) ? 1 : 0;
fpSum+=(!goal && pred) ? 1 : 0;
fnSum+=(goal && !pred) ? 1 : 0;
}
errorSum+=s.errorMagnitude;
weightedErrorSum+=s.weightedErrorMagnitude;
}
synchronized private void clearStats() {
errorSum=0;
weightedErrorSum=0;
tpSum=tnSum=fpSum=fnSum=0;
}
@Override
public int compareTo(WorkerThread o) {
return tid-o.tid;
}
private JobData job;
private final ArrayBlockingQueue<JobData> jobQueue;
private final float cutoffForEvaluation;
private double errorSum=0;
private double weightedErrorSum=0;
private int tpSum=0, tnSum=0, fpSum=0, fnSum=0;
// private Sample[] samples;
private ArrayList<Sample> samples;
private final ArrayList<Sample> positive=new ArrayList<Sample>();
private final ArrayList<Sample> negative=new ArrayList<Sample>();
private int maxSamples=0;
final Profiler tprof=new Profiler("W", 7);
/** Number of reads processed by this thread */
protected long linesProcessedT=0;
/** Number of reads retained by this thread */
protected long linesOutT=0;
protected boolean errorStateT=false;
/** True only if this thread has completed successfully */
boolean success=false;
private int epoch=0;
private boolean backprop;
/** Thread ID */
final int tid;
private CellNet net;
// private final CellNet net;
// @Deprecated
// private final Object LOCK; //Only for testing synchronization
}
|