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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.Boogie;
using Microsoft.Boogie.GraphUtil;
namespace Microsoft.Boogie.Houdini
{
public class StagedHoudini
{
private Program program;
private Func<string, Program> ProgramFromFile;
private StagedHoudiniPlan plan;
private List<Houdini>[] houdiniInstances;
private List<StagedHoudiniTask> tasks = new List<StagedHoudiniTask>();
private Dictionary<ScheduledStage, HoudiniOutcome> outcomes = new Dictionary<ScheduledStage,HoudiniOutcome>();
private const string tempFilename = "__stagedHoudiniTemp.bpl";
public StagedHoudini(Program program, Func<string, Program> ProgramFromFile) {
this.program = program;
this.ProgramFromFile = ProgramFromFile;
this.houdiniInstances = new List<Houdini>[CommandLineOptions.Clo.StagedHoudiniThreads];
for (int i = 0; i < CommandLineOptions.Clo.StagedHoudiniThreads; i++) {
houdiniInstances[i] = new List<Houdini>();
}
var candidateDependenceAnalyser = new CandidateDependenceAnalyser(program);
candidateDependenceAnalyser.Analyse();
this.plan = candidateDependenceAnalyser.ApplyStages();
if (CommandLineOptions.Clo.Trace)
{
candidateDependenceAnalyser.dump();
EmitProgram("staged.bpl");
}
}
public HoudiniOutcome PerformStagedHoudiniInference()
{
EmitProgram(tempFilename);
#region Prepare the tasks, but do not launch them
foreach (var s in plan) {
Debug.Assert(!plan.GetDependences(s).Contains(s));
tasks.Add(new StagedHoudiniTask(s, new Task(() => {
ExecuteStage(s);
}, TaskCreationOptions.LongRunning)));
}
#endregion
#region Launch the tasks, and wait for them to finish
foreach (var t in tasks) {
t.parallelTask.Start();
}
Task.WaitAll(tasks.Select(Item => Item.parallelTask).ToArray());
int count = 0;
foreach(var h in houdiniInstances) {
if(h.Count() > 0) {
count++;
System.Diagnostics.Debug.Assert(h.Count() == 1);
h[0].Close();
}
}
Console.WriteLine("Used " + count + " houdini instances");
#endregion
return UnifyOutcomes();
}
private HoudiniOutcome UnifyOutcomes()
{
HoudiniOutcome result = new HoudiniOutcome();
var scheduledStages = outcomes.Keys.ToList();
result.assignment = new Dictionary<string,bool>();
foreach(var c in outcomes[scheduledStages[0]].assignment.Keys) {
result.assignment[c] = outcomes.Select(Item => Item.Value).Select(Item => Item.assignment[c]).All(Item => Item);
}
result.implementationOutcomes = new Dictionary<string,VCGenOutcome>();
foreach(var p in outcomes[scheduledStages[0]].implementationOutcomes.Keys) {
var unifiedImplementationOutcome = outcomes[scheduledStages[0]].implementationOutcomes[p];
for(int i = 1; i < scheduledStages.Count(); i++) {
unifiedImplementationOutcome = ChooseOutcome(unifiedImplementationOutcome,
outcomes[scheduledStages[i]].implementationOutcomes[p]);
}
result.implementationOutcomes[p] = unifiedImplementationOutcome;
}
return result;
}
private void ExecuteStage(ScheduledStage s)
{
Task.WaitAll(tasks.Where(
Item => plan.GetDependences(s).Contains(Item.stage)).
Select(Item => Item.parallelTask).ToArray());
List<Houdini> h = AcquireHoudiniInstance();
if (h.Count() == 0)
{
h.Add(new Houdini(ProgramFromFile(tempFilename), new HoudiniSession.HoudiniStatistics(), "houdiniCexTrace_" + s.GetId() + ".bpl"));
}
System.Diagnostics.Debug.Assert(h.Count() == 1);
Dictionary<string, bool> mergedAssignment = null;
List<Dictionary<string, bool>> relevantAssignments;
IEnumerable<int> completedStages;
lock (outcomes)
{
relevantAssignments =
outcomes.Where(Item => plan.Contains(Item.Key)).
Select(Item => Item.Value).
Select(Item => Item.assignment).ToList();
//outcomes.Values.Select(Item => Item.assignment).ToList();
completedStages = plan.GetDependences(s).Select(Item => Item.GetId());
//completedStages = outcomes.Keys.Select(Item => Item.GetId());
}
if (relevantAssignments.Count() > 0)
{
mergedAssignment = new Dictionary<string, bool>();
foreach (var v in relevantAssignments[0].Keys)
{
mergedAssignment[v] = relevantAssignments.Select(Item => Item[v]).ToList().All(Item => Item);
}
}
HoudiniOutcome outcome = h[0].PerformHoudiniInference(
s.GetId(),
completedStages,
mergedAssignment);
lock (outcomes)
{
outcomes[s] = outcome;
}
ReleaseHoudiniInstance(h);
}
private static void ReleaseHoudiniInstance(List<Houdini> h)
{
Monitor.Exit(h);
}
private List<Houdini> AcquireHoudiniInstance()
{
List<Houdini> h = null;
do
{
foreach (var houdini in houdiniInstances)
{
if (Monitor.TryEnter(houdini))
{
h = houdini;
break;
}
else
{
Thread.Sleep(20);
}
}
} while (h == null);
return h;
}
private void EmitProgram(string filename)
{
using (TokenTextWriter writer = new TokenTextWriter(filename, true))
{
int oldPrintUnstructured = CommandLineOptions.Clo.PrintUnstructured;
CommandLineOptions.Clo.PrintUnstructured = 2;
program.Emit(writer);
CommandLineOptions.Clo.PrintUnstructured = oldPrintUnstructured;
}
}
private static VCGenOutcome ChooseOutcome(VCGenOutcome o1, VCGenOutcome o2) {
var vcOutcome1 = o1.outcome;
var vcOutcome2 = o2.outcome;
if(vcOutcome1 == vcOutcome2) {
return o1;
}
// Errors trump everything else
if(vcOutcome1 == VC.ConditionGeneration.Outcome.Errors) {
return o1;
}
if(vcOutcome2 == VC.ConditionGeneration.Outcome.Errors) {
return o2;
}
// If one outcome is Correct, return the other in case it is "worse"
if(vcOutcome1 == VC.ConditionGeneration.Outcome.Correct) {
return o2;
}
if(vcOutcome2 == VC.ConditionGeneration.Outcome.Correct) {
return o1;
}
// Neither outcome is correct, so if one outcome is ReachedBound, return the other in case it is "worse"
if(vcOutcome1 == VC.ConditionGeneration.Outcome.ReachedBound) {
return o2;
}
if(vcOutcome2 == VC.ConditionGeneration.Outcome.ReachedBound) {
return o1;
}
// Both outcomes must be timeout or memout; arbitrarily choose the first
return o1;
}
internal class StagedHoudiniTask {
internal ScheduledStage stage;
internal Task parallelTask;
internal StagedHoudiniTask(ScheduledStage stage, Task parallelTask) {
this.stage = stage;
this.parallelTask = parallelTask;
}
}
}
public class StagedHoudiniPlan : IEnumerable<ScheduledStage> {
private Graph<ScheduledStage> ScheduledStages;
private Dictionary<string, ScheduledStage> CandidateToStage;
internal StagedHoudiniPlan(Graph<ScheduledStage> ScheduledStages) {
this.ScheduledStages = ScheduledStages;
this.CandidateToStage = new Dictionary<string, ScheduledStage>();
foreach(var s in this) {
Debug.Assert(!GetDependences(s).Contains(s));
}
}
public IEnumerable<ScheduledStage> GetDependences(ScheduledStage s) {
IEnumerable<ScheduledStage> result;
lock(ScheduledStages) {
result = ScheduledStages.Successors(s);
}
return result;
}
private static int CompareStages(ScheduledStage s1, ScheduledStage s2) {
if(s1.GetId() < s2.GetId()) {
return -1;
}
if(s2.GetId() < s1.GetId()) {
return 1;
}
return 0;
}
public IEnumerator<ScheduledStage> GetEnumerator() {
List<ScheduledStage> sortedStages = ScheduledStages.Nodes.ToList();
sortedStages.Sort(CompareStages);
return sortedStages.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
internal ScheduledStage StageForCandidate(string c) {
if(CandidateToStage.ContainsKey(c)) {
return CandidateToStage[c];
}
foreach(var s in ScheduledStages.Nodes) {
if(s.ContainsCandidate(c)) {
CandidateToStage[c] = s;
return s;
}
}
return null;
}
public override string ToString()
{
string result = "";
foreach(ScheduledStage s in this) {
result += "Stage " + s;
result += " depends on stages: ";
foreach(var id in GetDependences(s).Select(Item => Item.GetId())) {
result += id + " ";
}
result += "\n";
}
return result;
}
}
public class ScheduledStage {
private int Id;
private HashSet<string> Candidates;
public ScheduledStage(int Id, HashSet<string> Candidates) {
this.Id = Id;
this.Candidates = Candidates;
}
internal void AddCandidate(string c) {
Candidates.Add(c);
}
internal bool ContainsCandidate(string c) {
return Candidates.Contains(c);
}
public int GetId() {
return Id;
}
public int Count() {
return Candidates.Count();
}
public override string ToString()
{
string result = "ID: " + Id + "{ ";
foreach(var c in Candidates) {
result += c + " ";
}
result += "}\n";
return result;
}
}
}
|