-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode.go
1165 lines (1039 loc) · 24.9 KB
/
node.go
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package fgbase
import (
"fmt"
"reflect"
"sync"
"sync/atomic"
"time"
)
// Node of a flowgraph.
type Node struct {
ID int64 // unique id
Name string // for tracing
Cnt int64 // execution count
Srcs []*Edge // upstream Edge's
Dsts []*Edge // downstream Edge's
RdyFunc NodeRdy // func to test Edge readiness
FireFunc NodeFire // func to fire off the Node
RunFunc NodeRun // func to repeatedly run Node
Aux interface{} // auxiliary empty interface to hold state
RdyState int // state of latest readiness
Owner interface{} // owner of this node
cases []reflect.SelectCase // select cases to read from Edge's
caseToEdgeDir map[int]edgeDir // map from index of selected case to associated Edge
edgeToCase map[*Edge]int // map from *Edge to index of associated select case
dataBackup []reflect.Value // backup data channels for inputs
flag uintptr // flags for package internal use
srcNames []string // source names
dstNames []string // destination names
srcIndexByName map[string]int // map of index of source Edge's by name
dstIndexByName map[string]int // map of index of destination Edge's by name
dotAttr string // attributes for dot outputs
}
type edgeDir struct {
edge *Edge
srcFlag bool
}
const (
flagPool = uintptr(1 << iota)
flagRecurse
flagRecursed
flagSelecting
)
// NodeRdy is the function signature for evaluating readiness of a Node to fire.
type NodeRdy func(*Node) bool
// NodeFire is the function signature for executing a Node.
// Any error message should be written using Node.LogError and
// nil written to any output Edge.
type NodeFire func(*Node) error
// NodeRun is the function signature for an alternate Node event loop.
type NodeRun func(*Node) error
func newNode(name string, ready NodeRdy, fire NodeFire) Node {
var n Node
i := atomic.AddInt64(&NodeID, 1)
n.ID = i - 1
n.Name = name
n.Cnt = -1
n.RdyFunc = ready
n.FireFunc = fire
n.caseToEdgeDir = make(map[int]edgeDir)
n.edgeToCase = make(map[*Edge]int)
return n
}
func makeNode(name string, srcs, dsts []*Edge, ready NodeRdy, fire NodeFire, pool, recurse bool) Node {
n := newNode(name, ready, fire)
if pool {
n.flag = n.flag | flagPool
}
if recurse {
n.flag = n.flag | flagRecurse
}
n.Srcs = srcs
n.Dsts = dsts
for _, v := range n.Srcs {
if v != nil {
v.srcRegister(&n)
}
}
for _, v := range n.Dsts {
if v != nil {
v.dstRegister(&n)
}
}
// n.Init()
return n
}
// Init initializes node internals after edges have been added
func (n *Node) Init() {
var cnt = 0
pool := (n.flag & flagPool) == flagPool
recurse := (n.flag & flagRecurse) == flagRecurse
for i := range n.Srcs {
srci := n.Srcs[i]
if srci == nil {
break
}
srci.RdyCnt = func() int {
if srci.Val != nil {
return 0
}
return 1
}()
if srci.Data != nil {
j := len(*srci.Data)
if j == 0 || !pool {
df := func() int {
if pool && recurse {
return 0
}
return ChannelSize
}()
*srci.Data = append(*srci.Data, make(chan interface{}, df))
} else {
j = 0
}
n.cases = append(n.cases, reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf((*srci.Data)[j])})
n.dataBackup = append(n.dataBackup, n.cases[cnt].Chan) // backup copy
n.caseToEdgeDir[cnt] = edgeDir{srci, true}
n.edgeToCase[srci] = cnt
cnt = cnt + 1
}
}
for i := range n.Dsts {
dsti := n.Dsts[i]
if dsti == nil {
break
}
dsti.RdyCnt = 0
if dsti.Ack != nil {
if pool {
dsti.Ack = make(chan struct{}, ChannelSize)
}
n.cases = append(n.cases, reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(dsti.Ack)})
n.caseToEdgeDir[cnt] = edgeDir{dsti, false}
n.edgeToCase[dsti] = cnt
cnt = cnt + 1
}
}
}
// makeNodeForPool returns a new Node with copies of source and destination Edge's.
// Both source channels and the destination data channel get shared.
// The destination ack channel is unique.
func makeNodeForPool(
name string,
srcs, dsts []Edge,
ready NodeRdy,
fire NodeFire,
recurse bool) Node {
var srcsp, dstsp []*Edge
for i := 0; i < len(srcs); i++ {
srcsp = append(srcsp, &srcs[i])
}
for i := 0; i < len(dsts); i++ {
dstsp = append(dstsp, &dsts[i])
}
return makeNode(name, srcsp, dstsp, ready, fire, true, recurse)
}
// MakeNode returns a new Node with slices of input and output Edge's and functions for testing readiness then firing.
func MakeNode(
name string,
srcs, dsts []*Edge,
ready NodeRdy,
fire NodeFire) Node {
return makeNode(name, srcs, dsts, ready, fire, false, false)
}
func prefixTracef(n *Node) (format string) {
var newFmt string
if TraceIndent {
for i := int64(0); i < n.ID; i++ {
newFmt += "\t"
}
}
if TraceStyle == New {
if n.Cnt == -1 {
newFmt += "*:"
} else {
newFmt += fmt.Sprintf("%d:", n.Cnt)
}
if TraceSeconds {
t := TimeSinceStart()
if t >= 0.0 {
newFmt += fmt.Sprintf("%.4f:", TimeSinceStart())
} else {
newFmt += "*:"
}
}
newFmt += " "
}
newFmt += n.Name
if TraceStyle == New {
newFmt += fmt.Sprintf("_%d", n.ID)
} else {
newFmt += fmt.Sprintf("(%d", n.ID)
}
if TraceStyle == Old && TraceFireCnt {
if n.Cnt >= 0 {
newFmt += fmt.Sprintf(":%d", n.Cnt)
} else {
newFmt += ":*"
}
}
if TraceStyle == Old && (TraceSeconds || TraceLevel >= VVVV) {
t := TimeSinceStart()
if t >= 0.0 {
newFmt += fmt.Sprintf(":%.4f", TimeSinceStart())
} else {
newFmt += ":*"
}
}
if TraceStyle == Old && TracePointer {
newFmt += fmt.Sprintf(":%p", n)
}
if TraceStyle == Old {
newFmt += ")\t"
}
return newFmt
}
// Tracef for debug trace printing. Uses atomic log mechanism.
func (n *Node) Tracef(format string, v ...interface{}) {
if TraceLevel < V {
return
}
newFmt := prefixTracef(n) + " "
newFmt += format
StdoutLog.Printf(newFmt, v...)
}
// LogError for logging of error messages. Uses atomic log mechanism.
func (n *Node) LogError(format string, v ...interface{}) {
// _,nm,ln,_ := runtime.Caller(1)
newFmt := prefixTracef(n)
newFmt += " ERROR: "
newFmt += format
// newFmt += fmt.Sprintf(" -- %s:%d ", nm, ln)
StderrLog.Printf(newFmt, v...)
}
// Panicf for quitting with formatted panic message.
func (n *Node) Panicf(format string, v ...interface{}) {
newFmt := prefixTracef(n)
newFmt += " ERROR: "
newFmt += format
panic(fmt.Sprintf(newFmt, v...))
}
// traceValRdySrc lists Node input values
func (n *Node) traceValRdySrc(valOnly bool) string {
newFmt := prefixTracef(n)
if !valOnly {
if TraceStyle == Old {
newFmt += "<<"
} else {
newFmt += " <"
}
} else if TraceStyle == New {
newFmt += " ("
}
for i := range n.Srcs {
if i != 0 {
newFmt += ","
}
srci := n.Srcs[i]
if TracePorts && n.srcNames != nil {
newFmt += "." + n.srcNames[i] + "("
}
if srci == nil {
newFmt += "*"
} else {
newFmt += fmt.Sprintf("%s=", srci.Name)
if srci.RdyZero() {
if IsSlice(srci.Val) {
newFmt += StringSlice(srci.Val)
} else {
if srci.Val == nil {
newFmt += "<nil>"
} else if v, ok := srci.Val.(error); ok && v.Error() == "EOF" {
newFmt += "EOF"
} else {
newFmt += fmt.Sprintf("%s", String(srci.Val))
}
}
} else {
newFmt += "_"
}
}
if TracePorts && n.srcNames != nil {
newFmt += ")"
}
if srci.blocked == ackBlock {
newFmt += "(α)"
}
}
if TraceStyle == New && valOnly {
newFmt += ")"
} else if TraceStyle == Old {
newFmt += ";"
} else {
newFmt += "><"
}
return newFmt
}
// traceValRdyDst lists Node output values or readiness.
func (n *Node) traceValRdyDst(valOnly bool) string {
var newFmt string
if TraceStyle == New && valOnly {
newFmt += "("
}
for i := range n.Dsts {
if i != 0 {
newFmt += ","
}
dsti := n.Dsts[i]
if TracePorts && n.dstNames != nil {
newFmt += "." + n.dstNames[i] + "("
}
if dsti == nil {
newFmt += "*"
} else {
dstiv := dsti.Val
if _, ok := dstiv.(ackWrap); ok {
dstiv = dstiv.(ackWrap).datum // remove wrapper for tracing
}
if valOnly {
newFmt += fmt.Sprintf("%s=", dsti.Name)
if !dsti.Flow {
newFmt += "_"
} else {
if dstiv != nil {
s := String(dstiv)
if v, ok := dstiv.(error); ok && v.Error() == "EOF" {
newFmt += "EOF"
} else if !IsSlice(dstiv) {
newFmt += fmt.Sprintf("%s", s)
} else {
newFmt += s
}
} else {
newFmt += "<nil>"
}
}
} else {
newFmt += fmt.Sprintf("%s=k%v", dsti.Name, dsti.RdyCnt)
if dsti.blocked == dataBlock {
newFmt += "(δ)"
}
}
}
if TracePorts && n.dstNames != nil {
newFmt += ")"
}
}
if !valOnly {
if TraceStyle == Old {
newFmt += ">>"
} else {
newFmt += ">"
}
}
if TraceStyle == New && valOnly {
newFmt += ")"
}
if summarizing {
newFmt += "\t// "
if n.flag&flagSelecting == flagSelecting {
newFmt += "!select,"
}
newFmt += "cases["
for i := range n.cases {
if i != 0 {
newFmt += " "
}
if n.cases[i].Chan == reflect.ValueOf(nil) {
newFmt += fmt.Sprintf("nil")
} else {
newFmt += fmt.Sprintf("%p", n.cases[i].Chan.Interface())
}
}
newFmt += "]"
}
return newFmt
}
// TraceValRdy lists Node input values and output readiness when TraceLevel >= VVV.
func (n *Node) TraceValRdy() {
if TraceLevel >= VVV {
n.traceValRdy(false)
}
}
// traceValRdy lists Node input values and output values or readiness.
func (n *Node) traceValRdy(valOnly bool) {
newFmt := n.traceValRdySrc(valOnly)
newFmt += n.traceValRdyDst(valOnly)
newFmt += "\n"
StdoutLog.Printf(newFmt)
}
// traceValRdyErr lists Node input values and output readiness to stderr.
func (n *Node) traceValRdyErr() {
newFmt := n.traceValRdySrc(false)
newFmt += n.traceValRdyDst(false)
newFmt += "\n"
StderrLog.Printf(newFmt)
}
// TraceVals lists input and output values for a Node.
func (n *Node) TraceVals() {
if TraceLevel != Q {
n.traceValRdy(true)
}
}
// incrFireCnt increments execution count of Node.
func (n *Node) incrFireCnt() {
if GlobalStats {
c := atomic.AddInt64(&globalFireCnt, 1)
n.Cnt = c - 1
} else {
n.Cnt = n.Cnt + 1
}
}
// DefaultRdyFunc tests for everything ready.
func (n *Node) DefaultRdyFunc() bool {
for i := range n.Srcs {
if !n.Srcs[i].SrcRdy(n) {
return false
}
}
for i := range n.Dsts {
if !n.Dsts[i].DstRdy(n) {
return false
}
}
return true
}
// restoreDataChannels restores data channels for next use
func (n *Node) restoreDataChannels() {
j := 0
for i := range n.Srcs {
if n.Srcs[i].Data != nil {
if n.Srcs[i].RdyCnt > 0 {
n.cases[j].Chan = n.dataBackup[j]
}
j++
}
}
if false {
chanstr := "cases["
for i := range n.cases {
if i != 0 {
chanstr += " "
}
if n.cases[i].Chan == reflect.ValueOf(nil) {
chanstr += fmt.Sprintf("nil")
} else {
chanstr += fmt.Sprintf("%p", n.cases[i].Chan.Interface())
}
}
chanstr += "]"
n.Tracef("DATA CHANNEL %p RESTORED %s\n", n.cases, chanstr)
}
}
// RdyAll tests readiness of Node to execute.
func (n *Node) RdyAll() (rdy bool) {
if n.RdyFunc == nil {
if !n.DefaultRdyFunc() {
rdy = false
return
}
} else {
if !n.RdyFunc(n) {
rdy = false
return
}
}
rdy = true
return
}
// Fire executes Node using function pointer.
func (n *Node) Fire() error {
var err error
n.incrFireCnt()
var newFmt string
if TraceLevel > Q {
newFmt = n.traceValRdySrc(true)
}
if n.FireFunc != nil {
err = n.FireFunc(n)
} else {
/* Generic PASS */
var v interface{}
for i := range n.Srcs {
v = n.Srcs[i].SrcGet()
if len(n.Dsts) > i {
n.Dsts[i].DstPut(v)
}
}
for i := len(n.Srcs); i < len(n.Dsts); i++ {
n.Dsts[i].DstPut(v)
}
}
if TraceLevel > Q {
// newFmt += "\t"
newFmt += n.traceValRdyDst(true)
if n.Aux != nil {
var s = ""
if IsStruct(n.Aux) {
s = String(n.Aux)
if s != "{}" {
// newFmt += "\t// " + s
newFmt += " // " + s
}
} else {
if v, ok := n.Aux.(fmt.Stringer); ok {
s = v.String()
if s != "{}" {
newFmt += " // " + v.String()
}
}
}
}
newFmt += "\n"
StdoutLog.Printf(newFmt)
}
return err
}
// SendAll writes all data and acks after new result is computed.
func (n *Node) SendAll() bool {
sent := false
for i := range n.Srcs {
sent = n.Srcs[i].SendAck(n) || sent
}
for i := range n.Dsts {
sent = n.Dsts[i].SendData(n) || sent
}
return sent
}
// RecvOne reads one data or ack and decrements RdyCnt.
func (n *Node) RecvOne() (recvOK bool) {
if TraceLevel >= VVV {
n.traceValRdy(false)
}
if len(n.cases) == 0 {
return false
}
n.flag = n.flag | flagSelecting
i, recv, recvOK := reflect.Select(n.cases)
n.flag = n.flag ^ flagSelecting
if !recvOK {
n.LogError("receive from select not ok for case %d", i)
return false
}
if n.caseToEdgeDir[i].srcFlag {
srci := n.caseToEdgeDir[i].edge
srci.Val = recv.Interface()
n.RemoveInputCase(srci)
srci.srcReadHandle(n, true)
} else {
dsti := n.caseToEdgeDir[i].edge
dsti.dstReadHandle(n, true)
}
return recvOK
}
// DefaultRunFunc is the default run func.
func (n *Node) DefaultRunFunc() error {
var err error
for {
for n.RdyAll() {
if TraceLevel >= VVV {
n.traceValRdy(false)
}
err = n.Fire()
sent := n.SendAll()
if err != nil {
return err
}
if !sent {
break
} // wait for external event
// just moved inside loop so it doesn't happen
n.restoreDataChannels()
}
if !n.RecvOne() { // bad receiving shuts down go-routine
break
}
}
return nil
}
// Run is an event loop that runs forever for each Node.
func (n *Node) Run() error {
if n.RunFunc != nil {
return n.RunFunc(n)
}
err := n.DefaultRunFunc()
return err
}
// MakeNodes returns a slice of Node.
func MakeNodes(sz int) []Node {
n := make([]Node, sz)
return n
}
// extendChannelCaps extends the channel capacity to support arbitrated fan-in and buffered fan-out
func extendChannelCaps(nodes []*Node) {
// for all the nodes in the slice
for _, n := range nodes {
// for all the destination edges
for j := range n.Dsts {
dstj := n.Dsts[j]
if dstj == nil {
break
}
// if that edge has more than one upstream node and isn't a pool node
if dstj.SrcCnt() > 1 && !n.IsPool() {
// for all the data channels on that node shared with downstream nodes
for k := range *dstj.Data {
// if the capacity of that channel is less than the the number of upstream nodes
if cap((*dstj.Data)[k]) < dstj.SrcCnt() {
// create and plugin a new channel with greater capacity
StdoutLog.Printf("Multiple upstream nodes on %s (len(*dstj.Data)=%d vs dstj.SrcCnt()=%d)\n", dstj.Name, len(*dstj.Data), dstj.DstCnt())
c := func() int {
if ChannelSize == 0 {
return dstj.SrcCnt() - 1
}
return dstj.SrcCnt() * ChannelSize
}()
(*dstj.Data)[k] = make(chan interface{}, c)
}
}
}
// if that edge is fanning out
if dstj.DstCnt() > 1 {
c := func() int {
if ChannelSize == 0 {
return dstj.DstCnt() - 1
}
return dstj.DstCnt() * ChannelSize
}()
dstj.Ack = make(chan struct{}, c)
for i := 0; i < dstj.DstCnt(); i++ {
dnstream := dstj.DstNode(i)
for _, v := range dnstream.Srcs {
if v.Same(dstj) {
v.Ack = dstj.Ack
break
}
}
}
}
if false {
if len(*dstj.Data) != dstj.DstCnt() {
StdoutLog.Printf("Multiple downstream nodes on %s (len(*dstj.Data)=%d vs dstj.DstCnt()=%d) -- %v\n", dstj.Name, len(*dstj.Data), dstj.SrcCnt(), dstj.edgeNodes)
}
}
}
}
}
// clearUpstreamAcks increments RdyCnt upstream for every initialized downstream Edge
// (Node input edge) to reflect the fact that flow is initialized here.
func clearUpstreamAcks(nodes []*Node) {
for _, n := range nodes {
for j := range n.Srcs {
if n.Srcs[j] == nil {
break
}
if n.Srcs[j].Val != nil {
n.RemoveInputCase(n.Srcs[j])
}
}
for j := range n.Dsts {
if n.Dsts[j] == nil {
break
}
if n.Dsts[j].Val != nil {
ecopies := n.Dsts[j].allEdgesPlus()
// count up all downstream copies of this edge that have Val set
for _, es := range ecopies {
if !es.srcFlag && es.edge.Val != nil {
n.Dsts[j].RdyCnt++
}
}
}
}
}
}
// RunAll calls Run for each Node, and times out after RunTime.
func RunAll(nodes []Node) {
// build slice of pointers
pnodes := make([]*Node, len(nodes))
for i := range nodes {
pnodes[i] = &nodes[i]
}
runAll(pnodes)
}
// RunGraph calls Run for each *Node, and times out after RunTime.
func RunGraph(nodes []*Node) {
runAll(nodes)
}
// runAll calls Run for each Node, and times out after RunTime.
func runAll(nodes []*Node) {
extendChannelCaps(nodes)
// builds node internals after edges attached
for _, v := range nodes {
v.Init()
}
if GmlOutput || DotOutput {
if GmlOutput {
OutputGml(nodes)
} else {
OutputDot(nodes)
}
TraceLevel = QQ
return
}
clearUpstreamAcks(nodes)
if TraceLevel >= VVVV {
summarizing = true
for _, n := range nodes {
n.TraceValRdy()
}
StdoutLog.Printf("<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>\n")
summarizing = false
}
StartTime = time.Now()
var wg sync.WaitGroup
wg.Add(len(nodes))
for i := range nodes {
node := nodes[i]
go func() {
defer func() {
wg.Done()
}()
node.Run()
}()
}
timeout := RunTime
if timeout > 0 {
time.Sleep(timeout)
if TraceLevel > QQ {
defer StdoutLog.Printf("\n")
}
} else {
wg.Wait()
}
if TraceLevel >= VVV {
time.Sleep(time.Second)
if TraceLevel >= VVVV {
summarizing = true
}
StdoutLog.Printf("<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>\n")
for i := 0; i < len(nodes); i++ {
nodes[i].traceValRdy(false)
}
/*
for i := 0; i < len(nodes); i++ {
nodes[i].showCases("")
}
*/
summarizing = false
}
}
// AckWrap bundles a Node pointer, and an ack channel with an empty interface, in order to
// pass information about an upstream node downstream. Used for acking back in a Pool.
func (n *Node) AckWrap(d interface{}, ack chan struct{}) interface{} {
return ackWrap{n, d, ack}
}
// Recursed returns true if a Node from the same Pool is upstream of this Node.
func (n *Node) Recursed() bool { return n.flag&flagRecursed == flagRecursed }
// IsPool returns true if Node is part of a Pool.
func (n *Node) IsPool() bool { return n.flag&flagPool == flagPool }
// RemoveInputCase removes the input of a Node from the select switch.
// It is restored after RdyAll.
func (n *Node) RemoveInputCase(e *Edge) {
if !e.IsConst() {
n.cases[n.edgeToCase[e]].Chan = reflect.ValueOf(nil) // don't read this again until after RdyAll
}
}
// SetDotAttr set the attribute string used for outputting this node in dot format
func (n *Node) SetDotAttr(attr string) {
n.dotAttr = attr
}
// DotAttr returns the attribute string used for outputting this node in dot format
func (n *Node) DotAttr() string {
return n.dotAttr
}
// OutputDot outputs .dot graphviz format
func OutputDot(nodes []*Node) {
fmt.Printf("digraph G {\n")
fmt.Printf("graph [ ordered=\"in\" ordered=\"out\" ]\n")
for _, iv := range nodes {
fmt.Printf("\n// %s\n", iv.Name)
fmt.Printf("%sɸ%d [ label=%s %s]\n", iv.Name, iv.ID, iv.Name, iv.DotAttr())
k := 0
for _, jv := range iv.Dsts {
if jv == nil {
break
}
for _, kv := range *jv.edgeNodes {
if kv.srcFlag {
continue
}
attr := ""
l := len(*jv.dotAttrs)
if l > 0 {
attr = " " + (*jv.dotAttrs)[k%l]
}
fmt.Printf("%sɸ%d", iv.Name, iv.ID)
fmt.Printf(" -> %sɸ%d", kv.node.Name, kv.node.ID)
onm := jv.linkName()
if onm != "" {
onm = "/" + onm
}
fmt.Printf(" [ label=\"%s%s\"%s ]\n", " "+jv.Name, onm, attr)
if attr != "" {
k++
}
}
}
}
fmt.Printf("}\n")
}
// OutputGml outputs .gml graph modeling language format
func OutputGml(nodes []*Node) {
fmt.Printf("graph\n[\n")
for _, iv := range nodes {
fmt.Printf(" node\n [\n id %s_%d\n ]\n", iv.Name, iv.ID)
}
for _, iv := range nodes {
for _, jv := range iv.Dsts {
for _, kv := range *jv.edgeNodes {
if !kv.srcFlag {
fmt.Printf(" edge\n [\n source %s_%d\n", iv.Name, iv.ID)
fmt.Printf(" target %s_%d\n", kv.node.Name, kv.node.ID)
fmt.Printf(" label \"%s", jv.Name)
fmt.Printf("\"\n ]\n")
}
}
}
}
fmt.Printf("]\n")
}
// SrcCnt returns the number of source edges.
func (n *Node) SrcCnt() int {
return len(n.Srcs)
}
// DstCnt returns the number of destination edges.
func (n *Node) DstCnt() int {
return len(n.Dsts)
}
// FindSrc returns incoming edge by name
func (n *Node) FindSrc(name string) (*Edge, bool) {
i, ok := n.FindSrcIndex(name)
if !ok {
return nil, false
}
return n.Srcs[i], true
}
// FindSrcIndex returns index of incoming edge by name
func (n *Node) FindSrcIndex(name string) (int, bool) {
if n.srcIndexByName == nil {
return -1, false
}
i, ok := n.srcIndexByName[name]
return i, ok
}
// FindDst returns outgoing edge by name
func (n *Node) FindDst(name string) (*Edge, bool) {
i, ok := n.FindDstIndex(name)
if !ok {
return nil, false
}
return n.Dsts[i], true
}
// FindDstIndex returns index of outgoing edge by name
func (n *Node) FindDstIndex(name string) (int, bool) {
if n.dstIndexByName == nil {
return -1, false
}
i, ok := n.dstIndexByName[name]
return i, ok
}
// SetSrcNames names the incoming edges
func (n *Node) SetSrcNames(name ...string) {
n.srcNames = name
l := len(n.Srcs)
if n.srcIndexByName == nil {
n.srcIndexByName = make(map[string]int)
}
for i, v := range name {
if i >= l {
n.Srcs = append(n.Srcs, nil)
}
n.srcIndexByName[v] = i
}
}
// SetDstNames names the outgoing edges
func (n *Node) SetDstNames(name ...string) {
n.dstNames = name
l := len(n.Dsts)
if n.dstIndexByName == nil {
n.dstIndexByName = make(map[string]int)
}
for i, v := range name {