基于Cython混合编程的电阻率法正演优化
Resistivity Forward Modeling Optimization Based on Cython Hybrid Programming
DOI: 10.12677/ag.2026.164049, PDF,    科研立项经费支持
作者: 李文海, 陈汉波*, 吴彬海, 李明晟, 王立辉:桂林理工大学地球科学学院,广西 桂林
关键词: 有限单元法混合编程Cython优化性能分析Finite Element Method Hybrid Programming Cython Optimization Performance Analysis
摘要: 本文以纯Python实现的电阻率法有限元正演算法为研究对象,开展了一套从性能剖析到靶向优化的混合编程实践研究。通过性能剖析发现,在中小规模网格下,矩阵组装耗时占总计算时间的75%以上,是程序的主要性能瓶颈。针对这一瓶颈,对全局刚度矩阵组装(含单元计算)模块进行Cython静态类型重写,显著提升了代码执行效率。实验结果表明,优化后的算法在保证计算精度的前提下,矩阵组装模块获得最高62倍的加速比,将Python解释器在热点函数中的开销降至可忽略水平。在典型测试模型下,矩阵组装模块获得53倍加速比,程序整体执行效率提升约6.4倍。
Abstract: This paper presents a case study of hybrid programming for a pure Python-implemented finite element forward modeling algorithm for the resistivity method, spanning from performance profiling to targeted optimization. Through performance profiling, it was found that for small to medium-scale meshes, the matrix assembly process accounts for over 75% of the total computation time, making it the primary performance bottleneck of the program. To address this, the global stiffness matrix assembly module (including element calculations) was rewritten with Cython static typing, significantly enhancing code execution efficiency. Experimental results demonstrate that the optimized algorithm achieves a speedup of up to 62x for the matrix assembly module while maintaining computational accuracy, reducing the overhead of the Python interpreter in hot functions to negligible levels. Under the typical test model, the matrix assembly module achieves a 53x speedup, and the overall program execution efficiency improves by approximately 6.4x.
文章引用:李文海, 陈汉波, 吴彬海, 李明晟, 王立辉. 基于Cython混合编程的电阻率法正演优化[J]. 地球科学前沿, 2026, 16(4): 548-557. https://doi.org/10.12677/ag.2026.164049

参考文献

[1] 黄俊革. 三维电阻率/极化率有限元正演模拟与反演成像[D]: [博士学位论文]. 长沙: 中南大学, 2003.
[2] Qin, C., Fu, W., Zhao, N. and Zhou, J. (2025) 3D Adaptive Finite-Element Forward Modeling for Direct Current Resistivity Method Using Geometric Multigrid Solver. Computers & Geosciences, 196, Article ID: 105840. [Google Scholar] [CrossRef
[3] Jarecka, D., Arabas, S., Fijalkowski, M., et al. (2012) On the Tradeoffs of Programming Language Choice for Numerical Modelling in Geoscience. A Case Study Comparing Modern Fortran, C++/Blitz++ and Python/NumPy. Proceedings of the EGU General Assembly Conference Abstracts, Vienna, 22-27 April 2012, EGU2012-680.
[4] Arabas, S., Jarecka, D. and Jaruga, A. (2013) Object-Oriented Implementations of the MPDATA Advection Equation Solver in C++, Python and Fortran. arXiv: 13011334.
[5] Behnel, S., Bradshaw, R., Citro, C., Dalcin, L., Seljebotn, D.S. and Smith, K. (2011) Cython: The Best of Both Worlds. Computing in Science & Engineering, 13, 31-39. [Google Scholar] [CrossRef
[6] 石靖. 基于Python一站式编程的谱激电法三维有限元数值模拟[D]: [硕士学位论文]. 桂林: 桂林理工大学, 2025.
[7] 徐世浙. 地球物理中的有限单元法[M]. 北京: 科学出版社, 1994.
[8] Zhao, S. and Yedlin, M.J. (1996) Some Refinements on the Finite-Difference Method for 3-D Dc Resistivity Modeling. Geophysics, 61, 1301-1307. [Google Scholar] [CrossRef
[9] 阮百尧, 熊彬, 徐世浙. 三维地电断面电阻率测深有限元数值模拟[J]. 地球科学: 中国地质大学学报, 2001, 26(1): 73-77.
[10] 熊彬, 熊心如, 徐志锋, 等. 引入Green函数实现电阻率法总电位高精度三维正演[J]. 地球物理学报, 2025, 68(7): 2786-2791.
[11] Lam, S.K., Pitrou, A. and Seibert, S. (2015) Numba: A LLVM-Based Python JIT Compiler. Proceedings of the Second Workshop on the LLVM Compiler Infrastructure in HPC, Austin, 15 November 2015, 1-6. [Google Scholar] [CrossRef
[12] Foreman, D.J., Chalasani, S. and Walker, M.R. (2025) Quantifying Code Binding Performance for Navigation Algorithms. 2025 IEEE/ION Position, Location and Navigation Symposium (PLANS), Salt Lake City, 28 April-1 May 2025, 166-172. [Google Scholar] [CrossRef
[13] Schenk, O. and Gärtner, K. (2004) Solving Unsymmetric Sparse Systems of Linear Equations with PARDISO. Future Generation Computer Systems, 20, 475-487. [Google Scholar] [CrossRef