|
| 1 | +package ndarray |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/go-ndarray/ndarray/internal/kernels" |
| 7 | +) |
| 8 | + |
| 9 | +// TestBroadcastRepeat checks the streamed broadcast (a full operand and a |
| 10 | +// repeated one, see repeatOperands) against an element-by-element oracle, for |
| 11 | +// the non-commutative ops in both operand orders, several suffix shapes, a |
| 12 | +// repeated operand too short to stream, and with the block loop forced onto |
| 13 | +// several cores. |
| 14 | +func TestBroadcastRepeat(t *testing.T) { |
| 15 | + grid := func(seed int, shape ...int) *Array { |
| 16 | + n := 1 |
| 17 | + for _, d := range shape { |
| 18 | + n *= d |
| 19 | + } |
| 20 | + d := make([]float64, n) |
| 21 | + for i := range d { |
| 22 | + d[i] = float64((i*13+seed)%29) - 14.5 |
| 23 | + } |
| 24 | + return mustArr(t, ok(FromData(d, shape...))) |
| 25 | + } |
| 26 | + cases := []struct { |
| 27 | + name string |
| 28 | + full, rep *Array |
| 29 | + streamable bool |
| 30 | + }{ |
| 31 | + {"matrix, row", grid(1, 9, 70), grid(2, 1, 70), true}, |
| 32 | + {"matrix, 1-D", grid(3, 5, 80), grid(4, 80), true}, |
| 33 | + {"3-D, 2-D", grid(5, 4, 3, 30), grid(6, 3, 30), true}, |
| 34 | + {"3-D, row of 1-D", grid(7, 2, 3, 100), grid(8, 1, 1, 100), true}, |
| 35 | + {"short row", grid(9, 6, 10), grid(10, 1, 10), false}, |
| 36 | + } |
| 37 | + ops := []struct { |
| 38 | + name string |
| 39 | + f func(x, y *Array) (*Array, error) |
| 40 | + g func(x, y float64) float64 |
| 41 | + }{ |
| 42 | + {"sub", (*Array).Sub, func(x, y float64) float64 { return x - y }}, |
| 43 | + {"div", (*Array).Div, func(x, y float64) float64 { return x / y }}, |
| 44 | + } |
| 45 | + run := func() { |
| 46 | + for _, c := range cases { |
| 47 | + if _, _, _, got := repeatOperands(c.full, c.rep, c.full.shape); got != c.streamable { |
| 48 | + t.Errorf("%s: streamable = %v, want %v", c.name, got, c.streamable) |
| 49 | + } |
| 50 | + for _, op := range ops { |
| 51 | + for _, swap := range []bool{false, true} { |
| 52 | + x, y := c.full, c.rep |
| 53 | + if swap { |
| 54 | + x, y = y, x |
| 55 | + } |
| 56 | + got, err := op.f(x, y) |
| 57 | + if err != nil { |
| 58 | + t.Fatal(err) |
| 59 | + } |
| 60 | + if !sameShape(got.shape, c.full.shape) { |
| 61 | + t.Fatalf("%s %s: shape %v", c.name, op.name, got.shape) |
| 62 | + } |
| 63 | + m := c.rep.Size() |
| 64 | + for i, v := range got.data { |
| 65 | + fv, rv := c.full.data[i], c.rep.data[i%m] |
| 66 | + want := op.g(fv, rv) |
| 67 | + if swap { |
| 68 | + want = op.g(rv, fv) |
| 69 | + } |
| 70 | + if v != want { |
| 71 | + t.Fatalf("%s %s swap=%v [%d]: %v, want %v", c.name, op.name, swap, i, v, want) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + run() |
| 79 | + saved := kernels.ParThreshold |
| 80 | + kernels.ParThreshold = 64 |
| 81 | + defer func() { kernels.ParThreshold = saved }() |
| 82 | + run() |
| 83 | +} |
0 commit comments