Skip to content

Commit 5c0a302

Browse files
committed
Update the cppyy-numba blogpost
1 parent ee69461 commit 5c0a302

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed

_posts/2023-04-05-language-interoperability-using-cppyy-and-cling.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ heterogeneous computing environments).
4747

4848
This is where the usefulness of language interoperability becomes evident.
4949
However, this requires an advanced integration solution, especially for high
50-
performance code that is executed in diverse environments.
50+
performance code that can suffer from penalties crossing the language barrier.
51+
52+
![numba extension](/images/blog/cppyy-numba-1.png){: style="{{ image_style }}"}
5153

5254
Numba, a just-in-time (JIT) compiler for Python, is a tool that is ideal for
5355
this task (with some enhancements). Numba is capable of compiling Python code
@@ -60,7 +62,7 @@ automatic runtime bindings generator).
6062
The target of this research was to demonstrate a generic prototype that
6163
automatically brings advanced C++ features (e.g., highly optimized numeric
6264
libraries) to Numba-accelerated Python, with help from cppyy. This required
63-
re-engineering of the cppyy back-end to directly use LLVM components. A new
65+
re-engineering of the cppyy-backend to directly use LLVM components. A new
6466
CppInterOp library was also introduced to implement interoperability
6567
primitives based on Cling and Clang-Repl (also an interactive interpreter, a
6668
progression on Cling).
@@ -91,20 +93,24 @@ either C++ or Python, as appropriate.
9193

9294
### Prototype Overview
9395

94-
To bring C++ to Numba, a reflection interface was developed on top of cppyy.
95-
This enables Python programmers to develop and debug their code in Python and
96-
selectively switching on the Numba JIT for performance-critical tasks.
96+
The primary motivation behind the addition of Numba support in cppyy is the elimination of the overhead that arises from crossing the languiage barrier, which can multiply into large slowdowns when using loops with cppyy objects. Since Numba compiles Python code into machine code it only crosses the language barrier once and the loops thus run faster
97+
98+
![numba extension](/images/blog/cppyy-numba-2.png){: style="{{ image_style }}"}
99+
100+
Python is a dynamically typed language. It wraps and later unwraps objects (referred to as boxing/unboxing). These costly operations are eliminated with Numba, which unboxes the inputs of a function and converts it to machine code. This improves the performance of heavily looped code that perform certain operations. At the end, the output is boxed so that Python can use it. For this to work, Numba needs to infer the types of not only the input and output but the intermediate variables as well.
101+
102+
To bring C++ to Numba, a custom module was developed on top of cppyy using the Numba low level extension API.
103+
This enables Python programmers to selectively enable Numba acceleration for performance-critical tasks by importing `cppyy.numba_ext`
104+
105+
![numba extension](/images/blog/cppyy-numba-3.png){: style="{{ image_style }}"}
106+
107+
The extension aids Numba's three phases which are- Typing, Lowering(to LLVM IR) and Boxing/Unboxing which process all (or most) C++ proxies held by the Python interpreter in the form of cppyy objects.
97108

98-
Python is a dynamically typed language. It wraps and later unwraps objects
99-
(referred to as boxing/unboxing). This is a costly operation that can be
100-
eliminated with Numba, while using the new Reflection API. The Reflection API
101-
uses a function called `__cpp_reflex__` that takes the reflection type and
102-
format as parameters and returns the requested information (e.g., an object’s
103-
C++ type).
109+
The biggest challenge while integrating cppyy support in Numba is to teach Numba what cppyy types and data mean. We approach this by utilising an improved reflection API within cppyy (`__cpp_reflex__`). Reflex returns information about cppyy objects within the scope of the Numba accelerated function. This allows us to inherit Numba's typing classes and populate them with more information without which we cannot box/unbox and lower to LLVM IR.
104110

105-
Let's look at the interaction between Numba, numba extention and cppyy.
111+
Let's look at the interaction between Cppyy, Numba and the Numba extension:
106112

107-
![numba extension](/images/blog/2023-04-05-numba-ext.png){: style="{{ image_style }}"}
113+
![numba extension](/images/blog/cppyy-numba-4.png){: style="{{ image_style }}"}
108114

109115
Numba analyzes a Python code and when it encounters cppyy types, it queries
110116
the cppyy’s pre-registered `numba_ext` module for the type information. If

images/blog/cppyy-numba-1.png

111 KB
Loading

images/blog/cppyy-numba-2.png

154 KB
Loading

images/blog/cppyy-numba-3.png

171 KB
Loading
File renamed without changes.

0 commit comments

Comments
 (0)