Method for storing sparse matrix, method for calculating vector, storage medium
By dividing the sparse matrix into blocks and selecting the optimal storage format, the problem of unstable compression efficiency in sparse matrix storage is solved, achieving efficient storage and fast access to sparse matrices.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- SHENZHEN GUOWEI FUXIN TECH CO LTD
- Filing Date
- 2022-08-11
- Publication Date
- 2026-07-14
AI Technical Summary
Existing sparse matrix storage methods have unstable compression efficiency under different distributions and sparsity levels, and cannot effectively save memory space.
By dividing the sparse matrix into blocks, adaptively adjusting the block size, and selecting the optimal storage format based on the element density of each block, a two-dimensional index structure is used for storage.
Stable compression efficiency for different sparse matrices is achieved, reducing memory usage and improving storage efficiency and read speed.
Smart Images

Figure CN115357852B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the technical field of vector computation, and more particularly to a method for storing sparse matrices. Background Technology
[0002] Integrated sparse matrix compression storage has wide applications in many fields such as graph computation, social network analysis, circuit simulation, structural mechanics, and fluid mechanics. For large sparse matrices in these applications, to meet the requirements of compressed storage, the zero elements in the sparse matrix are usually not stored; only the row and column coordinates and element value information of each non-zero element are recorded.
[0003] Existing technologies for storing sparse matrices include: cross-linked list method, triplet method, row compression method, and array storage method.
[0004] The cross-linked list method stores sparse matrices, containing the row and column coordinates of each element, along with two pointers pointing to the next element in the same row and the next element in the same column. This method records all coordinate information and includes two additional pointers, resulting in significant pointer overhead.
[0005] The triplet method uses three arrays to store the elements of the matrix sequentially: row coordinates, column coordinates, and element values, respectively. This reduces pointer overhead compared to the cross-linked list method. Figure 4 As shown, if the first and second rows of the matrix to be stored contain non-zero elements, then the `row` array stores the row coordinates 1 and 2. If the first and second columns of the matrix to be stored contain non-zero elements, then the `col` array stores the column coordinates 1 and 2. If the element values are 1 and 3, then the `value` array stores 1 and 3.
[0006] The CSR format commonly used in industry now also uses three arrays to record all the information, and the stored information is as follows:
[0007] Val[nonzero]: Records the value of each nonzero element. The array length is the number of nonzero elements.
[0008] Colindex[nonzero]: Records the column containing each nonzero element. The array length is the number of nonzero elements.
[0009] Ptr[m+1]: Records the index of the first non-zero element in each row in the array val[nonzero] colindex[nonzero], where ptr[m] = nz, and the length of the array is the row order of the matrix + 1.
[0010] like Figure 5 As shown, Val[nonzero] (i.e. Figure 5The value in `Colindex[nonzero]` records the values of the non-zero elements, which are 1, 2, 3, 4, 5, 6, and 7 respectively. Figure 5 The `colindex` in the array records the column containing non-zero elements, which are 0, 1, 2, 3, 1, 2, 3 respectively. `Ptr[m+1]` (i.e., ...) Figure 5 The ptr in the array records the index of the first non-zero element in each row, which is 0, 4, 7, 7, 7. This means that the first row has (4 - 0) elements (i.e., 4 elements), namely colindex[0] ~ colindex[3]. The second row has (7 - 4) elements (i.e., 3 elements), namely colindex[4] ~ colindex[6]. The third row has (7 - 7) elements (i.e., 0 elements). The fourth row is the same as the third row, also with 0 elements. Here, m is the row order of the matrix, and the length of the array is the row order of the matrix + nz. The black rectangle in the figure intuitively expresses the compression effect.
[0011] Row compression can compress the row coordinates of elements in the same row into a single value, thus saving memory space compared to triplet compression. Tests have shown that row compression is very stable for different matrices.
[0012] In actual production environments, some sparse matrices are often distributed in a diagonal-centric manner. Therefore, the diagonal format is also a commonly used compression storage method, using two arrays to store the elements in the matrix.
[0013] Offsets[]: Records the offset of the diagonal containing non-zero elements relative to the main diagonal.
[0014] Data[]: Stores all elements in the corresponding diagonal into an array of elements according to the sorting in offsets.
[0015] When the non-zero elements of a matrix are mainly distributed on the diagonal, the diagonal format can save a lot of coordinate information. However, it has the drawback of storing redundant zero elements. Therefore, the compression efficiency of the diagonal format is not stable. When the matrix elements are not mainly distributed on the diagonal, the memory usage of the diagonal format increases rapidly.
[0016] Existing technologies include patents and research papers that use a bitmap matrix with the same order as the matrix to identify the number of non-zero elements in the matrix. For positions with non-zero elements, the bit value at the corresponding position in the bitmap is changed to 1. This method effectively compresses coordinate information, but when the matrix elements are extremely sparse, the bitmap matrix will store a large amount of redundant position information, and the compression efficiency is not stable.
[0017] In summary, the following points should be considered for compressed storage of sparse matrices: 1. Stable compression efficiency should be achieved for sparse matrices of any distribution. 2. Stable compression efficiency should be achieved for sparse matrices of any sparsity. 3. Allocate as little memory as possible to coordinate information to obtain better compression efficiency.
[0018] Therefore, how to provide a new storage method for sparse matrices is a technical problem that the industry urgently needs to solve. Summary of the Invention
[0019] To address the problem that existing storage methods cannot achieve stable compression efficiency for sparse matrices with any distribution, this invention proposes a sparse matrix storage method, a vector calculation method, and a storage medium.
[0020] The sparse matrix storage method proposed in this invention includes:
[0021] Step 1: Obtain the basic information of the matrix to be stored and initialize the size of the blocks;
[0022] Step 2: Divide the matrix to be stored into blocks according to the current blocks, and calculate the storage efficiency corresponding to the current blocks;
[0023] Step 3: Increase the size of the blocks, divide the matrix to be stored into the current blocks, and calculate the storage efficiency of the current blocks.
[0024] Step 4: Compare the storage efficiency of the current block with that of the previous block. If the storage efficiency has improved, return to step 3. If the storage efficiency remains unchanged or decreases, take the size of the previous block corresponding to the current block as the optimal size and continue to the next step.
[0025] Step 5: Divide the matrix to be stored into blocks of the optimal size, and select the optimal storage format for each block's corresponding matrix region.
[0026] Furthermore, the basic information of the matrix to be stored includes the row order and column order.
[0027] Furthermore, if the row order or column order of the matrix to be stored is not divisible by the number of rows or columns of the largest block, the matrix to be stored is expanded in step 1 so that the row order of the matrix to be stored is divisible by the number of rows of the largest block, and the column order of the matrix to be stored is divisible by the number of columns of the largest block. The elements corresponding to the expanded rows or columns of the matrix to be stored are 0.
[0028] Furthermore, in step 2, the storage efficiency corresponding to the current block is calculated using the formula... The calculation is as follows: EFF is the storage efficiency corresponding to the current block, a is the data size that distinguishes the nature of each block, m is the row order of the matrix to be stored, n is the column order of the matrix to be stored, stepx is the number of rows of the current block, stepy is the number of columns of the current block, c is the empirical coefficient, avg is a function used to return the average value of the sampled input parameters, and node_num is the number of elements in any sampled block.
[0029] Furthermore, the number of elements within the block is obtained through the following steps:
[0030] Initialize a two-dimensional array, wherein the number of rows of the two-dimensional array is the row order of the matrix to be stored divided by the number of rows of the current block, and the number of columns of the two-dimensional array is the column order of the matrix to be stored divided by the number of columns of the current block;
[0031] Iterate through the non-zero elements in the matrix to be stored, count the number of elements in each block, and store them in the corresponding positions of the two-dimensional array.
[0032] Furthermore, selecting the optimal storage format for each block's corresponding matrix region includes the following steps:
[0033] Create a two-dimensional index structure index[m / stepx1][n / stepy1] to represent the matrix region to be stored corresponding to the block of the optimal size, where stepx1 is the number of rows of the block of the optimal size, stepy1 is the number of columns of the block of the optimal size, m is the row order of the matrix to be stored, and n is the column order of the matrix to be stored.
[0034] Calculate the storage efficiency of the current matrix region to be stored when selecting various storage formats;
[0035] Choose the storage format with the highest storage efficiency as the optimal storage format for storing the target storage matrix area.
[0036] Furthermore, the storage format includes any one of the following: triplet storage format, row-compressed storage format, and array storage format.
[0037] Furthermore, when the storage format is a triplet storage format, the formula is used. Calculate the storage efficiency of the corresponding matrix region to be stored represented by the two-dimensional index structure;
[0038] When the storage format is row-compressed storage format, the formula is used. Calculate the storage efficiency of the corresponding matrix region to be stored represented by the two-dimensional index structure;
[0039] When the storage format is array storage format, use the formula. Calculate the storage efficiency of the corresponding matrix region to be stored, represented by the two-dimensional index structure.
[0040] Furthermore, increasing the size of the blocks in step 3 specifically involves enlarging the rows of the blocks by a factor of 2, or enlarging the columns of the blocks by a factor of 2.
[0041] The vector calculation method proposed in this invention adopts the sparse matrix storage method described in the above technical solution.
[0042] The present invention proposes a computer-readable storage medium for storing a computer program, wherein the computer program executes the sparse matrix storage method described above when it runs.
[0043] This invention divides the sparse matrix and uses a heuristic algorithm to adjust the size of the blocks. This achieves a balance between the memory occupied by the two-dimensional index structure representing the sparse matrix region corresponding to the block and the number of blocks with higher element density. This allows this technical solution to effectively compress and store sparse matrices with different sparsity levels.
[0044] For different blocks in the index structure, this invention selects the optimal storage scheme calculated to store non-zero elements. For sparse matrices with different distributions, the technical solution provided by this invention can adaptively adjust according to the distribution of elements to achieve effective compressed storage.
[0045] Regarding the choice of storage format, in a preferred embodiment of the present invention, triplet, row compression, and array formats are selected. The triplet format stores only basic information such as element values and coordinates when the number of elements to be stored is small. When the number of elements exceeds the number of rows, the row compression format can compress the memory size used by the row index. When the element density is very high and closes to filling a block, the array format can minimize the storage of coordinate information, achieving excellent compression efficiency.
[0046] Since a two-dimensional index structure is used to divide the matrix region, and the size of each block is a power of 2, the division operation for locating a specific block can be replaced by the corresponding right shift operation. Furthermore, since each block contains a finite number of elements, the time complexity for randomly locating an element in this scheme is O(1), making it easier to use and modify. Attached Figure Description
[0047] The present invention will now be described in detail with reference to the embodiments and accompanying drawings, wherein:
[0048] Figure 1 This is a flowchart of an embodiment of the present invention.
[0049] Figure 2This is a schematic diagram illustrating how the present invention expands the storage matrix based on the largest block size.
[0050] Figure 3 This is an example of a two-dimensional array of the present invention.
[0051] Figure 4 This is an example of triplet storage.
[0052] Figure 5 This is another example of triplet storage.
[0053] Figure 6 This is the third example of triplet storage.
[0054] Figure 7 This is a schematic diagram of the final compressed storage result of the sparse matrix. Detailed Implementation
[0055] To make the technical problems to be solved, the technical solutions, and the beneficial effects of the present invention clearer, the present invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.
[0056] Therefore, a feature pointed out in this specification is used to describe one feature of one embodiment of the invention, and does not imply that every embodiment of the invention must have the described feature. Furthermore, it should be noted that this specification describes many features. Although certain features may be combined to illustrate possible system designs, these features may also be used in other combinations not explicitly stated. Therefore, unless otherwise stated, the described combinations are not intended to be limiting.
[0057] like Figure 1 As shown, the sparse matrix storage method proposed in this invention divides the sparse matrix into blocks, adaptively adjusts the block size, and finally uses different storage methods according to the element density in each block, which can achieve good compression efficiency.
[0058] The main steps of the sparse matrix storage method of the present invention are described below.
[0059] Step 1: First, obtain the basic information of the matrix to be stored and initialize the size of the blocks.
[0060] The basic information of the matrix to be stored refers to the information necessary for partitioning, such as the row order and column order of the matrix to be stored. In other embodiments, it may further include the number of elements of the matrix to be stored.
[0061] In one embodiment, the initial block size is typically chosen as the minimum block size. That is, the number of blocks into which the matrix to be stored is divided starts from the maximum possible number. In other embodiments, if the basic information of the matrix to be stored includes other additional information, those skilled in the art can appropriately choose a size other than the minimum size to begin dividing the matrix.
[0062] Step 2: Divide the matrix to be stored into the current blocks and calculate the storage efficiency of the current blocks.
[0063] In step 1, the initial block size can be obtained. At the start of the first partitioning iteration, the matrix to be stored is divided starting with the initial block size. Then, the storage efficiency of the matrix to be stored is calculated after the matrix to be stored is divided into multiple corresponding matrix regions.
[0064] In one embodiment, the storage efficiency corresponding to the current block is calculated using the formula... The calculation involves a polynomial where the first polynomial represents the total memory required after the matrix to be stored is divided into multiple regions by the current block. This represents the memory used to identify the storage method used for each block. The second polynomial represents the average element density of the sampled blocks, which is then multiplied by an empirical coefficient for correction. In the formula, EFF represents the storage efficiency corresponding to the current block, and 'a' represents the amount of data used to distinguish the nature of each block. In a specific embodiment, 2 bits are used to distinguish the nature of each block, therefore 'a' is 2, which is 2 * the total number of blocks (i.e., through...). The total number of blocks can be calculated. This represents the memory required for the matrix regions corresponding to these blocks to be stored. m is the row order of the matrix to be stored, n is the column order of the matrix to be stored, stepx is the number of rows in the current block, stepy is the number of columns in the current block, c is an empirical coefficient, and avg is a function that returns the average of its sampled partial input parameters. For example, in one embodiment, the function avg returns the average of the sampled 0.1% blocks. Average value. node_num is the number of elements in any randomly selected block during sampling.
[0065] Step 3: Increase the size of the blocks by dividing the matrix to be stored into the current blocks and calculating the storage efficiency corresponding to the current blocks. In one embodiment, when performing Step 3, the number of rows in the block is increased by a factor of 2, or the number of columns in the block is increased by a factor of 2.
[0066] During the first iteration, when step 3 is executed, two storage efficiencies can be obtained: one is the storage efficiency corresponding to the block division in step 2, and the other is the storage efficiency obtained in step 3. Then step 4 is executed.
[0067] Step 4: Compare the storage efficiency of the current block with that of the previous block. If the storage efficiency has improved, return to step 3.
[0068] In the first iteration, the storage efficiency corresponding to the block size in step 2 is compared with the storage efficiency corresponding to the block size after increasing the size based on the block size in step 2. If the storage efficiency is improved, it is necessary to return to step 3 to continue increasing the block size and proceed to the next iteration.
[0069] In the nth iteration, the storage efficiency of the block obtained in step 3 of the previous iteration is compared with the storage efficiency of the block obtained in step 3 of this iteration. If the storage efficiency is improved, it is necessary to return to step 3 to continue increasing the size of the block.
[0070] If, in step 4, the storage efficiency is compared twice and found to be unchanged or decreased, the size of the previous block corresponding to the current block is taken as the optimal size, and the process continues to the next step. In other words, the size of the block obtained in step 3 of the previous iteration is used as the optimal size to determine the final block size. While the current block size can also be chosen as the optimal size when storage efficiency is consistent, it is preferable to choose the size of the block from the previous round as the optimal size, as both have the same efficiency and require one less conversion.
[0071] It should be noted that the number of elements in any block in the formula for calculating storage efficiency is obtained through the following steps: Initialize a two-dimensional array, where the number of rows in the array is the row order of the matrix to be stored divided by the number of rows in the current block, and the number of columns is the column order of the matrix to be stored divided by the number of columns in the current block; iterate through the non-zero elements in the matrix to be stored, count the number of elements in each block, and store the count in the corresponding position of the two-dimensional array. For example... Figure 3 As shown, the matrix to be stored is logically divided according to its row and column orders. Therefore, a two-dimensional array `num[m1 / stepy][]` can be used to store the number of elements `node_num` in the matrix region corresponding to each block. The non-zero elements in the matrix to be stored are traversed, and the number of elements in the matrix region corresponding to each block can be counted based on the coordinates of the non-zero elements. The count of non-zero elements is added to the corresponding block in the two-dimensional array `num[][]`. After the traversal is complete, the two-dimensional array `num[][]` stores the number of elements `node_num` in the matrix region corresponding to each block.
[0072] Step 5: Divide the matrix to be stored into blocks of optimal size, and select the optimal storage format for each block's corresponding matrix region. This ensures that the matrix to be stored is not limited to a single storage format, allowing each part of the matrix to be stored to obtain the best and most efficient storage method according to its characteristics. As a result, the compression efficiency of the entire matrix to be stored can be improved, avoiding the defects of various storage formats. Figure 7 The final compressed storage result of a sparse matrix is shown.
[0073] Based on the above technical solutions, there are cases where the matrix to be stored can be divided into integer blocks by the optimal size of the blocks, and there are also cases where the matrix to be stored cannot be divided into integer blocks by the optimal size of the blocks obtained by the above technical solutions.
[0074] Therefore, in one embodiment, assuming the block size increases by a factor of 2 or 2 in each iteration, if the row or column order of the matrix is not divisible by the number of rows or columns of the largest block, the matrix to be stored is expanded in step 1. The elements corresponding to the expanded rows or columns of the matrix to be stored are 0, ensuring that the row order of the matrix to be stored is divisible by the number of rows and columns of the largest block. In other embodiments, besides increasing the block size by powers of 2, other expansion methods can be used, such as increasing row by row or column by column. Different expansion methods result in different final expansion results for the matrix to be stored. Since division is a relatively time-consuming operation in computer languages, compared to other block expansion methods, this invention ensures that the matrix size and block size are powers of 2, which can be achieved using bitwise operations. Bitwise operations are fast, thus reducing the time consumption of the algorithm.
[0075] Step 5 above, selecting the best storage format for each block's corresponding matrix region, specifically includes the following steps.
[0076] Create a two-dimensional index structure `index[m / stepx1][n / stepy1]` to represent the matrix region to be stored corresponding to the optimally sized blocks. `stepx1` is the number of rows in the optimally sized block, `stepy1` is the number of columns in the optimally sized block, `m` is the row order of the matrix to be stored, and `n` is the column order of the matrix to be stored. Here, `m / stepy1` indicates the number of blocks in a column, and `n / stepx1` indicates the number of blocks in a row.
[0077] The storage efficiency of the current matrix region to be stored is calculated for each of the various storage formats selected. In one embodiment, the selectable storage formats include any one of triplet storage format, row compression storage format, and array storage format. In other embodiments, if the best storage performance is not considered, or if the next best storage performance is considered, or if other considerations are taken into account, other storage formats described in the background art can be selected for the storage matrix region corresponding to each partition.
[0078] When the storage format is a triplet storage format, the formula is used. Calculate the storage efficiency of the corresponding matrix region to be stored, represented by the two-dimensional index structure.
[0079] When the storage format is row-compressed storage format, the formula is used. Calculate the storage efficiency of the corresponding matrix region to be stored, represented by the two-dimensional index structure.
[0080] When the storage format is array storage format, use the formula. Calculate the storage efficiency of the corresponding matrix region to be stored, represented by the two-dimensional index structure.
[0081] In the above formula, num_type is the memory size occupied by the data format of the element value, and index_type is the memory size occupied by the data format of the coordinate, such as int / float, etc. Different storage precisions will occupy different amounts of memory.
[0082] Choose the storage format with the highest storage efficiency as the optimal storage format for storing the target storage matrix area.
[0083] This invention also proposes a vector computation method that employs the sparse matrix storage method described above. The benchmark procedure for sparse matrix computation is spmv, which multiplies a matrix by a dense vector; essentially, it tests the efficiency of sequentially reading elements of a sparse matrix. While this sparse matrix storage method focuses on compression efficiency, the time complexity for reading any element is O(1).
[0084] The present invention also proposes a computer-readable storage medium for storing a computer program that executes the sparse matrix storage method described above when the computer program is run.
[0085] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention should be included within the protection scope of the present invention.
Claims
1. A method for storing sparse matrices, characterized in that, include: Step 1: Obtain the basic information of the matrix to be stored and initialize the size of the blocks; Step 2: Divide the matrix to be stored into blocks according to the current blocks, and calculate the storage efficiency corresponding to the current blocks; Step 3: Increase the size of the blocks, divide the matrix to be stored into the current blocks, and calculate the storage efficiency of the current blocks. Step 4: Compare the storage efficiency of the current block with the storage efficiency of the previous block. If the storage efficiency has improved, return to step 3. If storage efficiency remains unchanged or decreases, the size of the previous block corresponding to the current block is taken as the optimal size, and the next step is continued. Step 5: Divide the matrix to be stored into blocks of the optimal size, and select the optimal storage format for each block's corresponding matrix region. In step 2, the storage efficiency corresponding to the current block is calculated using the formula. The calculation is as follows: EFF is the storage efficiency corresponding to the current block, a is the data size that distinguishes the nature of each block, m is the row order of the matrix to be stored, n is the column order of the matrix to be stored, stepx is the number of rows of the current block, stepy is the number of columns of the current block, c is the empirical coefficient, avg is a function used to return the average value of the sampled input parameters, and node_num is the number of elements in any sampled block.
2. The sparse matrix storage method as described in claim 1, characterized in that, The basic information of the matrix to be stored includes the row order and column order.
3. The sparse matrix storage method as described in claim 2, characterized in that, If the row order or column order of the matrix to be stored is not divisible by the number of rows or columns of the largest block, the matrix to be stored is expanded in step 1 so that the row order of the matrix to be stored is divisible by the number of rows of the largest block, and the column order of the matrix to be stored is divisible by the number of columns of the largest block. The elements corresponding to the expanded rows or columns of the matrix to be stored are 0.
4. The sparse matrix storage method as described in claim 1, characterized in that, The number of elements within the block is obtained through the following steps: Initialize a two-dimensional array, wherein the number of rows of the two-dimensional array is the row order of the matrix to be stored divided by the number of rows of the current block, and the number of columns of the two-dimensional array is the column order of the matrix to be stored divided by the number of columns of the current block; Iterate through the non-zero elements in the matrix to be stored, count the number of elements in each block, and store them in the corresponding positions of the two-dimensional array.
5. The sparse matrix storage method as described in claim 4, characterized in that, Selecting the optimal storage format for each block's corresponding matrix region involves the following steps: Create a two-dimensional index structure index[m / stepx1][n / stepy1] to represent the matrix region to be stored corresponding to the block of the optimal size, where stepx1 is the number of rows of the block of the optimal size, stepy1 is the number of columns of the block of the optimal size, m is the row order of the matrix to be stored, and n is the column order of the matrix to be stored. Calculate the storage efficiency of the current matrix region to be stored when selecting various storage formats; Choose the storage format with the highest storage efficiency as the optimal storage format for storing the target storage matrix area.
6. The sparse matrix storage method as described in claim 5, characterized in that, The storage format includes any one of the following: triplet storage format, row-compressed storage format, and array storage format.
7. The sparse matrix storage method as described in claim 6, characterized in that, When the storage format is a triplet storage format, the formula is used. Calculate the storage efficiency of the corresponding matrix region to be stored represented by the two-dimensional index structure; When the storage format is row-compressed storage format, the formula is used. Calculate the storage efficiency of the corresponding matrix region to be stored represented by the two-dimensional index structure; When the storage format is array storage format, use the formula. Calculate the storage efficiency of the corresponding matrix region to be stored, represented by the two-dimensional index structure; index_type is the memory size occupied by the coordinate data format, and num_type is the memory size occupied by the element value data format.
8. The method for storing sparse matrices as described in claim 1, characterized in that, In step 3, increasing the size of the block specifically means: enlarging the row of the block by a factor of 2, or enlarging the column of the block by a factor of 2.
9. A vector calculation method, characterized in that, The storage method for sparse matrices as described in any one of claims 1 to 8 is adopted.
10. A computer-readable storage medium for storing a computer program, characterized in that, When the computer program runs, it executes the sparse matrix storage method as described in any one of claims 1 to 8.