github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/internal/asm/f32/ge_test.go (about) 1 // Copyright ©2017 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package f32_test 6 7 import ( 8 "fmt" 9 "testing" 10 11 . "github.com/jingcheng-WU/gonum/internal/asm/f32" 12 ) 13 14 var gerTests = []struct { 15 x, y, a []float32 16 want []float32 17 }{ // m x n ( kernels executed ) 18 { // 1 x 1 (1x1) 19 x: []float32{2}, 20 y: []float32{4.4}, 21 a: []float32{10}, 22 want: []float32{18.8}, 23 }, 24 { // 3 x 2 ( 2x2, 1x2 ) 25 x: []float32{-2, -3, 0}, 26 y: []float32{-1.1, 5}, 27 a: []float32{ 28 1.3, 2.4, 29 2.6, 2.8, 30 -1.3, -4.3, 31 }, 32 want: []float32{3.5, -7.6, 5.9, -12.2, -1.3, -4.3}, 33 }, 34 { // 3 x 3 ( 2x2, 2x1, 1x2, 1x1 ) 35 x: []float32{-2, 7, 12}, 36 y: []float32{-1.1, 0, 6}, 37 a: []float32{ 38 1.3, 2.4, 3.5, 39 2.6, 2.8, 3.3, 40 -1.3, -4.3, -9.7, 41 }, 42 want: []float32{3.5, 2.4, -8.5, -5.1, 2.8, 45.3, -14.5, -4.3, 62.3}, 43 }, 44 { // 5 x 3 ( 4x2, 4x1, 1x2, 1x1 ) 45 x: []float32{-2, -3, 0, 1, 2}, 46 y: []float32{-1.1, 5, 0}, 47 a: []float32{ 48 1.3, 2.4, 3.5, 49 2.6, 2.8, 3.3, 50 -1.3, -4.3, -9.7, 51 8, 9, -10, 52 -12, -14, -6, 53 }, 54 want: []float32{3.5, -7.6, 3.5, 5.9, -12.2, 3.3, -1.3, -4.3, -9.7, 6.9, 14, -10, -14.2, -4, -6}, 55 }, 56 { // 3 x 6 ( 2x4, 2x2, 1x4, 1x2 ) 57 x: []float32{-2, -3, 0}, 58 y: []float32{-1.1, 5, 0, 9, 19, 22}, 59 a: []float32{ 60 1.3, 2.4, 3.5, 4.8, 1.11, -9, 61 2.6, 2.8, 3.3, -3.4, 6.2, -8.7, 62 -1.3, -4.3, -9.7, -3.1, 8.9, 8.9, 63 }, 64 want: []float32{3.5, -7.6, 3.5, -13.2, -36.89, -53, 5.9, -12.2, 3.3, -30.4, -50.8, -74.7, -1.3, -4.3, -9.7, -3.1, 8.9, 8.9}, 65 }, 66 { // 5 x 5 ( 4x4, 4x1, 1x4, 1x1) 67 x: []float32{-2, 0, 2, 0, 7}, 68 y: []float32{-1.1, 8, 7, 3, 5}, 69 a: []float32{ 70 1.3, 2.4, 3.5, 2.2, 8.3, 71 2.6, 2.8, 3.3, 4.4, -1.5, 72 -1.3, -4.3, -9.7, -8.8, 6.2, 73 8, 9, -10, -11, 12, 74 -12, -14, -6, -2, 4, 75 }, 76 want: []float32{ 77 3.5, -13.6, -10.5, -3.8, -1.7, 78 2.6, 2.8, 3.3, 4.4, -1.5, 79 -3.5, 11.7, 4.3, -2.8, 16.2, 80 8, 9, -10, -11, 12, 81 -19.700000000000003, 42, 43, 19, 39, 82 }, 83 }, 84 { // 7 x 7 ( 4x4, 4x2, 4x1, 2x4, 2x2, 2x1, 1x4, 1x2, 1x1 ) < nan test > 85 x: []float32{-2, 8, 9, -3, -1.2, 5, 4.5}, 86 y: []float32{-1.1, nan, 19, 11, -9.22, 7, 3.3}, 87 a: []float32{ 88 1.3, 2.4, 3.5, 4.8, 1.11, -9, 2.2, 89 2.6, 2.8, 3.3, -3.4, 6.2, -8.7, 5.1, 90 -1.3, -4.3, -9.7, -3.1, 8.9, 8.9, 8, 91 5, -2.5, 1.8, -3.6, 2.8, 4.9, 7, 92 -1.3, -4.3, -9.7, -3.1, 8.9, 8.9, 8, 93 2.6, 2.8, 3.3, -3.4, 6.2, -8.7, 5.1, 94 1.3, 2.4, 3.5, 4.8, 1.11, -9, 2.2, 95 }, 96 want: []float32{ 97 3.5, nan, -34.5, -17.2, 19.55, -23, -4.4, 98 -6.2, nan, 155.3, 84.6, -67.56, 47.3, 31.5, 99 -11.2, nan, 161.3, 95.9, -74.08, 71.9, 37.7, 100 8.3, nan, -55.2, -36.6, 30.46, -16.1, -2.9, 101 0.02, nan, -32.5, -16.3, 19.964, 0.5, 4.04, 102 -2.9, nan, 98.3, 51.6, -39.9, 26.3, 21.6, 103 -3.65, nan, 89, 54.3, -40.38, 22.5, 17.05, 104 }, 105 }, 106 { // 15 x 15 ( 4x8 4x4, 4x2, 4x1, 2x8, 2x4, 2x2, 2x1, 1x8, 1x4, 1x2, 1x1 ) < nan test > 107 x: []float32{6.2, -5, 88.68, 43.4, -30.5, -40.2, 19.9, 3, 19.9, -40.2, -30.5, 43.4, 88.68, -5, 6.2}, 108 y: []float32{1.5, 21.7, -28.7, -11.9, 18.1, 3.1, 21, 8, 21, 3.1, 18.1, -11.9, -28.7, 21.7, 1.5}, 109 a: []float32{ 110 -20.5, 17.1, -8.4, -23.8, 3.9, 7.7, 6.25, 2.9, -0.29, 25.6, -9.4, 36.5, 9.7, 2.3, 4.1, 111 -34.1, 10.3, 4.5, -42.05, 9.4, 4, 19.2, 9.8, -32.7, 4.1, 4.4, -22.5, -7.8, 3.6, -24.5, 112 21.7, 8.6, -13.82, 3.05, -2.29, 39.4, -40, 7.9, -2.5, -7.7, 18.1, -25.5, -18.5, 43.2, 2.1, 113 -20.5, 17.1, -8.4, -23.8, 3.9, 7.7, 6.25, 2.9, -0.29, 25.6, -9.4, 36.5, 9.7, 2.3, 4.1, 114 -34.1, 10.3, 4.5, -42.05, 9.4, 4, 19.2, 9.8, -32.7, 4.1, 4.4, -22.5, -7.8, 3.6, -24.5, 115 21.7, 8.6, -13.82, 3.05, -2.29, 39.4, -40, 7.9, -2.5, -7.7, 18.1, -25.5, -18.5, 43.2, 2.1, 116 21.7, 8.6, -13.82, 3.05, -2.29, 39.4, -40, 7.9, -2.5, -7.7, 18.1, -25.5, -18.5, 43.2, 2.1, 117 -34.1, 10.3, 4.5, -42.05, 9.4, 4, 19.2, 9.8, -32.7, 4.1, 4.4, -22.5, -7.8, 3.6, -24.5, 118 -20.5, 17.1, -8.4, -23.8, 3.9, 7.7, 6.25, 2.9, -0.29, 25.6, -9.4, 36.5, 9.7, 2.3, 4.1, 119 21.7, 8.6, -13.82, 3.05, -2.29, 39.4, -40, 7.9, -2.5, -7.7, 18.1, -25.5, -18.5, 43.2, 2.1, 120 -34.1, 10.3, 4.5, -42.05, 9.4, 4, 19.2, 9.8, -32.7, 4.1, 4.4, -22.5, -7.8, 3.6, -24.5, 121 -20.5, 17.1, -8.4, -23.8, 3.9, 7.7, 6.25, 2.9, -0.29, 25.6, -9.4, 36.5, 9.7, 2.3, 4.1, 122 -20.5, 17.1, -8.4, -23.8, 3.9, 7.7, 6.25, 2.9, -0.29, 25.6, -9.4, 36.5, 9.7, 2.3, 4.1, 123 21.7, 8.6, -13.82, 3.05, -2.29, 39.4, -40, 7.9, -2.5, -7.7, 18.1, -25.5, -18.5, 43.2, 2.1, 124 -34.1, 10.3, 4.5, -42.05, 9.4, 4, 19.2, 9.8, -32.7, 4.1, 4.4, -22.5, -7.8, 3.6, -24.5, 125 }, 126 want: []float32{ 127 -11.200001, 151.64, -186.34, -97.58, 116.12, 26.919998, 136.45, 52.5, 129.91, 44.82, 102.82, -37.28, -168.24, 136.84, 13.4, 128 -41.6, -98.2, 148, 17.45, -81.1, -11.5, -85.8, -30.2, -137.7, -11.4, -86.1, 37, 135.7, -104.9, -32, 129 154.72, 1932.956, -2558.936, -1052.242, 1602.818, 314.30798, 1822.28, 717.34, 1859.78, 267.20798, 1623.208, -1080.792, -2563.616, 1967.556, 135.12001, 130 44.600006, 958.88007, -1253.9801, -540.26, 789.44006, 142.23999, 917.65, 350.1, 911.11005, 160.14, 776.14, -479.96002, -1235.8801, 944.0801, 69.200005, 131 -79.85, -651.55005, 879.85004, 320.9, -542.64996, -90.549995, -621.3, -234.2, -673.2, -90.45, -547.64996, 340.44998, 867.55005, -658.25006, -70.25, 132 -38.600002, -863.74005, 1139.9202, 481.43, -729.91003, -85.21999, -884.2, -313.7, -846.7, -132.31999, -709.5201, 452.88, 1135.2401, -829.14, -58.200005, 133 51.55, 440.43002, -584.95, -233.75998, 357.9, 101.09, 377.9, 167.09999, 415.4, 53.989998, 378.29, -262.31, -589.63, 475.03003, 31.949999, 134 -29.599998, 75.40001, -81.600006, -77.75, 63.700005, 13.299999, 82.2, 33.8, 30.3, 13.4, 58.700005, -58.199997, -93.90001, 68.700005, -20, 135 9.349998, 448.93002, -579.53, -260.61, 364.09, 69.39, 424.15, 162.09999, 417.61, 87.29, 350.79, -200.30998, -561.43, 434.13, 33.949997, 136 -38.600002, -863.74005, 1139.9202, 481.43, -729.91003, -85.21999, -884.2, -313.7, -846.7, -132.31999, -709.5201, 452.88, 1135.2401, -829.14, -58.200005, 137 -79.85, -651.55005, 879.85004, 320.9, -542.64996, -90.549995, -621.3, -234.2, -673.2, -90.45, -547.64996, 340.44998, 867.55005, -658.25006, -70.25, 138 44.600006, 958.88007, -1253.9801, -540.26, 789.44006, 142.23999, 917.65, 350.1, 911.11005, 160.14, 776.14, -479.96002, -1235.8801, 944.0801, 69.200005, 139 112.520004, 1941.456, -2553.5159, -1079.092, 1609.008, 282.608, 1868.53, 712.34, 1861.99, 300.508, 1595.708, -1018.792, -2535.416, 1926.6561, 137.12001, 140 14.200001, -99.9, 129.68, 62.55, -92.79, 23.900002, -145, -32.1, -107.5, -23.2, -72.4, 34, 125, -65.3, -5.4, 141 -24.8, 144.84, -173.44, -115.83, 121.62, 23.22, 149.4, 59.399998, 97.5, 23.32, 116.62, -96.28, -185.74, 138.14, -15.200001, 142 }, 143 }, 144 } 145 146 func TestGer(t *testing.T) { 147 const ( 148 tol = 1e-6 149 150 xGdVal, yGdVal, aGdVal = -0.5, 1.5, 10 151 gdLn = 4 152 ) 153 for i, test := range gerTests { 154 m, n := len(test.x), len(test.y) 155 for _, align := range align2 { 156 prefix := fmt.Sprintf("Test %v (%vx%v) align(x:%v,y:%v,a:%v)", 157 i, m, n, align.x, align.y, align.x^align.y) 158 xgLn, ygLn, agLn := gdLn+align.x, gdLn+align.y, gdLn+align.x^align.y 159 xg, yg := guardVector(test.x, xGdVal, xgLn), guardVector(test.y, yGdVal, ygLn) 160 x, y := xg[xgLn:len(xg)-xgLn], yg[ygLn:len(yg)-ygLn] 161 ag := guardVector(test.a, aGdVal, agLn) 162 a := ag[agLn : len(ag)-agLn] 163 164 var alpha float32 = 1.0 165 Ger(uintptr(m), uintptr(n), alpha, x, 1, y, 1, a, uintptr(n)) 166 for i := range test.want { 167 if !sameApprox(a[i], test.want[i], tol) { 168 t.Errorf(msgVal, prefix, i, a[i], test.want[i]) 169 return 170 } 171 } 172 if !isValidGuard(xg, xGdVal, xgLn) { 173 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:]) 174 } 175 if !isValidGuard(yg, yGdVal, ygLn) { 176 t.Errorf(msgGuard, prefix, "y", yg[:ygLn], yg[len(yg)-ygLn:]) 177 } 178 if !isValidGuard(ag, aGdVal, agLn) { 179 t.Errorf(msgGuard, prefix, "a", ag[:agLn], ag[len(ag)-agLn:]) 180 t.Errorf(msgReadOnly, prefix, "x") 181 } 182 if !sameStrided(test.y, y, 1) { 183 t.Errorf(msgReadOnly, prefix, "y") 184 } 185 } 186 187 for _, inc := range newIncSet(1, 2) { 188 prefix := fmt.Sprintf("Test %v (%vx%v) inc(x:%v,y:%v)", i, m, n, inc.x, inc.y) 189 xg := guardIncVector(test.x, xGdVal, inc.x, gdLn) 190 yg := guardIncVector(test.y, yGdVal, inc.y, gdLn) 191 x, y := xg[gdLn:len(xg)-gdLn], yg[gdLn:len(yg)-gdLn] 192 ag := guardVector(test.a, aGdVal, gdLn) 193 a := ag[gdLn : len(ag)-gdLn] 194 195 var alpha float32 = 3.5 196 Ger(uintptr(m), uintptr(n), alpha, 197 x, uintptr(inc.x), 198 y, uintptr(inc.y), 199 a, uintptr(n)) 200 for i := range test.want { 201 want := alpha*test.x[i/n]*test.y[i%n] + test.a[i] 202 if !sameApprox(a[i], want, tol) { 203 t.Errorf(msgVal, prefix, i, a[i], want) 204 } 205 } 206 checkValidIncGuard(t, xg, xGdVal, inc.x, gdLn) 207 checkValidIncGuard(t, yg, yGdVal, inc.y, gdLn) 208 if !isValidGuard(ag, aGdVal, gdLn) { 209 t.Errorf(msgGuard, prefix, "a", ag[:gdLn], ag[len(ag)-gdLn:]) 210 } 211 if !sameStrided(test.x, x, inc.x) { 212 t.Errorf(msgReadOnly, prefix, "x") 213 } 214 if !sameStrided(test.y, y, inc.y) { 215 t.Errorf(msgReadOnly, prefix, "y") 216 } 217 } 218 } 219 } 220 221 func BenchmarkGer(t *testing.B) { 222 const alpha = 3 223 for _, dims := range newIncSet(3, 10, 30, 100, 300, 1e3, 3e3, 1e4) { 224 m, n := dims.x, dims.y 225 if m/n >= 100 || n/m >= 100 { 226 continue 227 } 228 for _, inc := range newIncSet(1, 3, 4, 10) { 229 t.Run(fmt.Sprintf("Dger %dx%d (%d %d)", m, n, inc.x, inc.y), func(b *testing.B) { 230 x, y, a := gerData(m, n, inc.x, inc.y) 231 b.ResetTimer() 232 for i := 0; i < b.N; i++ { 233 Ger(uintptr(m), uintptr(n), alpha, 234 x, uintptr(inc.x), 235 y, uintptr(inc.y), 236 a, uintptr(n)) 237 } 238 }) 239 240 } 241 } 242 } 243 244 func gerData(m, n, incX, incY int) (x, y, a []float32) { 245 x = make([]float32, m*incX) 246 y = make([]float32, n*incY) 247 a = make([]float32, m*n) 248 ln := len(x) 249 if len(y) > ln { 250 ln = len(y) 251 } 252 if len(a) > ln { 253 ln = len(a) 254 } 255 for i := 0; i < ln; i++ { 256 v := float32(i) 257 if i < len(a) { 258 a[i] = v 259 } 260 if i < len(x) { 261 x[i] = v 262 } 263 if i < len(y) { 264 y[i] = v 265 } 266 } 267 return x, y, a 268 }