<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Pasquale Claudio Africa on Medium]]></title>
        <description><![CDATA[Stories by Pasquale Claudio Africa on Medium]]></description>
        <link>https://medium.com/@pcafrica?source=rss-c0b1d10153e3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*jL0wK-LMyym_SA2PJhijWw.jpeg</url>
            <title>Stories by Pasquale Claudio Africa on Medium</title>
            <link>https://medium.com/@pcafrica?source=rss-c0b1d10153e3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 22:29:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pcafrica/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[dealii-X — Digital Twins of Human
Organs]]></title>
            <link>https://medium.com/sissa-mathlab/dealii-x-digital-twins-of-human-organs-de9be7f6b9b3?source=rss-c0b1d10153e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/de9be7f6b9b3</guid>
            <category><![CDATA[digital-twin]]></category>
            <category><![CDATA[numerical-analysis]]></category>
            <category><![CDATA[high-performace-computing]]></category>
            <category><![CDATA[human-organ]]></category>
            <category><![CDATA[finite-element-method]]></category>
            <dc:creator><![CDATA[Pasquale Claudio Africa]]></dc:creator>
            <pubDate>Mon, 25 May 2026 01:37:25 GMT</pubDate>
            <atom:updated>2026-05-25T01:37:25.351Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Pasquale Claudio Africa, Andrea Cangiani, Marco Feder</em></p><p>Accurately modeling human organs is currently at the very limit of today’s computational capabilities, even when employing highly efficient and sophisticated numerical methods. Their biological functions involve highly complex geometries and multiscale phenomena (both in time and space), with fast signals between cells resulting in long-term consequences, such as the development of an illness.</p><p>Efficient and high-fidelity computational modeling of human organs is precisely the goal of the project <strong>dealii-X — Exascale Framework for Digital Twins of the Human Body</strong> (<a href="https://www.dealii-x.eu/">https://www.dealii-x.eu/</a>). The project is developing a high-performance, scalable computational platform, based on the deal.II library [2], that performs accurate digital twins of the brain, cardiovascular and respiratory systems, and the liver.</p><p>The dealii-X SISSA team is developing novel numerical techniques, such as reduced-order modeling, mesh adaptivity, and polytopic discretizations to mitigate the intrinsic complexity of biological processes. These techniques are tested within a comprehensive computational model of the human heart.</p><p>Mathematical modeling begins with reconstructing the organ tissues from medical imaging, such as computerized tomography (CT). An example is shown in the figure below, which depicts a real-life brain model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uKPB5KNjaQbNUFb4abYuyg.jpeg" /><figcaption>Tetrahedral mesh of a human brain geometry acquired from computerized tomography from [7].</figcaption></figure><p>The brain is represented in the computer as tens of millions of “pixels” (tetrahedra) that represent the geometry of the tissue as acquired from the CT scan. Performing simulations on such realistic geometries involves solving systems of equations with up to billions of variables multiple times. <em>Upscaling</em> techniques can be employed to reduce this prohibitive complexity. One such approach is that of agglomerating neighboring tetrahedral elements into larger polyhedral elements, thus reducing their number without compromising the resolution of the geometry (see the figure below). In turn, this considerable reduction in terms of the number of elements results in much smaller linear systems of equations to solve.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OcfgYXXmCFkcTy5LRs_RZw.jpeg" /><figcaption>Portion of the brain geometry from [7]. A coarse polyhedral mesh is extracted by agglomerating together neighbouring tetrahedral elements.</figcaption></figure><h3>Polytopic Discretizations and Multilevel Methods</h3><p>Classical Finite Element Methods (FEM) matured in the mid-20th century with simplicial (triangular and tetrahedral) and tensor product elements (quadrilateral and hexahedral); its roots are usually traced to Hrennikoff (1941) and Courant (1943), and the success of the method across engineering and applied mathematics is well established.</p><p>From the early 2000s, a family of <em>polytopic</em> discretizations emerged, allowing meshes made of general polygons in 2D and polyhedra in 3D — with further extensions to curved elements also allowed. Such flexibility permits, for instance, the meshing of complex geometries and handling mesh distortion within dynamic problems and hanging nodes within adaptive mesh refinement.</p><p>Important strands include the conforming polygonal FEM via generalized barycentric/natural-neighbor bases [8], mimetic finite difference (MFD) in polytopic meshes [3], preserving symmetry/stability for diffusion and beyond, the Virtual Element Method (VEM) [4], which replaces explicit shape functions by polynomial projections and stabilizations, and the <em>hp</em>-version discontinuous Galerkin methods [6], which naturally extend to extremely general mesh discretizations. Other approaches include the general class of <em>unfitted</em> methods, designed to flexibly and accurately handle complicated, evolving, and moving geometries by decoupling the domain description from the underlying discretization [5].</p><p>The development of polytopic discretizations requires a careful mathematical analysis as well as efficient implementations. The potential benefit is the ability to perform numerical computations of mathematical models involving highly complex multi-scale phenomena at an unprecedented level of resolution.</p><p>A rigorous mathematical analysis has shown that discontinuous Galerkin methods allow the use of mesh shapes of unprecedented generality [6]. In particular, upscaling of complicated geometries becomes possible via mesh agglomeration as described above. This can be used either to perform coarser (cheaper) computations or within multigrid solvers, a preconditioning technique that accelerates the solution of the underlying problem by exploiting multiple levels of meshes.</p><p>In this realm, we introduced a novel multilevel agglomeration and preconditioning strategy designed to solve the monodomain equation in cardiac electrophysiology using high-order discontinuous Galerkin (DG) methods. At the core of this approach is the R*-tree spatial data structure, which is used to generate coarse grid hierarchies from existing fine meshes. By traversing tree levels, the method creates agglomerated polytopic elements, enabling efficient and robust multilevel computations [7].</p><p>Mathematically, the method leverages inherited bilinear forms, constructing coarse operators via Galerkin projection, which eliminates the costly step of assembling operators on complex polytopic elements. The monodomain equation</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*6rFRYMofufJPc1437MEDAQ.png" /></figure><p>is discretized using Symmetric Interior Penalty DG with semi-implicit time stepping, treating diffusion implicitly and reaction terms explicitly.</p><p>The C++ implementation builds on the <a href="https://github.com/fdrmrc/Polydeal">Polydeal</a> library, which extends the deal.II library capabilities to handle general polytopal shapes. Our agglomeration algorithm scales efficiently from 128 to 1024 processors, even on realistic three-dimensional cardiac geometries, while maintaining load balance. Memory consumption is minimized, when possible, through matrix-free implementations of prolongation and restriction operators.</p><p>Numerical experiments show clear performance advantages over traditional algebraic multigrid preconditioning. For polynomial degrees <em>p=1</em> to <em>p=5</em>, our method consistently reduces iteration counts, with gains increasing for higher-order polynomials (see figures below). Its performance is largely independent of the number of multigrid levels.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6Wf0Rm1pmnKot8iKcGYpGw.jpeg" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m45gwurFYrxyPWkUUUlbfA.jpeg" /><figcaption>Ventricle electrophysiology model from [1]. The multigrid solver based on mesh agglomeration requires roughly half the iteration needed by state-of-the-art algebric multigrid solver, thus reducing computational time.</figcaption></figure><p>The coarsening algorithm preserves geometric quality and the spatial locality of agglomerated elements, resulting in well-conditioned coarse operators. The preconditioner integrates a V-cycle multigrid with Chebyshev smoothers, where eigenvalue estimates are efficiently computed using Lanczos iterations.</p><h3>References</h3><p>[1] P. C. Africa and M. Feder, <em>A polytopic agglomeration-based multigrid strategy for the discontinuous Galerkin discretization of cardiac electrophysiology</em>, In preparation.<br>[2] D. Arndt, W. Bangerth, D. Davydov, T. Heister, L. Heltai, M. Kronbichler, M. Maier, J.-P. Pelteret, B. Turcksin, and D. Wells, <em>The deal.II finite element library: Design, features, and insights</em>, <strong>Computers &amp; Mathematics with Applications</strong>, 81 (2021), pp. 407–422.<br>[3] L. C. Beirao da Veiga, K. Lipnikov, and G. Manzini, <em>The mimetic finite difference method for elliptic problems</em>, vol. 11 of <strong>MS&amp;A. Modeling, Simulation and Applications</strong>, Springer, Cham, 2014.<br>[4] L. Beirao da Veiga, F. Brezzi, A. Cangiani, G. Manzini, L. Marini, and A. Russo, <em>Basic principles of virtual element methods</em>, <strong>Math. Models Methods Appl. Sci.</strong>, 23 (2013), pp. 199–214.<br>[5] D. Boffi, A. Cangiani, M. Feder, L. Gastaldi, and L. Heltai, <em>A comparison of non-matching techniques for the finite element approximation of interface problems</em>, <strong>Comput. Math. Appl.</strong>, 151 (2023), pp. 101–115.<br>[6] A. Cangiani, Z. Dong, E. H. Georgoulis, and P. Houston, <em>hp-version discontinuous Galerkin methods on polygonal and polyhedral meshes</em>, <strong>SpringerBriefs in Mathematics</strong>, Springer, Cham, 2017.<br>[7] M. Feder, A. Cangiani, and L. Heltai, <em>R3mg: R-tree based agglomeration of polytopal grids with applications to multilevel methods</em>, <strong>Journal of Computational Physics</strong>, 526 (2025), p. 113773.<br>[8] N. Sukumar and A. Tabarraei, <em>Conforming polygonal finite elements</em>, <strong>Internat. J. Numer. Methods Engrg.</strong>, 61 (2004), pp. 2045–2066.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de9be7f6b9b3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/sissa-mathlab/dealii-x-digital-twins-of-human-organs-de9be7f6b9b3">dealii-X — Digital Twins of Human
Organs</a> was originally published in <a href="https://medium.com/sissa-mathlab">SISSA mathLab</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Integrating C++ and Python for Scientific Computing]]></title>
            <link>https://medium.com/sissa-mathlab/integrating-c-and-python-for-scientific-computing-2dda79b0da73?source=rss-c0b1d10153e3------2</link>
            <guid isPermaLink="false">https://medium.com/p/2dda79b0da73</guid>
            <category><![CDATA[cplusplus]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[pybind11]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[scientific-computing]]></category>
            <dc:creator><![CDATA[Pasquale Claudio Africa]]></dc:creator>
            <pubDate>Wed, 10 Apr 2024 01:59:57 GMT</pubDate>
            <atom:updated>2024-04-10T01:59:57.126Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7k01gt_olyoR5pIBW2dwhg.png" /><figcaption>Generated by DALL·E 2 with the prompt: “Create a minimalistic banner for a tutorial on integrating C++ and Python”.</figcaption></figure><p>Scientific computing has been revolutionizing research and development, with its impact felt across data science, physics, engineering, biology, and finance. At the heart of this revolution is the synergy between different programming languages, particularly C++ and Python. This tutorial isn’t just about theory; it’s about unlocking a new realm of possibilities by combining Python’s simplicity and C++’s speed.</p><p>In the realm of programming, C++ and Python stand as giants. Python, known for its user-friendly syntax, vast libraries, and strong community support, is often the language of choice for scientists and researchers for data analysis, machine learning, visualization, and rapid prototyping. However, when it comes to performance-intensive computations, C++ emerges as a preferred option due to its optimized memory management and execution speed. Imagine performing complex data analyses with Python’s ease and visualizing results with the raw power of C++ — this is what we aim to achieve here.</p><p>But what happens when you merge the robustness of C++ with the versatility of Python? You get a combination that can tackle complex computing challenges with both speed and ease. This is especially vital in research and industry, where the need for efficient yet rapid development is paramount.</p><p>Integrating C++ and Python isn’t just a theoretical exercise; it’s a practical need in software development. To achieve this, several libraries are at your disposal, each with its unique strengths and limitations. Here we just mention a few:</p><ol><li><strong>Boost.Python </strong>(<a href="https://www.boost.org/doc/libs/1_84_0/libs/python/doc/html/index.html">https://www.boost.org/doc/libs/1_84_0/libs/python/doc/html/index.html</a>): A well-documented and widely used library that offers seamless interoperability between C++ and Python.</li><li><strong>SWIG </strong>(<a href="https://www.swig.org/">https://www.swig.org/</a>): Great for multi-language projects, but can be less efficient and less <em>Pythonic.</em></li><li><strong>pybind11 </strong>(<a href="https://pybind11.readthedocs.io/en/stable/">https://pybind11.readthedocs.io/en/stable/</a>): Stands out for its modern approach, being lightweight and easy to use.</li><li><strong>Cython </strong>(<a href="https://cython.org/">https://cython.org/</a>): Known for allowing C extensions in Python-like syntax.</li><li><strong>ctypes </strong>(<a href="https://docs.python.org/3/library/ctypes.html">https://docs.python.org/3/library/ctypes.html</a>): A part of Python’s standard library, suitable for basic tasks involving C functions.</li></ol><p>Among these, pybind11 shines for its balance of power and simplicity. It’s a header-only library, meaning it’s easy to include in your projects without heavy setup.</p><h3>Why pybind11?</h3><p>pybind11 is not just another library; it’s a modern tool that resonates with the demands of today’s industry. It’s designed for simplicity, yet it doesn’t compromise on power. By offering more Pythonic bindings compared to other alternatives, it becomes an ideal choice for a wide range of projects.</p><p>pybind11 is all about creating Python bindings of existing C++ code. Its design is akin to Boost.Python but focuses more on being minimalistic. By leveraging C++11 features, it sidesteps the complexities associated with its competitors, making it a go-to for modern C++/Python integration.</p><h3>Prerequisites</h3><p>To effectively use pybind11 for integrating C++ and Python, there are several prerequisites that you should be familiar with.</p><ol><li><strong>Basic knowledge of Python and C++</strong>: this includes familiarity with Python’s syntax, data types, and basic programming constructs, as well as C++ syntax, object-oriented programming, and memory management.</li><li><strong>C++ compiler</strong>: A C++ compiler that is compatible with the C++11 standard or later is required, as pybind11 utilizes C++11 features. Common compilers like GCC (<a href="https://gcc.gnu.org/">https://gcc.gnu.org/</a>), Clang (<a href="https://clang.llvm.org/">https://clang.llvm.org/</a>), or MSVC (<a href="https://visualstudio.microsoft.com/vs/features/cplusplus/">https://visualstudio.microsoft.com/vs/features/cplusplus/</a>) (on Windows) are suitable.</li><li><strong>Python environment</strong>: You should have Python (<a href="https://www.python.org/">https://www.python.org/</a>) installed on your system. Most Python distributions come with the necessary tools and libraries to get started. It’s also beneficial, although not mandatory, to be familiar with virtual environments in Python for managing dependencies. One such example is Conda (<a href="https://docs.conda.io/en/latest/">https://docs.conda.io/en/latest/</a>).</li><li><strong>Development tools</strong>:<br>– <strong>Build tools</strong>: Knowledge of build systems like CMake (<a href="https://cmake.org/">https://cmake.org</a>) is important, as pybind11 is commonly integrated with CMake for building extensions. This is not essential for the basics covered in this tutorial.<br>– <strong>IDE/Text editor</strong>: While not strictly necessary, using an Integrated Development Environment (IDE) or a text editor that supports both Python and C++ can make your development process smoother.</li></ol><h3>Setting the stage</h3><p>Getting started with pybind11 is straightforward. You have multiple options depending on your environment:</p><ul><li><strong>Via PyPI</strong>: pip install pybind11.</li><li><strong>Using conda</strong>: conda install -c conda-forge pybind11.</li><li><strong>Using Homebrew/Linuxbrew</strong>: brew install pybind11.</li><li><strong>Git submodule </strong>(<a href="https://pybind11.readthedocs.io/en/stable/installing.html#include-as-a-submodule">https://pybind11.readthedocs.io/en/stable/installing.html#include-as-a-submodule</a>): Ideal for Git-based projects, where you can include pybind11 as part of your repository.</li><li><strong>Installing from source</strong>.</li></ul><p>For those interested in exploring the code examples and concepts discussed in this post in greater detail, the entire codebase is available on this GitHub repository: <a href="https://github.com/pcafrica/introduction_to_pybind11">https://github.com/pcafrica/introduction_to_pybind11</a>. This availability ensures reproducibility and allows readers to experiment directly with the code, facilitating a hands-on learning experience. You can clone or fork the repository to try out the examples, make modifications, and see the effects in real-time on your machine.</p><p>Before diving into coding examples, it’s crucial to set up our environment correctly. For all our examples, we’ll include the main pybind11 header and define a convenient namespace shortcut:</p><pre>#include &lt;pybind11/pybind11.h&gt;<br>namespace py = pybind11;</pre><p>This setup allows us to access pybind11 features quickly, ensuring our code remains clean and readable.</p><h3>Binding 101: creating simple functions</h3><p>Imagine you have a simple C++ function, like one that adds two numbers. How do you make this function accessible in Python? That’s where pybind11 shines. Here’s a glimpse of how you’d do it:</p><pre>int add(int i, int j) {<br>    return i + j;<br>}<br><br>// Syntax: PYBIND11_MODULE(&lt;module name&gt;, &lt;module object&gt;)<br>PYBIND11_MODULE(example, m) {<br>    m.doc() = &quot;pybind11 example plugin&quot;; // Optional module docstring.<br>    m.def(&quot;add&quot;, &amp;add, &quot;A function that adds two numbers&quot;); // Bind the function and include it in the module.<br>}</pre><p><em>The magic behind the </em><em>PYBIND11_MODULE macro is the heart of pybind11&#39;s binding mechanism. It creates a function that Python calls upon importing the module.</em></p><p>More specifically, the PYBIND11_MODULE() macro is designed to define a function that is executed when Python issues an import command. It takes two input arguments. The name of the module, in this case example, is provided as the first argument of this macro and should not be enclosed in quotes. Following this, the second argument, labeled m, instantiates an object of type py::module_. This variable acts as the pybind11 placeholder structure for binding functions, classes, variables, documentation, and much more into the module.</p><p>Through the use of the py::module_::def() method, binding code is produced, effectively allowing the C++ function add() to be accessible and usable within Python. Finally, the py::module_::doc() method allows to include a descriptive string that explains what the function does, its parameters, return type, and any other relevant information. This documentation is then accessible in Python, typically through the built-in help() function or by using the .__doc__ attribute.</p><h3>Keyword and default arguments</h3><p>Pybind11 also allows you to define <em>keyword arguments</em>, enhancing the readability of your Python function calls. For example:</p><pre>// m is the pybind11 module object.<br>m.def(&quot;add&quot;, &amp;add, &quot;A function which adds two numbers&quot;,<br>      py::arg(&quot;i&quot;), py::arg(&quot;j&quot;));</pre><p>In this example, py::arg(&quot;i&quot;) and py::arg(&quot;j&quot;) explicitly define the arguments i and j for the add function. This modification lets Python users call your function using keyword arguments, like add(i=3, j=4) or add(j=3, i=4). This approach is clearer and more self-documenting, especially when a function accepts several parameters. It helps to avoid confusion about the order of arguments and makes the code more self-explanatory, enhancing the overall readability and maintainability of the code.</p><p>In addition to defining keyword arguments, Pybind11 also supports the specification of <em>default arguments</em>, which further enhances the flexibility and user-friendliness of your Python-bound C++ functions. Default arguments allow you to specify default values for parameters, making them optional when the function is called from Python. For example:</p><pre>// m is the pybind11 module object.<br>m.def(&quot;add&quot;, &amp;add, &quot;A function which adds two numbers&quot;,<br>      py::arg(&quot;i&quot;) = 1, py::arg(&quot;j&quot;) = 2);</pre><p>In this case, py::arg(&quot;i&quot;) = 1 and py::arg(&quot;j&quot;) = 2 set default values for i and j. When this function is called from Python without arguments, it uses these default values. So, a call like add() in Python is equivalent to add(1, 2), and both add(3) and add(j=4) are valid calls, using the default for the non-specified argument(s).</p><h3>Compiling</h3><p>Notice how little code was needed to expose our function to Python: all details regarding the function’s parameters and return value were automatically inferred using template metaprogramming.</p><p><em>pybind11 is a header-only library, hence it is not necessary to link against any special libraries and there are no intermediate (magic) translation steps. </em>Given a C++ compiler, the above example, assumed to be saved in a example.cpp source file, can be compiled into a module using the following command:</p><pre>c++ \                                            # The name of the compiler.<br>  -std=c++11 -O3 -shared -fPIC \                 # Specify the standard C++11, enable optimizations and generate a shared library.<br>  $(python3 -m pybind11 --includes) \            # Add flags for including pybind11 directory.<br>  example.cpp \                                  # The file to be compiled.<br>  -o example$(python3-config --extension-suffix) # Output filename, with proper Python extension suffix.</pre><p>The command above will generate a dynamic module example.cpython-version-arch.so that can be imported in Python as follows:</p><pre>import example<br><br>help(example) # Print module docstring.<br>example.add(1, 2); # Output: 3</pre><h3>Supported data types</h3><p>pybind11 offers comprehensive support for a wide range of data types, facilitating seamless conversions between C++ and Python, which is crucial for effective integration. Among the supported types are the standard Python data types such as integers, floating-point numbers, strings, lists, tuples, dictionaries, and sets. These are automatically converted to and from their C++ counterparts, like int, double, std::string, std::vector, std::tuple, std::map, and std::set, respectively. This automatic conversion simplifies the process of passing data between the two languages.</p><p>A notable feature of pybind11 is its compatibility with the NumPy (<a href="https://numpy.org/">https://numpy.org/</a>) library, a cornerstone in Python’s scientific computing stack. NumPy arrays are efficiently converted to and from C++’s Eigen (<a href="https://eigen.tuxfamily.org/index.php">https://eigen.tuxfamily.org/index.php</a>) library types or STL containers like std::vector and std::array, allowing for high-performance matrix and vector operations crucial in scientific computations. This integration means that NumPy arrays can be directly passed to C++ functions expecting Eigen types without the need for explicit conversion, and vice versa. Crucially, this seamless interoperability extends to other powerful libraries built on top of NumPy, such as SciPy (<a href="https://scipy.org/">https://scipy.org/</a>), Matplotlib (<a href="https://matplotlib.org/">https://matplotlib.org/</a>), pandas (<a href="https://pandas.pydata.org/">https://pandas.pydata.org/</a>), and many more.</p><p>A large number of data types are supported out of the box and can be used seamlessly as functions arguments, return values or with py::cast in general. For a full overview, see the conversion table (<a href="https://pybind11.readthedocs.io/en/stable/advanced/cast/overview.html#conversion-table">https://pybind11.readthedocs.io/en/stable/advanced/cast/overview.html#conversion-table</a>) and the general documentation (<a href="https://pybind11.readthedocs.io/en/stable/advanced/cast/index.html">https://pybind11.readthedocs.io/en/stable/advanced/cast/index.html</a>).</p><h3>C++ vs. native Python: a performance showdown</h3><p>Let’s take a common scientific computing task: dense matrix multiplication. Python makes it easy, but when performance is key, C++ takes the lead. Here’s how pybind11 bridges this gap.</p><p>Consider the following streamlined C++ code, which represents a matrix using a vector of vectors. We assume the matrices mat1 and mat2 are compatible in size for multiplication:</p><pre>#include &lt;pybind11/numpy.h&gt;<br>#include &lt;pybind11/pybind11.h&gt;<br>#include &lt;pybind11/stl.h&gt;<br><br>#include &lt;vector&gt;<br><br>std::vector&lt;std::vector&lt;double&gt;&gt;<br>matrix_multiply(const std::vector&lt;std::vector&lt;double&gt;&gt; &amp;mat1,<br>                const std::vector&lt;std::vector&lt;double&gt;&gt; &amp;mat2) {<br>  const size_t rows = mat1.size();<br>  const size_t cols = mat2[0].size();<br>  const size_t inner_dim = mat2.size();<br><br>  std::vector&lt;std::vector&lt;double&gt;&gt; result(rows, std::vector&lt;double&gt;(cols, 0));<br><br>  for (size_t i = 0; i &lt; rows; ++i) {<br>    for (size_t j = 0; j &lt; cols; ++j) {<br>      for (size_t k = 0; k &lt; inner_dim; ++k) {<br>        result[i][j] += mat1[i][k] * mat2[k][j];<br>      }<br>    }<br>  }<br><br>  return result;<br>}<br><br>// pybind11 module definition.<br>namespace py = pybind11;<br>PYBIND11_MODULE(matrix_ops, m) {<br>  m.def(&quot;matrix_multiply&quot;, &amp;matrix_multiply,<br>        &quot;A function which multiplies two NumPy matrices&quot;);<br>}</pre><p>Compile this C++ code into a Python module with the command:</p><pre>c++ \<br>  -std=c++11 -O3 -shared -fPIC \<br>  $(python3 -m pybind11 --includes) \<br>  matrix_multiplication.cpp \<br>  -o matrix_ops$(python3-config --extension-suffix)</pre><p>In Python, define an equivalent function to compare performance:</p><pre>def matrix_multiply_python(mat1, mat2):<br>    rows = len(mat1)<br>    cols = len(mat2[0])<br>    inner_dim = len(mat2)<br>    <br>    result = [[0 for _ in range(cols)] for _ in range(rows)]<br>    for i in range(rows):<br>        for j in range(cols):<br>            for k in range(inner_dim):<br>                result[i][j] += mat1[i][k] * mat2[k][j]<br>    return result</pre><p>Next, generate two random square matrices of size n = 500 using NumPy for testing:</p><pre>import numpy as np<br><br># Create random matrices.<br>n = 500<br>mat1 = np.random.rand(n, n)<br>mat2 = np.random.rand(n, n)</pre><p>Now, let’s benchmark both the C++ and Python implementations:</p><pre>import matrix_ops<br>import time<br><br># Benchmark C++ implementation (using pybind11).<br>start_time = time.time()<br>result_cpp = matrix_ops.matrix_multiply(mat1, mat2)<br>cpp_duration = time.time() - start_time<br>print(f&quot;C++ implementation took {cpp_duration:.4f} seconds.&quot;)<br><br># Benchmark Python implementation.<br>start_time = time.time()<br>result_python = matrix_multiply_python(mat1, mat2)<br>python_duration = time.time() - start_time<br>print(f&quot;Python implementation took {python_duration:.4f} seconds.&quot;)<br>print(f&quot;Speedup: {python_duration // cpp_duration}.&quot;)</pre><p>On an Intel® Core™ i7–8565U CPU @ 1.80GHz the C++ implementation takes on average around 0.15 seconds, whereas native Python takes up to around 60 seconds. This equates to a speedup of approximately 300 to 400 times!</p><p>The impact of such huge difference becomes even more dramatic if we consider algorithms where matrix-matrix products have to be computed several times.</p><p>This example illustrates how a computationally intensive task, when implemented in C++ and exposed to Python using pybind11, can lead to significant performance gains, all while maintaining the ease of use that Python offers.</p><p>It’s important to note that the matrix multiplication implementations in our example, both in Python and C++, are not optimized in any specific manner, particularly regarding cache utilization and efficiency.</p><p>As an engaging <strong>exercise</strong> for the reader, it would be insightful to compare the performance of the C++ and Python implementations against native NumPy matrix multiplication, <em>i.e.</em>, using the @ multiplication operator (<em>e.g.</em>, mat1 @ mat2). NumPy is highly optimized for vectorized operations, making it an excellent benchmark for performance comparison.</p><h3>Conclusions</h3><p>With pybind11, the fusion of C++ and Python in scientific computing isn’t just seamless: it’s powerful!</p><p>As we’ve seen, whether it’s simple function binding or complex matrix operations, pybind11 stands as a bridge, allowing you to leverage the strengths of both languages. Remember, this is just the beginning. The real adventure starts when you apply these concepts to your unique challenges and data types.</p><p>Dive in, experiment, and witness how integrating C++ and Python can transform your scientific computing projects!</p><h3>References and further readings</h3><ul><li>Examples discussed in this post: <a href="https://github.com/pcafrica/introduction_to_pybind11">https://github.com/pcafrica/introduction_to_pybind11</a></li><li>pybind11 documentation: <a href="https://pybind11.readthedocs.io/en/stable/">https://pybind11.readthedocs.io/en/stable/</a></li><li>Example pybind11 module built with a CMake-based build system: <a href="https://github.com/pybind/cmake_example">https://github.com/pybind/cmake_example</a></li><li>Example pybind11 module built with a Python-based build system: <a href="https://github.com/pybind/python_example">https://github.com/pybind/python_example</a></li><li>pybind11 testsuite: <a href="https://github.com/pybind/pybind11/tree/master/tests">https://github.com/pybind/pybind11/tree/master/tests</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2dda79b0da73" width="1" height="1" alt=""><hr><p><a href="https://medium.com/sissa-mathlab/integrating-c-and-python-for-scientific-computing-2dda79b0da73">Integrating C++ and Python for Scientific Computing</a> was originally published in <a href="https://medium.com/sissa-mathlab">SISSA mathLab</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>