forked from go-air/gini
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathax.go
More file actions
374 lines (344 loc) · 9.66 KB
/
Copy pathax.go
File metadata and controls
374 lines (344 loc) · 9.66 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright 2016 The Gini Authors. All rights reserved. Use of this source
// code is governed by a license that can be found in the License file.
package ax
import (
"time"
"github.com/go-air/gini/inter"
"github.com/go-air/gini/z"
)
// Interface T describes an assumptions exchanger (ax).
//
// Objects implementing T process incremental solving requests
// and give solving responses. The fundamental operation supported
// is an exchange (Ex), in which exactly one of the following two
// events occur.
//
// 1. A request is submitted to the system and an inter.S is guaranteed
// to process it unless T is Stop'd.
// 2. A response to some previous request is returned.
//
// Once the user is done with the object, it should be Stopped with Stop().
// The number of pending requests is returned in each Response.
//
// Objects implementing T must be safe for usage in mutlitple goroutines.
type T interface {
// Ex blocks until an exchange occurs
// If the request is accepted, ok is true, and false otherwise.
// If a response is ready, it is returned in resp, otherwise
// resp is nil.
//
// As a special case, if r is nil, then Ex blocks
// until a response is ready but does not try to submit the request.
Ex(r *Request) (resp *Response)
// TryEx tries to perform an exchange but does not block.
// It has the same semantics for Request and Response as
// Ex, but it can return a nil response and not process
// the supplied request. In this case, ok is false. Otherwise,
// ok is true.
TryEx(r *Request) (resp *Response, ok bool)
// Stop stops the ax.
Stop()
}
// NewT creates a new ax.T from a prototype solver inter.S.
// NewT uses proto as the first solving unit and each time
// a request is received, if:
//
// 1. there are less than cap copies; and
// 2. all existing copies are busy.
//
// then T will make another copy of proto.
//
// If cap < 1, then NewT panics.
func NewT(proto inter.S, cap int) T {
return newAx(proto, cap)
}
// implementation comments from here down.
// Diagram of interactions over channels. ax, Client, and u* each run a
// goroutine in parallel. Synchronously, pool processes requests and sends
// them to a u, waiting for one to become available. When u's are done,
// they send a resp to the pool over a 1-buffered channel, so atleast one
// can finish and become available for processing a request from the pool.
//
// The ax selects (s) between such client requests and responses from
// u's. Responses from u's are sent immediately unbuffered to the client.
//
// The Client selects on the ability to send requests and receive responses from
// the ax. She can send up to number(u)+1 requests in parallel. and up to
// number(u) requests get processed in parallel. if number(u) requests are
// outstanding (*), then pool accepts one more request and waits for a u to
// become available.
//
// When the ax receives a channel close on the request channel from the
// client, it immediately shuts down the u's, which select on a cancelation
// channel along with their request channel.
//
//
// -ax-(s)-----<Req<----------(s)---Client
// | | |
// --- --(o) |
// | | | |
// ------(c)-- | |
// | | | |
// | |----(+)--|----->cResp>--|
// | | |
// | ^ (+)
// | uResp[1] |
// | ^ ->uReq>===========
// | | | | |
// | |---(*)---u.1<--(s)----| | |
// | |---(*)---u.2<--(s)------| |
// | |---(*)---...<--(s)--------|
// | | | |
// | | | |
// =====(+)====>Cancel>=======---|
//
// (s) select
// --| or |-- select branch (select all channels on path except if (+) or (*) intervenes)
// | |
// >,^ channel direction
// (*) solve process, unknown duration.
// (+) combinational code, short/synchronous time step duration
// (c) channel closure case
// (o) channel receive w/out closure (open)
// = many select lines/paths
// u.i processing unit/solver i
// [n] n buffered channel
//
//
// Each u has a solver with a CNF. When the pool dispatches a request to a u, it
// looks for one which has processed the most similar requests in terms of a sort
// of Hamming distance. This heuristic means the selected u will likely be best
// tuned to solve such problems
//
// Responses are tagged with solve times so the client can decide on the
// estimated difficulty of requests to send.
//
// To solve SAT with DFS/DPLL/CDCL the client runs a solver which can mark
// branches as dispatched as well closed. dispatched branches have unknown
// solutions/results and may be blocked in the client so the client will not
// revisit the same space as a dispatched solve. However, if they are blocked,
// and the client side solver gives UNSAT, then the client side must wait for the
// dispatched solve responses to determine unsatisfiability.
type ax struct {
reqChn chan *Request
respChn chan *Response
uRespChn chan *Response
us []*unit
avail []int
cap int
}
func newAx(proto inter.S, cap int) *ax {
if cap < 1 {
panic("cannot pool <= 0 ginis")
}
//log.Printf("creating pool with %s vars, cap %d\n", proto.MaxVar(), cap)
m := &ax{
reqChn: make(chan *Request),
respChn: make(chan *Response),
uRespChn: make(chan *Response, 1),
us: make([]*unit, 1, cap),
avail: make([]int, 1, cap),
cap: cap}
m.us[0] = newUnit(proto, 0)
m.avail[0] = 0
go m.serve()
return m
}
func (m *ax) Ex(req *Request) *Response {
if req == nil {
return <-m.respChn
}
select {
case m.reqChn <- req:
return nil
case resp := <-m.respChn:
return resp
}
}
func (m *ax) TryEx(req *Request) (*Response, bool) {
if req == nil {
select {
case resp := <-m.respChn:
return resp, true
default:
return nil, false
}
}
// Q(wsc) Priority to resp for routing?
select {
case m.reqChn <- req:
return nil, true
case resp := <-m.respChn:
return resp, true
default:
return nil, false
}
}
func (m *ax) Stop() {
close(m.reqChn)
}
func (m *ax) serve() {
for {
select {
case req, ok := <-m.reqChn:
if !ok {
m.shutdown()
return
}
m.handleReq(req)
case resp := <-m.uRespChn:
m.handleResp(resp)
}
}
}
func (m *ax) handleReq(req *Request) {
if len(m.avail) == 0 {
resp := <-m.uRespChn
m.handleResp(resp)
}
u := m.getunit(req)
//log.Printf("start request %d by %d\n", req.Id, u.I)
u.handleReq(m.uRespChn, req)
}
// invariant: we always have atleast one
// non-nil free u in m.us until
// the client requests len(m.us) solves
func (m *ax) getunit(req *Request) *unit {
if len(m.us) < m.cap && len(m.avail) == 1 {
// grow only as needed. we create the next available gini
// ahead of time so that we can copy a non-running
// S without calling pause.
ai := m.avail[0]
protounit := m.us[ai]
m.avail = append(m.avail, len(m.us))
m.us = append(m.us, newUnit(protounit.S.SCopy(), len(m.us)))
}
//log.Printf("avail %+v/%d/%d\n", m.avail, len(m.us), m.cap)
scMax := -(1 << 30)
aiMax := -1
uiMax := -1
for i, a := range m.avail {
u := m.us[a]
sc := u.score(req.Ms)
if sc > scMax {
scMax = sc
aiMax = i
uiMax = a
}
}
al := len(m.avail) - 1
m.avail[aiMax], m.avail[al] = m.avail[al], m.avail[aiMax]
m.avail = m.avail[:al]
return m.us[uiMax]
}
func (m *ax) handleResp(resp *Response) {
m.avail = append(m.avail, resp.Who)
m.respChn <- resp // shouldn't block if client agrees to API
}
func (m *ax) shutdown() {
for _, u := range m.us {
if u != nil {
u.cancel <- struct{}{}
}
}
}
type unit struct {
I int
S inter.S
Solve inter.Solve
cancel chan struct{}
Start time.Time
Pos []int // Pos[var] gives count of supplied cubes with positive sign for var
Neg []int
}
func newUnit(s inter.S, i int) *unit {
return &unit{
I: i,
S: s,
Solve: nil,
cancel: make(chan struct{}),
Pos: make([]int, s.MaxVar()+1),
Neg: make([]int, s.MaxVar()+1)}
}
func (u *unit) handleReq(respChn chan<- *Response, req *Request) {
u.Start = time.Now()
// TBD: add trail size, new units in response.
//log.Printf("start req %d by %d:%p\n", req.Id, u.I, u.S)
u.S.Assume(req.Ms...)
u.Solve = u.S.GoSolve()
ticker := time.NewTicker(100 * time.Microsecond)
var alarm <-chan time.Time
if req.Limit != 0 {
//log.Printf("limiting to %s\n", req.Limit)
alarm = time.After(req.Limit)
}
go func() {
defer ticker.Stop()
for {
select {
case <-alarm: // alarm nil/blocks if not time limit.
res, ok := u.Solve.Test()
if !ok {
u.Solve.Stop()
u.Solve = nil
}
u.handleResult(req, res, respChn)
return
case <-ticker.C:
res, ok := u.Solve.Test()
if ok {
u.handleResult(req, res, respChn)
return
}
case <-u.cancel:
//log.Printf("cancelling u %d\n", u.I)
u.Solve.Stop()
u.Solve = nil
return
}
}
}()
}
func (u *unit) handleResult(req *Request, res int, c chan<- *Response) {
resp := &Response{
Who: u.I,
Req: req,
Res: res,
Dur: time.Since(u.Start)}
// TBD units, model, etc
u.Solve = nil
// get model if available and requested
if res == 1 && req.Flag.Model() {
M := u.S.MaxVar()
for i := z.Var(1); i <= M; i++ {
m := i.Pos()
if !u.S.Value(i.Pos()) {
m = m.Not()
}
resp.Ms = append(resp.Ms, m)
}
}
// get why if avaialable and requested
if res == -1 && req.Flag.Why() {
resp.Ms = u.S.Why(resp.Ms)
}
//log.Printf("end req %d by %d:%p\n", req.Id, u.I, u.S)
c <- resp
}
func (u *unit) score(ms []z.Lit) int {
if true {
return 0
}
res := 0
for _, m := range ms {
v := m.Var()
if m.IsPos() {
res += u.Pos[v]
res -= u.Neg[v]
continue
}
res += u.Neg[v]
res -= u.Pos[v]
}
return res
}