Vector ordering apparatus, method and storage medium
By dividing the vector into segments and executing differentiated instructions based on the order of the segments, sorted segments are selected for fewer comparisons and merging, thus solving the problem of low efficiency in vector sorting in the prior art and achieving a more efficient sorting process.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- SHENZHEN INTELLIFUSION TECHNOLOGIES CO LTD
- Filing Date
- 2022-12-30
- Publication Date
- 2026-05-15
AI Technical Summary
Existing technologies fail to effectively handle differences in array length and randomness or regularity during vector sorting, resulting in redundant comparisons and swaps and low efficiency.
By dividing the vector into segments and executing differentiated instructions based on the sorting of the segments, the selection unit selects sorted segments for fewer comparisons and mergings. A combination of selection units, comparison units, and merging units is used to achieve fast sorting.
This improves the efficiency of vector sorting, avoids repeated sorting of regular segments, and enhances the efficiency of the sorting process.
Smart Images

Figure CN116225364B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of sorting algorithms, and more specifically to a vector sorting device, method, and storage medium. Background Technology
[0002] In vector sorting, the length of the arrays within the vector to be sorted, as well as the randomness or regularity of the arrays, may vary. Ignoring the internal elements of a vector during sorting, especially when the vector contains arrays that may already be partially sorted or follow a pattern, and applying a uniform vector sorting method to vectors with different internal conditions, often leads to redundancy in the comparison and exchange processes, reducing the efficiency of vector sorting. Summary of the Invention
[0003] To address the technical problem of low efficiency in existing vector sorting processes, this invention proposes a vector sorting device, method, and storage medium.
[0004] A first aspect of the present invention provides a vector sorting apparatus, comprising:
[0005] Select cells, compare and swap cells, merge cells;
[0006] The selection unit includes a vector segment selection unit and an element selection unit;
[0007] The comparison and exchange unit includes a comparison unit and an exchange unit;
[0008] The merging unit includes an element merging unit and a vector fragment merging unit;
[0009] The selection unit performs selection based on the sorting of vector segments, such that elements in sorted vector segments require fewer comparisons than elements in unsorted vector segments.
[0010] In some embodiments, the vector sorting device includes a first sorting instruction, a second sorting instruction, and a third sorting instruction;
[0011] The first sorting instruction is used to establish the sorting between the data vector and the index vector;
[0012] The second sorting instruction is used to establish the sorting between data vectors;
[0013] The third sorting instruction is used to establish the sorting between index vectors.
[0014] In some embodiments, the vector sorting device includes a first operation instruction, a second operation instruction, and a third operation instruction;
[0015] The first operation instruction is used to sequentially establish the sorting between two adjacent elements;
[0016] The second operation instruction is used to sequentially establish the sorting between two adjacent elements after excluding the first and last elements;
[0017] The third operation instruction is used to select one element in the first half and the second half of the vector respectively, and to establish the sorting of the two elements selected in the first half and the second half of the vector.
[0018] In some embodiments, the vector segment selection unit is configured to perform vector segment selection within the first sorting instruction, and / or the second sorting instruction, and / or the third sorting instruction; the element selection unit is configured to perform vector segment selection between the first operation instruction, and / or the second operation instruction, and / or the third operation instruction based on the vector segments selected by the vector segment selection unit.
[0019] In some embodiments, the element merging unit is used to place elements with swapped order within a fragment into sorted positions; the vector fragment merging unit is used to merge vector fragments.
[0020] In some embodiments, the first sorting instruction is:
[0021] The data vector is sorted using the first operation instruction, the second operation instruction, or the third operation instruction to obtain a sorted data vector.
[0022] The index vector is sorted based on the sorted data vector to obtain a sorted index vector.
[0023] In some embodiments, the second sorting instruction is:
[0024] Select the first segment of the first data vector and the second segment of the second data vector;
[0025] The first segment of the merged first data vector and the second segment of the second data vector are sorted using the first operation instruction, the second operation instruction, or the third operation instruction to obtain a sorted data vector.
[0026] Merge the first segment of the sorted data vector with the second segment of the first data vector, and merge the first segment of the second data vector with the second segment of the sorted data vector.
[0027] The first segment of the first data vector contains the same number of elements as the second segment of the first data vector; the first segment of the second data vector contains the same number of elements as the second segment of the second data vector.
[0028] In some embodiments, the third sorting instruction is:
[0029] Select the first segment of the first index vector and the second segment of the second index vector;
[0030] Based on the sorting information, the first segment of the merged first index vector and the second segment of the second index vector are sorted to obtain a sorted index vector;
[0031] Merge the first segment of the sorted index vector with the second segment of the first index vector, and merge the first segment of the second index vector with the second segment of the sorted index vector;
[0032] The first segment of the first index vector contains the same number of elements as the second segment of the first index vector; the first segment of the second index vector contains the same number of elements as the second segment of the second index vector.
[0033] A second aspect of this invention provides a vector sorting method, comprising:
[0034] Select at least one first segment of a vector and one second segment of a vector;
[0035] Select at least one element from the first segment of the vector and an element from the second segment of the vector;
[0036] Perform comparison and sorting on the elements of the first segment of at least one vector and the elements of the second segment of the vector;
[0037] Merge the first segment of the sorted vector and the second segment of the vector;
[0038] The selection of at least one first segment and one second segment of a vector is such that the elements of the sorted vector segment are not compared or swapped within the vector segment by the comparison and swap unit;
[0039] The selection of at least one first segment of a vector and a second segment of a vector is based on a first sorting instruction, and / or a second sorting instruction, and / or a third sorting instruction;
[0040] The first sorting instruction is used to establish the sorting between the data vector and the index vector;
[0041] The second sorting instruction is used to establish the sorting between data vectors;
[0042] The third sorting instruction is used to establish the sorting between index vectors;
[0043] The selection of at least one element of the first segment of a vector and the element of the second segment of a vector is based on a first operation instruction, a second operation instruction, and a third operation instruction;
[0044] The first operation instruction is used to sequentially establish the sorting between two adjacent elements;
[0045] The second operation instruction is used to sequentially establish the sorting between two adjacent elements after excluding the first and last elements;
[0046] The third operation instruction is used to select one element in the first half and the second half of the vector respectively, and to establish the sorting of the two elements selected in the first half and the second half of the vector.
[0047] A third aspect of the present invention provides a computer-readable storage medium having the above-described vector sorting apparatus stored thereon.
[0048] This invention divides vectors into segments and executes differentiated instructions based on the characteristics within each segment, enabling rapid and efficient sorting of vectors of arbitrary length with varying internal randomness and regularity. This avoids repeatedly sorting regular segments within a vector, thus improving vector sorting efficiency. Attached Figure Description
[0049] One or more embodiments are illustrated by way of example with reference numerals in the accompanying drawings. These illustrations do not constitute a limitation on the embodiments. Elements with the same reference numerals in the drawings are denoted as similar elements. Unless otherwise stated, the figures in the drawings are not to be limited by scale.
[0050] Figure 1 A block diagram of a vector sorting device provided in the first embodiment of the present invention;
[0051] Figure 2 A flowchart illustrating the execution of a first sorting instruction in a vector sorting apparatus provided in a first embodiment of the present invention;
[0052] Figure 3 This is a flowchart illustrating the execution of a second sorting instruction in a vector sorting device provided in the first embodiment of the present invention.
[0053] Figure 4 This is a flowchart illustrating the execution of the third sorting instruction in the vector sorting apparatus provided in the first embodiment of the present invention.
[0054] Figure 5The operation flowchart of the first operation instruction op0, the second operation instruction op1, and the third operation instruction op2 provided in the first embodiment of the present invention
[0055] Figure 6 A flowchart of the vector sorting method provided in the second embodiment of the present invention;
[0056] Figure 7 A flowchart of the operation of a first operation instruction op0, a second operation instruction op1, and a third operation instruction op2 for a vector sorting method provided in the second embodiment of the present invention;
[0057] Figure 8 The flowchart illustrates the operation of a first operation instruction op0, a second operation instruction op1, and a third operation instruction op2 for a vector sorting method provided in the second embodiment of the present invention. Detailed Implementation
[0058] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the various embodiments of the present invention will be described in detail below with reference to the accompanying drawings. However, those skilled in the art will understand that many technical details are presented in the various embodiments of the present invention to facilitate a better understanding of this application. However, the technical solutions claimed in this application can be implemented even without these technical details and various changes and modifications based on the following embodiments. The division of the various embodiments below is for ease of description and should not constitute any limitation on the specific implementation of the present invention. The various embodiments can be combined with and referenced by each other without contradiction.
[0059] The first embodiment of the present invention provides a vector sorting device, see below. Figure 1 ,include
[0060] Select cells, compare and swap cells, merge cells;
[0061] The selection unit includes a vector segment selection unit and an element selection unit;
[0062] The comparison and exchange unit includes a comparison unit and an exchange unit;
[0063] The merging unit includes an element merging unit and a vector fragment merging unit.
[0064] The selection unit selects the vector segment according to the sorting status of the vector segment, so that the elements in the sorted vector segment require fewer comparisons than the elements in the unsorted vector segment.
[0065] In some embodiments, the vector fragment selection unit is used to perform fragment selection in a sorting instruction on the input vector. The sorting instruction is a first sorting instruction, a second sorting instruction, or a third sorting instruction that matches the element type of the input vector. The first sorting instruction is used to establish a sort between data vectors and index vectors; the second sorting instruction is used to establish a sort between data vectors; and the third sorting instruction is used to establish a sort between index vectors.
[0066] See Figure 2 In some embodiments, the execution of the first sorting instruction includes the following steps:
[0067] The input vectors are data vector vds0 and index vector vds1. The data vector vds0 is sorted by the first operation instruction op0, the second operation instruction op1, or the third operation instruction op2 to obtain the sorted data vector change info.
[0068] Based on the sorted data vector change info, the index vector vds1 is sorted to obtain the sorted index vector;
[0069] Store the sorted data vector change info back as the output data vector vds0, and store the sorted index vector back to obtain the output index vector vds1.
[0070] See Figure 3 In some embodiments, the execution of the second sorting instruction includes the following steps:
[0071] The input vectors are the first data vector vds1 and the second data vector vds2. Select the first segment vds1.hi of the first data vector vds1 and the second segment vds1.lo of the second data vector vds0.
[0072] Merge the first segment vds1.hi of the first data vector vds1 with the second segment vds1.lo of the second data vector vds0;
[0073] The first segment vds1.hi of the merged first data vector and the second segment vds0.lo of the second data vector are sorted using the first operation instruction op0, the second operation instruction op1, or the third operation instruction op2 to obtain the sorted data vector change info.
[0074] At the same time, the first segment vds1.hi.rank of the sorted data vector is merged with the second segment vds1.lo of the first data vector, and the first segment vds0.hi of the second data vector is merged with the second segment vds0.lo.rank of the sorted data vector.
[0075] The number of elements contained in the first segment vds1.hi of the first data vector is equal to the number of elements contained in the second segment vds1.lo of the first data vector, and the number of elements contained in the first segment vds0.hi of the second data vector is equal to the number of elements contained in the second segment vds0.lo of the second data vector.
[0076] See Figure 4 In some embodiments, the execution of the third sorting instruction includes the following steps:
[0077] The input vectors are the first index vector vds1 and the second index vector vds2. Select the first segment vds1.hi of the first index vector vds1 and the second segment vds1.lo of the second index vector vds0.
[0078] Merge the first segment vds1.hi of the first index vector vds1 with the second segment vds1.lo of the second index vector vds0;
[0079] Based on the sorting information of the sorting data vector change info saved in the prior instruction, sort the first segment vds1.hi of the merged first index vector vds1 and the second segment vds1.lo of the second index vector vds0;
[0080] Merge the first segment of the sorted index vector vds1.hi.rank with the second segment of the first index vector vds1.lo, and merge the first segment of the second index vector vds0.hi with the second segment of the sorted index vector vds0.lo.rank.
[0081] The number of elements contained in the first segment vds1.hi of the first index vector is equal to the number of elements contained in the second segment vds1.lo of the first index vector, and the number of elements contained in the first segment vds0.hi of the second index vector is equal to the number of elements contained in the second segment vds0.lo of the second index vector.
[0082] In some embodiments, the element selection unit is used to select vector segments between the first operation instruction op0 and / or the second operation instruction op1 and / or the third operation instruction op2 based on the vector segment selected by the vector segment selection unit.
[0083] See Figure 5The first operation instruction op0 is used to sequentially establish the sorting between two adjacent elements; the second operation instruction op1 is used to sequentially establish the sorting between two adjacent elements after excluding the first and last elements; the third operation instruction op2 is used to sequentially select one element in the first half and the second half of the vector, respectively, and establish the sorting of the two elements selected in the first half and the second half of the vector.
[0084] In some embodiments, the comparison and exchange unit includes a comparison unit and an exchange unit. The comparison unit compares the size between two elements, and the exchange unit, according to ascending or descending sorting instructions and based on the size between the two elements output by the comparison unit, exchanges or maintains the order of the two compared elements.
[0085] In some embodiments, the element merging unit is used to place the sorted elements back into segments of a vector. The segment merging unit is used to merge segments into a vector.
[0086] In some embodiments, the vector sorting device further includes a register R, which is used to store data that needs to be temporarily placed during the execution of the vector sorting device.
[0087] A second embodiment of the present invention provides a vector sorting method, see below. Figure 6 It includes the following steps:
[0088] The segment selection in the sorting instructions is executed on input vector 1 and input vector 2. The sorting instructions are the instructions that match the element type of the input vectors from the first, second, and third sorting instructions. The first sorting instruction is used to establish the sorting between data vectors and index vectors; the second sorting instruction is used to establish the sorting between data vectors; and the third sorting instruction is used to establish the sorting between index vectors.
[0089] In some embodiments, the execution of the first sorting instruction includes the following steps:
[0090] The input vectors are data vector vds0 and index vector vds1. The data vector vds0 is sorted by the first operation instruction op0, the second operation instruction op1, or the third operation instruction op2 to obtain the sorted data vector change info.
[0091] Based on the sorted data vector change info, the index vector vds1 is sorted to obtain the sorted index vector;
[0092] Store the sorted data vector change info back as the output data vector vds0, and store the sorted index vector back to obtain the output index vector vds1.
[0093] In some embodiments, the execution of the second sorting instruction includes the following steps:
[0094] The input vectors are the first data vector vds1 and the second data vector vds2. Select the first segment vds1.hi of the first data vector vds1 and the second segment vds1.lo of the second data vector vds0.
[0095] Merge the first segment vds1.hi of the first data vector vds1 with the second segment vds1.lo of the second data vector vds0;
[0096] The first segment vds1.hi of the merged first data vector and the second segment vds0.lo of the second data vector are sorted using the first operation instruction op0, the second operation instruction op1, or the third operation instruction op2 to obtain the sorted data vector change info.
[0097] At the same time, the first segment vds1.hi.rank of the sorted data vector is merged with the second segment vds1.lo of the first data vector, and the first segment vds0.hi of the second data vector is merged with the second segment vds0.lo.rank of the sorted data vector.
[0098] The number of elements contained in the first segment vds1.hi of the first data vector is equal to the number of elements contained in the second segment vds1.lo of the first data vector, and the number of elements contained in the first segment vds0.hi of the second data vector is equal to the number of elements contained in the second segment vds0.lo of the second data vector.
[0099] In some embodiments, the execution of the third sorting instruction includes the following steps:
[0100] The input vectors are the first index vector vds1 and the second index vector vds2. Select the first segment vds1.hi of the first index vector vds1 and the second segment vds1.lo of the second index vector vds0.
[0101] Merge the first segment vds1.hi of the first index vector vds1 with the second segment vds1.lo of the second index vector vds0;
[0102] Based on the sorting information of the sorting data vector change info saved in the prior instruction, sort the first segment vds1.hi of the merged first index vector vds1 and the second segment vds1.lo of the second index vector vds0;
[0103] Merge the first segment of the sorted index vector vds1.hi.rank with the second segment of the first index vector vds1.lo, and merge the first segment of the second index vector vds0.hi with the second segment of the sorted index vector vds0.lo.rank.
[0104] The number of elements contained in the first segment vds1.hi of the first index vector is equal to the number of elements contained in the second segment vds1.lo of the first index vector, and the number of elements contained in the first segment vds0.hi of the second index vector is equal to the number of elements contained in the second segment vds0.lo of the second index vector.
[0105] Element selection is performed on the split and selected vector segments. In some embodiments, the element selection unit is used to select vector segments between the first operation instruction op0 and / or the second operation instruction op1 and / or the third operation instruction op2 based on the split and selected vector segments by the vector segment selection unit.
[0106] The first operation instruction op0 is used to sequentially establish the sorting between two adjacent elements; the second operation instruction op1 is used to sequentially establish the sorting between two adjacent elements after excluding the first and last elements; the third operation instruction op2 is used to sequentially select one element in the first half and the second half of the vector, respectively, and establish the sorting of the two elements selected in the first half and the second half of the vector.
[0107] In some embodiments, to keep unselected data in sync with selected data and avoid timing errors, a register is also set up during the element selection process to place the selected element.
[0108] Compare the size between the two selected elements, and swap or maintain the order of the two compared elements according to the ascending or descending sorting instructions.
[0109] The sorted elements are placed back into the segments of the vector, and the segments are merged into output vector 1 and output vector 2.
[0110] In one embodiment, see Figure 7 The input vectors vds0 and vds1 are both completely unsorted and irregular data vectors. After element selection, N completely unsorted and irregular data are obtained. After looping through the first operation instruction op0 N / 2 times and the second operation instruction op1 N / 2 times, the N completely unsorted and irregular data are completely sorted and stored back into the vector as the output vector.
[0111] In one embodiment, see Figure 8The input vectors vds0 and vds1 are the first half of the data vectors, which are already sorted and represent regular data vectors. The second half is the remaining unsorted data vector. During the sorting process, the first and second halves are merged and then sorted. The sorting process sequentially goes through one third operation instruction op2, one second operation instruction op1, and repeats the first operation instruction op0 and the second operation instruction op1 (N / 4-1) times, completing the complete sorting of N data vectors and storing them back into the output vector.
[0112] For vectors with pre-existing patterns that have already been sorted, this embodiment, based on the regularity of the input vector, performs reasonable slicing and integration of the input vector and adjusts the operation instructions, so that sorting can be completed with a small number of operation instructions, avoiding repeated sorting of regular segments within the vector and improving vector sorting efficiency.
[0113] A third embodiment of the present invention provides a computer-readable storage medium having the above-described vector sorting device stored thereon.
[0114] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The program can be stored in a computer-readable storage medium, and when the program is executed, it can include the processes of the embodiments of the methods described above.
[0115] Those skilled in the art will understand that the above embodiments are specific examples of implementing the present invention, and in practical applications, various changes in form and detail may be made without departing from the spirit and scope of the present invention.
Claims
1. A vector sorting device, characterized in that, include: Select unit, compare and swap unit, merge unit, register; The register is used to store data temporarily placed during the execution of the vector sorting device; The selection unit includes a vector segment selection unit and an element selection unit; The comparison and exchange unit includes a comparison unit and an exchange unit; The merging unit includes an element merging unit and a vector fragment merging unit; The selection unit performs selection based on the sorting of vector segments, such that elements in the sorted vector segments require fewer comparisons than elements in the unsorted vector segments. The vector sorting device includes a first sorting instruction, a second sorting instruction, and a third sorting instruction; The first sorting instruction is used to establish the sorting between the data vector and the index vector; The second sorting instruction is used to establish the sorting between data vectors; The third sorting instruction is used to establish the sorting between index vectors; The vector sorting device includes a first operation instruction, a second operation instruction, and a third operation instruction; The first operation instruction is used to sequentially establish the sorting between two adjacent elements; The second operation instruction is used to sequentially establish the sorting between two adjacent elements after excluding the first and last elements; The third operation instruction is used to select one element in the first half and the second half of the vector respectively, and to establish the sorting of the two elements selected in the first half and the second half of the vector. The vector segment selection unit is configured to perform vector segment selection within the first sorting instruction, and / or the second sorting instruction, and / or the third sorting instruction; the element selection unit is configured to perform vector segment selection between the first operation instruction, and / or the second operation instruction, and / or the third operation instruction based on the vector segments selected by the vector segment selection unit.
2. The vector sorting device according to claim 1, characterized in that, The element merging unit is used to place elements with swapped order within a fragment into sorted positions; the vector fragment merging unit is used to merge vector fragments.
3. The vector sorting device according to claim 1, characterized in that, The first sorting instruction is: The data vector is sorted using the first operation instruction, the second operation instruction, or the third operation instruction to obtain a sorted data vector. The index vector is sorted based on the sorted data vector to obtain a sorted index vector.
4. The vector sorting device according to claim 1, characterized in that, The second sorting instruction is: Select the first segment of the first data vector and the second segment of the second data vector; The first segment of the merged first data vector and the second segment of the second data vector are sorted using the first operation instruction, the second operation instruction, or the third operation instruction to obtain a sorted data vector. Merge the first segment of the sorted data vector with the second segment of the first data vector, and merge the first segment of the second data vector with the second segment of the sorted data vector. The first segment of the first data vector contains the same number of elements as the second segment of the first data vector; the first segment of the second data vector contains the same number of elements as the second segment of the second data vector.
5. The vector sorting device according to claim 1, characterized in that, The third sorting instruction is: Select the first segment of the first index vector and the second segment of the second index vector; Based on the sorting information, the first segment of the merged first index vector and the second segment of the second index vector are sorted to obtain a sorted index vector; Merge the first segment of the sorted index vector with the second segment of the first index vector, and merge the first segment of the second index vector with the second segment of the sorted index vector; The first segment of the first index vector contains the same number of elements as the second segment of the first index vector; the first segment of the second index vector contains the same number of elements as the second segment of the second index vector.
6. A vector sorting method, characterized in that, include: Select at least one first segment of a vector and one second segment of a vector; Select at least one element of the first segment of a vector and an element of the second segment of a vector. During the element selection process, a register is also set to place the selected element. Perform comparison and sorting on the elements of the first segment of at least one vector and the elements of the second segment of the vector; Merge the first segment of the sorted vector and the second segment of the vector; The selection of at least one first segment and one second segment of a vector is such that the element-sorted vector segment is not compared or swapped within the vector segment by the comparison and swap unit; The selection of at least one first segment of a vector and a second segment of a vector is based on a first sorting instruction, and / or a second sorting instruction, and / or a third sorting instruction; The first sorting instruction is used to establish the sorting between the data vector and the index vector; The second sorting instruction is used to establish the sorting between data vectors; The third sorting instruction is used to establish the sorting between index vectors; The selection of at least one element of the first segment of a vector and the element of the second segment of a vector is based on a first operation instruction, a second operation instruction, and a third operation instruction; The first operation instruction is used to sequentially establish the sorting between two adjacent elements; The second operation instruction is used to sequentially establish the sorting between two adjacent elements after excluding the first and last elements; The third operation instruction is used to select one element in the first half and the second half of the vector respectively, and to establish the sorting of the two elements selected in the first half and the second half of the vector.
7. A computer-readable storage medium, characterized in that, It stores a program that implements the vector sorting method as described in claim 6.