Debian Codegraph @

current/ml/WorkerThread.java

raw symbols SCIP
1 package ml;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.concurrent.ArrayBlockingQueue;
6
7 import shared.Tools;
8
9 class WorkerThread extends Thread implements Comparable<WorkerThread> {
10 //Constructor
11 WorkerThread(final int tid_, final ArrayBlockingQueue<JobData> wq, /*final Object LOCK_, */final float cutoffForEvaluation_){
12 tid=tid_;
13 jobQueue=wq;
14 // LOCK=LOCK_;
15 cutoffForEvaluation=cutoffForEvaluation_;
16 }
17
18 //Called by start()
19 @Override
20 public void run(){
21 //Do anything necessary prior to processing
22
23 //Process the samples
24 processInner();
25
26 //Do anything necessary after processing
27
28 //Indicate successful exit status
29 success=true;
30 // System.err.println("Worker Done.");
31 }
32
33 /** Iterate through the lines */
34 void processInner(){
35
36 while(true) {
37 // synchronized(LOCK) {
38 // synchronized(this) {
39 tprof.reset();
40 // if(net!=null) {net.clear();} //Not a good place since master could read it
41 clearStats();
42 tprof.log();//0
43 // }
44 // }
45
46
47 final JobData job=getJob();
48 tprof.log();//1: 47855878
49 if(job==JobData.POISON) {break;}
50
51 synchronized(this) {
52 prepareForWork(job);
53 assert(net!=null);
54 tprof.log();//2: 209349
55
56 if(job.sort){sortSamples(job);}
57 tprof.log();//3: 0/1000000?
58
59 synchronized(net) {
60 int processed=processSamples(job.weightMult);
61 tprof.log();//4: 7417842/8404496
62 sendResults(processed, job);
63 if(job.setLock!=null) {job.setLock.readLock().unlock();}
64 this.job=null;
65 tprof.log();//4: 45718/39325
66 }
67 }
68 }
69 // System.err.println("Worker "+tid+" finished");
70 }
71
72
73 // SITT:
74 // W 464 47855878 209349 ? 7417842 45718 0
75 //
76 // SITF:
77 // W 473 44546439 212696 ? 8404496 39325 0
78
79 JobData getJob() {
80 JobData job=null;
81 while(job==null) {
82 try {
83 job=jobQueue.take();//Could process any network here, with the same dimensions
84 } catch (InterruptedException e) {
85 // TODO Auto-generated catch block
86 e.printStackTrace();
87 }
88 // System.err.println("Thread "+tid+" got "+token.seed+"; "+(token==poison)+", size="+workerQueue.size());
89 }
90 //System.err.println("W"+tid+" got permission for epoch e="+epoch+", ce="+currentEpoch);
91 return job;
92 }
93
94 void prepareForWork(JobData job) {
95 epoch=job.epoch;
96 backprop=job.backprop;
97 maxSamples=job.maxSamples;
98 samples=job.list;
99 this.job=job;
100 if(job.setLock!=null) {job.setLock.readLock().lock();}
101 assert(samples==null || maxSamples<=samples.size());
102 if(maxSamples<1) {
103 assert(false) : job.maxSamples+", "+samples.size()+", "+epoch;
104 return;
105 }
106
107
108 if(job.doCopy) {//TODO: If job size is zero, a null net can be returned
109 // assert(Trainer.copyNetInWorkerThread);//Not currently true for scanner threads
110 assert(job.mutableNet==null);
111 net=job.immutableNet.copy(false);//Works, but the reason is uncertain
112 net.transpose();
113 }else{
114 // synchronized(net) {//Does not work; a new net is needed
115 // assert(false);
116 // net.setFrom(job.net, false);
117 // net.clear();
118 // }
119 net=job.mutableNet;
120 synchronized(net) {
121 assert(Trainer.setNetInWorkerThread==(job.immutableNet!=null)) : Trainer.setNetInWorkerThread;
122 if(job.immutableNet!=null) {
123 net.setFrom(job.immutableNet, false);
124 }
125 net.clear();
126 net.transpose();
127 }
128 }
129 assert(net!=null);
130 // }
131 // net.setFrom(job.net, false);
132 }
133
134 int processSamples(float weightMult) {
135 // System.err.println("processEpoch()");
136
137 int samplesProcessed=0;
138 //System.err.println("W"+tid+" starts processing samples for "+epoch);
139
140 // for(int pos=tid; pos<maxSampleT && pos<samples.length; pos+=threads) {
141 // final Sample s=samples[pos];
142 // synchronized(s) {//Syncing here on LOCK solves nondeterminism...
143 // processSample(s, backprop);
144 // samplesProcessed++;
145 // }
146 // }
147 //
148 // for(final Sample s : samples) {
149 // synchronized(s) {//Syncing here on LOCK solves nondeterminism...
150 // processSample(s, backprop);
151 // samplesProcessed++;
152 // }
153 // }
154
155 if(job.set!=null) {
156 for(int i=job.jid; i<maxSamples; i+=job.jobsPerEpoch) {
157 Sample s=job.set[i];
158 synchronized(s) {
159 processSample(s, backprop, weightMult);
160 samplesProcessed++;
161 }
162 }
163 }else {
164 for(int i=0; i<maxSamples; i++) {
165 Sample s=samples.get(i);
166 synchronized(s) {
167 processSample(s, backprop, weightMult);
168 samplesProcessed++;
169 }
170 }
171 }
172 return samplesProcessed;
173 }
174
175 void sendResults(int samplesProcessed, JobData job) {
176 assert(maxSamples==samplesProcessed || samples==null) : maxSamples+", "+samplesProcessed+", "+(samples==null ? job.set.length : samples.size());
177 JobResults jr=new JobResults(maxSamples>0 ? net : null, epoch, samplesProcessed, tid, job.jid,
178 errorSum, weightedErrorSum, tpSum, tnSum, fpSum, fnSum);
179 net=null;//This is necessary
180 job.jobResultsQueue.add(jr);
181 }
182
183 /**
184 * Process a sample.
185 * @param line sample number
186 */
187 void processSample(final Sample sample, boolean backprop, float weightMult){
188 sample.setEpoch(Tools.max(epoch, sample.epoch()));
189 sample.setLastTID(tid);
190
191 net.processSample(sample, backprop, weightMult);
192
193 addToStats(sample);
194 sample.setPivot();
195
196 linesProcessedT++;
197 }
198
199 void sortSamples(JobData job) {
200 // if(true) {return;}
201 // positive.clear();
202 // negative.clear();
203 assert(positive.size()==0);
204 assert(negative.size()==0);
205
206 for(Sample s : samples) {
207 // if(s.epoch()<2) {s.setPivot();}
208 s.setPivot();
209 assert(s.checkPivot()) : job;
210 if(s.positive) {positive.add(s);}
211 else {negative.add(s);}
212 }
213
214 Collections.sort(positive);
215 Collections.sort(negative);
216 // assert(false) : positive.size()+", "+negative.size();
217
218 final int limit=samples.size();
219 samples.clear();
220 int ppos=0, npos=0;
221 while(samples.size()<limit) {
222 if(npos<negative.size()) {
223 samples.add(negative.get(npos));
224 npos++;
225 }
226 if(ppos<positive.size()) {
227 samples.add(positive.get(ppos));
228 ppos++;
229 }
230 }
231 assert(limit==samples.size());
232 assert(limit==positive.size()+negative.size());
233 assert(ppos==positive.size());
234 assert(npos==negative.size());
235 positive.clear();
236 negative.clear();//Avoids dangling references
237 }
238
239 void addToStats(Sample s) {
240 for(int i=0; i<s.result.length; i++){
241 // boolean goal=(s.goal[0]>=Trainer.booleanCutoffGoal);
242 boolean goal=(s.goal[0]>=cutoffForEvaluation);
243 boolean pred=(s.result[0]>=cutoffForEvaluation);
244 tpSum+=(goal && pred) ? 1 : 0;
245 tnSum+=(!goal && !pred) ? 1 : 0;
246 fpSum+=(!goal && pred) ? 1 : 0;
247 fnSum+=(goal && !pred) ? 1 : 0;
248 }
249 errorSum+=s.errorMagnitude;
250 weightedErrorSum+=s.weightedErrorMagnitude;
251 }
252
253 synchronized private void clearStats() {
254 errorSum=0;
255 weightedErrorSum=0;
256 tpSum=tnSum=fpSum=fnSum=0;
257 }
258
259 @Override
260 public int compareTo(WorkerThread o) {
261 return tid-o.tid;
262 }
263
264 private JobData job;
265
266 private final ArrayBlockingQueue<JobData> jobQueue;
267 private final float cutoffForEvaluation;
268
269 private double errorSum=0;
270 private double weightedErrorSum=0;
271 private int tpSum=0, tnSum=0, fpSum=0, fnSum=0;
272
273 // private Sample[] samples;
274 private ArrayList<Sample> samples;
275 private final ArrayList<Sample> positive=new ArrayList<Sample>();
276 private final ArrayList<Sample> negative=new ArrayList<Sample>();
277 private int maxSamples=0;
278 final Profiler tprof=new Profiler("W", 7);
279
280 /** Number of reads processed by this thread */
281 protected long linesProcessedT=0;
282
283 /** Number of reads retained by this thread */
284 protected long linesOutT=0;
285
286 protected boolean errorStateT=false;
287
288 /** True only if this thread has completed successfully */
289 boolean success=false;
290
291 private int epoch=0;
292 private boolean backprop;
293
294 /** Thread ID */
295 final int tid;
296
297 private CellNet net;
298 // private final CellNet net;
299
300 // @Deprecated
301 // private final Object LOCK; //Only for testing synchronization
302 }