Kdb+ FFI: Access external libraries more easily from q

22 Nov 2017 | ,
Share on:

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.

Happy coding!

Fintan Quill is Global Head of Sales Engineering for Kx and is based in New York City.

SUGGESTED ARTICLES

kdb+ and NAG library with PyQ

Using the NAG Library for Python with kdb+ and PyQ

25 Jun 2019 | , ,

This paper provides detailed instructions on how to use the Numerical Algorithms Group (NAG) Library for Python with kdb+ and PyQ. The NAG Library contains more than 1,800 mathematical and statistical routines, and is accessible by numerous programming languages (including Python, C++, Java, Fortran, etc.).

A comparison of Python and q for data problem solving

8 May 2019 |

This article takes a simple, real-life problem and analyzes different solutions in Python and q. The problem leads us to discover nice areas of both programming languages, including vector operations, Einstein summation, adverbs and functional form of select statements. Each solution has lessons that deepen our IT knowledge, especially when we consider performance.

Web Scraping – A Kdb+ Use case

24 Jan 2019 | , ,

By Abin Saju Web scraping is a method through which human readable content is extracted from a web page using an automated system. The system can be implemented using a bot/web crawler which traverses through domains or through a web browser which mimics human interaction with a page. There are many use cases for the […]