BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News C++ Interpreter Cling Embraces Python Interoperability and Jupyter Notebooks

C++ Interpreter Cling Embraces Python Interoperability and Jupyter Notebooks

This item in japanese

Bookmarks

Cling is an interactive C++ interpreter built on top of LLVM aiming to make C++ more suitable for exploration and rapid application development. In a recent series of articles, research software engineer Vassil Vassilev describes how they are evolving it to enable interoperability with Python, Jupyter Notebooks, and support for hardware accelerators.

Cling accepts partial input and ensures that the compiler process keeps running to act on code as it comes in. It includes an API providing access to the properties of recently compiled chunks of code. Cling can apply custom transformations to each chunk before execution.

The approach above is aimed to enable a few features that are key for interactivity and that C++, being a compiled language, does not support well. Those include incremental execution of statements in the global scope, results reporting and error recovery, entity redefinition, and code removal to deal with semantic errors. All of these features requires some intricate solution involving a deep knowledge of Clang and LLVM internals. For example,

Cling implements entity redefinition using inline namespaces and rewires clang lookup rules to give higher priority to more recent declarations.

Similarly, Cling has to know how to prevent a session crash when, say, a null pointer is dereferenced, and how to go back to a valid state by discarding invalid code.

Cling has found great adoption especially in the scientific community, says Vassilev, and this motivates his effort to enable native-live, dynamic interoperability with Python and the utilization of hardware accelerators. As an example of Cling importance for research, Vassilev mentions ROOT, a data analysis framework used by, among others, high energy physics. Cling is what makes eval-style programming possible in ROOT:

ROOT uses Cling as a reflection information service for data serialization. The C++ objects are stored in a binary format, vertically. The content of a loaded data file is made available to the users and C++ objects become a first class citizen.

Cling is also the enabler of the possibility of using C++ in Jupyter notebooks, which allows users to create and share documents containing live code, equations, visualizations and explanatory text. The C++ Jupyter kernel, xeus-cling is built on top the xeus, a library meant to make it easier to create Jupyter kernels. Vassilev lists three main future developments for xeus-cling: support for the Jupyter console interface, support for cling “dot commands” as Jupyter magics, and support for the new Jupyter debugger protocol.

Cling is also ready for parallel computing thanks to CUDA support:

To execute CUDA C++ Code, Cling activates an extension in the compiler frontend to understand the CUDA C++ dialect and creates a second compiler instance that compiles the code for the GPU.

This area will also see future developments including support for the complete CUDA API; redefinition of CUDA Kernels, and support for other GPUs.

Another area that Vassilev explores in his articles is using Cling for interoperability. This is achieved, for example, in cppyy, which provides automatic Python bindings at runtime.

cppyy achieves high performance through an all-lazy approach to runtime bindings construction and specializations of common cases through runtime reflection. As such, it has a much lower call overhead than e.g. pybind11, and looping over a std::vector through cppyy is faster than looping over a numpy array of the same type.

Cling can also be used as a library inside a compiled C++ program to make the vision of a C++ interpreter/compiler as a service possible. Additionally, Cling can be extended by plugins to make it support domain-specific capabilities.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT