A method for implementing diagonal matrix SPMV based on hybrid compression format on GPU
By converting the COO format matrix into DIA and CSR formats and performing parallel calculations on the GPU, the problems of zero element filling and load imbalance of sparse matrices are solved, and the SPMV efficiency of ship maritime navigation simulation is improved.
Patent Information
- Application Number
- CN202210569070.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-05-24
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2042-05-24
AI Technical Summary
In ship navigation simulation, zero-element filling of sparse matrices and load imbalance problems lead to a decrease in GPU parallel computing performance. Existing optimization methods have problems with long preprocessing time and load imbalance between blocks.
The diagonal matrix SPMV method based on hybrid compression format is used to convert the COO format matrix into DIA and CSR formats, divided into blocks by the standard deviation of the number of non-zero elements, and parallel computing is performed on the GPU, combined with the blocking strategy to optimize storage and Compute,reduce zero-element padding and load imbalance.
It effectively reduces storage and computing overhead, improves parallel computing performance, reduces pre-processing time, achieves load balancing, and improves the SPMV efficiency of ship navigation simulation.
Smart Images

Figure CN115048215B_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the field of ship marine navigation simulation, and in particular relates to a method for implementing a diagonal matrix SPMV based on a hybrid compression format on a GPU. Background Art
[0002] Sparse matrix-vector multiplication (SPMV) is one of the basic operations for solving linear systems. For many scientific and engineering applications, such as ship navigation simulation, the generated matrices may be large and sparse. In addition, these sparse matrices have various sparsity properties: irregularity, symmetry, diagonality, etc. In a given parallel computing environment, it is a challenging problem to implement and optimize SPMV using a suitable matrix compression algorithm.
[0003] Due to the complexity of the data grid of ship navigation simulation at sea and the irregularity of the computational domain boundary, the sparse patterns of the linear system become more diverse. When storing and calculating sparse matrices, a large number of zero elements will not only take up additional storage space, but also cause computational redundancy. Therefore, designing a suitable sparse matrix storage format can effectively improve the parallel computing performance of SPMV. For tri-diagonal / multi-diagonal matrices, the diagonal (DIA) storage method has better parallel computing efficiency than the row compression (CSR), coordinate format (COO) and hybrid format (HYB) methods. The DIA method is stored according to the number of non-zero diagonals, but in order to maintain the diagonal structure of the matrix, a large number of zero elements need to be filled. When there is a zero-length part on the diagonal, or when multiple diagonals are far from the main diagonal, or there are multiple discrete points in the matrix, this type of matrix can be called a far matrix. Compressing far matrices requires a large number of zero elements to be filled, which will lead to a decrease in GPU parallel computing performance.
[0004] In addition to the zero element problem, the DIA method also has the problem of load imbalance between blocks. Load imbalance will affect the performance of parallel SPMV, may cause some computing resources to be idle, and reduce the utilization of GPU. Therefore, the research on load balancing strategy is also an important direction in the field of ship navigation simulation and high-performance computing.
[0005] In recent years, several SPMV methods for optimizing diagonal matrices have emerged. The basic idea of the Hybrid of DIA and CSR (HDC) method is to store only diagonals with a sufficient number of non-zero elements (usually the threshold is half the number of elements in the main diagonal) in the DIA compressed matrix, and store other non-zero elements in the CSR format. HDC works well when the number of non-zero elements in most diagonals is close to the number of elements in the main diagonal. However, when the number of non-zero elements in most diagonals is close to the threshold, a large number of zero elements need to be filled in the DIA compressed matrix, which reduces the computational performance. The Diagonal Compressed Storage based on Row-Blocks (BRCSD) method introduces the concept of slice points and completes the parallel block strategy based on block clipping, thereby improving the compression ratio of the DIA algorithm, but there is a problem of load imbalance between blocks. The Modified HDC (M-HDC) method can extract partial diagonal structures quickly and efficiently. However, the distribution of parallel blocks requires a lot of experiments to obtain the distribution form to be judged, and the preprocessing time is long. Summary of the invention
[0006] The present invention provides a method for implementing a diagonal matrix SPMV based on a hybrid compression format on a GPU, so as to improve the SPMV parallel computing efficiency of a ship's marine navigation simulation.
[0007] The invention provides an electronic device.
[0008] The present invention provides a non-transitory computer-readable storage medium.
[0009] The present invention is achieved through the following technical solutions:
[0010] A method for implementing a diagonal matrix SPMV based on a hybrid compression format on a GPU, the method comprising the following steps:
[0011] Step 1: Input the ship marine navigation simulation matrix data file in COO format and convert it into a traditional matrix form;
[0012] Step 2: Based on the minimum standard deviation strategy of the number of non-zero elements, divide the matrix transformed in step 1 into a DIA matrix and a diagonal offset array;
[0013] Step 3: Use the remaining data in the input matrix transformed in step 1 to store it using the CSR method based on the block strategy;
[0014] Step 4: Transfer the DIA matrix data of step 2 and the CSR related data of step 3 from the host side to the device side respectively, and perform GPU parallel SPMV operation in the manner of processing one row per thread;
[0015] Step 5: The calculation results of the two stages in step 4 are transferred from the device side to the host side, and integrated on the host side to improve the simulation efficiency of the ship's maritime navigation.
[0016] A method for implementing a diagonal matrix SPMV based on a hybrid compression format on a GPU, wherein step 1 specifically includes the following steps:
[0017] Step 1.1: Obtain a matrix file in COO format from the sparse matrix dataset of ship navigation simulation at sea, and use it as the input matrix;
[0018] Step 1.2: Read the rows, columns and parameter values of non-zero elements in the matrix file of step 1.1;
[0019] Step 1.3: Set a dynamic two-dimensional pointer, read the matrix data in COO format into the pointer, and generate a traditional sparse matrix.
[0020] A method for implementing a diagonal matrix SPMV based on a hybrid compression format on a GPU, wherein step 2 specifically includes the following steps:
[0021] Step 2.1: Based on the matrix transformed in step 1, obtain the number of non-zero elements in each diagonal and calculate the total number of non-zero elements;
[0022] Step 2.2: Get the average number of non-zero elements in each diagonal;
[0023] Step 2.3: Use the average number of non-zero elements as the threshold to obtain the DIA matrix part and the offset array in the input matrix;
[0024] Step 2.4: Get the number of non-zero elements in each column from the DIA matrix, which corresponds to the number of non-zero elements in each row of the input matrix; use array A to store the number of non-zero elements in each column, and calculate the average value of the array in each block;
[0025] Step 2.5: Use the average value obtained on each block in step 2.4 as the threshold, and insert the non-zero values that are greater than the value in array A into a new array B one by one. i In, until B i Until the total number is greater than or equal to the average value, i Save as a block; calculate the standard deviation between each block, and obtain the optimal number of blocks with the smallest standard deviation;
[0026] Step 2.6: Store the non-zero elements in the DIA matrix in a diagonal manner and obtain the diagonal offset.
[0027] A method for implementing a diagonal matrix SPMV based on a hybrid compression format on a GPU, wherein step 3 specifically includes the following steps:
[0028] Step 3.1: After removing the DIA matrix from the input matrix, obtain the number of non-zero elements in each row and store it in array C;
[0029] Step 3.2: Set the number of blocks i, and store the first i numbers in array C in step 3.1 in the form of a column to the new two-dimensional pointer D i Then judge D i Find the minimum value of the sum of the number of non-zero elements in each row in , and record the row number of the row;
[0030] Step 3.3: Insert the data in array C into the two-dimensional pointer D based on the row number in step 3.2. i middle;
[0031] Step 3.4: Get the two-dimensional pointers D of multiple blocks i , and calculate the standard deviation of the non-zero elements in each block. Compare the size of the standard deviation between multiple blocks. If D i The standard deviation of is the smallest, then block i is the optimal number of blocks;
[0032] A method for implementing a diagonal matrix SPMV based on a hybrid compression format on a GPU, wherein step 4 is specifically as follows:
[0033] Step 4.1: Pass the DIA matrix and diagonal offset from the host to the device; start the parallel DIA_SPMV operation;
[0034] Step 4.2: Pass the CSR array, column index, and non-zero offset from the host to the device; start the parallel CSR_SPMV operation.
[0035] A method for implementing a diagonal matrix SPMV based on a hybrid compression format on a GPU, wherein step 5 comprises the following steps:
[0036] Step 5.1: Set up CPU and GPU synchronization functions;
[0037] Step 5.2: Based on the synchronization function in step 5.1, the result of parallel DIA calculation is transferred from the device to the host;
[0038] Step 5.3: Based on the host side in step 5.1, the result of the parallel CSR calculation is transmitted from the device side to the host side;
[0039] Step 5.4: Integrate the results of Step 5.2 and Step 5.3.
[0040] A method for implementing a diagonal matrix SPMV based on a hybrid compression format on a GPU, wherein the step 5.2 of parallel DIA calculation specifically includes setting a calculation starting position of each thread;
[0041] Set the position of the calculated y value of each thread; set the maximum number of non-zero element calculations for each thread;
[0042] Calculate the SPMV calculation for each thread, and each thread accumulates the number of elements in one iteration to get the value of y.
[0043] A method for implementing a diagonal matrix SPMV based on a hybrid compression format on a GPU, wherein the step 5.3 parallel CSR calculation is specifically to set each thread to be able to access a value within a corresponding range of an array storing non-zero elements;
[0044] Based on the maximum data length in each block in step 3.3, calculate the length of the array accessed by the thread;
[0045] Start executing the parallel calculation operation of SPMV, and locate the correct position of the x value according to the column coordinates of the non-zero element for multiplication;
[0046] Each thread iterates to perform product operations and performs merge operations;
[0047] Until every non-zero element in the CSR parallel stage has completed the SPMV calculation and saved the y value in the GPU.
[0048] An electronic device comprises a memory, a processor and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, any one of the implementation methods described above is implemented.
[0049] A non-temporary computer-readable storage medium stores a computer program, wherein the computer program, when executed by a processor, implements any of the above-mentioned implementation methods.
[0050] The beneficial effects of the present invention are:
[0051] The present invention only stores the non-zero elements and the corresponding index addresses, thereby reducing unnecessary storage and calculation costs.
[0052] The present invention combines the characteristics of the ship's marine navigation simulation data structure and improves the existing technology from multiple aspects such as the compression structure and algorithm of the sparse diagonal matrix. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] Figure 1 It is a structural schematic diagram of the present invention.
[0054] Figure 2 It is a schematic diagram of DIA compression matrix block in the PHCM storage format of the present invention.
[0055] Figure 3 It is an irregular matrix diagram of the present invention.
[0056] Figure 4 It is a partitioning strategy diagram of the number of non-zero elements in the matrix of the present invention.
[0057] Figure 5 It is the optimized DIA parallel algorithm flow chart of the present invention. DETAILED DESCRIPTION
[0058] The technical solutions in the embodiments of the present invention will be described clearly and completely below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0059] While using compressed storage to reduce storage space requirements, it also introduces a large number of discrete memory access and indirect memory access operations, which increases memory access overhead.
[0060] Sparse matrix algorithms are typical irregular algorithms with limited memory access and low computational memory access ratio. Since compressed storage destroys the temporal and spatial locality in the computation process, the computational efficiency of sparse matrix algorithms on traditional cache-based general-purpose processors is very low.
[0061] Due to the presence of a large number of zero elements in sparse matrices, the use of traditional dense matrix storage and calculation methods will result in a waste of storage and computing resources, and may even cause insufficient storage space in the system.
[0062] (1) CPU preprocessing stage
[0063] Before parallel computing, the present invention needs to effectively compress the matrix. The compressed matrix can reduce the filling of zero elements on a large scale, thereby reducing computational redundancy and storage redundancy. In order to more effectively process multi- / tri-diagonal matrices and distant matrices where each diagonal is far from the main diagonal, it is necessary to adopt the DIA compression method. However, DIA may have the problem of excessive filling of zero elements. Therefore, the present invention introduces the setting of thresholds based on the DIA compression method, combined with the advantages of the CSR method, and can effectively improve the degree of matrix compression.
[0064] A method for implementing the SPMV diagonal matrix based on a hybrid compression format on a GPU is mainly divided into the following parts in the CPU preprocessing stage:
[0065] Step 1: Before performing the matrix compression operation, the sparse matrix data set of the ship's maritime navigation simulation needs to be read into the program as the input matrix. The data set is displayed in COO format and needs to be converted into a traditional matrix form first.
[0066] Step 2: For the sum of the number of diagonals and the number of non-zero elements, the present invention can obtain the parameter avg of the average number of non-zero elements in each block. Based on the value of avg, the input matrix is divided into a DIA compression matrix and a CSR matrix. In this step, the DIA compression matrix is reasonably divided into blocks and effective storage is completed.
[0067] Step 3: Use CSR to store the matrix data after the division in the DIA stage. Since the distribution of the number of non-zero elements between rows is uneven, it is necessary to optimize the CSR storage method to meet the SPMV load balancing conditions based on the CSR compression method. If the similarity of the number of non-zero elements between each block is the largest, the parallel computing efficiency can be effectively improved.
[0068] Step 4: Implement data transmission between CPU and GPU.
[0069] Furthermore, the step 1 comprises the following steps:
[0070] Step 1.1: Analyze and obtain the COO format data files of diagonal matrix and irregular matrix in the data set of ship navigation simulation.
[0071] Step 1.2: Restore the COO format data file to a traditional sparse matrix form that can be accommodated in memory.
[0072] Furthermore, the step 2 comprises the following steps:
[0073] Step 2.1: Get the number of non-zero elements on each diagonal of the input matrix and save it to the specified array.
[0074] Step 2.2: Set a threshold, store the diagonal data in the input matrix that is greater than the threshold using the DIA compression matrix, and store the remaining matrix data using the CSR matrix.
[0075] Step 2.3: Analyze the upper and lower limits of the number of blocks divided in the DIA matrix, and obtain the number of non-zero elements in each column of the DIA compressed matrix.
[0076] Step 2.4: Determine the difference between the value of each column element and the average value. If it is greater than or equal to the average value, divide the elements into the same block. By following this strategy, multiple groups can be generated.
[0077] Step 2.5: Calculate the standard deviation between multiple blocks in each group, record the group number with the minimum standard deviation, and you can get the optimal number of blocks participating in SPMV parallel computing and the distribution of data.
[0078] Furthermore, the step 3 comprises the following steps:
[0079] Step 3.1: Compress the remaining data after the DIA stage using CSR storage.
[0080] Step 3.2: Get the number of non-zero elements in each row of the remaining matrix and rearrange them. Set up a new matrix and put the number of non-zero elements in the first block of the array into the first column of the new matrix according to the number of blocks.
[0081] Step 3.3: Based on the principle of minimizing the total number of non-zero elements in each row of the new matrix, effectively divide the remaining data to achieve almost the same number of non-zero elements between blocks.
[0082] Furthermore, step 4 includes the following steps:
[0083] Step 4.1: Transfer the DIA compressed data from the host to the device;
[0084] Step 4.2: Pass the CSR compressed data from the host to the device
[0085] (II) GPU parallel computing stage
[0086] A method for implementing the SPMV diagonal matrix based on a hybrid compression format on a GPU is mainly divided into the following parts in the GPU parallel computing stage:
[0087] Step 5: Based on the PHCM compression method, the present invention proposes a PHCM-based far matrix parallel optimization algorithm. The load balance between blocks is guaranteed. The algorithm is divided into two stages: the DIA parallel calculation stage and the CSR parallel calculation stage. First, the y value of the DIA part is obtained, and then the y value of the CSR part is calculated.
[0088] Step 6: Return the calculation results in the GPU to the CPU, and merge the y values generated in the two stages for the simulation process of the ship sailing at sea.
[0089] Furthermore, the step 5 comprises the following steps:
[0090] Step 5.1: In the DIA parallel computing phase, based on a reasonable partitioning strategy and thread allocation scheme, complete the design and implementation of the parallel algorithm for the diagonal data.
[0091] Step 5.2: In the CSR parallel computing stage, for the distribution of non-zero elements in the irregular matrix, a parallel computing method of one thread processing one row is adopted, and a block strategy is combined to ensure load balance between blocks.
[0092] Furthermore, the step 6 comprises the following steps:
[0093] Step 6.1: The device side transmits the calculated result y value to the host side respectively.
[0094] Step 6.2: After receiving the y values at the host end, merge the y values generated in the two stages.
[0095] An electronic device comprises a memory, a processor and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, any one of the implementation methods described above is implemented.
[0096] A non-temporary computer-readable storage medium stores a computer program, wherein the computer program, when executed by a processor, implements any of the above-mentioned implementation methods.
[0097] In the following detailed description, reference is made to the accompanying drawings which form a part hereof. Figure 1 As shown in the figure, the implementation method of the hybrid compression format diagonal matrix SPMV on the GPU mainly includes two stages: CPU preprocessing stage and GPU parallel computing stage. The CPU preprocessing stage mainly includes the optimization of the DIA matrix compression method and the CSR matrix compression method. Its purpose is to use the optimized DIA storage for quasi-diagonal matrices as much as possible on the basis of maintaining the diagonal form, and to use the optimized CSR method for storage of scattered or irregular matrices. The GPU parallel computing stage is to perform parallel SPMV calculations based on the optimized compression method PHCM.
[0098] The CPU preprocessing stage mainly includes four steps: data reading stage, DIA optimization stage, CSR optimization stage and data transmission stage between CPU and GPU.
[0099] (1) CPU preprocessing stage
[0100] Step 1: During the SPMV process, it is necessary to test and analyze real data. This data is the matrix data extracted from engineering applications. It is read into the matrix compression algorithm.
[0101] The step 1 specifically comprises the following steps:
[0102] Step 1.1: From the sparse matrix data set of the ship navigation simulation, obtain the data file displayed in COO format: .mtx file as the input matrix.
[0103] Step 1.2: Read the COO format file and convert it into a traditional sparse matrix form, that is, the input matrix contains zero elements filled.
[0104] Step 2: DIA optimization stage;
[0105] The step 2 specifically comprises the following steps:
[0106] Step 2.1: Based on the matrix data in step 1, first obtain the number of non-zero elements on each diagonal and store it in the array dia_num. In order to ensure the continuity of the diagonal matrix, it is necessary to store more diagonal data that meet the conditions in the DIA matrix. Taking matrix A as an example (as shown in formula (1)), in the order from the lower left diagonal containing non-zero elements to the upper right diagonal containing non-zero elements, dia_num = {1, 2, 4, 3, 3} can be obtained.
[0107]
[0108] Step 2.2: Calculate the sum of the dia_num array and obtain the average value k_Diagonal of dia_num. According to k_Diagonal, matrix A is divided into DIA matrix and CSR matrix. As shown in matrix A, k_Diagonal = 2. Store the diagonals in matrix A with more than 2 non-zero elements in each diagonal in the DIA compression matrix, and store the other diagonals containing non-zero elements in the CSR array in row-major order. According to the partition of matrix A, the DIA compression matrix (DIA-Matrix) can be obtained, as shown in formula (2):
[0109]
[0110] There are three diagonals in the DIA-Matrix. Each column represents the distribution of each row of data in the matrix A. In addition, it can be seen from the DIA-Matrix that 8 zero elements still need to be filled. The DIA-Matrix needs to be further optimized.
[0111] Step 2.3: Set the number of non-zero elements in each column of the DIA-Matrix matrix to be stored in the array Array_DIA. According to formula (2), the array form of Array_DIA can be obtained: Array_DIA = {3, 3, 2, 2}. The block threshold is set, which is the number of blocks that need to be divided into the DIA-Matrix matrix on the GPU. The minimum value range of the number of blocks (Blocks) is 2, and the maximum value is the maximum number of columns of non-zero elements in the DIA-Matrix matrix. It can be seen from the DIA-Matrix matrix that the minimum setting of Blocks is 2 and the maximum setting is 4.
[0112] Step 2.4: Give the formula for calculating the optimal number of blocks:
[0113] Array_DIA={x0,x1,x2......x k} (3)
[0114] avg=(x0+x1+x2+...+x k ) / Blocks (4)
[0115] According to formula (3) and formula (4), the elements in the Array_DIA array are compared with avg in order from left to right. If x0>avg or x0=avg, then put them in a block. Otherwise, continue to add x1 until the number of non-zero elements in a block is greater than or equal to avg. In this way, the distribution of multiple different groups of blocks can be obtained.
[0116] Step 2.5: Based on step 2.4, calculate the standard deviation of each group within the block, and then determine which group has the smallest standard deviation, indicating that the total number of non-zero elements between the blocks in this group is almost equal, satisfying the load balancing condition. A group contains multiple blocks, and the standard deviation formula is:
[0117]
[0118] Taking the DIA-Matrix matrix as an example, when Blocks = 2, avg = 10 / 2 = 5. The final block is {[3, 3], [2, 2]}. When Blocks = 3, avg = 10 / 3 = 3. According to formula (5), when Blocks = 2, s_deviation = √((6-5) 2 +(4-5) 2 ) / 2 = 1. When Blocks = 3, the resulting blocks are {[3], [3], [2, 2]}, s_deviation = √((3-3) 2 +(3-3) 2 +(4-3) 2) / 3=√0.3. Therefore, three blocks are selected for matrix partitioning to make the load more balanced. The distribution of data in the blocks after DIA-Matrix partitioning is as follows Figure 2 shown.
[0119] from Figure 2 It can be seen that there are 2 zero elements in the DIA compression matrix of the PHCM method that need to be filled. Although the number of fillings is the same as that of the HDC method, the PHCM method ensures the continuity of the DIA diagonal and x-continuous data during storage, which improves the efficiency of sparse matrix access. Compared with the BRCSD method, the PHCM method fills fewer zero elements and the load between blocks is more balanced.
[0120] Step 3: CSR optimization stage;
[0121] The step 3 specifically comprises the following steps:
[0122] Step 3.1: After the input matrix is compressed in the DIA stage, the remaining data needs to be stored effectively using the CSR algorithm. According to the matrix data in formula (1), the remaining data after the DIA optimization stage is: 11, 12, 13. This example is small in scale and cannot fully reflect the problems existing in CSR. When the DIA stage ends, all the remaining data is stored in CSR. When the matrix scale is large, load imbalance problems are prone to occur. In order to solve this problem, a processing method based on optimal block is adopted. Figure 3 Example of matrix shown.
[0123] Step 3.2: From Figure 3 It can be seen that the matrix in the figure is an irregular matrix. Assume that this matrix is the distribution of the remaining non-zero elements of matrix A after processing the DIA stage. First, obtain the non-zero element array Row_Array_Old for each row of the matrix, as shown in formula (6).
[0124] Row_Array_Old={5,3,7,1,3,1,1} (6)
[0125] Rearrange the array Row_Array_Old in descending order from large to small, as shown in formula (7):
[0126] Row_Array={7,5,3,3,1,1,1} (7)
[0127] Next, we begin to effectively partition the CSR portion of the matrix into multiple blocks. To describe whether each block is the best partition, we first use an example of 3 blocks to explain how to partition the array. Display the first three rows of the array Row_Array as a column, such as Figure 4As shown in (a), a new matrix Matrix_Segment is created to store the information of each block.
[0128] Step 3.3: Since 3 is Figure 4 (a) The minimum value among "7", "5", and "3", add "3" in Row_Array to Figure 4 The "3" line in (a) forms Figure 4 (b). Our goal is to minimize the total number of non-zero elements in each row of Matrix_Segment. Each row in the matrix Matrix_Segment corresponds to the number of non-zero elements in each block in the original matrix. Figure 4 In (b), after calculation, the sum of the number of non-zero elements in the three blocks is 7, 5, and 6 respectively. Insert the remaining numbers in the array Row_Array into the non-zero elements and the smallest row in the matrix Matrix_Segment. Figure 4 In (b), the total number of non-zero elements in the three rows is: "7", "5", and "6". Since "5" is the minimum value of "7", "5", and "6", the next value of the array Row_Array: "1" is inserted into Figure 4 The row where "5" is located in (b) forms Figure 4 (c). Follow this strategy until every element of the array Row_Array is inserted into the matrix Matrix_Segment, such as Figure 4 (e) After partitioning the matrix, the array Row_Array is updated with the values in Matrix_Segment in row-major order.
[0129] Based on this strategy, Figure 4 As shown in (e), the final block distribution can be obtained as shown in formula (8):
[0130] Block_Array={[7],[5,1,1],[3,3,1]} (8)
[0131] The number of blocks needs to be set. Calculate the average number of non-zero elements in each block. Given formula (9-10):
[0132] Row_Array=[a1,a2,a3,...,a n ] (9)
[0133] Row_Array_Mean=(a1+a2+......a n ) / Block i (10)
[0134] Combined with the standard deviation calculation formula in formula (5), if the standard deviation s_deviation of the i-th block is the smallest, then Block i is the optimal number of blocks. Figure 4 When the matrix in selects Block=3, the standard deviation is the smallest, and load balancing between blocks can be achieved.
[0135] In order to store the matrix more conveniently using the CSR method, the original matrix needs to be rearranged according to the partition result of the array Row_Array. It should be noted here that if there are few non-zero elements left after the DIA stage, it is recommended to use CSR directly. After experimental analysis, it is recommended to use CSR directly for the number of non-zero elements within 100. When dealing with a small number of non-zero elements, our method has no obvious advantage over CSR, but it may increase the time of preprocessing operations.
[0136] Step 4: Data transfer between CPU and GPU.
[0137] The step 4 specifically comprises the following steps:
[0138] Step 4.1: Transfer the parallel DIA-related arrays from the host to the device. The transfer function uses cudaMemcpy, and the time synchronization function uses cudaDeviceSynchronize().
[0139] Step 4.2: Transfer the parallel CSR-related arrays from the host to the device. The transfer function uses cudaMemcpy and the time synchronization function uses cudaDeviceSynchronize().
[0140] (II) GPU parallel computing stage
[0141] Step 5: Based on the PHCM compression method, the present invention proposes a PHCM-based far matrix parallel optimization algorithm. The algorithm adopts CUDA parallel optimization technology to ensure load balance between blocks on the basis of reducing the padding of zero elements in the far matrix. The algorithm is divided into two stages: DIA parallel calculation stage (PHCM-DIA parallel algorithm) and CSR (PHCM-CSR parallel algorithm) parallel calculation stage. First calculate the y value of the DIA part, and then calculate the y value of the CSR part. After calculating the two stages, merge the y values.
[0142] The step 5 specifically comprises the following steps:
[0143] Step 5.1: DIA parallel computing process
[0144] In addition to the array dev_Array_DIA that stores the data, the parameters x, dev_Block_Ele_offset, dev_Block_Row_offset, dev_Array_Index_DIA, dev_num_diag, dev_Block_Row, and dev_NNZ_K are passed to the device. Among them, dev_Block_Ele_offset indicates the offset of the number of data elements in the block. dev_Block_Row_offset indicates the number of matrix rows contained in each block. dev_Array_Index_DIA indicates the column index coordinates of the data relative to the original matrix. dev_num_diag indicates the number of diagonals stored in the DIA compressed matrix. dev_Block_Row indicates the offset of the number of matrix rows contained in each block. dev_NNZ_K indicates the number of diagonals in each block.
[0145] exist Figure 5 In the example, the data in the DIA matrix is divided into three blocks according to the partition strategy, and one thread in each block processes a row in the matrix A. Figure 5 Each column of data in corresponds to a row of data in matrix A. Therefore, when calculating y = Ax in the GPU, each thread iteratively performs multiplication and accumulation operations to obtain the value of y. Figure 5 In the algorithm, dev_block_ele_offset = {0, 3, 6, 12}, dev_Block_Row_offset = {1, 1, 2}, dev_Block_Row = {0, 4}, dev_Array_Index_DIA = {0, 1, 2, 1, 2, 3, 3, 3, 4, 4, 5}, dev_NNZ_K = {3, 3, 4}. The specific implementation process is shown in the algorithm PHCM-DIA:
[0146]
[0147]
[0148] Step 5.2: CSR parallel computing process
[0149] According to the partitioning strategy in step 3.3 and the array transferred to the GPU in step 4.2, CSR parallel SPMV calculation is performed. The present invention provides a PHCM-CSR parallel algorithm, and the main steps of the algorithm are as follows:
[0150] 1. Set each thread to be able to access the values in the corresponding range of the array storing non-zero elements.
[0151] 2. Based on the maximum data length in each block in step 3.3, calculate the length of the array accessed by the thread.
[0152] 3. Start executing the parallel computing operation of SPMV, and at the same time locate the correct position of the x value according to the column coordinates of the non-zero elements for multiplication.
[0153] 4. Each thread iterates and performs product operations and merge operations.
[0154] 5. Until each non-zero element in the CSR parallel stage has completed the SPMV calculation and saved the y value in the GPU.
[0155] Finally, the DIA result and the CSR result are transferred from the device to the host using cudaMemcpy with the special parameter cudaMemcpyDeviceToHost. The y values obtained from the two stages of SPMV are merged on the host to get the final result.
[0156] Step 6: Return the calculation results from the GPU to the CPU, and merge the y values of the two algorithms.
[0157] The step 6 specifically comprises the following steps:
[0158] Step 6.1: The y values calculated by CSR and DIA are passed through the cudaMemcpy function respectively, and the host-side related array inherits this y value.
[0159] Step 6.2: On the host side, the y values calculated by CSR and DIA are combined to generate the final result and iteratively realize the simulation process of the ship's sea voyage.
Claims
1. A method for implementing a diagonal matrix SPMV on a GPU based on a hybrid compression format. It is characterized in that The implementation method comprises the following steps: Step 1: Input the ship marine navigation simulation matrix data file in COO format and convert it into a traditional matrix form; Step 2: Based on the minimum standard deviation strategy of the number of non-zero elements, divide the matrix transformed in step 1 into a DIA matrix and a diagonal offset array; The step 2 specifically includes the following steps: Step 2.1: Based on the matrix transformed in step 1, obtain the number of non-zero elements in each diagonal and calculate the total number of non-zero elements; Step 2.2: Get the average number of non-zero elements in each diagonal; Step 2.3: Use the average number of non-zero elements as the threshold to obtain the DIA matrix part of the original matrix and the offset array; Step 2.4: Obtain the number of non-zero elements in each column from the DIA matrix, which corresponds to the number of non-zero elements in each row of the original matrix; use array A to store the number of non-zero elements in each column, and calculate the average value of the array in each block; Step 2.5: Use the average value obtained on each block in step 2.4 as the threshold, and insert the non-zero values that are greater than the value in array A into a new array B one by one. i In, until B i Until the total number is greater than or equal to the average value, i Save as a block; calculate the standard deviation between each block, and obtain the optimal number of blocks with the smallest standard deviation; Step 2.6: Store the non-zero elements in the DIA matrix in a diagonal manner and obtain the diagonal offset; Step 3: Use the remaining data in the matrix transformed in step 1 to store it using the CSR method based on the block strategy; Step 4: Transfer the DIA matrix data of step 2 and the CSR related data of step 3 from the host side to the device side respectively, and perform GPU parallel SPMV operation in the manner of processing one row per thread; Step 5: The calculation results of the two stages in step 4 are transferred from the device side to the host side, and integrated on the host side to realize the simulation of the ship's sea voyage.
2. According to claim 1, a method for implementing a diagonal matrix SPMV based on a hybrid compression format on a GPU, It is characterized in that The step 1 specifically comprises the following steps: Step 1.1: Obtain a matrix file in COO format from the sparse matrix dataset of the ship's maritime navigation simulation; Step 1.2: Read the rows, columns and parameter values of non-zero elements in the matrix file of step 1.1; Step 1.3: Set a dynamic two-dimensional pointer, read the matrix data in COO format into the pointer, and generate a traditional sparse matrix.
3. According to the method for implementing the hybrid compression format-based diagonal matrix SPMV on a GPU as described in claim 1, It is characterized in that The step 3 specifically comprises the following steps: Step 3.1: After removing the DIA matrix from the original matrix, obtain the number of non-zero elements in each row and store it in array C; Step 3.2: Set the number of blocks i, and store the first i numbers in array C in step 3.1 in the form of a column to the new two-dimensional pointer D i Then judge D i Find the minimum value of the sum of the number of non-zero elements in each row in , and record the row number of the row; Step 3.3: Insert the data in array C into the two-dimensional pointer D based on the row number in step 3.
2. i middle; Step 3.4: Get the two-dimensional pointers D of multiple blocks i , and calculate the standard deviation of the number of non-zero elements in each block; if D i The standard deviation of is the smallest, then block i is the optimal number of blocks.
4. According to claim 3, a method for implementing a diagonal matrix SPMV based on a hybrid compression format on a GPU, It is characterized in that The step 4 is specifically as follows: Step 4.1: Pass the DIA matrix and diagonal offset from the host to the device; start the parallel DIA_SPMV operation; Step 4.2: Pass the CSR array, column index, and non-zero offset from the host to the device; start the parallel CSR_SPMV operation.
5. According to claim 4, a method for implementing a diagonal matrix SPMV based on a hybrid compression format on a GPU, It is characterized in that The step 5 comprises the following steps: Step 5.1: Set up CPU and GPU synchronization functions; Step 5.2: Based on the synchronization function in step 5.1, the result of parallel DIA calculation is transferred from the device to the host; Step 5.3: Based on the host side in step 5.1, the result of the parallel CSR calculation is transmitted from the device side to the host side; Step 5.4: Integrate the results of Step 5.2 and Step 5.
3.
6. According to claim 5, a method for implementing a diagonal matrix SPMV based on a hybrid compression format on a GPU, It is characterized in that The step 5.2 of parallel DIA calculation specifically includes setting the calculation starting position of each thread; Set the position of the calculated y value of each thread; set the maximum number of non-zero elements processed by each thread; Calculate the SPMV calculation for each thread, and each thread iteratively accumulates the number of elements to obtain the value of y.
7. According to claim 5, a method for implementing a hybrid compression format-based diagonal matrix SPMV on a GPU, It is characterized in that The step 5.3 of parallel CSR calculation is specifically to set each thread to be able to access the values in the corresponding range of the array storing non-zero elements; Based on the maximum data length in each block in step 3.3, calculate the length of the array accessed by the thread; Start executing the parallel calculation operation of SPMV, and locate the correct position of the x value according to the column coordinates of the non-zero element for multiplication; Each thread iterates to perform product operations and performs merge operations; Until every non-zero element in the CSR parallel stage has completed the SPMV calculation and saved the y value in the GPU.
8. An electronic device, It is characterized in that The method comprises a memory, a processor and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the method as claimed in any one of claims 1 to 7 is implemented.
9. A non-transitory computer-readable storage medium having stored thereon a computer program, It is characterized in that When the computer program is executed by a processor, the implementation method as described in any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Sparse matrix storage method CSRL (Compressed Sparse Row with Local Information) and SpMV (Sparse Matrix Vector Multiplication) realization method based on same
CN103336758A
Method optimizing sparse matrix vector multiplication to improve incompressible pipe flow simulation efficiency
CN103984527A
