Skip to content

Instantly share code, notes, and snippets.

@ksato9700
Created May 19, 2012 14:17
Show Gist options
  • Save ksato9700/2730965 to your computer and use it in GitHub Desktop.
Save ksato9700/2730965 to your computer and use it in GitHub Desktop.
Systolic array simulator implementation in Go
package main
import "fmt"
const SIZE = 3
//
// CalcCell
//
type CalcCell struct {
ain, bin, aout, bout, cout chan int
c int
name string
}
func (cell *CalcCell) init (name string, aout, bout chan int) {
cell.name = name
cell.ain = make (chan int, SIZE)
cell.bin = make (chan int, SIZE)
cell.aout = aout
cell.bout = bout
cell.cout = make (chan int)
cell.c = 0
}
func (cell *CalcCell) run () {
for true {
a := <- cell.ain
b := <- cell.bin
cell.aout <- a
cell.bout <- b
if a <0 || b <0 {
cell.cout <- cell.c
break
} else {
cell.c += a * b
}
}
}
func (cell *CalcCell) String() string {
return fmt.Sprintf("%s: %d", cell.name, <- cell.cout)
}
//
// functions
//
func input (in []int, out chan int) {
for _, v := range in {
out <- v
}
out <- -1
}
func generate_cell(name string, aout, bout chan int) *CalcCell {
cell := new (CalcCell)
cell.init(name, aout, bout)
go cell.run()
return cell
}
//
// main
//
func main() {
term := make (chan int, 100)
c33 := generate_cell ("c33", term , term )
c32 := generate_cell ("c32", c33.ain, term )
c23 := generate_cell ("c23", term , c33.bin)
c22 := generate_cell ("c22", c23.ain, c32.bin)
c31 := generate_cell ("c31", c32.ain, term )
c13 := generate_cell ("c13", term , c23.bin)
c21 := generate_cell ("c21", c22.ain, c31.bin)
c12 := generate_cell ("c12", c13.ain, c22.bin)
c11 := generate_cell ("c11", c12.ain, c21.bin)
go input ([]int{3,2,3}, c11.ain)
go input ([]int{4,5,2}, c21.ain)
go input ([]int{2,3,5}, c31.ain)
go input ([]int{3,4,2}, c11.bin)
go input ([]int{2,5,3}, c12.bin)
go input ([]int{3,2,5}, c13.bin)
fmt.Println(c11)
fmt.Println(c12)
fmt.Println(c13)
fmt.Println(c21)
fmt.Println(c22)
fmt.Println(c23)
fmt.Println(c31)
fmt.Println(c32)
fmt.Println(c33)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment