NumPy numerical calculation optimization method and system based on RISC-V vector extension and application

By optimizing the computational flow of NumPy on the RISC-V architecture and combining it with RISC-V vector extension instructions, the problem of limited computational performance of NumPy on the RISC-V platform is solved, achieving efficient vector and matrix operations, which are suitable for scientific computing and artificial intelligence.

CN121233166AActive Publication Date: 2025-12-30SHANDONG UNIV
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202511181346.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-08-22
Publication Date
2025-12-30
Estimated Expiration
2045-08-22

AI Technical Summary

Technical Problem

In the existing technology, NumPy lacks a systematic optimization scheme based on RISC-V vector extension on the RISC-V architecture, which limits its computing performance, and existing hardware improvement schemes are difficult to adapt to widely.

Method used

By analyzing the computation process of NumPy and combining it with the RISC-V vector extension instruction set, the data loading, element-wise operation, and data output stages are optimized. Dynamic hardware detection and process-oriented design are adopted to optimize vector extension for specific scenarios, including element-wise operations on vectors and matrices, broadcast operations, etc.

Benefits of technology

It significantly improves the efficiency of operations without calling the BLAS library, enhances the NumPy computing performance on the RISC-V platform, especially in broadcast and element-wise computing scenarios, and has strong compatibility, making it suitable for scientific computing, artificial intelligence, and big data analysis.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121233166A_ABST
    Figure CN121233166A_ABST
Patent Text Reader

Abstract

The invention relates to a NumPy numerical calculation optimization method and system based on RISC-V vector extension and application, belongs to the technical field of computer science and high-performance calculation application, and is used for performing high-performance optimization on a core calculation part which does not call a BLAS library, such as vector element-by-element calculation and matrix element-by-element calculation. On the basis of analysis and calculation steps, the performance of NumPy in broadcast operation and element-by-element operation scenes is improved through technologies such as underlying code substitution, loop optimization, data loading and memory alignment optimization, so that support is provided for scientific calculation and high-performance calculation based on the RISC-V architecture.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to a RISC-V Vector Extension (RVV)-based optimization method and system for NumPy numerical computation, and its application in the open-source numerical computation library NumPy, belonging to the fields of computer science and high-performance computing application technology. Background Technology

[0002] NumPy is a fundamental tool in the field of Python scientific computing, widely used in scenarios such as artificial intelligence, data analysis, and numerical computation. Its efficiency heavily relies on the optimization of the underlying hardware. However, currently, on the RISC-V architecture, NumPy's element-wise operations still lack a systematic optimization scheme based on RISC-V vector extensions, which significantly limits its computational performance.

[0003] Existing work includes NumPy optimization schemes for the ARM architecture, which identify key modules by scanning the code and achieve optimization through SIMD instructions and loop unrolling. While this approach improves performance to some extent, it is limited to simple parallelization of low-level instructions, lacking global optimization design for the algorithm itself and data flow. Furthermore, it is deeply tied to the ARM architecture, making direct adaptation to the RISC-V architecture difficult. In addition, existing RISC-V-based computing devices mostly focus on hardware design and system architecture, accelerating specific types of computing tasks by designing dedicated AI processors, multi-precision vector operation devices, or coprocessors. These solutions aim to improve performance by changing or adding hardware, resulting in limited improvements and limited applicability.

[0004] Therefore, optimizing NumPy's computational efficiency by combining RISC-V vector extensions with the RISC-V architecture, and overcoming the limitations of existing work on the underlying hardware, has become an urgent problem to be solved. Summary of the Invention

[0005] To address the shortcomings of existing technologies, this invention provides a method, system, and application for optimizing NumPy numerical computation based on RISC-V vector extension instructions, aiming to improve the running efficiency of NumPy on the RISC-V platform. Compared with existing technologies, this invention mainly focuses on the computational flow of NumPy, optimizing the data loading, element-wise operations, and data output stages using RISC-V vector extension instructions. Since NumPy typically calls external high-performance computing libraries (such as BLAS) for inner product operations and matrix multiplication, this invention focuses on high-performance optimization of core computational parts that do not call BLAS libraries, such as vector element-wise operations and matrix element-wise calculations. Based on the analysis of computational steps, techniques such as low-level code replacement, loop optimization, and data loading and memory alignment optimization are used to improve the performance of NumPy in broadcast and element-wise operation scenarios, thereby providing support for scientific computing and high-performance computing based on the RISC-V architecture.

[0006] This invention proposes a design framework for computational process decomposition and optimization based on the original computational flow of NumPy and the characteristics of the RISC-V instruction set architecture. Unlike existing methods that uniformly accelerate computationally intensive code by scanning the entire codebase, this invention starts with the code execution flow and proposes an optimized flow based on RISC-V vector extensions for element-wise operations in specific scenarios. By implementing specific operations (such as element-wise vector addition and element-wise matrix multiplication) using the RISC-V vector extension instruction set, and combining memory access optimization and loop unrolling, this invention can significantly improve the efficiency of operations that do not call the BLAS library.

[0007] The technical solution of the present invention is as follows: A method for optimizing NumPy numerical computation based on RISC-V vector extension instructions includes the following steps: Step 1: Dynamic hardware detection and RISC-V vector extension support determination; After code analysis, this invention designed a hardware environment dynamic detection module to determine whether the target device supports RISC-V vector extension instructions, and then decide whether to enable RISC-V vector extension-related optimization steps. Preferred options include: RISC-V Vector Extensions Support Detection: When the program starts, it reads the processor hardware information and determines whether the target device supports RISC-V vector extension by accessing CSRs (Control and Status Registers). The current RISC-V vector extension version features, including the vector register length VLEN and the supported operation bit width, are detected to match the available optimization instructions. Specifically, it is checked whether half-precision floating-point operations (Zvfh) are supported. If the hardware does not support 16-bit floating-point vector instructions, then the corresponding vector instructions cannot be used for optimization of float16 arrays in NumPy. Dynamic path selection: If the hardware supports RISC-V vector extension and meets the minimum optimization requirements, proceed to step 2; the minimum optimization requirements are that the vector extension specification version is 0.7.1 or higher. If the hardware does not support RISC-V vector extensions or does not meet the minimum optimization requirements, it will fall back to the traditional scalar implementation path to ensure that the function works properly in all environments.

[0008] Step 2: Classification and Dynamic Management This invention designs a dynamic computation process management method to determine whether to call an external high-efficiency library and select an adaptation strategy: Methods for calling external high-efficiency libraries: For computational tasks that call external high-efficiency libraries (such as BLAS) (such as matrix multiplication, matrix inner product operations, etc.), the external library is directly called for execution without enabling the RISC-V vector extension optimization path; Element-wise operations without calling external high-efficiency libraries: For core computational tasks that do not call external high-efficiency libraries (such as element-wise addition, subtraction, multiplication and division of vectors or matrices), enable the RISC-V vector extension optimization strategy and proceed to step 3 to achieve efficient computation by combining hardware characteristics.

[0009] Step 3: Optimize the process This invention adopts a process-oriented design approach. Based on the NumPy calculation process, the optimization focus is placed on the core calculation part that does not call external BLAS libraries. The overall process is as follows: Analyzing the underlying implementation and execution flow of NumPy, the calculation steps are divided into the following parts: 1-1. Data loading and type conversion; 1-2. Core calculations, including element-wise operations on vectors or matrices; 1-3. Data output and result conversion.

[0010] Step-by-step optimization strategy: Specific algorithms and RISC-V vector extension instructions are used to optimize the process, minimizing changes to the existing NumPy code structure and improving performance.

[0011] Based on a comprehensive analysis of the core operations of NumPy element-wise operations, this invention proposes a series of optimization methods based on RISC-V vector extensions for common element-wise vector and matrix operations. These optimization methods meticulously design each step of the element-wise operation, and combined with the parallel computing capabilities of RISC-V vector extension instructions, significantly improve data processing efficiency and overall performance. Specifically, these methods include the following sub-methods.

[0012] Preferably, 1-1, data loading and type conversion optimization includes the following steps: NumPy computations involve loading native Python data structures (such as lists) into NumPy arrays; since data loading and type conversion are typically the initial steps in the computation process, their performance directly impacts subsequent calculations. This invention optimizes the following details: Python list dereferencing and value extraction: When the input data is a Python list, first iterate through all elements, dereference each element, and extract the value; The RISC-V Vector Extensions instruction set is used to perform batch optimization of pointer dereference operations in the list, and vector registers are used to load values ​​in parallel, avoiding the performance overhead of dereferencing point by point.

[0013] Optimize loading mechanism: Uniform data type: When the data types are consistent, such as all integers or floating-point numbers, the RISC-V vector extension vector loading instructions (e.g., VLE) can be used directly. <sew>_v_f <vsew> m <lmul>Batch loading is performed into the vector register to maximize the use of parallel loading capabilities; Non-uniform data type: When data types are inconsistent, this invention sets up a boundary recovery module based on RISC-V vector extension optimization to perform individual verification and adaptation on non-uniform data types, ensuring that the data processing is correct and error-free. By optimizing RVV parallel processing from serial sequential processing of data loading and type conversion, the data initialization speed is significantly accelerated, providing a foundation for efficient execution of subsequent operations.

[0014] Preferably, steps 1-2 include the following: 1) Optimization of element-wise vector operations Element-wise vector operations are among the most commonly used operations in NumPy, including but not limited to addition, subtraction, multiplication, division, and multiplication-addition operations (such as a + b, a / b). For this type of operation, this invention designs an efficient parallel implementation method on the RVV platform, with the specific optimization strategy as follows: Parallel loading operands: Take two vector arrays to be operated on (e.g., array1 and array2), and use RISC-V vector extension vector loading instructions (e.g., VLE) to load the vectors. <sew>_v_f <vsew> m <lmul>This batch loads its elements into the vector register, reducing the number of memory access calls; Element-by-element computation optimization: Utilizing RISC-V vector extension instructions (e.g., vfadd_vv_f) <vsew> m <lmul>Used for element-wise addition, vfmul_vv_f <vsew> m <lmul>For element-wise multiplication), directly perform element-wise operations in the vector register; at the same time, pipeline the calculation results in the register to maximize the instruction execution efficiency and reduce the complexity of operations between registers; Data alignment and memory access optimization: When storing the results, directly complete the storage operation in batches in the vector register to avoid frequent memory access operations; In the loading and calculation stages, complete the alignment processing of memory data in advance, optimize the memory access order, and improve the cache hit rate; Through the above optimization measures, the processing efficiency of vector element-wise operations has been significantly improved, especially in the case of large data scales.

[0015] 2) Matrix element-wise operation optimization Matrix element-wise calculations (including A + B, A - B, A * B, A / B, A > B, A < B, A == B, MAX(A, B)) are another common operation in NumPy calculations. In order to achieve efficient operation by combining the structured parallel mechanism of RVV, the present invention designs the following specific strategies: The optimizations include the following: Matrix block processing: Decompose the large matrix to be operated into multiple smaller blocks to adapt to the storage capacity of the RISC-V vector register; the size of the blocks is not arbitrarily set, but dynamically determined according to the vector processing ability of the processor; specifically, the number of elements continuously processed in each block in memory is set based on the key parameters of the RISC-V vector extension, including the vector register length (VLEN), the bit width of the data elements in the current operation (SEW), and the vector register grouping multiplier (LMUL); Further preferably, the size of each block is set to an integer multiple of (VLEN / SEW) * LMUL to ensure that in loop processing, the vector load instruction can fill the vector register maximally, thereby achieving the highest calculation parallelism. Therefore, the total number of blocks is determined by dividing the total size of the matrix by the size of a single block calculated according to the above parameters, without presetting a fixed number.

[0016] Independently perform element-wise calculations on each block, and use RISC-V vector extension instructions (such as vadd.vv or vmul.vv) to implement element-by-element processing between matrix blocks; Memory access pattern optimization: For the row alignment and column alignment problems in matrix operations, use the vector load instruction (such as vle) in RVV in combination with the register rearrangement mechanism to optimize the memory access order and minimize the performance loss caused by cross-row or cross-column memory access; Output storage optimization: After each block of computation is completed, the result is written directly back to memory, using RVV's vector write-back mechanism (such as VSE) to write multiple elements at once. Specifically, in the processing loop, after each block of computation is completed, the result of that block is immediately written from the vector register back to the corresponding location in memory. This process uses RVV's vector storage instructions (such as VSE) to write multiple elements at once, ensuring that the computation result is released from register resources and written to disk in a timely manner, preparing for the computation of the next block. This pipelined mode of alternating "computation-storage" minimizes memory access bottlenecks and intermediate data caching pressure caused by waiting for all computations to complete, and reduces memory access overhead during output.

[0017] By combining block partitioning and memory operation optimization schemes, this invention can efficiently handle matrix element-wise calculation scenarios, especially in high-dimensional matrix operations.

[0018] 3) Broadcast operation optimization NumPy supports a broadcast mechanism. When a vector or matrix is ​​broadcast to a scalar value, or a matrix is ​​broadcast to a vector (the method of this invention is applicable to various broadcast operation scenarios, including but not limited to arithmetic operations (such as addition, subtraction, multiplication, and division) and comparison operations (such as greater than, less than, and equal to)), the scalar value must be matched with other elements of the vector or matrix in a broadcast manner. This method has the following optimization design for broadcast scenarios: RISC-V Vector Computation Optimization: In the RISC-V vector register, a scalar value is loaded as a broadcast vector, and batch operations are performed using RVV vector-scalar computation instructions (such as vfadd_vf_f). <vsew> m <lmul>Used for scalar + vector, vfmul_vf_f <vsew> m <lmul>For scalar * vector, the specific calls to Intrinsic functions include, but are not limited to, floating-point arithmetic operations, integer arithmetic operations, and comparison operations: Floating-point arithmetic operations: Vector + scalar addition: vfadd_vf_f64m2 Vector-scalar subtraction: vfsub_vf_f64m2 Scalar-vector subtraction: vfrsub_vf_f64m2 Vector * scalar multiplication: vfmul_vf_f64m2 Vector / scalar division: vfdiv_vf_f64m2 Integer arithmetic operations: Vector + scalar addition: vadd_vx_i64m2 Vector-scalar subtraction: vsub_vx_i64m2 Vector * scalar multiplication: vmul_vx_i64m2 Bitwise AND: vand_vx_i64m2 Bitwise OR: vor_vx_i64m2 Comparison operations: This type of operation returns a boolean mask vector, which can be used for subsequent conditional execution or logical judgment.

[0019] Equals: vmfeq_vf_f32m1_b32 Not equal to: vmfne_vf_f32m1_b32 Greater than: vmfgt_vf_f32m1_b32 Less than: vmflt_vf_f32m1_b32 This avoids processing each element individually, thereby improving computational efficiency.

[0020] Preferably, in steps 1-3, the output and data type conversion are optimized. After element-wise operations, NumPy typically needs to restore the result from the array to a Python object or return it to an external program; this step optimizes subsequent processing steps, specifically: Batch reading and conversion: The calculation results in NumPy arrays are read in batches using RISC-V vector loading instructions (such as vle32 or vle64). The number of data read each time is dynamically adjusted by the vector length in the hardware, that is, flexibility is achieved by dynamically setting the VL parameter. The read data is converted into Python objects in batches using parallel processing: Integers are wrapped as PyLongObjects; Floating-point numbers are wrapped as PyFloatObject.

[0021] The above optimizations significantly reduce the overhead of the output conversion process and shorten the response time from calculation results to program interaction.

[0022] A NumPy numerical computation optimization system based on RISC-V vector extension includes a function interface and distribution module, a hardware detection module, a dynamic process management module, and an RVV optimization module. The function interface and dispatch module is used to receive NumPy function call requests from upper-layer applications (such as Python scripts) at runtime and obtain array data to be processed; this module is the entry point of the entire optimization method, which dispatches the received function call type and data to the dynamic process management module; this corresponds to the starting point of method execution; Hardware detection module: Used to detect whether the target device supports RVV extended instructions when the program starts, and to query its specific hardware characteristics; Dynamic process management module: used to determine whether a specific computing task calls an external high-efficiency library (BLAS) and select the corresponding computing path; RVV Optimization Module: Used to optimize element-wise computation tasks that do not call external libraries using the RVV instruction set, improving performance through vectorized computation.

[0023] A computer-readable storage medium having a program stored thereon that, when executed by a processor, implements the steps in the previously described RISC-V vector extension-based NumPy numerical computation optimization method.

[0024] An electronic device includes a memory, a processor, and a program stored in the memory and executable on the processor, wherein the processor executes the program to implement the steps in the NumPy numerical computation optimization method based on RISC-V vector extension as described above.

[0025] This invention belongs to the field of computer software and algorithm optimization, and runs on general-purpose processors conforming to the RISC-V standard. The "computational process decomposition and process optimization" framework proposed in this invention is an effective method that tightly integrates high-level algorithm logic with low-level instruction set characteristics. Through in-depth analysis of NumPy's internal working mechanism, it achieves more efficient and precise performance improvements than general-purpose compiler optimization, especially in scenarios such as broadcasting and element-wise computation. The method of this invention does not rely on any custom coprocessors or non-standard instruction sets. It only utilizes the official standard vector extensions of RISC-V, therefore it can be applied to any RISC-V processor equipped with standard vector extension functionality, resulting in wider applicability and lower implementation costs.

[0026] The beneficial effects of this invention are as follows: 1. Significantly improved computational efficiency. This invention optimizes NumPy element-wise computation by introducing RVV vector extension instructions on the RISC-V architecture hardware platform. For example, batch element-wise operations on vectors and matrices convert scalar computation into parallel computation, significantly reducing loop iterations and memory accesses, and greatly improving computational efficiency.

[0027] 2. Improved hardware utilization. This invention fully leverages the hardware parallel computing capabilities of RVV, utilizes vector registers for large-scale batch processing operations, optimizes memory access patterns, avoids the waste of hardware resources in traditional scalar computing, and further reduces the bottleneck impact of memory access latency on computing performance.

[0028] 3. Strong compatibility and adaptability. During the compilation phase, this invention detects whether the target processor supports RVV instruction extensions. When RVV is not supported, it automatically selects to fall back to the NumPy general implementation path, ensuring the integrity and compatibility of the computational logic and adapting to the needs of different hardware platforms.

[0029] 4. Wide range of practical applications. This invention is applicable to scenarios requiring efficient element-wise computation of vectors and matrices, such as scientific computing, artificial intelligence model training, and big data analysis. It offers significant performance improvements and computational advantages, especially in handling large-scale data tasks, expanding the possibilities for NumPy applications on the RISC-V platform. Attached Figure Description

[0030] Figure 1 This is a flowchart of an optimization method for NumPy numerical computation based on RISC-V vector extension. Detailed Implementation

[0031] The present invention will be further described below with reference to the embodiments and accompanying drawings, but is not limited thereto. Example

[0032] A method for optimizing NumPy numerical computation based on RISC-V vector extension instructions, the process is as follows: Figure 1 As shown, the steps are as follows: Step 1: Dynamic hardware detection and RISC-V vector extension support determination.

[0033] After code analysis, this invention designed a hardware environment dynamic detection module to determine whether the target device supports RISC-V vector extension instructions, and then decide whether to enable RISC-V vector extension-related optimization steps.

[0034] Specifically, it includes: RISC-V Vector Extensions Support Detection: When the program starts, it reads the processor hardware information and determines whether the target device supports RISC-V vector extension by accessing CSRs (Control and Status Registers).

[0035] The current RISC-V vector extension version features are detected, including the vector register length VLEN and the supported operation bit width, to match the available optimization instructions. Specifically, it checks whether half-precision floating-point operations (Zvfh) are supported. If the hardware does not support 16-bit floating-point vector instructions, then the corresponding vector instructions cannot be used for optimization of float16 arrays in NumPy.

[0036] Dynamic path selection: If the hardware supports RISC-V vector extension and meets the minimum optimization requirements, proceed to step 2; the minimum optimization requirements are that the vector extension specification version is 0.7.1 or higher.

[0037] If the hardware does not support RISC-V vector extensions or does not meet the minimum optimization requirements, it will fall back to the traditional scalar implementation path to ensure that the function works properly in all environments.

[0038] Step 2: Classification and Dynamic Management This invention designs a dynamic computation process management method to determine whether to call an external high-efficiency library and select an adaptation strategy: Methods for calling external high-efficiency libraries: For computational tasks that call external high-efficiency libraries (such as BLAS) (such as matrix multiplication, matrix inner product operations, etc.), the external library is directly passed for execution without enabling the RISC-V vector extension optimization path.

[0039] Element-wise operations without calling external high-efficiency libraries: For core computational tasks that do not call external high-efficiency libraries (such as element-wise addition, subtraction, multiplication and division of vectors or matrices), enable the RISC-V vector extension optimization strategy and proceed to step 3 to achieve efficient computation by combining hardware characteristics.

[0040] Step 3: Optimize the process This invention adopts a process-oriented design approach. Based on the NumPy calculation process, the optimization focus is placed on the core calculation part that does not call external BLAS libraries. The overall process is as follows: Analyzing the underlying implementation and execution flow of NumPy, the calculation steps are divided into the following parts: 1-1. Data loading and type conversion; 1-2. Core calculations, including element-wise operations on vectors or matrices; 1-3. Data output and result conversion.

[0041] Step-by-step optimization strategy: Specific algorithms and RISC-V vector extension instructions are used to optimize the process, minimizing changes to the existing NumPy code structure and improving performance.

[0042] Based on a comprehensive analysis of the core operations of NumPy element-wise operations, this invention proposes a series of optimization methods based on RISC-V vector extensions for common element-wise vector and matrix operations. These optimization methods meticulously design each step of the element-wise operation, and combined with the parallel computing capabilities of RISC-V vector extension instructions, significantly improve data processing efficiency and overall performance. Specifically, these methods include the following sub-methods.

[0043] 1-1. Data loading and type conversion optimization, including the following steps: NumPy computations involve loading native Python data structures (such as lists) into NumPy arrays; since data loading and type conversion are typically the initial steps in the computation process, their performance directly impacts subsequent calculations. This invention optimizes the following details: Python list dereferencing and value extraction: When the input data is a Python list, first iterate through all elements, dereference each element, and then extract the value.

[0044] The RISC-V Vector Extensions instruction set is used to perform batch optimization of pointer dereference operations in the list, and vector registers are used to load values ​​in parallel, avoiding the performance overhead of dereferencing point by point.

[0045] Optimize loading mechanism: Uniform data type: When the data types are consistent, such as all integers or floating-point numbers, the RISC-V vector extension vector loading instructions (e.g., VLE) can be used directly. <sew>_v_f <vsew> m <lmul>Batch loading is performed into the vector register to maximize the use of parallel loading capabilities.

[0046] Non-uniform data types: When data types are inconsistent, this invention sets up a boundary recovery module based on RISC-V vector extension optimization to perform individual verification and adaptation on non-uniform data types, ensuring that the data processing is correct.

[0047] By optimizing RVV parallel processing from serial sequential processing of data loading and type conversion, the data initialization speed is significantly accelerated, providing a foundation for efficient execution of subsequent operations.

[0048] Steps 1-2 include the following: 1) Optimization of element-wise vector operations Element-wise vector operations are among the most commonly used operations in NumPy, including but not limited to addition, subtraction, multiplication, division, and multiplication-addition operations (such as a + b, a / b). For this type of operation, this invention designs an efficient parallel implementation method on the RVV platform, with the specific optimization strategy as follows: Parallel loading operands: Take two vector arrays to be operated on (e.g., array1 and array2), and use RISC-V vector extension vector loading instructions (e.g., VLE) to load the vectors. <sew>_v_f <vsew> m <lmul>This batch loads its elements into the vector register, reducing the number of memory access calls.

[0049] Element-by-element computation optimization: Utilizing RISC-V vector extension instructions (e.g., vfadd_vv_f) <vsew> m <lmul>Used for element-wise addition, vfmul_vv_f <vsew> m <lmul>Directly perform element-wise operations in vector registers; meanwhile, pipeline the calculation results within the registers to maximize the efficiency of instruction execution and reduce the complexity of inter-register operations.

[0050] Data alignment and memory access optimization: Directly complete the storage operation in batches in the vector register when storing the results, avoiding frequent memory access operations.

[0051] Align the memory data in advance during the loading and calculation stages, optimize the memory access order, and improve the cache hit rate.

[0052] Through the above optimization measures, the processing efficiency of vector element-wise operation is significantly improved, especially in large data scale.

[0053] 2) Matrix element-wise operation optimization Matrix element-wise calculation (including A + B, A-B, A * B, A / B, A>B, A<B, A == B, MAX(A, B)) is another common operation in NumPy calculation. In order to realize efficient operation combined with the structured parallel mechanism of RVV, the following specific strategies are designed: Matrix block processing: The large matrix to be operated is decomposed into multiple smaller blocks to adapt to the storage capacity of RISC-V vector registers; the size of the block is not arbitrarily set, but is dynamically determined according to the vector processing capacity of the processor; specifically, the number of elements processed continuously in memory for each block is set according to the key parameters of RISC-V vector extension, including vector register length (VLEN), current data element bit width (SEW), and vector register grouping multiplier (LMUL).

[0054] The size of each block is set to an integer multiple of (VLEN / SEW) * LMUL to ensure that the vector load instruction can maximize the filling of the vector register during loop processing, thereby achieving the highest calculation parallelism. Therefore, the total number of blocks is determined by the total size of the matrix divided by the size of a single block calculated according to the above parameters, without pre-setting a fixed number.

[0055] Perform element-wise calculation on each block independently, and use RISC-V vector extension instructions (such as vadd.vv or vmul.vv) to implement element-wise processing between matrix blocks; Memory access mode optimization: For the row alignment and column alignment problems in matrix operation, use the vector load instruction (such as vle) in RVV to optimize the memory access order with the register rearrangement mechanism to minimize the performance loss caused by cross-row or cross-column memory access.

[0056] Output storage optimization: After each block of computation is completed, the result is written directly back to memory, using RVV's vector write-back mechanism (such as VSE) to write multiple elements at once. Specifically, in the processing loop, after each block of computation is completed, the result of that block is immediately written from the vector register back to the corresponding location in memory. This process uses RVV's vector storage instructions (such as VSE) to write multiple elements at once, ensuring that the computation result is released from register resources and written to disk in a timely manner, preparing for the computation of the next block. This pipelined mode of alternating "computation-storage" minimizes memory access bottlenecks and intermediate data caching pressure caused by waiting for all computations to complete, and reduces memory access overhead during output.

[0057] By combining block partitioning and memory operation optimization schemes, this invention can efficiently handle matrix element-wise calculation scenarios, especially in high-dimensional matrix operations.

[0058] 3) Broadcast operation optimization NumPy supports a broadcast mechanism. When a vector or matrix is ​​broadcast to a scalar value, or a matrix is ​​broadcast to a vector (the method of this invention is applicable to various broadcast operation scenarios, including but not limited to arithmetic operations (such as addition, subtraction, multiplication, and division) and comparison operations (such as greater than, less than, and equal to)), the scalar value must be matched with other elements of the vector or matrix in a broadcast manner. This method has the following optimization design for broadcast scenarios: RISC-V Vector Computation Optimization: In the RISC-V vector register, a scalar value is loaded as a broadcast vector, and batch operations are performed using RVV vector-scalar computation instructions (such as vfadd_vf_f). <vsew> m <lmul>For scalar + vector, vfmul_vf_f <vsew> m <lmul>For scalar-vector, specific call Intrinsic functions, including but not limited to floating-point arithmetic operations, integer arithmetic operations, comparison operations: Floating-point arithmetic operations: Vector + scalar addition: vfadd_vf_f64m2 Vector - scalar subtraction: vfsub_vf_f64m2 Scalar - vector subtraction: vfrsub_vf_f64m2 Vector * scalar multiplication: vfmul_vf_f64m2 Vector / scalar division: vfdiv_vf_f64m2 Integer arithmetic operations: Vector + scalar addition: vadd_vx_i64m2 Vector - scalar subtraction: vsub_vx_i64m2 Vector * scalar multiplication: vmul_vx_i64m2 Bitwise AND: vand_vx_i64m2 Bitwise OR: vor_vx_i64m2 Comparison operations: These operations return a boolean mask vector for subsequent conditional execution or logical judgment.

[0059] Equal: vmfeq_vf_f32m1_b32 Not equal: vmfne_vf_f32m1_b32 Greater than: vmfgt_vf_f32m1_b32 Less than: vmflt_vf_f32m1_b32 Avoid element-wise processing to improve operation efficiency.

[0060] In steps 1-3, output and data type conversion optimization After element-wise operations, NumPy usually needs to restore the results from arrays to Python objects or return them to external programs; this step optimizes the post-processing link, which is: Batch reading and conversion: Use RISC-V vector load instructions (such as vle32 or vle64) to batch read the calculation results in the NumPy array, and the number of reads is dynamically adjusted by the hardware vector length, that is, flexibility is achieved by dynamically setting the VL parameter.

[0061] The read data is batch converted to Python objects through parallel processing: Integers are wrapped as PyLongObjects; Floating point numbers are wrapped as PyFloatObjects.

[0062] The above optimization greatly reduces the running overhead of the output conversion link, shortens the response time from the calculation result to the program interaction.

[0063] Embodiment 2 A NumPy numerical calculation optimization system based on RISC-V vector extension includes a function interface and distribution module, a hardware detection module, a dynamic flow management module, and an RVV optimization module.

[0064] The function interface and distribution module is used to receive NumPy function call requests from upper-layer application programs (such as Python scripts) at runtime and obtain array data to be processed. The module is the entrance of the entire optimization method, and it distributes the received function call types and data to the dynamic flow management module. This corresponds to the starting point of method execution.

[0065] The hardware detection module is used to detect whether the target device supports RVV extension instructions at program startup and query its specific hardware characteristics.

[0066] The dynamic flow management module is used to determine whether a specific calculation task calls an external efficient library (BLAS) and select the corresponding calculation path.

[0067] The RVV optimization module is used to optimize element-by-element operation tasks that do not call external libraries using the RVV instruction set to improve performance through vectorized computation.

[0068] Embodiment 3 A computer-readable storage medium having a program stored thereon, which, when executed by a processor, implements the steps of the NumPy numerical calculation optimization method based on RISC-V vector extension as described in Embodiment 1.

[0069] Embodiment 4 An electronic device includes a memory, a processor, and a program stored on the memory and executable on the processor, and the processor executes the program to implement the steps of the NumPy numerical calculation optimization method based on RISC-V vector extension as described in Embodiment 1.< / lmul> < / vsew> < / lmul> < / vsew> < / lmul> < / vsew> < / lmul> < / vsew> < / lmul> < / vsew> < / sew> < / lmul> < / vsew> < / sew> < / lmul> < / vsew> < / lmul> < / vsew> < / lmul> < / vsew> < / lmul> < / vsew> < / lmul> < / vsew> < / sew> < / lmul> < / vsew> < / sew>

Claims

1. A NumPy numerical computation optimization method based on RISC-V vector extension, characterized in that, Comprise the following steps: Step 1, dynamic hardware detection and RISC-V vector extension support judgment; For judging whether the target device supports RISC-V vector extension instruction, and then deciding whether to enable RISC-V vector extension related optimization steps; Step 2, classification judgment, judge whether to call external efficient library and select adaptive strategy: The method of calling external efficient library: for the calculation task of calling external efficient library, directly pass the call external library execution, do not enable RISC-V vector expansion optimization path; Element by element operation without calling external efficient library: for the calculation task without calling external efficient library, enable RISC-V vector expansion optimization strategy, enter step 3, realize efficient calculation combined with hardware characteristics; Step 3, optimization process Analysis of the underlying implementation and execution process of NumPy, the calculation steps are divided into the following parts: 1-1, data loading and type conversion; 1-2, core calculation, including element by element operation of vector or matrix; 1-3, data output and result conversion.

2. The NumPy numerical computation optimization method based on RISC-V vector extension according to claim 1, characterized in that, Specifically, step 1 includes: RISC-V vector extension support detection: When the program starts, read the processor hardware information, judge whether the target device supports RISC-V vector extension by accessing control and status register; Detect the current RISC-V vector extension version characteristics, including vector register length VLEN and supported operation bit width, to match the available optimization instructions, specifically: detect whether half precision floating point operation is supported, if the hardware does not support 16 bit floating point vector instruction, then the optimization for float16 array in NumPy cannot use the corresponding vector instruction; Dynamic path selection: If the hardware supports RISC-V vector extension and meets the minimum optimization requirement, enter step 2; The minimum optimization requirement is that the support vector extension specification version is 0.7.1 and above; If the hardware does not support RISC-V vector extension or does not meet the minimum optimization requirement, fall back to the traditional scalar implementation path. Step 1-1, data loading and type conversion optimization, including the following steps:

3. The NumPy numerical computation optimization method based on RISC-V vector extension according to claim 1, wherein, NumPy calculation includes loading Python native data structure into NumPy array; Optimization includes the following: Python list dereference and value extraction: when the input data is Python list, first traverse all elements, dereference each element and extract the value; Use RISC-V vector extension instruction set to batch optimize pointer dereference operation in list, use vector register to load values in parallel; Optimize the loading mechanism: Uniform type data: when the data type is consistent, such as all integers or floating point numbers, directly use RISC-V vector expansion vector loading instruction to batch load to vector register; Non uniform type data: when the data type is inconsistent, set the boundary recovery module on the basis of RISC-V vector expansion optimization, and execute element by element check and adaptation for non uniform type data; Optimize RVV parallel processing by optimizing serial processing of data loading and type conversion. Step 1-2 includes the following:

4. The NumPy numerical computation optimization method based on RISC-V vector extension of claim 1, wherein, 1) vector element by element operation optimization ​ Vector element-wise operations include addition, subtraction, multiplication, division, and multiply-add operations. For this type of operation, a high-efficiency parallel implementation method is designed on the RVV platform, and the specific optimization strategies are as follows: Parallel loading of operands: Two vector arrays to be operated are loaded into vector registers in batches using RISC-V vector extension vector loading instructions. Element-wise calculation optimization: Directly perform element-wise operations in the vector register using RISC-V vector extension instructions; at the same time, pipeline the calculation results in the register. Data alignment and memory access optimization: Directly complete the storage operation in the vector register when storing the results. Align the memory data in advance during the loading and calculation stages. 2) Matrix element-wise operation optimization Matrix element-wise calculation includes A + B, A-B, A * B, A / B, A > B, A < B, A == B, MAX(A, B), and the optimization includes the following: Matrix block processing: The large matrix to be operated is divided into multiple smaller blocks; the size of the block is dynamically determined based on the vector processing capability of the processor; specifically, the number of elements processed continuously in memory for each block is determined based on the key parameters of RISC-V vector extension, including vector register length VLEN, current data element bit width SEW, and vector register grouping multiplier LMUL. Perform element-wise calculation on each block independently using RISC-V vector extension instructions to process each element of the matrix blocks. Memory access mode optimization: For row alignment and column alignment in matrix operations, use the vector loading instructions in RVV to optimize the memory access order in combination with the register rearrangement mechanism. Output storage optimization: After each block calculation is completed, the results are directly written back to memory, and multiple elements are written at once using the RVV vector write-back mechanism; specifically: in the processing loop, as soon as the calculation task of a block is completed, the calculation results of the block are written back to the corresponding position in memory from the vector register; this process uses the vector storage instruction of RVV to write multiple elements at once. 3) Broadcast operation optimization NumPy supports the broadcast mechanism, which allows vector or matrix operations with scalar values or matrix operations with vectors. In this case, the scalar value must be matched with the vector or matrix elements in broadcast form. This method optimizes the broadcast scenario as follows: RISC-V vector calculation optimization: Load the scalar value into a broadcast vector in the RISC-V vector register, and use the RVV vector-scalar calculation instruction to perform batch operations. Specifically, call the Intrinsic function, including but not limited to floating-point arithmetic operations, integer arithmetic operations, and comparison operations.

5. The NumPy numerical computation optimization method based on RISC-V vector extension according to claim 4, characterized in that, In step 2), the size of each block is set to an integer multiple of (VLEN / SEW) * LMUL, and the vector loading instruction can maximize the filling of the vector register during loop processing. Therefore, the total number of blocks is determined by the total size of the matrix divided by the size of a single block calculated based on the above parameters.

6. The NumPy numerical computation optimization method based on RISC-V vector extension according to claim 1, wherein, In steps 1-3, the output and data type conversion optimization After the element-wise operation, NumPy needs to restore the result from the array to a Python object or return it to the external program; this step optimizes the post-processing link, which is as follows: Batch reading and conversion: Batch read the calculation results in the NumPy array using the RISC-V vector load instruction, and the number of data read each time is dynamically adjusted according to the vector length of the hardware, that is, flexibility is achieved by dynamically setting the VL parameter; The read data is converted into a Python object in a parallel processing manner: Integer is wrapped as PyLongObject; Floating-point numbers are wrapped as PyFloatObject.

7. A NumPy numerical computation optimization system based on RISC-V vector extension, characterized in that, The function interface and distribution module, the hardware detection module, the dynamic flow management module, and the RVV optimization module; The function interface and distribution module is used to receive the NumPy function call request from the upper application program at runtime and obtain the array data to be processed; This module is the entrance of the entire optimization method, which distributes the function call type and data received to the dynamic flow management module; Hardware detection module: used to detect whether the target device supports RVV extension instructions at program startup, and query its specific hardware characteristics; Dynamic flow management module: used to judge whether the specific calculation task calls external high-efficiency library and select the corresponding calculation path; The RVV optimization module is used to optimize the element-wise operation task that does not call external library using the RVV instruction set, and improve the performance through vectorized calculation.

8. A computer-readable storage medium, characterized in that, A program is stored thereon, which, when executed by a processor, implements the steps in the NumPy numerical calculation optimization method based on RISC-V vector extension according to any one of claims 1-6.

9. An electronic device, comprising: A memory, a processor, and a program stored on the memory and executable on the processor, wherein the processor executes the program to implement the steps in the NumPy numerical calculation optimization method based on RISC-V vector extension according to any one of claims 1-6.

Citation Information

Patent Citations

  • Arm architecture-based NumPy operation acceleration optimization method

    CN112783503A

  • Method for improving parallel NumPy computing performance by using characteristics of non-uniform memory access architecture

    CN112860530A

  • NumPy optimization method using MPI and system

    CN113515365A

  • Unified BLAS algorithm library method and system for heterogeneous hardware

    CN117908861A

  • RISC-V-oriented PyTorch vector library optimization method and system and PyTorch vector library application method

    CN119759341A