Skip to content
This repository was archived by the owner on Nov 24, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ install:
# Get deps, build, test, and ensure the code is gofmt'ed.
# If we are building as gonum, then we have access to the coveralls api key, so we can run coverage as well.
script:
- if [[ "$BLAS_LIB" == "gonum" ]]; then pushd native; fi
- go get -d -t -v ./...
- go build -v ./...
- go test -v ./...
- diff <(gofmt -d *.go) <("")
- if [[ $TRAVIS_SECURE_ENV_VARS = "true" ]]; then bash .travis/test-coverage.sh; fi
- if [[ $TRAVIS_SECURE_ENV_VARS = "true" ]]; then bash -c "${TRAVIS_BUILD_DIR}/.travis/test-coverage.sh"; fi
6 changes: 3 additions & 3 deletions .travis/linux/ATLAS/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export CGO_LDFLAGS="-L/usr/lib -latlas -llapack_atlas"
go get github.com/gonum/blas
go get github.com/gonum/matrix/mat64

pushd cgo
sudo perl genLapack.pl -L/usr/lib -latlas -llapack_atlas
popd
pushd cgo/clapack
go install -v -x
popd
6 changes: 3 additions & 3 deletions .travis/linux/OpenBLAS/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export CGO_LDFLAGS="-L/usr/lib -lopenblas"
go get github.com/gonum/blas
go get github.com/gonum/matrix/mat64

pushd cgo
sudo perl genLapack.pl -L/usr/lib -lopenblas
popd
pushd cgo/clapack
go install -v -x
popd
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@ Install OpenBLAS:
make
```

Then install the lapack/cgo package:
```sh
CGO_LDFLAGS="-L/path/to/OpenBLAS -lopenblas" go install github.com/gonum/lapack/cgo
```

For Windows you can download binary packages for OpenBLAS at
http://sourceforge.net/projects/openblas/files/

generate lapack bindings
```
cd $GOPATH/src/github.com/gonum/lapack/clapack
./genLapack.pl -L/path/to/OpenBLAS -lopenblas
```

If you want to use the Intel MKL and all of your paths are properly set
```
cd $GOPATH/src/github.com/gonum/lapack/clapack
./genLapack.pl -lmkl_rt
If you want to use a different BLAS package such as the Intel MKL you can
adjust the `CGO_LDFLAGS` variable:
```sh
CGO_LDFLAGS="-lmkl_rt" go install github.com/gonum/lapack/cgo
```
should work.

## Packages

Expand Down
10,211 changes: 10,211 additions & 0 deletions cgo/clapack/clapack.go

Large diffs are not rendered by default.

109 changes: 55 additions & 54 deletions cgo/genLapack.pl → cgo/clapack/genLapack.pl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}

open(my $clapack, "<", $clapackHeader) or die;
open(my $golapack, ">", "lapack.go") or die;
open(my $golapack, ">", "clapack.go") or die;

my %done;

Expand All @@ -32,11 +32,18 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package clapack provides bindings to a C LAPACK library.
package clapack

/*
#cgo CFLAGS: -g -O2
#cgo LDFLAGS: ${lib}
EOH

if ($lib) {
print $golapack "#cgo LDFLAGS: ${lib}\n"
}

printf $golapack <<"EOH";
#include "${clapackHeader}"
*/
import "C"
Expand All @@ -47,8 +54,6 @@ package clapack
"unsafe"
)

type Lapack struct{}

// Type order is used to specify the matrix storage format. We still interact with
// an API that allows client calls to specify order, so this is here to document that fact.
type order int
Expand All @@ -58,11 +63,6 @@ package clapack
colMajor
)

func init() {
_ = lapack.Complex128(Lapack{})
_ = lapack.Float64(Lapack{})
}

func isZero(ret C.int) bool { return ret == 0 }

EOH
Expand Down Expand Up @@ -179,23 +179,16 @@ sub processProto {
if ($params eq "") {
return
}
print $golapack "func (Lapack) ".$gofunc."(".$params.") ".$GoRetType."{\n";
print $golapack "func ".$gofunc."(".$params.") ".$GoRetType."{\n";
print $golapack "\t";
if ($ret ne 'void') {
print $golapack "\n".$bp."\n"."return ".$GoRet."(";
print $golapack $bp."return ".$GoRet."(";
}
print $golapack "C.LAPACKE_$func(".processParamToC($func, $paramList).")";
if ($ret ne 'void') {
print $golapack ")";
}
print $golapack "\n}\n";
}

sub Gofunc {
my $fnName = shift;
my ($pack, $func, $tail) = split '_', $fnName;
return ucfirst $func . ucfirst $tail if $tail;
return ucfirst $func;
print $golapack "\n}\n\n";
}

sub processParamToGo {
Expand All @@ -213,48 +206,69 @@ sub processParamToGo {
};
$var =~ /trans/ && do {
my $bp = << "EOH";
var $var C.char
if go$var == blas.NoTrans{ $var = 'n' }
if go$var == blas.Trans{ $var = 't' }
if go$var == blas.ConjTrans{ $var = 'c' }
switch $var {
case blas.NoTrans:
$var = 'N'
case blas.Trans:
$var = 'T'
case blas.ConjTrans:
$var = 'C'
default:
panic("lapack: bad trans")
}
EOH
push @boilerplate, $bp;
push @processed, "go".$var." blas.Transpose"; next;
push @processed, $var." blas.Transpose"; next;
};
$var eq "uplo" && do {
$var = "ul";
my $bp = << "EOH";
var $var C.char
if go$var == blas.Upper{ $var = 'u' }
if go$var == blas.Lower{ $var = 'l' }
switch $var {
case blas.Upper:
$var = 'U'
case blas.Lower:
$var = 'L'
default:
panic("lapack: illegal triangle")
}
EOH
push @boilerplate, $bp;
push @processed, "go".$var." blas.Uplo"; next;
push @processed, $var." blas.Uplo"; next;
};
$var eq "diag" && do {
$var = "d";
my $bp = << "EOH";
var $var C.char
if go$var == blas.Unit{ $var = 'u' }
if go$var == blas.NonUnit{ $var = 'n' }
switch $var {
case blas.Unit:
$var = 'U'
case blas.NonUnit:
$var = 'N'
default:
panic("lapack: illegal diagonal")
}
EOH
push @boilerplate, $bp;
push @processed, "go".$var." blas.Diag"; next;
push @processed, $var." blas.Diag"; next;
};
$var eq "side" && do {
$var = "s";
my $bp = << "EOH";
var $var C.char
if go$var == blas.Left{ $var = 'l' }
if go$var == blas.Right{ $var = 'r' }
switch $var {
case blas.Left:
$var = 'L'
case blas.Right:
$var = 'R'
default:
panic("lapack: bad side")
}
EOH
push @boilerplate, $bp;
push @processed, "go".$var." blas.Side"; next;
push @processed, $var." blas.Side"; next;
};
$var eq "compq" && do {
$var =~ /^comp./ && do {
push @processed, $var." lapack.CompSV"; next;
};
$var =~ /job+/ && do {
$var =~ /job/ && do {
push @processed, $var." lapack.Job"; next;
};
$var eq "select" && do {
Expand All @@ -270,25 +284,12 @@ sub processParamToGo {

my $goType = $typeConv{$type};

if (substr($type,-1) eq "*") {
my $base = $typeConv{$type};
$base =~ s/\[\]//;
my $bp = << "EOH";
var $var *$base
if len(go$var) > 0 {
$var = &go$var [0]
}
EOH
push @boilerplate, $bp;
$var = "go".$var;
}

if (not $goType) {
die "missed Go parameters from '$func', '$type', '$param'";
}
push @processed, $var." ".$goType; next;
}
return ((join ", ", @processed), (join "\n", @boilerplate));
return ((join ", ", @processed), (join "", @boilerplate));
}

sub processParamToC {
Expand Down Expand Up @@ -326,9 +327,9 @@ sub processParamToC {
chop $type;

if ($type eq "char") {
push @processed, "(*C.".$type.")(unsafe.Pointer(".$var."))"; next;
push @processed, "(*C.".$type.")(unsafe.Pointer(&".$var."[0]))"; next;
} else {
push @processed, "(*C.".$type.")(".$var.")"; next;
push @processed, "(*C.".$type.")(&".$var."[0])"; next;
}
}else{
push @processed, "(C.".$type.")(".$var.")"; next;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
52 changes: 52 additions & 0 deletions cgo/lapack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright ©2015 The gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package cgo provides an interface to bindings for a C LAPACK library.
package cgo

import (
"github.com/gonum/blas"
"github.com/gonum/lapack"
"github.com/gonum/lapack/cgo/clapack"
)

// Copied from lapack/native. Keep in sync.
const (
badDirect = "lapack: bad direct"
badLdA = "lapack: index of a out of range"
badSide = "lapack: bad side"
badStore = "lapack: bad store"
badTau = "lapack: tau has insufficient length"
badTrans = "lapack: bad trans"
badUplo = "lapack: illegal triangle"
badWork = "lapack: insufficient working memory"
badWorkStride = "lapack: insufficient working array stride"
negDimension = "lapack: negative matrix dimension"
nLT0 = "lapack: n < 0"
shortWork = "lapack: working array shorter than declared"
)

// Implementation is the cgo-based C implementation of LAPACK routines.
type Implementation struct{}

var _ lapack.Float64 = Implementation{}

// Dpotrf computes the cholesky decomposition of the symmetric positive definite
// matrix a. If ul == blas.Upper, then a is stored as an upper-triangular matrix,
// and a = U U^T is stored in place into a. If ul == blas.Lower, then a = L L^T
// is computed and stored in-place into a. If a is not positive definite, false
// is returned. This is the blocked version of the algorithm.
func (impl Implementation) Dpotrf(ul blas.Uplo, n int, a []float64, lda int) (ok bool) {
// ul is checked in clapack.Dpotrf.
if n < 0 {
panic(nLT0)
}
if lda < n {
panic(badLdA)
}
if n == 0 {
return true
}
return clapack.Dpotrf(ul, n, a, lda)
}
17 changes: 17 additions & 0 deletions cgo/lapack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright ©2015 The gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package cgo

import (
"testing"

"github.com/gonum/lapack/testlapack"
)

var impl = Implementation{}

func TestDpotrf(t *testing.T) {
testlapack.DpotrfTest(t, impl)
}