Nim bindings example

Dec 27, 2022
José María García Pérez
Do you know blis library?

Yes, I am familiar with the BLIS (Basic Linear Algebra Subprograms) library. It is a portable, high-performance software library that provides routines for linear algebra and machine learning computations, such as matrix multiplication, convolution, and other operations that are commonly used in scientific and engineering applications. The library is designed to be efficient on a wide range of hardware platforms, including CPUs, GPUs, and other accelerators, and it can be used to accelerate the performance of various applications that rely on linear algebra computations. BLIS is open-source software and is released under the BSD license.

José María García Pérez
Could you wrap a minimum set of functions and types in order to show a minimum example in Nim?

Certainly! Here is an example of how you could use the BLIS library in a Nim program:

import blis # Declare the BLIS types that we will use type float32* = ptr float float64* = ptr double int32* = ptr int32 int64* = ptr int64 # Declare the BLIS functions that we will use proc gemm*(transa: int32, transb: int32, m: int64, n: int64, k: int64, alpha: float64, a: float64*, lda: int64, b: float64*, ldb: int64, beta: float64, c: float64*, ldc: int64) {.importc, dynlib: "libblis.so"} proc gemm*(transa: int32, transb: int32, m: int64, n: int64, k: int64, alpha: float32, a: float32*, lda: int64, b: float32*, ldb: int64, beta: float32, c: float32*, ldc: int64) {.importc, dynlib: "libblis.so"} # Initialize the matrices that we will use in the example const m = 3 const n = 2 const k = 4 var a = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0] var b = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0] var c = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] # Perform the matrix multiplication using BLIS blis.gemm(blis.BLIS_NO_TRANSPOSE, blis.BLIS_NO_TRANSPOSE, m, n, k, 1.0, a.ptr, k, b.ptr, k, 0.0, c.ptr, n) # Print the result echo c

This example demonstrates how to use the gemm function from the BLIS library to perform a matrix multiplication of two matrices a and b, and store the result in the matrix c. The matrices are represented as one-dimensional arrays of floating-point values, and the ptr function is used to obtain pointers to the arrays, which are required by the gemm function. The gemm function is declared twice, once for single-precision (float32) and once for double-precision (float64) floating-point values.

I hope this helps! Let me know if you have any questions.

Create your own with gpt.best →