gonum.org/v1/gonum@v0.14.0/lapack/testlapack/dlangb_bench.go (about)

     1  // Copyright ©2021 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 testlapack
     6  
     7  import (
     8  	"fmt"
     9  	"math"
    10  	"testing"
    11  
    12  	"golang.org/x/exp/rand"
    13  
    14  	"gonum.org/v1/gonum/lapack"
    15  )
    16  
    17  func DlangbBenchmark(b *testing.B, impl Dlangber) {
    18  	var result float64
    19  	rnd := rand.New(rand.NewSource(1))
    20  	for _, bm := range []struct {
    21  		n, k int
    22  	}{
    23  		{n: 1000, k: 0},
    24  		{n: 1000, k: 1},
    25  		{n: 1000, k: 2},
    26  		{n: 1000, k: 5},
    27  		{n: 1000, k: 8},
    28  		{n: 1000, k: 10},
    29  		{n: 1000, k: 20},
    30  		{n: 1000, k: 30},
    31  		{n: 10000, k: 0},
    32  		{n: 10000, k: 1},
    33  		{n: 10000, k: 2},
    34  		{n: 10000, k: 5},
    35  		{n: 10000, k: 8},
    36  		{n: 10000, k: 10},
    37  		{n: 10000, k: 30},
    38  		{n: 10000, k: 60},
    39  		{n: 10000, k: 100},
    40  	} {
    41  		n := bm.n
    42  		k := bm.k
    43  		lda := 2*k + 1
    44  		aCopy := make([]float64, n*lda)
    45  		for i := range aCopy {
    46  			aCopy[i] = 1 - 2*rnd.Float64()
    47  		}
    48  		a := make([]float64, len(aCopy))
    49  
    50  		for _, norm := range []lapack.MatrixNorm{lapack.MaxAbs, lapack.MaxRowSum, lapack.MaxColumnSum} {
    51  			name := fmt.Sprintf("%v_N=%v_K=%v", normToString(norm), n, k)
    52  			b.Run(name, func(b *testing.B) {
    53  				for i := 0; i < b.N; i++ {
    54  					result = impl.Dlangb(norm, n, n, k, k, a, lda)
    55  				}
    56  			})
    57  		}
    58  
    59  		// Frobenius norm is benchmarked separately because its execution time
    60  		// depends on the element magnitude.
    61  		norm := lapack.Frobenius
    62  		for _, scale := range []string{"Small", "Medium", "Big"} {
    63  			name := fmt.Sprintf("%v_N=%v_K=%v_%v", normToString(norm), n, k, scale)
    64  			var scl float64
    65  			switch scale {
    66  			default:
    67  				scl = 1
    68  			case "Small":
    69  				scl = smlnum
    70  			case "Big":
    71  				scl = bignum
    72  			}
    73  			// Scale some elements so that the matrix contains a mix of small
    74  			// and medium, all medium, or big and medium values.
    75  			copy(a, aCopy)
    76  			for i := range a {
    77  				if i%2 == 0 {
    78  					a[i] *= scl
    79  				}
    80  			}
    81  			b.Run(name, func(b *testing.B) {
    82  				for i := 0; i < b.N; i++ {
    83  					result = impl.Dlangb(norm, n, n, k, k, a, lda)
    84  				}
    85  			})
    86  		}
    87  	}
    88  	if math.IsNaN(result) {
    89  		b.Error("unexpected NaN result")
    90  	}
    91  }