Skip to content

Commit

Permalink
Merge branch 'master' of github.com:gorgonia/gorgonia
Browse files Browse the repository at this point in the history
  • Loading branch information
owulveryck committed Oct 10, 2019
2 parents b9b352c + d650d30 commit 251ea02
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions x/vm/vm_gomachine.go
Expand Up @@ -2,6 +2,7 @@ package xvm

import (
"log"
"sync"

"gorgonia.org/gorgonia"
)
Expand Down Expand Up @@ -44,10 +45,10 @@ func (g *GoMachine) RunAll() error {
log.Fatal("chan edge not found")
}
}
go g.opWorker(currentNode, inputC, outputC)
go opWorker(currentNode, inputC, outputC)
// Send the input to the self nodes...
case currentNode.Op() == nil && currentNode.Value() != nil:
go g.valueFeeder(currentNode, outputC)
go valueFeeder(currentNode, outputC)
default:
log.Fatal("Yerk?")
}
Expand Down Expand Up @@ -80,25 +81,64 @@ func NewGoMachine(g *gorgonia.ExprGraph) *GoMachine {
}
}

func (g *GoMachine) opWorker(n *gorgonia.Node, inputC []<-chan gorgonia.Value, outputC []chan<- gorgonia.Value) {
func opWorker(n *gorgonia.Node, inputC []<-chan gorgonia.Value, outputC []chan<- gorgonia.Value) {
vals := make([]gorgonia.Value, len(inputC))
var wg sync.WaitGroup
wg.Add(len(inputC))
for i := range inputC {
vals[i] = <-inputC[i]
go func(i int, vals []gorgonia.Value, inputC []<-chan gorgonia.Value) {
vals[i] = <-inputC[i]
wg.Done()
}(i, vals, inputC)
}
wg.Wait()
output, err := n.Op().Do(vals...)
if err != nil {
log.Fatal(err)
}
gorgonia.UnsafeLet(n, output)
wg.Add(len(outputC))
for i := range outputC {
outputC[i] <- output
go func(i int, outputC []chan<- gorgonia.Value) {
outputC[i] <- output
wg.Done()
}(i, outputC)
}
wg.Wait()
}

func (g *GoMachine) valueFeeder(n *gorgonia.Node, feedC []chan<- gorgonia.Value) {
func valueFeeder(n *gorgonia.Node, feedC []chan<- gorgonia.Value) {
var wg sync.WaitGroup
wg.Add(len(feedC))
for i := range feedC {
feedC[i] <- n.Value()
go func(i int, feedC []chan<- gorgonia.Value) {
feedC[i] <- n.Value()
}(i, feedC)
}
wg.Wait()
}

func (g *GoMachine) populateChanDB() error {
edgesIt := g.g.Edges()
for edgesIt.Next() {
currentEdge := edgesIt.Edge()
head := currentEdge.From().ID()
tail := currentEdge.To().ID()
g.db.upsert(make(chan gorgonia.Value, 0), tail, head)
}
nodesIt := g.g.Nodes()
for nodesIt.Next() {
currentNode := nodesIt.Node().(*gorgonia.Node)
if g.g.From(currentNode.ID()).Len() == 0 {
// Node is an input
g.db.upsert(make(chan gorgonia.Value, 0), currentNode.ID(), g.db.inputNodeID)
}
if g.g.To(currentNode.ID()).Len() == 0 {
// Node is an output
g.db.upsert(make(chan gorgonia.Value, 0), g.db.outputNodeID, currentNode.ID())
}
}
return nil
}

func (g *GoMachine) populateChanDB() error {
Expand Down

0 comments on commit 251ea02

Please sign in to comment.