Skip to content

Instantly share code, notes, and snippets.

@IshitaTakeshi
Last active November 25, 2022 16:56
Show Gist options
  • Save IshitaTakeshi/4010d8419e5ba5a1e07cbeb8a94ddb20 to your computer and use it in GitHub Desktop.
Save IshitaTakeshi/4010d8419e5ba5a1e07cbeb8a94ddb20 to your computer and use it in GitHub Desktop.
Calling LAPACK/BLAS from Kotlin Native

Calling LAPACK/BLAS from Kotlin Native

The original C file is cblas_example1.c

$cinterop -def liblapack.def -o build/c_interop/liblapack
$kotlinc-native lapack.kt -library build/c_interop/liblapack
$./program.kexe 
11.0
14.0
17.0
20.0
import liblapack.*
import kotlinx.cinterop.*
fun main() {
val m = 4; /* Size of Column ( the number of rows ) */
val n = 4; /* Size of Row ( the number of columns ) */
val lda = 4; /* Leading dimension of 5 * 4 matrix is 5 */
val incx = 1;
val incy = 1;
val alpha = 1.0;
val beta = 0.0;
memScoped {
val a = cValuesOf(
1.0, 2.0, 3.0, 4.0,
1.0, 1.0, 1.0, 1.0,
3.0, 4.0, 5.0, 6.0,
5.0, 6.0, 7.0, 8.0
)
val x = cValuesOf(1.0, 2.0, 1.0, 1.0)
var y = nativeHeap.allocArray<DoubleVar>(4)
cblas_dgemv(CblasColMajor, CblasNoTrans,
m, n, alpha, a, lda, x, incx, beta, y, incy)
val yp = y.getPointer(ArenaBase())
for(i in 0..3) {
println(yp[i])
}
}
}
headers = lapacke.h \
openblas/cblas.h
headerFilter = openblas/*
compilerOpts.linux = -I/usr/include -I/usr/include/x86_64-linux-gnu
linkerOpts.linux = -L/usr/lib -L/usr/lib/x86_64-linux-gnu -llapacke -llapack -lopenblas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment