By Fintan Quill
Following on from the hugely popular Python library and interface embedPy and PyQ, Kx has released an FFI as part of the Fusion for kdb+ interfaces. As with embedPy and PyQ, this FFI is open-sourced under the Apache 2 license.
The kdb+ FFI is a foreign function interface library for loading and calling dynamic libraries from q code. It has been adapted and expanded upon from a library originally written by Alex Belopolsky of Enlightenment Research. With the kdb+ FFI you can now call your favorite C/C++ libraries directly from q without the overhead of having to compile shared objects and load into q using the 2: command.
In order to use the kdb+ FFI, you need to install kdb+ version 3.4 or later as well as libffi3.1+. Instructions are available for installing libffi using your package manager of choice such as apt-get or brew. The kdb+ FFI package then needs to be downloaded and unpacked. Once unpacked, simply load the ffi.q script into your q process and you’re done!
There are two main functions of note within the interface, .ffi.cf to do a once-off call to a function and .ffi.bind binds a C call to a q function for multiple calls.
What are the advantages of using the kdb+ FFI? Users no longer have to write their own C/C++ code or use compiler toolchains to connect to many common libraries. A second advantage is when interacting with the operating system the kdb+ FFI doesn’t carry the overhead of forking an extra process similar to the system command in q.
Within the project are several examples including:
- PCRE bindings for regular expressions
- BLAS bindings for common linear algebra operations
- Rmath bindings for random number generators and probability functions
- Callback registration
You can check out the project on the Kx Systems’ Github where you can also send feedback and add to the project.
Fintan Quill is Global Head of Sales Engineering for Kx and is based in New York City.