Skip to content

Instantly share code, notes, and snippets.

@matteo-grella
Forked from xoba/blas.go
Created November 18, 2019 00:09
Show Gist options
  • Save matteo-grella/51a230837b5c93a7da452c160dceee5f to your computer and use it in GitHub Desktop.
Save matteo-grella/51a230837b5c93a7da452c160dceee5f to your computer and use it in GitHub Desktop.
how easy it is to use blas from go-lang
// see http://www.netlib.org/clapack/cblas/dgemv.c
package main
/*
#cgo LDFLAGS: -L/usr/lib/libblas -lblas
#include <stdlib.h>
extern void dgemv_(char* trans, int *m, int *n, double *alpha,
double *A, int *lda, double *x, int *incx, double *beta, double *y,
int *incy);
*/
import "C"
import (
"fmt"
"unsafe"
)
/*
matrix-vector multiplication:
[1, 3] * [ 5 ] = [ 23 ]
[2, 4] [ 6 ] [ 34 ]
mra@desktop ~/Desktop $ go run blas.go
[23 34]
*/
func main() {
a := []float64{1, 2, 3, 4}
x := []float64{5, 6}
y := make([]float64, 2)
Dgemv("N", 2, 2, 1.0, a, 2, x, 1, 0.0, y, 1)
fmt.Println(y)
}
func Dgemv(transA string, M int, N int, alpha float64,
A []float64, lda int, X []float64, incX int, beta float64,
Y []float64, incY int) {
ctransA := C.CString(transA)
defer C.free(unsafe.Pointer(ctransA))
C.dgemv_(ctransA,
(*C.int)(unsafe.Pointer(&M)),
(*C.int)(unsafe.Pointer(&N)),
(*C.double)(unsafe.Pointer(&alpha)),
(*C.double)(unsafe.Pointer(&A[0])),
(*C.int)(unsafe.Pointer(&lda)),
(*C.double)(unsafe.Pointer(&X[0])),
(*C.int)(unsafe.Pointer(&incX)),
(*C.double)(unsafe.Pointer(&beta)),
(*C.double)(unsafe.Pointer(&Y[0])),
(*C.int)(unsafe.Pointer(&incY)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment