Method and apparatus for stencil computation, and computation device
By pre-processing the calculation conditions and generating the judgment result vector in the stencil calculation, the inefficiency problem caused by the judgment of the calculation conditions is solved, and the stencil calculation is converted into vector calculation, thereby improving the calculation efficiency.
Patent Information
- Application Number
- PCT/CN2024/100798
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2023-09-07
- Filing Date
- 2024-06-21
- Publication Date
- 2025-03-13
AI Technical Summary
In stencil calculation, it is necessary to judge the node data according to the calculation conditions and perform different calculation operations, which makes it impossible to convert the calculation into a vector calculation, which is inefficient.
By pre-judging the calculation conditions, a judgment result vector is generated, and then vector calculation is performed with the data vector, so that each node data performs calculation operations corresponding to the calculation conditions it satisfies.
Converting stencil calculations including computational conditions into vector calculations improves the efficiency of stencil calculations, especially in structured and unstructured grids.
Smart Images

Figure CN2024100798_13032025_PF_FP_ABST
Abstract
Description
Stencil calculation method, device and computing equipment
[0001] This application claims priority to Chinese patent application No. 202311161061.1, filed on September 7, 2023, entitled “Method, device and computing equipment for stencil calculation,” the entire contents of which are incorporated herein by reference. Technical Field
[0002] The present application relates to the field of computing technology, and in particular to a stencil computing method, apparatus, and computing device. Background Art
[0003] Stencil calculation is a common cyclic calculation method, which is characterized by traversing the node data in the calculation area and performing similar calculation processing on the node data traversed each time.
[0004] In stencil calculations, the calculations performed on the node data of each traversal are generally scalar calculations. Therefore, scalar calculations on the node data of multiple traversals can be converted into vector calculations. This allows batch calculations of node data to be performed through vector calculations, thereby improving the efficiency of stencil calculations.
[0005] However, during stencil calculations, node data may also be judged based on the calculation conditions. Different calculations need to be performed on the node data to correspond to different judgment results. For example, when the node data is an odd number, 1 can be added to the node data, and when the node data is an even number, 2 can be added to the node data. This requires sequential judgment processing on the traversed node data and then performing the corresponding calculations based on the judgment results. Stencil calculations cannot be converted into vector calculations, resulting in low efficiency in executing stencil calculations.
[0006] Summary of the Invention
[0007] The embodiments of the present application provide a stencil calculation method, apparatus, and computing device, which can improve the efficiency of stencil calculation. The corresponding technical solutions are as follows:
[0008] In a first aspect, a method for performing stencil calculation is provided. The stencil calculation includes at least one calculation condition and a corresponding calculation operation. The method for performing stencil calculation includes:
[0009] The processor obtains multiple node data included in the calculation area and combines the multiple node data into a data vector. Based on at least one calculation condition included in the stencil calculation, the multiple node data are judged and processed to obtain a judgment result for each calculation condition corresponding to each node data. For each calculation condition, the judgment results corresponding to the calculation condition of the multiple node data are combined into a judgment result vector to obtain a judgment result vector corresponding to the calculation condition. Based on the data vector and the judgment result vector corresponding to each calculation condition, a calculation operation corresponding to each calculation condition in the stencil calculation is performed to obtain a calculation result of the stencil calculation.
[0010] In the solution presented in this application, each node data is pre-judged and processed using various computational conditions to obtain a judgment result vector corresponding to each computational condition. Vector calculations are then performed on the judgment result vector corresponding to each computational condition and the data vector composed of multiple node data, thereby enabling each node data to perform the computational operation corresponding to the computational condition it satisfies. This solution can convert stencil calculations involving computational conditions into vector calculations, thereby improving the efficiency of executing stencil calculations.
[0011] In one achievable manner, the computational region is a structured grid or an unstructured grid. Thus, the solution presented in this application can further improve the computational efficiency of stencil computations on structured grids or unstructured grids.
[0012] In one conceivable implementation, the computational domain is an unstructured grid, and the processing of forming a data vector from the plurality of node data includes: allocating memory storage space in a memory for the plurality of node data; mapping the plurality of node data from the original storage space to the memory storage space in accordance with a traversal order of the plurality of node data in the computational domain; and reading the plurality of node data from the memory storage space based on a vector read instruction to obtain the data vector.
[0013] In the solution shown in this application, for unstructured grids, it is possible to map the node data from the original storage space (such as a hard disk) to the memory storage space in the order in which the node data is traversed. In this way, when performing stencil calculations on the unstructured grid, the node data continuously read from the memory storage space is the node data traversed in the traversal order. In this way, there is no need to read the node data non-continuously in the original storage space in the traversal order each time, and the unstructured grid stencil calculation can be converted into a vector calculation, which can improve the efficiency of the unstructured grid stencil calculation.
[0014] In one achievable manner, for each calculation condition, the judgment results of the multiple node data corresponding to the calculation condition are combined into a judgment result vector to obtain the judgment result vector corresponding to the calculation condition, including: for each calculation condition, the judgment results of the multiple node data corresponding to the calculation condition are combined into a judgment result vector according to the traversal order of the multiple node data in the calculation area, to obtain the judgment result vector corresponding to the calculation condition. In this way, the judgment results of each node data corresponding to each calculation condition can be combined into a judgment result vector according to the traversal order of each node data. The combined judgment result vector can then be subjected to vector calculation with the data vector, thereby converting the stencil calculation into vector calculation, which can improve the efficiency of the stencil calculation.
[0015] In one achievable manner, the above-mentioned judgment processing of multiple node data based on at least one calculation condition included in the stencil calculation to obtain a judgment result for each node data corresponding to each calculation condition includes: judging and processing the multiple node data based on at least one calculation condition included in the stencil calculation. For each node data, if the node data does not meet the calculation condition, the judgment result corresponding to the node data is determined to be a first value; if the node data meets the calculation condition, the judgment result corresponding to the node data is determined to be a second value. In one example, the first value is 0 and the second value is 1.
[0016] In the solution shown in this application, for node data that does not meet the calculation conditions, the corresponding judgment result can be set to a first value, and for node data that meets the calculation conditions, the corresponding judgment result can be set to a second value. In this way, setting the first and second values in the judgment result vector corresponding to the calculation conditions can realize the execution of the corresponding calculation operation for the node data in the data vector that meets the calculation conditions, and then realize stencil calculation through vector calculation, which can improve the efficiency of stencil calculation.
[0017] In one achievable manner, the above-mentioned method of executing the calculation operation corresponding to each calculation condition in the stencil calculation based on the data vector and the judgment result vector corresponding to each calculation condition to obtain the calculation result of the stencil calculation includes: for each calculation condition, based on the vector calculation instruction, sequentially executing the vector calculation of the judgment result vector corresponding to each calculation condition and the input vector to obtain the calculation result vector corresponding to each calculation condition. Among them: the input vector corresponding to the first calculation condition is the data vector, and the input vector corresponding to the calculation condition after the first calculation condition is the calculation result vector corresponding to the previous calculation condition; in the calculation result vector corresponding to each calculation condition, there is an updated element relative to the corresponding input vector, which is the calculation result of executing the corresponding calculation operation for the node data in the input vector that meets the calculation condition, and the calculation result vector corresponding to the last calculation condition is the calculation result of the stencil calculation.
[0018] In the solution shown in this application, the data vector can be used as the initial input vector to perform vector calculation with the judgment result vector corresponding to the calculation condition. The obtained calculation result vector can be used as the input vector for vector calculation of the judgment result vector corresponding to the next calculation condition. In this way, after performing vector calculation on the judgment result vector corresponding to each calculation condition, the calculation result of the stencil calculation can be obtained. Converting the stencil calculation into vector calculation in this way can improve the efficiency of the stencil calculation.
[0019] In a second aspect, a device for performing stencil calculation is provided. The stencil calculation includes at least one calculation condition and a corresponding calculation operation. The device includes:
[0020] An acquisition module, configured to acquire a plurality of node data included in a calculation area and combine the plurality of node data into a data vector;
[0021] A judgment module, configured to perform judgment processing on a plurality of node data according to at least one calculation condition included in the stencil calculation, and obtain a judgment result corresponding to each calculation condition for each node data;
[0022] A generation module is used to form a judgment result vector for each calculation condition by combining the judgment results of the calculation conditions corresponding to the multiple node data to obtain the judgment result vector corresponding to the calculation condition;
[0023] The calculation module is used to perform the calculation operation corresponding to each calculation condition in the stencil calculation based on the data vector and the judgment result vector corresponding to each calculation condition, and obtain the calculation result of the stencil calculation.
[0024] In one achievable manner, the calculation region is a structured grid or an unstructured grid.
[0025] In one feasible manner, the computation area is an unstructured grid, and the acquisition module is used to: apply for memory storage space for multiple node data in the memory; map the multiple node data from the original storage space to the memory storage space according to the traversal order of the multiple node data in the computation area; and read the multiple node data from the memory storage space based on a vector read instruction to obtain a data vector.
[0026] In one feasible manner, the above-mentioned generation module is used to: for each calculation condition, form a judgment result vector by combining the judgment results of the calculation conditions corresponding to multiple node data according to the traversal order of the multiple node data in the calculation area, and obtain a judgment result vector corresponding to the calculation condition.
[0027] In one implementable manner, the above-mentioned judgment module is used to: perform judgment processing on multiple node data based on at least one calculation condition included in the stencil calculation; for each node data, if the node data does not meet the calculation condition, the judgment result corresponding to the node data is determined to be a first value; if the node data meets the calculation condition, the judgment result corresponding to the node data is determined to be a second value.
[0028] In one implementable manner, the above-mentioned calculation module is used to: for each calculation condition, based on the vector calculation instruction, sequentially execute the vector calculation of the judgment result vector and the input vector corresponding to each calculation condition to obtain the calculation result vector corresponding to each calculation condition, wherein the input vector corresponding to the first calculation condition is the data vector, and the input vector corresponding to the calculation condition after the first calculation condition is the calculation result vector corresponding to the previous calculation condition; in the calculation result vector corresponding to each calculation condition, there is an updated element relative to the corresponding input vector, which is the calculation result of executing the corresponding calculation operation for the node data in the input vector that meets the calculation condition, and the calculation result vector corresponding to the last calculation condition is the calculation result of the stencil calculation.
[0029] In one implementable manner, the first value is 0 and the second value is 1.
[0030] In a third aspect, a computing device is provided, which includes a memory and a processor, wherein the memory stores at least one instruction, and the processor executes at least one instruction to execute the method described in the first aspect and / or any achievable method of the first aspect.
[0031] In a fourth aspect, a computer-readable storage medium is provided, which stores computer program code. When the computer program code is executed by a computer device, the computer device executes the method described in the first aspect and / or any achievable method of the first aspect.
[0032] In a fifth aspect, a computer program product comprising instructions is provided, which, when executed on a computer device, enables the computer device to execute the method described in the first aspect and / or any achievable manner of the first aspect. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] FIG1 is a schematic diagram of a structured grid provided in an embodiment of the present application;
[0034] FIG2 is a schematic diagram of the structure of an unstructured grid provided in an embodiment of the present application;
[0035] FIG3 is a schematic diagram of the structure of a computing device provided in an embodiment of the present application;
[0036] FIG4 is a flow chart of a method for performing stencil calculations provided in an embodiment of the present application;
[0037] FIG5 is a flow chart of a method for performing stencil calculation provided in an embodiment of the present application;
[0038] FIG6 is a schematic diagram of a method for performing stencil calculation provided in an embodiment of the present application;
[0039] FIG7 is a schematic diagram of the structure of a device for performing stencil calculations provided in an embodiment of the present application. DETAILED DESCRIPTION
[0040] In order to make the objectives, technical solutions and advantages of this application clearer, the implementation methods of this application will be further described in detail below with reference to the accompanying drawings.
[0041] Stencil computing is a common computing model in high-performance computing (HPC) and is widely used in various fields, such as image processing and solving differential equations. Stencil computing is characterized by traversing the node data in the computational area and performing similar computations on the node data each time it is traversed.
[0042] A grid is a network structure formed by multiple interconnected grid nodes. It is often used to describe geometric shapes, topological structures, and related physical and mathematical problems. It is a frequently encountered computational object in fields such as computer graphics, computational fluid dynamics, and finite element methods.
[0043] Meshes can be structurally divided into structured and unstructured meshes. As shown in Figure 1, a structured mesh generally refers to a mesh where the edges between mesh nodes form a quadrilateral or hexahedron structure. As shown in Figure 2, an unstructured mesh generally refers to a mesh where the edges between mesh nodes form a structure other than a quadrilateral or hexahedron structure. For example, in an unstructured mesh, the edges between mesh nodes form a triangle or tetrahedron structure.
[0044] When performing Stencil calculations on structured grids, the order in which the node data for each grid node in the structured grid is stored is the same as the order in which the nodes are traversed (that is, the order in which the nodes are calculated). Therefore, scalar calculations corresponding to each grid node in the structured grid can generally be converted into vector calculations corresponding to multiple grid nodes. In this way, scalar calculations of node data for multiple grid nodes can be performed through sequential vector calculations, thereby improving the efficiency of performing Stencil calculations on structured grids.
[0045] When performing Stencil calculations on unstructured grids, the order in which the node data of each grid node in the unstructured grid is stored is different from the order in which the node data of each grid node in the structured grid is traversed. As shown in Figure 2, the node data of the grid nodes on the disk are stored in the order of the grid node subscripts, that is, the storage order of the node data is grid node 1, grid node 2, grid node 3, ..., grid node n. When traversing the grid nodes, the traversal is generally performed in the order of the edges or grids formed by the grid nodes in the unstructured grid. For example, the order of the grid nodes in the table and inside is grid node 2, grid node 8, grid node 14, ..., grid node m. In this way, if the Stencil calculation of the unstructured grid is to be converted into a vector calculation, a large number of non-continuous accesses are required to form the data vector for the vector calculation.
[0046] In related technologies, whether performing Stencil calculations on structured or unstructured grids or other computational domains, it is necessary to determine the computational conditions for each grid node traversal and perform different computational operations on the traversed node data based on the determination results. In this case, it is impossible to convert the scalar calculations corresponding to each grid node into vector calculations, resulting in low efficiency in executing Stencil calculations.
[0047] Figure 3 is a schematic diagram of the structure of a computing device provided in an embodiment of the present application. The computing device can be used to execute the stencil calculation method provided in an embodiment of the present application, and can convert the Stencil calculation of an unstructured grid into a vector calculation, and can also convert the Stencil calculation with calculation conditions into a vector calculation, which can improve the efficiency of executing the Stencil calculation. As shown in Figure 3, the computing device 300 may include: a bus 302, a processor 304, and a memory 306. Optionally, the computing device 300 may also include a communication interface 308. The processor 304, the memory 306 and the communication interface 308 communicate with each other via the bus 302. The computing device 300 can be a server or a terminal device. It should be understood that the present application does not limit the number of processors and memories in the computing device 300. The computing device 300 can be a device for running a model, a terminal or a server. When the computing device 300 is a terminal, the computing device 300 includes but is not limited to a desktop computer, a mobile phone, a notebook, a tablet computer, etc. When the computing device 300 is a server, the computing device 300 may be a separate server or a server cluster composed of multiple servers. It may be a physical machine or a virtual machine or container virtualized by virtualization technology.
[0048] Bus 302 may be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, among others. Buses may be classified as address buses, data buses, control buses, and the like. For ease of illustration, FIG3 shows a single bus line, but this does not imply a single bus or type of bus. Bus 302 may include a path for transmitting information between various components of computing device 300 (e.g., memory 306, processor 304, and communication interface 308).
[0049] The processor 304 may include any one or more processors such as a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP), or a neural network processing unit (NPU). The processor 304 may further include a vector operation unit, which is used to perform vector calculations after stencil calculation conversion to improve the efficiency of stencil calculations.
[0050] The memory 306 may include a volatile memory, such as a random access memory (RAM). The memory 306 may also include a non-volatile memory, such as a read-only memory (ROM), a flash memory, a hard disk drive (HDD), or a solid state drive (SSD). The memory 106 stores an executable program code, and the processor 104 executes the executable program code to implement the stencil calculation method provided in the embodiment of the present application.
[0051] The communication interface 308 uses a transceiver module such as, but not limited to, a network interface card or a transceiver to implement communication between the computing device 300 and other devices or a communication network.
[0052] FIG4 is a method for performing stencil calculation provided by an embodiment of the present application. The method may be executed by a processor in the computing device shown in FIG3 . Referring to FIG4 , the steps of the processor performing the stencil calculation method include:
[0053] Step 401: The processor obtains a plurality of node data included in a calculation area, and combines the plurality of node data into a data vector.
[0054] The computational domain may be a structured grid, and the processor may read node data of each grid node stored in the memory into the memory. The processor may then use a vector read instruction to read the node data of each grid node stored in the memory into a vector register.
[0055] In one example, each node data can be organized into a data vector according to the order in which the node data in the computational region is traversed. Specifically, when traversing node data in an unstructured or structured grid, the node data may be traversed in the order of the edges connecting the grid nodes, or in the order of the polygons formed between the grid nodes. Accordingly, for the traversed node data, scalar calculations may be performed on each node data, or scalar calculations may be performed between multiple grid nodes connected by each edge or polygon. For example, the node data corresponding to each edge connecting two grid nodes may be summed.
[0056] When scalar calculations are required between multiple mesh nodes connected by each edge or polygon, the traversed nodes can be grouped into multiple data vectors. Each data vector corresponds to a mesh node at a certain location connected by each edge or polygon. For example, if the node data corresponding to two mesh nodes connected by each edge needs to be summed, the node data corresponding to the left mesh node of each edge can be combined into one data vector, and the node data corresponding to the right mesh node of each edge can be combined into another data vector. In this way, by performing a vector addition operation on these two data vectors, the addition operation between the mesh nodes connected by each edge can be achieved.
[0057] Step 402: Perform judgment processing on multiple node data according to at least one calculation condition included in the stencil calculation to obtain a judgment result of each node data corresponding to each calculation condition.
[0058] Among them, the calculation condition can be an if judgment statement. The calculation condition included in the stencil calculation can be one or more. In the process of stencil calculation, for each traversed calculation node, it is necessary to judge according to the calculation condition included in the stencil calculation, and then perform the corresponding calculation operation on the traversed calculation node according to the judgment result. When there are multiple calculation conditions included in the stencil calculation, the node data can only meet one of the calculation conditions. For example, the stencil calculation includes two calculation conditions, namely, judging whether the node data is an odd number and judging whether the node data is an even number. When the node data is an odd number, 1 can be added to the node data, and when the node data is an even number, 2 can be added to the node data.
[0059] In one example, the judgment object of the calculation condition can be node data. For example, the calculation condition can determine whether the node data is an odd number, an even number, or whether it is greater than a specified value. Or the calculation condition can also be to perform a specified calculation on the node data and determine whether the calculation result is within a specified numerical range. It should be noted that the setting of the calculation condition is related to the specific business involving stencil calculation, and the examples provided in the embodiments of the present application are only illustrative examples for facilitating the understanding of the solution.
[0060] In implementation, the determination result for each node data corresponding to each calculation condition can be determined sequentially in the order in which the node data is traversed. For each node data, if the node data does not meet the calculation condition, the determination result corresponding to the node data is determined to be a first value; if the node data meets the calculation condition, the determination result corresponding to the node data is determined to be a second value. In one example, the first value can be 0 and the second value can be 1, or the first value can be 1 and the second value can be 0, etc.
[0061] Step 403: For each calculation condition, the judgment results of the calculation condition corresponding to the multiple node data are combined into a judgment result vector to obtain the judgment result vector corresponding to the calculation condition.
[0062] In implementation, for any calculation condition, multiple node data can be judged in the order in which each node data is traversed, to obtain the judgment result of the calculation condition corresponding to each node data. After obtaining the judgment result corresponding to each calculation condition for each node data, the judgment results of each node data corresponding to the calculation condition can be sequentially combined into a judgment result vector, thereby obtaining the judgment result vector corresponding to the calculation condition. In one example, the judgment results obtained for each calculation condition can be combined into a judgment result vector in the order in which each node data is traversed.
[0063] Furthermore, corresponding to the example of step 401 above, when there are multiple data vectors composed of multiple node data, then in step 403, the judgment results of the multiple node data corresponding to a single computation condition can be combined to form a judgment result vector corresponding to each data vector. That is, each data vector has the same length as the corresponding judgment result vector, and the value at each position in the judgment result vector can be used to indicate whether the node data at the same position in the corresponding data vector meets the computation condition.
[0064] Step 404: Based on the data vector and the judgment result vector corresponding to each calculation condition, perform the calculation operation corresponding to each calculation condition in the stencil calculation to obtain the calculation result of the stencil calculation.
[0065] In implementation, after obtaining the data vector and the judgment result vector corresponding to each calculation condition in the stencil calculation, a vector calculation instruction can be used to implement a vector calculation in the vector operation unit of the processor on the data vector and the judgment result vector corresponding to each calculation condition, thereby causing each node data to perform the calculation operation corresponding to the calculation condition it meets. After the stencil calculation is completed, the memory storage space requested in step 401 can be released.
[0066] Among them, the processor for executing step 404 can be the same as or different from the processor for the above steps 401-403. For example, the processors for executing the above steps 401-404 can all be CPUs, and the vector operation unit for executing the judgment result vector and data vector operations can be a vector operation unit included in the CPU. Alternatively, the processor for executing the above steps 401-403 can be a CPU, and the processor for executing step 404 can be an NPU, GPU, etc., that is, the vector operation unit for executing the judgment result vector and data vector operations can be a vector operation unit included in the NPU or GPU. In this case, before step 404, the CPU can calculate the judgment result vector and data vector to be calculated to the NPU or GPU, etc.
[0067] In one example, for each calculation condition, based on the vector calculation instruction, the vector calculation of the judgment result vector and the input vector corresponding to each calculation condition is executed in sequence to obtain the calculation result vector corresponding to each calculation condition, wherein the input vector corresponding to the first calculation condition is the data vector, and the input vector corresponding to the calculation condition after the first calculation condition is the calculation result vector corresponding to the previous calculation condition; in the calculation result vector corresponding to each calculation condition, there is an updated element relative to the corresponding input vector, which is the calculation result of executing the corresponding calculation operation for the node data in the input vector that meets the calculation condition, and the calculation result vector corresponding to the last calculation condition is the calculation result of the stencil calculation.
[0068] In implementation, the calculation operation corresponding to each calculation condition can be executed in sequence according to the data vector and the judgment result vector corresponding to each calculation condition, wherein the execution order of the calculation operations corresponding to multiple calculation conditions is not limited.
[0069] In the process of sequentially executing the calculation operations corresponding to each calculation condition, the calculation operation corresponding to the first calculation condition can be implemented by performing a vector calculation on the judgment result vector of the first calculation condition and the data vector. By performing a vector calculation on the judgment result vector of the first calculation condition and the data vector, the corresponding calculation operation can be performed on the node data in the data vector that meets the first calculation condition.
[0070] The specific execution process of the vector calculation corresponding to each calculation condition will not be introduced here.
[0071] For node data in the data vector that meets the first calculation condition, after performing the vector operation, the corresponding calculation result is located in the calculation result vector at the same position as the node data in the data vector. This calculation result is the calculation result of performing the corresponding calculation operation on the node data. For node data in the data vector that does not meet the first calculation condition, after performing the vector operation, the corresponding calculation result is located in the calculation result vector at the same position as the node data in the data vector. The value corresponding to this calculation result is still the value of the node data.
[0072] In this way, after executing the vector calculation corresponding to the first calculation condition, the execution result of the corresponding calculation operation performed on the node data that meets the first calculation condition can be obtained. Afterwards, the calculation result vector corresponding to the first calculation condition can be vector-calculated with the judgment result vector corresponding to the second calculation condition. The calculation result vector corresponding to the second calculation condition obtained in this way can include the calculation result after the node data that meets the second calculation condition performs the corresponding calculation operation. That is to say, the calculation result vector corresponding to the second calculation condition has updated elements relative to the calculation result vector of the first calculation condition, that is, the calculation result after the node data that meets the second calculation condition in the calculation result vector of the first calculation condition performs the corresponding calculation operation. That is to say, the calculation result vector corresponding to the second calculation condition includes the calculation result after the node data that meets the first calculation condition performs the calculation operation, the calculation result after the node data that meets the second calculation condition performs the calculation operation, and the node data that does not meet the first calculation condition and the second calculation condition.
[0073] Similarly, for each calculation condition after the first one, the calculation operation corresponding to the node data that satisfies the current calculation condition can be achieved by performing a vector calculation on the calculation result vector corresponding to the previous calculation condition and the judgment result vector corresponding to the current calculation condition. In this way, the calculation result vector corresponding to the final calculation condition includes the result data of the corresponding calculation operation performed on each node data.
[0074] In addition, corresponding to the example of step 401 above, when there are multiple data vectors composed of multiple node data, after performing vector operations on each data vector and multiple judgment result vectors, corresponding vector operations can be performed on the multiple calculation result vectors obtained to achieve scalar calculations between multiple grid nodes connected by each edge or each polygon as described in step 401.
[0075] In one example, the stencil calculation includes three calculation conditions, and the corresponding data vector can be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. Among them, the first three elements in the data vector meet the first calculation condition, the middle four elements of the data vector meet the second calculation condition, and the last three elements of the data vector meet the third calculation condition. The judgment result vector corresponding to the first calculation condition can be [1, 1, 1, 0, 0, 0, 0, 0, 0], the judgment result vector corresponding to the second calculation condition can be [0, 0, 0, 1, 1, 1, 1, 0, 0, 0], and the judgment result vector corresponding to the third calculation condition can be [0, 0, 0, 0, 0, 0, 1, 1, 1].
[0076] After vector calculation is performed on the judgment result vector corresponding to the first calculation condition and the data vector, the first calculation result vector [a1, a2, a3, 3, 4, 5, 6, 7, 8, 9] can be obtained, where "a1", "a2", and "a3" are the calculation results obtained by executing the calculation operation corresponding to the first calculation condition on the first three elements in the data vector.
[0077] After vector calculation is performed on the judgment result vector corresponding to the second calculation condition and the first calculation result vector, the second calculation result vector [a1, a2, a3, b1, b2, b3, b4, 7, 8, 9] can be obtained, where "b1", "b2", "b3", and "b4" are the calculation results obtained by performing the calculation operation corresponding to the second calculation condition on the four middle elements of the data vector.
[0078] After performing vector calculations on the judgment result vector corresponding to the third calculation condition and the second calculation result vector, we obtain the third calculation result vector [a1, a2, a3, b1, b2, b3, b4, c1, c2, c3], where "c1," "c2," "c3," and "c4" are the calculation results of the calculation operation corresponding to the third calculation condition on the last three elements of the data vector. This third calculation result vector is the result of the stencil calculation.
[0079] The following example illustrates the vector calculation corresponding to each calculation condition:
[0080] 1. Addition operation changes to multiplication and accumulation operation
[0081] If the calculation operation corresponding to the calculation condition is adding a specified value to the node data, the corresponding vector calculation can be the multiplication and accumulation of the specified value vector consisting of the input vector, the judgment result vector, and the specified value. For example, the judgment result vector A can be [1,1,1,0,0,0,0,0,0,0], the specified value vector B can be [1,1,1,1,1,1,1,1,1,1], and the input vector C can be [0,1,2,3,4,5,6,7,8,9]. The first three elements of the input vector meet the calculation condition, that is, the first three elements of the input vector need to be added by 1. The corresponding vector calculation can be the bitwise multiplication of the judgment result vector A and the specified value vector B, and then adding the input vector C. That is, the calculation result vector S[i] = A[i]*B[i]+C[i], and the calculation result vector S = [1,2,3,3,4,5,6,7,8,9].
[0082] 2. Assignment changes to mutually exclusive weighting
[0083] If the calculation operation corresponding to the calculation condition is assigning a value to node data, the corresponding vector calculation can be the mutually exclusive weighted sum of the input vector, the judgment result vector, and the assigned data. For example, the judgment result vector A can be [0,0,0,1,1,1,1,0,0,0], the assigned vector B can be [1,1,1,1,1,1,1,1,1,1], and the input vector C can be [1,2,3,3,4,5,6,7,8,9]. The four middle elements of the input vector meet the calculation condition, that is, the four middle elements of the input vector are assigned a value of 1. The corresponding vector calculation can be the mutually exclusive weighted sum of the corresponding elements of the assigned vector B and the input vector C, that is, S[i] = (1-A[i])*C[i] + A[i]*B[i]. The calculated result vector S = [1,2,3,1,1,1,1,7,8,9].
[0084] 3. Multiplication becomes multi-step multiplication
[0085] If the calculation operation corresponding to the calculation condition is multiplying the node data by a specified value, the corresponding vector calculation can be a multi-step multiplication of the specified value vector consisting of the input vector, the judgment result vector, and the specified value. For example, the judgment result vector A can be [0,0,0,0,0,0,0,1,1,1], the assignment vector B can be [2,2,2,2,2,2,2,2,2,2], and the input vector C can be [1,2,3,1,1,1,1,7,8,9]. The last three elements of the input vector meet the calculation condition, that is, the last three elements of the input vector need to be multiplied by 2. The corresponding vector calculation can be to multiply the judgment result vector A, the assignment vector B, and the input vector C, and then perform a mutually exclusive weighted addition with the input vector C, that is, S[i] = C[i]*A[i]*B[i] + (1-A[i])*C[i]. The calculated result vector S = [1,2,3,1,1,1,1,1,14,16,18].
[0086] It can be seen that vector calculations using input vectors, judgment result vectors, and other vectors can replace the original judgment processing of node data and perform scalar calculations on node data based on the judgment results, thereby improving the efficiency of stencil calculations.
[0087] The above is an example of converting the scalar calculation in the original stencil calculation into a vector calculation. Depending on the business involved in the stencil calculation, the stencil calculation can also include other types of scalar calculations. However, for any type of stencil calculation, after converting to a vector calculation, by judging the result vector, it can be achieved that the corresponding values of the node data in the data vector that do not meet the calculation conditions in the calculation result vector remain unchanged, and the corresponding values of the node data in the data vector that meet the calculation conditions in the calculation result vector are the calculation results of the original scalar calculation performed on the node data.
[0088] In the embodiment of the present application, each node data is judged and processed by pre-processing various calculation conditions to obtain a judgment result vector corresponding to each calculation condition. Then, the judgment result vector corresponding to each calculation condition is respectively used to perform vector calculations with the data vector composed of multiple node data, thereby enabling each node data to perform the calculation operation corresponding to the calculation condition it meets. It can be seen that the method for performing stencil calculations provided by the embodiment of the present application can convert stencil calculations including calculation conditions into vector calculations, thereby improving the efficiency of performing stencil calculations.
[0089] FIG5 is a flow chart of a method for converting stencil calculation of an unstructured grid into vector calculation provided by an embodiment of the present application. Accordingly, the calculation area in step 401 above may also be an unstructured grid. The processing of this method includes:
[0090] Step 501: Apply for memory storage space in memory for multiple node data.
[0091] The size of the memory storage space applied for the multiple node data in the memory may be consistent with the size of the storage space occupied by the multiple node data in the memory (such as a hard disk).
[0092] Step 502: Map the plurality of node data from the original storage space (memory map) to the memory storage space according to the traversal order of the plurality of node data in the calculation area.
[0093] After allocating memory storage space in the memory, the storage address of each node data in the original storage space can be obtained in sequence according to the traversal order of the multiple node data, and the storage address of each node data in the memory storage space can be determined in sequence. The multiple node data are stored continuously in the memory storage space in the traversal order.
[0094] After obtaining the storage address of each node data in the original storage space and the storage address of each node data in the memory storage space, a mapping relationship between the node data in the original storage space and the memory storage space can be established, and then the multiple node data stored in the original storage space are mapped to the memory storage space in the traversal order. The mapping process belongs to the prior art and will not be described in detail in the embodiments of this application.
[0095] Step 503: Based on the vector read instruction, multiple node data are read from the memory storage space to obtain a data vector.
[0096] After mapping multiple node data to the memory storage space, the processor can continuously read multiple node data from the memory storage space through vector read instructions, and store the multiple node data in the vector register, thereby obtaining vector data composed of multiple node data.
[0097] As shown in Figure 6, the storage order of the node data corresponding to the grid on the hard disk is b, d, c, a, and the corresponding traversal order is a, b, c, d. The CPU can re-arrange and map b, d, c, a to the memory according to the traversal order. In this way, the NPU can use vector load instructions to load a, b, c, d into the NPU's vector registers based on the storage addresses of a, b, c, d in memory, thereby performing vector operations on the data vector composed of a, b, c, d.
[0098] In one example, the processor can also record the mapping order of each node data. After obtaining the data vector and executing the vector calculation corresponding to the stencil calculation, each result data in the calculation result vector can be mapped back to the memory according to the mapping order, and then the result data can be stored in the memory according to the storage order of the node data in the original storage space.
[0099] In practice, the method provided in the embodiments of the present application can be configured as a function added to a corresponding mathematical library or an application involving stencil calculations. The function can provide a mapping interface, a demapping interface, a transport interface, a conditional judgment interface, etc. Applications involving stencil calculations can implement the node data mapping, demapping, transport, and other functions involved in the above embodiments by calling the interface provided by the function.
[0100] The method provided in the embodiments of the present application for converting stencil calculations for unstructured grids into vector calculations can map multiple node data to be stencil calculated to memory in the order of traversal. This avoids discontinuous access to multiple node data, and further enables stencil calculations for unstructured grids to be performed through vector calculations, thereby improving the efficiency of stencil calculations.
[0101] Stencil calculation may require multiple traversal calculations of the node data in the grid. In this way, using the embodiment of the present application, it is only necessary to rearrange the node data and map it to the memory. Each traversal can then continuously access the node data through the mapping address of the node data in the memory. This can avoid discontinuous access to the node data in each traversal and further improve the efficiency of stencil calculation.
[0102] Based on the same inventive concept, an embodiment of the present application further provides a device for performing stencil calculation, wherein the stencil calculation includes at least one calculation condition and a corresponding calculation operation. The device may be a computing device for performing the stencil calculation method described above. FIG7 is a schematic diagram of the structure of a device for performing stencil calculation provided by an embodiment of the present application. As shown in FIG7 , the device includes:
[0103] The acquisition module 710 is used to acquire multiple node data included in the calculation area and combine the multiple node data into a data vector, which can be used to implement the acquisition function in the above step 401 and its implicit steps.
[0104] The judgment module 720 is configured to perform judgment processing on the plurality of node data based on at least one calculation condition included in the stencil calculation, and obtain a judgment result for each node data corresponding to each calculation condition. Specifically, this module can be used to implement the judgment function in the above-mentioned step 402 and its implicit steps.
[0105] The generation module 730 is configured to, for each calculation condition, combine the judgment results of the plurality of node data corresponding to the calculation condition into a judgment result vector, thereby obtaining the judgment result vector corresponding to the calculation condition. Specifically, this module can be used to implement the generation function in the above-mentioned step 403 and its implicit steps.
[0106] The calculation module 740 is configured to perform the calculation operation corresponding to each calculation condition in the stencil calculation based on the data vector and the judgment result vector corresponding to each calculation condition, thereby obtaining the calculation result of the stencil calculation. Specifically, this module can be used to implement the calculation functions in the above-mentioned step 404 and its implicit steps.
[0107] In one achievable manner, the calculation region is a structured grid or an unstructured grid.
[0108] In one achievable manner, the calculation area is an unstructured grid, and the acquisition module 710 is configured to:
[0109] Applying memory storage space for the plurality of node data in memory;
[0110] Mapping the plurality of node data from the original storage space to the memory storage space according to a traversal order of the plurality of node data in the calculation area;
[0111] Based on the vector read instruction, the plurality of node data are read from the memory storage space to obtain the data vector.
[0112] In one achievable manner, the generating module 730 is configured to:
[0113] For each calculation condition, the judgment results of the multiple node data corresponding to the calculation condition are combined into a judgment result vector according to the traversal order of the multiple node data in the calculation area to obtain the judgment result vector corresponding to the calculation condition.
[0114] In one achievable manner, the determination module 720 is configured to:
[0115] Performing judgment processing on the plurality of node data according to at least one calculation condition included in the stencil calculation;
[0116] For each node data, if the node data does not meet the calculation condition, the judgment result corresponding to the node data is determined to be a first value; if the node data meets the calculation condition, the judgment result corresponding to the node data is determined to be a second value.
[0117] In one achievable manner, the calculation module 740 is configured to:
[0118] For each calculation condition, based on the vector calculation instruction, the vector calculation of the judgment result vector and the input vector corresponding to each calculation condition is performed in sequence to obtain the calculation result vector corresponding to each calculation condition, wherein the input vector corresponding to the first calculation condition is the data vector, and the input vector corresponding to the calculation condition after the first calculation condition is the calculation result vector corresponding to the previous calculation condition; in the calculation result vector corresponding to each calculation condition, there is an updated element relative to the corresponding input vector, which is the calculation result of executing the corresponding calculation operation for the node data in the input vector that meets the calculation condition, and the calculation result vector corresponding to the last calculation condition is the calculation result of the stencil calculation.
[0119] In one achievable manner, the first value is 0, and the second value is 1.
[0120] It should be noted that: the device for performing stencil calculation provided in the above embodiment only uses the division of the above functional modules as an example to illustrate when performing stencil calculation. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the computing device or program that performs stencil calculation is divided into different functional modules to complete all or part of the functions described above. In addition, the device for performing stencil calculation provided in the above embodiment and the method embodiment for performing stencil calculation belong to the same concept. The specific implementation process is detailed in the method embodiment and will not be repeated here.
[0121] The present application also provides a computer program product comprising instructions. The computer program product may be software or a program product comprising instructions that can be run on a computing device or stored in any available medium. When the computer program product is run on at least one computing device, the at least one computing device executes the method for performing stencil calculations provided in the present application.
[0122] The embodiment of the present application also provides a computer-readable storage medium. The computer-readable storage medium can be any available medium that can be stored by a computing device or a data storage device such as a data center containing one or more available media. The available medium can be a magnetic medium (for example, a floppy disk, a hard disk, a tape), an optical medium (for example, a DVD), or a semiconductor medium (for example, a solid-state drive). The computer-readable storage medium includes instructions that instruct the computing device to execute the method for performing stencil calculations in the embodiment of the present application.
[0123] In this application, the terms "first", "second", etc. are used to distinguish between identical or similar items having substantially the same effects and functions. It should be understood that there is no logical or temporal dependency between "first" and "second", nor is the quantity and execution order limited. It should also be understood that although the following description uses the terms first, second, etc. to describe various elements, these elements should not be limited by the terms. These terms are only used to distinguish one element from another. For example, without departing from the scope of various examples, the first numerical value can be referred to as the second numerical value, and similarly, the second numerical value can be referred to as the first numerical value. The first numerical value and the second numerical value can both be collectively referred to as numerical values, and in some cases, can be separate and different numerical values.
[0124] The term "at least one" in this application means one or more, and the term "plurality" in this application means two or more.
[0125] The above description is merely a specific embodiment of the present application, but the scope of protection of the present application is not limited thereto. Any person skilled in the art can easily conceive of various equivalent modifications or substitutions within the technical scope disclosed in this application, and such modifications or substitutions should be included in the scope of protection of the present application. Therefore, the scope of protection of the present application should be based on the scope of protection of the claims.
Claims
1. A method for performing stencil calculation, characterized in that: The stencil calculation includes at least one calculation condition and a corresponding calculation operation, and the method includes: Acquire a plurality of node data included in the calculation area, and combine the plurality of node data into a data vector; According to at least one calculation condition included in the stencil calculation, the plurality of node data are judged and processed to obtain a judgment result of each node data corresponding to each calculation condition; For each calculation condition, the judgment results of the plurality of node data corresponding to the calculation condition are combined into a judgment result vector to obtain the judgment result vector corresponding to the calculation condition; Based on the data vector and the judgment result vector corresponding to each calculation condition, the calculation operation corresponding to each calculation condition in the stencil calculation is performed to obtain the calculation result of the stencil calculation.
2. The method according to claim 1, characterized in that: The calculation region is a structured grid or an unstructured grid.
3. The method according to claim 2, characterized in that The calculation area is an unstructured grid, and the step of forming the plurality of node data into a data vector includes: Applying memory storage space for the plurality of node data in the memory; Mapping the plurality of node data from the original storage space to the memory storage space according to the traversal order of the plurality of node data in the computing area; Based on the vector read instruction, the plurality of node data are read from the memory storage space to obtain the data vector.
4. The method according to any one of claims 1 to 3, characterized in that: For each calculation condition, the judgment results of the plurality of node data corresponding to the calculation condition are combined into a judgment result vector to obtain the judgment result vector corresponding to the calculation condition, including: For each calculation condition, the judgment results of the plurality of node data corresponding to the calculation condition are combined into a judgment result vector according to the traversal order of the plurality of node data in the calculation area to obtain the judgment result vector corresponding to the calculation condition.
5. The method according to any one of claims 1 to 4, characterized in that: The step of performing judgment processing on the plurality of node data according to at least one calculation condition included in the stencil calculation to obtain a judgment result of each node data corresponding to each calculation condition includes: According to at least one calculation condition included in the stencil calculation, the plurality of node data are judged and processed; For each node data, if the node data does not meet the calculation condition, the judgment result corresponding to the node data is determined to be a first value; if the node data meets the calculation condition, the judgment result corresponding to the node data is determined to be a second value.
6. The method according to any one of claims 1 to 5, characterized in that: The step of performing a calculation operation corresponding to each calculation condition in the stencil calculation based on the data vector and the judgment result vector corresponding to each calculation condition to obtain a calculation result of the stencil calculation includes: For each calculation condition, based on the vector calculation instruction, the vector calculation of the judgment result vector and the input vector corresponding to each calculation condition is performed in sequence to obtain the calculation result vector corresponding to each calculation condition, wherein, The input vector corresponding to the first calculation condition is the data vector, and the input vector corresponding to the calculation condition after the first calculation condition is the calculation result vector corresponding to the previous calculation condition; in the calculation result vector corresponding to each calculation condition, the updated elements relative to the corresponding input vector are the calculation results of executing the corresponding calculation operation on the node data in the input vector that meets the calculation condition, and the calculation result vector corresponding to the last calculation condition is the calculation result of the stencil calculation.
7. The method according to claim 5, characterized in that The first value is 0, and the second value is 1.
8. A device for performing stencil calculations, characterized in that: The stencil calculation includes at least one calculation condition and a corresponding calculation operation, and the device includes: An acquisition module, used for acquiring a plurality of node data included in the calculation area, and composing the plurality of node data into a data vector; A judgment module, configured to perform judgment processing on the plurality of node data according to at least one calculation condition included in the stencil calculation, and obtain a judgment result of each node data corresponding to each calculation condition; A generating module, for each calculation condition, combining the judgment results of the plurality of node data corresponding to the calculation condition into a judgment result vector, so as to obtain the judgment result vector corresponding to the calculation condition; A calculation module is used to perform a calculation operation corresponding to each calculation condition in the stencil calculation based on the data vector and the judgment result vector corresponding to each calculation condition, so as to obtain a calculation result of the stencil calculation.
9. The device according to claim 8, characterized in that: The calculation region is a structured grid or an unstructured grid.
10. The device according to claim 9, characterized in that The calculation area is an unstructured grid, and the acquisition module is used to: Applying memory storage space for the plurality of node data in the memory; Mapping the plurality of node data from the original storage space to the memory storage space according to the traversal order of the plurality of node data in the computing area; Based on the vector read instruction, the plurality of node data are read from the memory storage space to obtain the data vector.
11. The device according to any one of claims 8 to 10, characterized in that The generating module is used for: For each calculation condition, the judgment results of the plurality of node data corresponding to the calculation condition are combined into a judgment result vector according to the traversal order of the plurality of node data in the calculation area to obtain the judgment result vector corresponding to the calculation condition.
12. The device according to any one of claims 8 to 11, characterized in that The judging module is used for: According to at least one calculation condition included in the stencil calculation, the plurality of node data are judged and processed; For each node data, if the node data does not meet the calculation condition, the judgment result corresponding to the node data is determined to be a first value; if the node data meets the calculation condition, the judgment result corresponding to the node data is determined to be a second value.
13. The device according to any one of claims 8 to 12, characterized in that The computing module is used for: For each calculation condition, based on the vector calculation instruction, the vector calculation of the judgment result vector and the input vector corresponding to each calculation condition is performed in sequence to obtain the calculation result vector corresponding to each calculation condition, wherein, The input vector corresponding to the first calculation condition is the data vector, and the input vector corresponding to the calculation condition after the first calculation condition is the calculation result vector corresponding to the previous calculation condition; in the calculation result vector corresponding to each calculation condition, the updated elements relative to the corresponding input vector are the calculation results of executing the corresponding calculation operation on the node data in the input vector that meets the calculation condition, and the calculation result vector corresponding to the last calculation condition is the calculation result of the stencil calculation.
14. The device according to claim 12, characterized in that The first value is 0, and the second value is 1.
15. A computing device, characterized in that: The computing device includes a memory and a processor, wherein the memory stores at least one instruction, and the processor executes the at least one instruction to execute the method according to any one of claims 1 to 7.
16. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program code, and when the computer program code is executed by a computer device, the computer device executes the method according to any one of claims 1 to 7.
17. A computer program product comprising instructions, characterized in that When the computer program product is run on a computer device, the computer device is caused to execute the method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Method, apparatus, and computing device for stencil computation
CN119576409B
Vector calculation device and method
CN113407154A
Code optimization method and device, computing equipment and computer storage medium
CN114594954A
Method and system for accelerating uniform grid Stencil calculation based on GPU (Graphics Processing Unit) mixing precision
CN115660936A
Kernel convolution for stencil computation optimization
US20170358124A1