Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

Commit

Permalink
Merge pull request #72 from gonum/optimizeinner
Browse files Browse the repository at this point in the history
Optimized Inner with the new assembly code
  • Loading branch information
btracey committed Jan 12, 2015
2 parents 874edd8 + 07a5cb5 commit 0460987
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
10 changes: 10 additions & 0 deletions mat64/bench_test.go
@@ -0,0 +1,10 @@
package mat64

import "github.com/gonum/blas/testblas"

const (
Sm = testblas.SmallMat
Med = testblas.MediumMat
Lg = testblas.LargeMat
Huge = testblas.HugeMat
)
6 changes: 4 additions & 2 deletions mat64/inner.go
Expand Up @@ -4,6 +4,8 @@

package mat64

import "github.com/gonum/internal/asm"

// Inner computes the generalized inner product
// x^T A y
// between vectors x and y with matrix A. This is only a true inner product if
Expand All @@ -28,8 +30,8 @@ func Inner(x []float64, A Matrix, y []float64) float64 {
case RawMatrixer:
bmat := b.RawMatrix()
for i, xi := range x {
for j, yj := range y {
sum += xi * bmat.Data[i*bmat.Stride+j] * yj
if xi != 0 {
sum += xi * asm.DdotUnitary(bmat.Data[i*bmat.Stride:i*bmat.Stride+n], y)
}
}
default:
Expand Down
33 changes: 33 additions & 0 deletions mat64/inner_test.go
Expand Up @@ -5,6 +5,9 @@
package mat64

import (
"testing"

"github.com/gonum/blas/blas64"
"gopkg.in/check.v1"
)

Expand Down Expand Up @@ -73,3 +76,33 @@ func (s *S) TestInner(c *check.C) {
c.Check(want, check.Equals, got, check.Commentf("Test %v: want %v, got %v", i, want, got))
}
}

func benchmarkInner(b *testing.B, m, n int) {
x := make([]float64, m)
randomSlice(x)
y := make([]float64, n)
randomSlice(y)
data := make([]float64, m*n)
randomSlice(data)
mat := &Dense{blas64.General{Rows: m, Cols: n, Stride: n, Data: data}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
Inner(x, mat, y)
}
}

func BenchmarkInnerSmSm(b *testing.B) {
benchmarkInner(b, Sm, Sm)
}

func BenchmarkInnerMedMed(b *testing.B) {
benchmarkInner(b, Med, Med)
}

func BenchmarkInnerLgLg(b *testing.B) {
benchmarkInner(b, Lg, Lg)
}

func BenchmarkInnerLgSm(b *testing.B) {
benchmarkInner(b, Lg, Sm)
}

0 comments on commit 0460987

Please sign in to comment.