gitee.com/quant1x/num@v0.3.2/internal/functions/matrix_test.go (about)

     1  package functions
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/num/internal/rand"
     6  	"gitee.com/quant1x/pkg/testify/require"
     7  	"testing"
     8  )
     9  
    10  func TestMat4Mul(t *testing.T) {
    11  	rand.Seed(2)
    12  	for i := 0; i < 100; i++ {
    13  		{
    14  			x := Random[float64](16)
    15  			y := Random[float64](16)
    16  			r1 := make([]float64, 16)
    17  			r2 := make([]float64, 16)
    18  
    19  			Mat4Mul_Go(r1, x, y)
    20  			Mat4Mul_AVX2_F64(r2, x, y)
    21  			require.InDeltaSlice(t, r1, r2, 0.001)
    22  		}
    23  	}
    24  }
    25  
    26  func TestMatMul(t *testing.T) {
    27  	rand.Seed(2)
    28  	for _, m := range []int{1, 10, 100} {
    29  		for _, n := range []int{1, 10, 100} {
    30  			for _, p := range []int{1, 10, 100} {
    31  				for i := 0; i < 10; i++ {
    32  					{
    33  						x := Random[float64](m * n)
    34  						y := Random[float64](n * p)
    35  						r1 := make([]float64, m*p)
    36  						r2 := make([]float64, m*p)
    37  
    38  						MatMul_Go(r1, x, y, m, n, p)
    39  						MatMul_AVX2_F64(r2, x, y, m, n, p)
    40  						require.InDeltaSlice(t, r1, r2, 0.001)
    41  
    42  						r1 = make([]float64, m*p)
    43  						r2 = make([]float64, m*p)
    44  
    45  						MatMul_Go(r1, x, y, m, n, p)
    46  						MatMul_AVX2_F64(r2, x, y, m, n, p)
    47  						require.InDeltaSlice(t, r1, r2, 0.001)
    48  
    49  						r1 = make([]float64, m*p)
    50  						r2 = make([]float64, m*p)
    51  
    52  						MatMul_Parallel_Go(r1, x, y, m, n, p)
    53  						MatMul_Parallel_AVX2_F64(r2, x, y, m, n, p)
    54  						require.InDeltaSlice(t, r1, r2, 0.001)
    55  					}
    56  				}
    57  			}
    58  		}
    59  	}
    60  }
    61  
    62  func BenchmarkMat4Mul(b *testing.B) {
    63  	x := Random[float64](16)
    64  	y := Random[float64](16)
    65  	x32 := Random[float32](16)
    66  	y32 := Random[float32](16)
    67  
    68  	dst := make([]float64, 16)
    69  	dst32 := make([]float32, 16)
    70  
    71  	b.Run(fmt.Sprintf("go_f64"), func(b *testing.B) {
    72  		for i := 0; i < b.N; i++ {
    73  			Mat4Mul_Go(dst, x, y)
    74  		}
    75  	})
    76  	b.Run(fmt.Sprintf("go_f32"), func(b *testing.B) {
    77  		for i := 0; i < b.N; i++ {
    78  			Mat4Mul_Go(dst32, x32, y32)
    79  		}
    80  	})
    81  	b.Run(fmt.Sprintf("avx2_f64"), func(b *testing.B) {
    82  		for i := 0; i < b.N; i++ {
    83  			Mat4Mul_AVX2_F64(dst, x, y)
    84  		}
    85  	})
    86  	b.Run(fmt.Sprintf("avx2_f32"), func(b *testing.B) {
    87  		for i := 0; i < b.N; i++ {
    88  			Mat4Mul_AVX2_F32(dst32, x32, y32)
    89  		}
    90  	})
    91  }
    92  
    93  func BenchmarkMatMul(b *testing.B) {
    94  	for _, m := range []int{100, 1000} {
    95  		for _, n := range []int{100, 1000} {
    96  			for _, p := range []int{100, 1000} {
    97  				x := Random[float64](m * n)
    98  				y := Random[float64](n * p)
    99  				x32 := Random[float32](m * n)
   100  				y32 := Random[float32](n * p)
   101  
   102  				b.Run(fmt.Sprintf("go_f64_%d_%d_%d", m, n, p), func(b *testing.B) {
   103  					for i := 0; i < b.N; i++ {
   104  						b.StopTimer()
   105  						dst := make([]float64, m*p)
   106  						b.StartTimer()
   107  						MatMul_Go(dst, x, y, m, n, p)
   108  					}
   109  				})
   110  				b.Run(fmt.Sprintf("go_f32_%d_%d_%d", m, n, p), func(b *testing.B) {
   111  					for i := 0; i < b.N; i++ {
   112  						b.StopTimer()
   113  						dst32 := make([]float32, m*p)
   114  						b.StartTimer()
   115  						MatMul_Go(dst32, x32, y32, m, n, p)
   116  					}
   117  				})
   118  				b.Run(fmt.Sprintf("go_parallel_f64_%d_%d_%d", m, n, p), func(b *testing.B) {
   119  					for i := 0; i < b.N; i++ {
   120  						b.StopTimer()
   121  						dst := make([]float64, m*p)
   122  						b.StartTimer()
   123  						MatMul_Parallel_Go(dst, x, y, m, n, p)
   124  					}
   125  				})
   126  				b.Run(fmt.Sprintf("go_parallel_f32_%d_%d_%d", m, n, p), func(b *testing.B) {
   127  					for i := 0; i < b.N; i++ {
   128  						b.StopTimer()
   129  						dst32 := make([]float32, m*p)
   130  						b.StartTimer()
   131  						MatMul_Parallel_Go(dst32, x32, y32, m, n, p)
   132  					}
   133  				})
   134  				b.Run(fmt.Sprintf("avx2_f64_%d_%d_%d", m, n, p), func(b *testing.B) {
   135  					for i := 0; i < b.N; i++ {
   136  						b.StopTimer()
   137  						dst := make([]float64, m*p)
   138  						b.StartTimer()
   139  						MatMul_AVX2_F64(dst, x, y, m, n, p)
   140  					}
   141  				})
   142  				b.Run(fmt.Sprintf("avx2_f32_%d_%d_%d", m, n, p), func(b *testing.B) {
   143  					for i := 0; i < b.N; i++ {
   144  						b.StopTimer()
   145  						dst32 := make([]float32, m*p)
   146  						b.StartTimer()
   147  						MatMul_AVX2_F32(dst32, x32, y32, m, n, p)
   148  					}
   149  				})
   150  				b.Run(fmt.Sprintf("avx2_parallel_f64_%d_%d_%d", m, n, p), func(b *testing.B) {
   151  					for i := 0; i < b.N; i++ {
   152  						b.StopTimer()
   153  						dst := make([]float64, m*p)
   154  						b.StartTimer()
   155  						MatMul_Parallel_AVX2_F64(dst, x, y, m, n, p)
   156  					}
   157  				})
   158  				b.Run(fmt.Sprintf("avx2_parallel_f32_%d_%d_%d", m, n, p), func(b *testing.B) {
   159  					for i := 0; i < b.N; i++ {
   160  						b.StopTimer()
   161  						dst := make([]float32, m*p)
   162  						b.StartTimer()
   163  						MatMul_Parallel_AVX2_F32(dst, x32, y32, m, n, p)
   164  					}
   165  				})
   166  			}
   167  		}
   168  	}
   169  }