[Paper Review] An efficient way to assemble finite element matrices in vector languages
This paper presents highly optimized, vectorized algorithms for assembling finite element matrices in arbitrary dimensions using high-level vector languages like MATLAB, Octave, Python, and Julia. By eliminating loops through advanced sparse matrix operations and leveraging symmetry, the proposed OptVS algorithm achieves performance within 2.9x of C in scalar cases and 4.1x in vector cases, demonstrating that vector languages can rival compiled languages for finite element assembly when properly optimized.
Efficient Matlab codes in 2D and 3D have been proposed recently to assemble finite element matrices. In this paper we present simple, compact and efficient vectorized algorithms, which are variants of these codes, in arbitrary dimension, without the use of any lower level language. They can be easily implemented in many vector languages (e.g. Matlab, Octave, Python, Scilab, R, Julia, C++ with STL,...). The principle of these techniques is general, we present it for the assembly of several finite element matrices in arbitrary dimension, in the P1 finite element case. We also provide an extension of the algorithms to the case of a system of PDE's. Then we give an extension to piecewise polynomials of higher order. We compare numerically the performance of these algorithms in Matlab, Octave and Python, with that in FreeFEM++ and in a compiled language such as C. Examples show that, unlike what is commonly believed, the performance is not radically worse than that of C : in the best/worst cases, selected vector languages are respectively 2.3/3.5 and 2.9/4.1 times slower than C in the scalar and vector cases. We also present numerical results which illustrate the computational costs of these algorithms compared to standard algorithms and to other recent ones.
Motivation & Objective
- To develop efficient, portable, and readable vectorized algorithms for assembling finite element matrices in arbitrary dimensions using high-level vector languages.
- To overcome the performance bottleneck of loop-based matrix assembly in scientific computing environments.
- To demonstrate that vectorized implementations in languages like MATLAB, Octave, and Python can achieve performance close to that of compiled C code.
- To extend the approach to systems of PDEs and higher-order finite elements, ensuring broad applicability.
Proposed method
- The authors introduce a vectorized assembly strategy based on sparse matrix operations, replacing nested loops with element-wise array operations and matrix multiplications.
- The core technique uses global index mapping and Kronecker-like operations to compute element contributions in bulk, avoiding explicit loops over elements.
- The OptVS algorithm exploits matrix symmetry to reduce redundant computations, improving efficiency and memory usage.
- The method is generalized to arbitrary dimensions and extended to handle systems of PDEs with variable coefficients.
- The approach is implemented using standard sparse matrix data structures (e.g., CSR/CSC) available in most vector languages.
- The algorithm is validated across multiple languages and problem types, including stiffness and elastic stiffness matrices in 2D and 3D.
Experimental results
Research questions
- RQ1Can vectorized algorithms in high-level languages like MATLAB, Octave, and Python achieve performance competitive with compiled C code for finite element matrix assembly?
- RQ2How does the computational complexity and performance of loop-based vs. vectorized assembly methods scale with problem size and dimensionality?
- RQ3To what extent can symmetry and sparse matrix operations be leveraged to optimize matrix assembly in finite element methods?
- RQ4How do different vector languages (MATLAB, Octave, Python) compare in performance when implementing the same optimized algorithm?
- RQ5Can the proposed vectorization strategy be generalized to systems of PDEs and higher-order finite elements?
Key findings
- The OptVS algorithm achieves a computational complexity of O(n_dof), making it asymptotically optimal for matrix assembly.
- In the best-case scenario, the vectorized implementation in selected languages is only 2.3 times slower than C for scalar problems, and 2.9 times slower for vector problems.
- For the 2D stiffness matrix, the base implementation is up to 550 times slower in Octave compared to the OptVS version.
- The OptV1 version is 40–550 times slower than OptVS across different languages, highlighting the performance gain from vectorization.
- The performance gap between vector languages and C is significantly smaller than commonly believed, especially when using optimized sparse matrix operations.
- The method scales efficiently to 3D and higher-order elements, with only minor modifications required for higher-order P1 finite elements.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.