|
@@ -0,0 +1,187 @@
|
|
|
+package flow_test
|
|
|
+
|
|
|
+import (
|
|
|
+ flow "flow/x/flowt1"
|
|
|
+ "log"
|
|
|
+ "reflect"
|
|
|
+ "testing"
|
|
|
+
|
|
|
+ vecasm "github.com/gohxs/vec-benchmark/asm"
|
|
|
+)
|
|
|
+
|
|
|
+// Global registry
|
|
|
+
|
|
|
+func vecmul(a []float32, b []float32) []float32 {
|
|
|
+ sz := len(a)
|
|
|
+ if sz < len(b) {
|
|
|
+ sz = len(b)
|
|
|
+ }
|
|
|
+ out := make([]float32, sz)
|
|
|
+
|
|
|
+ vecasm.VecMulf32x8(a, b, out)
|
|
|
+
|
|
|
+ return out
|
|
|
+}
|
|
|
+
|
|
|
+type Iterate struct {
|
|
|
+ i float32
|
|
|
+}
|
|
|
+
|
|
|
+func (it *Iterate) generate() []float32 {
|
|
|
+ it.i++
|
|
|
+ return []float32{it.i, it.i}
|
|
|
+}
|
|
|
+
|
|
|
+func init() {
|
|
|
+ it := &Iterate{}
|
|
|
+
|
|
|
+ flow.Register("vecmul", vecmul)
|
|
|
+ flow.Register("iter", it.generate)
|
|
|
+}
|
|
|
+
|
|
|
+func TestImplementation(t *testing.T) {
|
|
|
+ f := flow.New("myapp")
|
|
|
+ f.SetParam("in", []float32{2, 2, 2, 2})
|
|
|
+
|
|
|
+ op1 := f.Op("vecmul", []float32{1, 2, 3, 4}, []float32{1, 2, 3, 4})
|
|
|
+ op2 := f.Op("vecmul", f.Param("in"), op1)
|
|
|
+
|
|
|
+ res := f.Run(op1, op2)
|
|
|
+ t.Log("Res:", res)
|
|
|
+
|
|
|
+ test := []interface{}{
|
|
|
+ []float32{1, 4, 9, 16},
|
|
|
+ []float32{2, 8, 18, 32},
|
|
|
+ }
|
|
|
+ if !reflect.DeepEqual(test, res) {
|
|
|
+ t.Fatal("Arrays does not match:", test, res)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestChaining(t *testing.T) {
|
|
|
+ f1 := flow.New("flow1")
|
|
|
+ // Set the main output to operation
|
|
|
+ // Simple 2 const arrays
|
|
|
+ f1.SetOutput(f1.Op("vecmul", []float32{1, 2, 3}, []float32{1, 2, 3}))
|
|
|
+
|
|
|
+ f2 := flow.New("flow2")
|
|
|
+ f2.SetParam("p1", []float32{0.5, 0.5, 0.5})
|
|
|
+
|
|
|
+ // Multiply flow1 with flow2 parameter 'p1'
|
|
|
+ op := f2.Op("vecmul", f1, f2.Param("p1"))
|
|
|
+ res := f2.Run(op)
|
|
|
+
|
|
|
+ test := []interface{}{
|
|
|
+ []float32{0.5, 2, 4.5},
|
|
|
+ }
|
|
|
+ if !reflect.DeepEqual(test, res) {
|
|
|
+ t.Fatal("Arrays does not match:", test, res)
|
|
|
+
|
|
|
+ }
|
|
|
+ t.Log("Multiplied 2 flows", res)
|
|
|
+}
|
|
|
+
|
|
|
+func TestParam(t *testing.T) {
|
|
|
+ f := flow.New("flow1")
|
|
|
+ f.SetOutput(f.Op("vecmul", f.Param("in"), []float32{1, 2, 3}))
|
|
|
+
|
|
|
+ {
|
|
|
+ f.SetParam("in", []float32{2, 2, 2})
|
|
|
+ res := f.Run()
|
|
|
+ t.Log("Result:", res)
|
|
|
+ test := []interface{}{
|
|
|
+ []float32{2, 4, 6},
|
|
|
+ }
|
|
|
+ if !reflect.DeepEqual(res, test) {
|
|
|
+ t.Fatal("Result mismatch")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ {
|
|
|
+ f.SetParam("in", []float32{0.5, 0.5, 0.5})
|
|
|
+ res := f.Run()
|
|
|
+ t.Log("Result2:", res)
|
|
|
+
|
|
|
+ test := []interface{}{
|
|
|
+ []float32{0.5, 1.0, 1.5},
|
|
|
+ }
|
|
|
+ if !reflect.DeepEqual(res, test) {
|
|
|
+ t.Fatal("Result mismatch")
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestIterate(t *testing.T) {
|
|
|
+ f := flow.New("Myapp")
|
|
|
+ it := f.Op("iter")
|
|
|
+ f.SetOutput(f.Op("vecmul", f.Op("vecmul", it, it), it))
|
|
|
+ res := f.Run()
|
|
|
+ t.Log("Res:", res)
|
|
|
+ res = f.Run()
|
|
|
+ t.Log("Res2:", res)
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+/*func TestRecurse(t *testing.T) {
|
|
|
+ f := flow.New("flow1")
|
|
|
+ f.Op("vecmul", f, []float32{1, 2, 3, 4})
|
|
|
+ t.Log("F result:", f.Run())
|
|
|
+}*/
|
|
|
+
|
|
|
+func TestString(t *testing.T) {
|
|
|
+ f := flow.New("Myapp")
|
|
|
+ f.SetParam("vec1", []float32{1, 2, 3})
|
|
|
+
|
|
|
+ f2 := flow.New("flow2")
|
|
|
+ f2.SetParam("vec2", []float32{2, 2, 2})
|
|
|
+
|
|
|
+ m := f.Op("vecmul", []float32{1, 2, 3}, f.Param("vec1")) // Result 1,4,9
|
|
|
+ f.SetOutput(f.Op("vecmul", m, []float32{2, 2, 2})) // Result 2,8,18
|
|
|
+
|
|
|
+ f2.SetOutput(
|
|
|
+ f2.Op("vecmul", f2.Param("vec2"), f), // Result 4,16,36
|
|
|
+ )
|
|
|
+
|
|
|
+ res := f2.Run()
|
|
|
+ t.Log("Result:", res)
|
|
|
+ t.Log("Flow:\n", f2)
|
|
|
+}
|
|
|
+
|
|
|
+func TestSerialize(t *testing.T) {
|
|
|
+ f := flow.New("Myapp")
|
|
|
+
|
|
|
+ //v := f.Variable([]float32{1})
|
|
|
+
|
|
|
+ f.SetParam("vec1", []float32{1, 2, 3})
|
|
|
+
|
|
|
+ c := f.Op("const", []float32{3, 3, 3})
|
|
|
+
|
|
|
+ m1 := f.Op("vecmul", []float32{2, 2, 2}, f.Param("vec1"))
|
|
|
+
|
|
|
+ // Two operations
|
|
|
+ v1 := f.Op("vecmul", c, m1)
|
|
|
+ v2 := f.Op("vecmul", c, m1)
|
|
|
+
|
|
|
+ l := f.Op("vecmul", v2, v1)
|
|
|
+
|
|
|
+ f.SetOutput(l)
|
|
|
+ if f.Err != nil {
|
|
|
+ t.Fatal("Err:", f.Err)
|
|
|
+ }
|
|
|
+
|
|
|
+ log.Println("json:", f.ToJSON())
|
|
|
+
|
|
|
+ /*f2 := flow.New("flow2")
|
|
|
+ f2.SetParam("vec2", []float32{2, 2, 2})
|
|
|
+
|
|
|
+ m := f.Op("vecmul", []float32{1, 2, 3}, f.Param("vec1")) // Result 1,4,9
|
|
|
+ f.SetOutput(f.Op("vecmul", m, []float32{2, 2, 2})) // Result 2,8,18
|
|
|
+
|
|
|
+ f2.SetOutput(
|
|
|
+ f2.Op("vecmul", f2.Param("vec2"), f), // Result 4,16,36
|
|
|
+ )
|
|
|
+
|
|
|
+ r := f2.ToJSON()
|
|
|
+
|
|
|
+ t.Log("T:\n", r)*/
|
|
|
+
|
|
|
+}
|