simd_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package cpu
  2. import (
  3. "math/rand"
  4. "testing"
  5. )
  6. func TestDotFloat32MatchesScalar(t *testing.T) {
  7. a := []float32{1, 2, 3, 4, 5, 6, 7, 8}
  8. b := []float32{8, 7, 6, 5, 4, 3, 2, 1}
  9. got := DotFloat32(a, b)
  10. want := dotFloat32Scalar(a, b)
  11. if diff := absDiff(got, want); diff > 1e-5 {
  12. t.Fatalf("dot mismatch: got %f want %f (diff %f)", got, want, diff)
  13. }
  14. }
  15. func TestDotFloat32ZeroLen(t *testing.T) {
  16. got := DotFloat32(nil, nil)
  17. if got != 0 {
  18. t.Fatalf("expected 0 for empty slices, got %f", got)
  19. }
  20. }
  21. func BenchmarkDotFloat32(b *testing.B) {
  22. size := 256
  23. a := make([]float32, size)
  24. bb := make([]float32, size)
  25. for i := 0; i < size; i++ {
  26. a[i] = rand.Float32()
  27. bb[i] = rand.Float32()
  28. }
  29. b.ReportAllocs()
  30. b.SetBytes(int64(size * 4 * 2))
  31. b.ResetTimer()
  32. for i := 0; i < b.N; i++ {
  33. _ = DotFloat32(a, bb)
  34. }
  35. }
  36. func BenchmarkDotFloat32Scalar(b *testing.B) {
  37. size := 256
  38. a := make([]float32, size)
  39. bb := make([]float32, size)
  40. for i := 0; i < size; i++ {
  41. a[i] = rand.Float32()
  42. bb[i] = rand.Float32()
  43. }
  44. b.ReportAllocs()
  45. b.SetBytes(int64(size * 4 * 2))
  46. b.ResetTimer()
  47. for i := 0; i < b.N; i++ {
  48. _ = dotFloat32Scalar(a, bb)
  49. }
  50. }
  51. func absDiff(a, b float32) float32 {
  52. if a > b {
  53. return a - b
  54. }
  55. return b - a
  56. }
  57. func TestAxpy(t *testing.T) {
  58. x := []float32{1, 2, 3}
  59. y := []float32{4, 5, 6}
  60. Axpy(2, x, y)
  61. want := []float32{6, 9, 12}
  62. for i := range y {
  63. if diff := absDiff(y[i], want[i]); diff > 1e-6 {
  64. t.Fatalf("axpy mismatch at %d: got %f want %f", i, y[i], want[i])
  65. }
  66. }
  67. }
  68. func BenchmarkDotFloat32Ptr(b *testing.B) {
  69. size := 256
  70. a := make([]float32, size)
  71. bb := make([]float32, size)
  72. for i := 0; i < size; i++ {
  73. a[i] = rand.Float32()
  74. bb[i] = rand.Float32()
  75. }
  76. b.ReportAllocs()
  77. b.SetBytes(int64(size * 4 * 2))
  78. b.ResetTimer()
  79. for i := 0; i < b.N; i++ {
  80. _ = DotFloat32Ptr(&a[0], &bb[0], size)
  81. }
  82. }
  83. func BenchmarkAxpy(b *testing.B) {
  84. size := 256
  85. x := make([]float32, size)
  86. y := make([]float32, size)
  87. for i := 0; i < size; i++ {
  88. x[i] = rand.Float32()
  89. y[i] = rand.Float32()
  90. }
  91. alpha := float32(0.7)
  92. b.ReportAllocs()
  93. b.SetBytes(int64(size * 4 * 2))
  94. b.ResetTimer()
  95. for i := 0; i < b.N; i++ {
  96. Axpy(alpha, x, y)
  97. }
  98. }
  99. func BenchmarkAxpyPtr(b *testing.B) {
  100. size := 256
  101. x := make([]float32, size)
  102. y := make([]float32, size)
  103. for i := 0; i < size; i++ {
  104. x[i] = rand.Float32()
  105. y[i] = rand.Float32()
  106. }
  107. alpha := float32(0.7)
  108. b.ReportAllocs()
  109. b.SetBytes(int64(size * 4 * 2))
  110. b.ResetTimer()
  111. for i := 0; i < b.N; i++ {
  112. AxpyPtr(alpha, &x[0], &y[0], size)
  113. }
  114. }