EMMA Coverage Report (generated Sat Aug 20 11:00:51 CDT 2011)
[all classes][tuffy.infer]

COVERAGE SUMMARY FOR SOURCE FILE [InferBucket.java]

nameclass, %method, %block, %line, %
InferBucket.java100% (2/2)73%  (11/15)75%  (269/358)77%  (58.4/76)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class InferBucket$CompWorker100% (1/1)100% (3/3)74%  (88/119)76%  (15.9/21)
run (): void 100% (1/1)63%  (49/78)67%  (10/15)
$SWITCH_TABLE$tuffy$util$Config$TUFFY_INFERENCE_TASK (): int [] 100% (1/1)93%  (25/27)92%  (0.9/1)
InferBucket$CompWorker (InferBucket): void 100% (1/1)100% (14/14)100% (5/5)
     
class InferBucket100% (1/1)67%  (8/12)76%  (181/239)77%  (42.4/55)
addSamples (int): void 0%   (0/1)0%   (0/18)0%   (0/3)
getSamples (): double 0%   (0/1)0%   (0/4)0%   (0/1)
setMrfInitStrategy (MRF$INIT_STRATEGY): void 0%   (0/1)0%   (0/22)0%   (0/4)
setNumThreads (int): void 0%   (0/1)0%   (0/4)0%   (0/2)
addCost (double): void 100% (1/1)83%  (15/18)94%  (2.8/3)
getTask (): Component 100% (1/1)90%  (27/30)94%  (3.8/4)
runInferParallel (): void 100% (1/1)95%  (61/64)88%  (14/16)
flushAtomStates (DataMover, String): void 100% (1/1)97%  (31/32)98%  (5.9/6)
InferBucket (Bucket): void 100% (1/1)100% (29/29)100% (10/10)
getCost (): double 100% (1/1)100% (3/3)100% (1/1)
getNumThreads (): int 100% (1/1)100% (3/3)100% (1/1)
infer (Settings): void 100% (1/1)100% (12/12)100% (4/4)

1package tuffy.infer;
2 
3 
4import java.util.ArrayList;
5 
6import tuffy.ground.partition.Bucket;
7import tuffy.ground.partition.Component;
8import tuffy.ground.partition.Partition;
9import tuffy.infer.MRF.INIT_STRATEGY;
10import tuffy.infer.ds.GAtom;
11import tuffy.util.Config;
12import tuffy.util.ExceptionMan;
13import tuffy.util.Settings;
14import tuffy.util.UIMan;
15/**
16 * 
17 * A bucket of inference tasks that can run in prallel. Currently, each task
18 * correspond to an MRF component so that the components can be processed in parallel.
19 */
20public class InferBucket{
21        private Bucket bucket;
22        private int numThreads = 2;
23        private double cost;
24        
25        private int totalSamples = 0;
26 
27        Config.TUFFY_INFERENCE_TASK task = null;
28        Settings settings = null;
29        
30        public void infer(Settings s){
31                settings = s;
32                task = Config.TUFFY_INFERENCE_TASK.valueOf(s.getString("task"));
33                this.runInferParallel();
34        }
35        
36        
37        public InferBucket(Bucket bucket){
38                this.bucket = bucket;
39                numThreads = Config.getNumThreads();
40        }
41 
42        public void flushAtomStates(DataMover dmover, String relAtoms){
43                ArrayList<GAtom> gatoms = new ArrayList<GAtom>();
44                for(Component c: bucket.getComponents()){
45                        if(c.atoms == null) continue;
46                        gatoms.addAll(c.atoms.values());
47                }
48                dmover.flushAtomStates(gatoms, relAtoms);
49        }
50 
51        public void setMrfInitStrategy(INIT_STRATEGY strategy){
52                for(Partition p : bucket.getPartitions()){
53                        if(p.mrf == null) continue;
54                        p.mrf.setInitStrategy(strategy);
55                }
56        }
57        
58        /**
59         * Get the cost after inference.
60         */
61        public double getCost(){
62                return cost;
63        }
64        
65        public double getSamples(){
66                return this.totalSamples;
67        }
68 
69        private Object sentinel = new Object();
70        
71        /**
72         * Add up the cost.
73         * 
74         * @see CompWorker#run()
75         * @param c
76         */
77        public void addCost(double c){
78                synchronized(sentinel){
79                        cost += c;
80                }
81        }
82        
83        /**
84         * Add up the cost.
85         * 
86         * @see CompWorker#run()
87         * @param c
88         */
89        public void addSamples(int c){
90                synchronized(sentinel){
91                        this.totalSamples += c;
92                }
93        }
94        
95        /**
96         * A worker thread that runs inference on one component at a time.
97         * Used for parallelization in a producer-consumer model.
98         */
99        public static class CompWorker extends Thread{
100                InferBucket ibucket;
101                Config.TUFFY_INFERENCE_TASK task;
102                Settings settings;
103                
104                public CompWorker(InferBucket bucket){
105                        this.ibucket = bucket;
106                        settings = bucket.settings;
107                        task = bucket.task;
108                }
109                
110                public void run(){
111                        while(true){
112                                Component comp = ibucket.getTask();
113                                if(comp == null) return;
114                                //UIMan.println(this + " is processing " + comp + " with " + comp.numAtoms + " atoms and " + comp.numClauses);
115                                InferComponent ic = new InferComponent(comp);
116                                switch(task){
117                                case MAP:
118                                        int ntries = (Integer)(settings.get("ntries"));
119                                        double flipsPerAtom = (Double)(settings.get("flipsPerAtom"));
120                                        int nflips = (int)(flipsPerAtom * comp.numAtoms);
121                                        //UIMan.println("flips = " + nflips + "; tries = " + ntries);
122                                        ic.inferMAP(ntries, nflips);
123                                        ibucket.addCost(ic.getCost());
124                                        break;
125                                case MARGINAL:
126                                        int nsamples = (Integer)(settings.get("nsamples"));
127                                        flipsPerAtom = (Double)(settings.get("flipsPerAtom"));
128                                        nflips = (int)(flipsPerAtom * comp.numAtoms);
129                                        ibucket.addCost(ic.inferMarginal(nsamples, nflips));
130                                        break;
131                                }
132                        }
133                }
134        }
135        
136        /**
137         * The queue of components to be processed
138         */
139        private ArrayList<Component> q = null;
140        
141        /**
142         * Get the next unprocessed component in the queue
143         * 
144         * @see CompWorker#run()
145         */
146        public Component getTask(){
147                synchronized(sentinel){
148                        if(q.isEmpty()) return null;
149                        Component c = q.remove(q.size()-1);
150                        //UIMan.println("    got " + c + " from the queue");
151                        return c;
152                }
153        }
154 
155        /**
156         * Solve the components in parallel.
157         * @param ntries
158         * @param nflips
159         */
160        private void runInferParallel(){
161                cost = 0;
162                q = new ArrayList<Component>();
163                q.addAll(bucket.getComponents());
164                ArrayList<CompWorker> workers = new ArrayList<CompWorker>();
165                
166                if(numThreads > 1){
167                        UIMan.setSilent(true);
168                }
169                for(int i=0; i<numThreads; i++){
170                        CompWorker t = new CompWorker(this);
171                        workers.add(t);
172                        t.start();
173                }
174                for(CompWorker t : workers){
175                        try {
176                                t.join();
177                        } catch (InterruptedException e) {
178                                ExceptionMan.handle(e);
179                        }
180                }
181                UIMan.setSilent(false);
182        }
183 
184 
185        public void setNumThreads(int numThreads) {
186                this.numThreads = numThreads;
187        }
188 
189 
190        public int getNumThreads() {
191                return numThreads;
192        }
193        
194 
195        
196}

[all classes][tuffy.infer]
EMMA 2.0.5312 EclEmma Fix 2 (C) Vladimir Roubtsov