Data sorting method and device and electronic equipment
By adopting the data sorting method of multi-stage comparator array on integrated circuits, the calculation dependency chain length and complexity problems in hardware implementation are solved, and fast parallel sorting is achieved, suitable for large throughput scenarios and has easy to implement and highly scalable Features.
Patent Information
- Application Number
- CN202411078117.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-07
- Publication Date
- 2024-12-17
AI Technical Summary
In the hardware implementation, the prior art has problems such as long computing dependence chain, not suitable for large throughput parallel computing, and/or there are problems such as complex hardware implementation and many computing steps, resulting in insufficiency in sorting.
Using the integrated circuit-based data sorting method, the input data is divided into multiple data groups through a multi-level comparator array, and the rapid sorting is achieved through multiple rounds of comparison. It is adapted to the commonly used streaming structure of hardware, with low hardware consumption and small data dependence. chain.
It realizes fast and parallel sorting result output, adapts to large throughput computing scenarios, has the advantages of being easy to implement and strong scalability, and can process integer powers of 2 and any number of data.
Smart Images

Figure CN119148971A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure belongs to the field of data processing technology, and relates to a data sorting method based on integrated circuits, and in particular to a data sorting method and device, and an electronic device. Background Art
[0002] Sorting is one of the common operations in data processing, which is used to arrange a set of data in a specific order. Sorting methods are widely used in various fields, including digital signal processing, high-speed processors, database systems, etc. Efficient sorting algorithms play a vital role in improving system performance and optimizing data processing. Summary of the invention
[0003] The purpose of the present disclosure is to provide a data sorting method and device, and an electronic device, which are used to implement fast sorting based on integrated circuits, and are used to implement data sorting with a simple structure, low hardware complexity, few steps, and fast processing speed.
[0004] In a first aspect, an embodiment of the present disclosure provides a data sorting method, the data sorting method comprising: obtaining a plurality of input data, the number of the plurality of input data being a power of 2, wherein N is an integer greater than 1; dividing the plurality of input data evenly into 2 N-1 data groups and sort the data in each data group to obtain an intermediate sorting result; and divide the intermediate sorting result obtained in the previous sorting into M / 2 data groups and sort the data in each data group to obtain a new intermediate sorting result, and repeat this step N-1 times to obtain the final sorting result, where M is the number of data groups in the previous sorting.
[0005] In some implementations, obtaining multiple input data includes: obtaining multiple original input data, the number of the multiple original input data is not an integer power of 2; and using extended data to expand the multiple original input data to obtain expanded data whose number is an integer power of 2, and using the expanded data as the multiple input data, wherein the data sorting method also includes: deleting the extended data in the final sorting result.
[0006] In some implementations, the expanded data is a maximum value or a minimum value that can be represented by the plurality of original input data.
[0007] In some implementations, the plurality of input data are evenly divided into 2 N-1 The method of sorting the data in each data group to obtain an intermediate sorting result includes: treating each two data in the plurality of input data as a data group; inputting the two data in each data group into 2 N-1 corresponding comparators in the comparators; and through 2N-1 The comparator output has 2 N-1 The intermediate sorting result of each data group includes two data arranged in order of size.
[0008] In some implementations, dividing the intermediate sorting result obtained by the previous sorting into M / 2 data groups and sorting the data in each data group to obtain a new intermediate sorting result includes: taking each two data groups in the intermediate sorting result of each data group as a next-level data group; inputting the two data groups in each next-level data group into 2 N-2 corresponding comparators in the comparators; and through 2 N-2 The comparator output has 2 N-2 The new intermediate sorting result of each data group includes four data arranged in order of size, wherein the ... N-2 The comparator output has 2 N-2 The new intermediate sorting results of the data groups include: N-2 Each of the comparators compares the first sequence data in one data group with the first sequence data in another data group, and compares the second sequence data in the one data group with the second sequence data in the other data group, so as to output sorting results each including four data arranged in order of size, and the sorting results serve as the output data group corresponding to one comparator.
[0009] In some implementations, repeating this step N-1 times to obtain the final sorting result includes: taking each two data groups in the intermediate sorting result obtained by the previous sorting as a next-level data group; inputting the two data groups in each next-level data group into corresponding comparators in M / 2 comparators respectively; outputting the new intermediate sorting result having M / 2 data groups through the M / 2 comparators, and the new intermediate sorting result of each data group includes 2 data groups arranged in order of size. N Divide by M / 2 data; and repeat the above steps to include 2 arranged in order of size N The method further comprises: using a plurality of comparison units of each of the M / 2 comparators to compare the data in one data group arranged in order of size with the data in another data group arranged in order of size. N Divide by M rounds of comparison to output 2 N The sorting result is divided by M / 2 data, and the sorting result is used as an output data group corresponding to a comparator.
[0010] In some implementations, the plurality of input data are evenly divided into 2 N-1 The method of sorting the data in each data group to obtain an intermediate sorting result includes sorting two data in each data group in ascending order logic or descending order logic to obtain the intermediate sorting result.
[0011] In some implementations, dividing the intermediate sorting results obtained from the previous sorting into M / 2 data groups includes: merging the intermediate sorting results corresponding to every two data groups in the previous sorting into one data group to obtain the M / 2 data groups.
[0012] In some implementations, dividing the intermediate sorting result obtained from the previous sorting into M / 2 data groups and sorting the data in each data group to obtain a new intermediate sorting result includes: combining the first data group and the second data group in the previous sorting to obtain a third data group; and sorting the data in the third data group to obtain the sorting result of the third data group, including: performing K rounds of comparison based on the first data group and the second data group, and obtaining the sorting result of the third data group based on the maximum data and the minimum data obtained from each round of comparison, where K is the number of data contained in the first data group.
[0013] In some implementations, performing K rounds of comparison based on the first data group and the second data group includes: in the first round of comparison, comparing the data with the same sorting in the first data group and the second data group and reordering them according to the comparison result to obtain the first round of sorting sub-results; and in the kth round of comparison, comparing adjacent data other than the maximum data and the minimum data in the k-1th round of sorting sub-results, and reordering them according to the comparison result to obtain the kth round of sorting sub-results, where k is an integer greater than 1 and less than or equal to K.
[0014] In some implementations, the data sorting method uses ascending logic for sorting, and performs K rounds of comparison based on the first data group and the second data group, including: in the first round of comparison, using K comparison units CMP1_0 to CMP1_(K-1) in a comparator to compare the data with the same sorting in the first data group and the second data group, wherein CMP1_m1 is used to compare the data sorted as m1 in the first data group and the second data group, the smaller value output by CMP1_0 is the minimum data in the first round of comparison, the larger value output by CMP1_(K-1) is the maximum data in the first round of comparison, and m1 is greater than or equal to 0 and less than K; and in the k-th round of comparison, the K-k+1 comparison units CMPk_0 to CMPk_(Kk) in the comparator are used to compare the data other than the maximum data and the minimum data in the k-1th round of comparison results, wherein CMPk_m2 is used to compare the larger value output by CMP(k-1)_m2 and the smaller value output by CMP(k-1)_(m2+1) in the k-1th round of comparison, the smaller value output by CMPk_0 is the minimum data of the k-th round of comparison, the larger value output by CMPk_(Kk) is the maximum data of the k-th round of comparison, k is an integer greater than 1 and less than or equal to K, and m2 is an integer greater than or equal to 0 and less than or equal to Kk.
[0015] In some implementations, the data sorting method uses ascending logic for sorting, and obtains the sorting result of the third data group based on the maximum data and the minimum data obtained in each round of comparison, including: in the sorting result of the third data group, the minimum data and the maximum data obtained in the first round of comparison are sorted as 0 and 2K-1, respectively, and the minimum data and the maximum data obtained in the kth round of comparison are sorted as k-1 and 2K-k, respectively.
[0016] In some implementations, the data sorting method uses descending logic for sorting, and performs K rounds of comparison based on the first data group and the second data group, including: using K comparison units CMP1_0 to CMP1_(K-1) in a comparator to compare the data with the same sorting in the first data group and the second data group in the first round of comparison, wherein CMP1_m1 is used to compare the data sorted as m1 in the first data group and the second data group, the larger value output by CMP1_0 is the maximum data in the first round of comparison, the smaller value output by CMP1_(K-1) is the minimum data in the first round of comparison, and m1 is the larger value. is an integer equal to 0 and less than K; and in the k-th round of comparison, the K-k+1 comparison units CMPk_0 to CMPk_(Kk) in the comparator are used to compare the data other than the maximum data and the minimum data in the k-1th round of comparison results, wherein CMPk_m2 is used to compare the smaller value output by CMP(k-1)_m2 in the k-1th round of comparison with the larger value output by CMP(k-1)_(m2+1), the larger value output by CMPk_0 is the maximum data of the k-th round of comparison, the smaller value output by CMPk_(Kk) is the minimum data of the k-th round of comparison, and m2 is an integer greater than or equal to 0 and less than or equal to Kk.
[0017] In some implementations, the data sorting method uses descending logic for sorting, and obtaining the sorting result of the third data based on the maximum data and the minimum data obtained in each round of comparison includes: in the sorting result of the third data, the maximum data and the minimum data obtained in the first round of comparison are sorted as 0 and 2K-1, respectively, and the maximum data and the minimum data obtained in the kth round of comparison are sorted as k-1 and 2K-k, respectively.
[0018] The data sorting method provided by the embodiment of the present disclosure can achieve fast and parallel output of sorting results through a hardware structure with low hardware consumption and a small data dependency chain, and can adapt to the commonly used pipeline structure of hardware, and has the advantages of easy implementation and strong scalability.
[0019] The data sorting method provided by the embodiment of the present disclosure can sort data whose number is an integer power of 2, and can also sort any number of data, thereby meeting the sorting requirements in different scenarios.
[0020] In a second aspect, an embodiment of the present disclosure provides a data sorting device, comprising: a data acquisition module, configured to acquire multiple input data, the number of the multiple input data is 2 to the Nth power, where N is an integer greater than 1; and a data sorting module, comprising a multi-level comparator array, the number of comparators in each level of the multi-level comparator array is twice the number of comparators in the next level of comparators, and the data sorting module is configured to: divide the multiple input data into N / 2 data groups on average, sort the data in each data group by the N / 2 comparators in the first level of comparators to obtain an intermediate sorting result; divide the intermediate sorting result obtained in the last sorting into M / 2 data groups, sort the data in each data group by the M / 2 comparators in the next level of comparators to obtain a new intermediate sorting result, and repeat this step N-1 times to obtain a final sorting result, where M is the number of data groups in the last sorting.
[0021] In a third aspect, an embodiment of the present disclosure provides an electronic device, comprising: a memory configured to store an executable program; and a processor configured to call the program to execute the data sorting method described in any one of the first aspects of the embodiments of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] Figure 1A A flow chart of a data sorting method provided by an embodiment of the present disclosure is shown.
[0023] Figure 1B It is shown that in the embodiment of the present disclosure, multiple input data are evenly divided into 2 N-1 A flowchart of sorting data groups and sorting the data in each data group to obtain an intermediate sorting result.
[0024] Figure 1C It is shown as a flow chart of dividing the intermediate sorting result obtained by the previous sorting into M / 2 data groups and sorting the data in each data group to obtain a new intermediate sorting result in an embodiment of the present disclosure.
[0025] Figure 1D Shown is a flow chart of obtaining the final sorting result in an embodiment of the present disclosure.
[0026] Figure 2 Shown is a schematic diagram of the data sorting process in an embodiment of the present disclosure.
[0027] Figure 3A and Figure 3B They are schematic diagrams of using ascending logic and descending logic to compare data in the embodiments of the present disclosure.
[0028] Figure 4AIt is shown as a flow chart of dividing the intermediate sorting result obtained by the previous sorting into M / 2 data groups and sorting the data in each data group to obtain a new intermediate sorting result in an embodiment of the present disclosure.
[0029] Figure 4B Shown is a flowchart of sorting the data in the third data group to obtain the sorting result of the third data group in an embodiment of the present disclosure.
[0030] Figure 5 Shown is a schematic diagram of sorting by the CMP8 module in an embodiment of the present disclosure.
[0031] Figure 6 Shown is a schematic diagram of the structure of a data sorting device provided in an embodiment of the present disclosure.
[0032] Figure 7 Shown is a schematic diagram of the structure of an electronic device provided by an embodiment of the present disclosure. DETAILED DESCRIPTION
[0033] The following is an explanation of the embodiments of the present disclosure by specific examples, and those skilled in the art can easily understand other advantages and effects of the present disclosure from the contents disclosed in this specification. The present disclosure can also be implemented or applied through other different specific embodiments, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present disclosure. It should be noted that the following embodiments and features in the embodiments can be combined with each other without conflict.
[0034] It should be noted that the illustrations provided in the following embodiments are only used to schematically illustrate the basic concept of the present disclosure. Therefore, the drawings only show components related to the present disclosure rather than being drawn according to the number, shape and size of components in actual implementation. In actual implementation, the type, quantity and proportion of each component may be changed arbitrarily, and the component layout may also be more complicated.
[0035] In the description of the present disclosure, the terms "first" and "second" are used for descriptive purposes only and are not to be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of the feature. In the description of the present disclosure, the meaning of "plurality" is two or more, unless otherwise clearly and specifically defined.
[0036] In the present disclosure, unless otherwise clearly specified and limited, the terms such as "connected" and "connection" should be understood in a broad sense, for example, it can be a mechanical connection or an electrical connection; it can be a direct connection or an indirect connection through an intermediate medium, it can be the internal connection of two elements or the interaction relationship between two elements. For ordinary technicians in this field, the specific meanings of the above terms in the present disclosure can be understood according to specific circumstances.
[0037] Sorting is one of the common operations in the data processing process. In some technical solutions, technical solutions based on software sorting algorithm development and design are often used to sort data. These software sorting algorithms are such as quick sort, merge sort, heap sort, etc. Quick sort is a sorting algorithm based on the divide-and-conquer method. It divides the array into two parts by selecting a reference element, and then recursively sorts the sub-array. Merge sort is also a divide-and-conquer algorithm, which divides the array into two equal parts, sorts each part separately, and then merges them. Heap sort uses the data structure of the heap to achieve sorting by repeatedly taking the largest (or smallest) element from the heap. In addition, there are basic sorting algorithms such as bubble sort, insertion sort, and selection sort. However, when the above sorting algorithms are implemented in hardware, there are situations where the calculation depends on the chain length, is not suitable for large throughput parallel computing, and / or there are problems such as complex hardware implementation and many calculation steps.
[0038] At least to address the above-mentioned problems, an embodiment of the present disclosure provides a data sorting method, which has the advantages of high computing parallelism, adaptability to large-throughput computing scenarios, short computing dependency chain, low hardware overhead, and simple steps.
[0039] The technical solutions in the embodiments of the present disclosure will be described in detail below in conjunction with the accompanying drawings in the embodiments of the present disclosure.In this document, a comparator or a comparator circuit may be referred to as a comparison module, and a comparator, a comparator circuit or a comparison module may include one or more comparison units.
[0040] Figure 1A is a flow chart showing a data sorting method provided by an embodiment of the present disclosure. Figure 1A As shown, the data sorting method provided by the embodiment of the present disclosure includes the following steps S11 to S14.
[0041] In step S11, multiple input data are obtained. The number of the multiple input data is 2 N , where N is an integer greater than 1.
[0042] In step S12, the plurality of input data are evenly divided into 2 N-1 data groups, and sort the data in each data group to obtain an intermediate sorting result.
[0043] In step S13, the intermediate sorting result obtained by the previous sorting is divided into M / 2 data groups, and the data in each data group is sorted to obtain a new intermediate sorting result.
[0044] In step S14, step S13 is repeated N-1 times to obtain the final sorting result, where M is the number of data groups in the previous sorting.
[0045] See also Figure 1B In some implementations, multiple input data are evenly divided into 2 N-1 The method of sorting the data in each data group and obtaining an intermediate sorting result includes the following steps S121 to S123.
[0046] In step S121, every two data among a plurality of input data are regarded as a data group.
[0047] In step S122, the two data in each data group are input into 2 N-1 A corresponding comparator among the comparators.
[0048] In step S123, by 2 N-1 The comparator output has 2 N-1 The intermediate sorting result of each data group includes two data arranged in order of size.
[0049] See also Figure 1C In the following implementation, dividing the intermediate sorting result obtained in the previous sorting into M / 2 data groups and sorting the data in each data group to obtain a new intermediate sorting result includes the following steps S131 to S133.
[0050] In step S131, every two data groups in the intermediate sorting results of the data groups are regarded as a next-level data group.
[0051] In step S132, the two data groups in each next level data group are input into 2 N-2 A corresponding comparator among the comparators.
[0052] In step S133, by 2 N-2 The comparator output has 2 N-2 The new intermediate sorting result of each data group includes four data arranged in order of size.
[0053] In some embodiments, by 2 N-2 The comparator output has 2 N-2 The new intermediate sorting results of the data groups include:N-2 Each of the comparators compares the first sequence data in one data group with the first sequence data in another data group, and compares the second sequence data in one data group with the second sequence data in another data group, so as to output sorting results each including four data arranged in order of size, and the sorting results serve as the output data group corresponding to one comparator.
[0054] See also Figure 1D In some implementations, repeating this step N-1 times to obtain the final sorting result includes the following steps S141 to S144.
[0055] In step S141, every two data groups in the intermediate sorting result obtained in the previous sorting are taken as a next-level data group.
[0056] In step S142, two data groups in each next-level data group are respectively input into corresponding comparators among the M / 2 comparators.
[0057] In step S143, M / 2 comparators are used to output new intermediate sorting results of M / 2 data groups, each of which includes 2 data groups arranged in order of size. N Divide by M / 2 data.
[0058] In step S144, the above steps S141 to S143 are repeated to include 2 N The sorting result of each data is taken as the final sorting result.
[0059] In some embodiments, outputting the new intermediate sorting result having M / 2 data groups through M / 2 comparators includes: comparing the data arranged in order of size in one data group with the data arranged in order of size in another data group through a plurality of comparison units of each of the M / 2 comparators. N Divide by M rounds of comparison to output 2 N The sorting result is divided by M / 2 data, and the sorting result is used as an output data group corresponding to a comparator.
[0060] Next, we will introduce the above data sorting method through an example. The number of input data in this example is 2 N .like Figure 2 As shown, the data sorting device includes a plurality of comparison modules, and these comparison modules include CMP2, CMP4, CMP8, ..., CMP2 N , where the number of CMPn modules is 2 NEach CMP2 module is used to compare two input data. Each CMPn module is used to compare n input data, where n input data includes two groups of sorted data, where n>2 and n is an integer power of 2.
[0061] In the first sort, 2 N Each two data in the input data are taken as a data group, and a total of 2 N-1 Each CMP2 module can sort two data in one data group, using 2 N-1 The CMP2 module is used to N-1 By sorting the data in each data group, the sorting result of each data group can be obtained as the intermediate sorting result of the first sorting.
[0062] In the second sorting, the output data of the two CMP2 modules are taken as one data group, and a total of 2 N-2 Each CMP4 module can sort 4 data in 1 data group, using 2 N-2 The CMP4 module is used to N-2 By sorting the data in each data group, the sorting result of each data group can be obtained as the intermediate sorting result of the second sorting.
[0063] Similarly, in the log2(n)th sort, the output data of the two CMP(n / 2) modules are taken as one data group, and a total of 2 N-n Each CMPn module can sort n data in a data group, using 2 N-n The CMPn module is used to N-n By sorting the data in each data group, the sorting result of each data group can be obtained as the intermediate sorting result of the log2(n)th sorting.
[0064] In the Nth sort, two CMP2 N-1 The output data of the module is treated as a data group and uses a CMP2 N The module sorts the data in the data group to obtain the final sorting result.
[0065] According to the above, it can be known that in the embodiment of the present disclosure, N sortings can be performed to achieve 2 N The sorting process can be realized through a hardware structure with low hardware consumption and small data dependency chain, and can adapt to the commonly used pipeline structure of hardware, with the advantages of easy implementation and strong scalability.
[0066] In some implementations, multiple input data are evenly divided into 2 N-1The method of sorting the data in each data group to obtain an intermediate sorting result includes sorting two data in each data group in ascending order or descending order to obtain an intermediate sorting result. Figure 3A is a logic diagram showing the sorting of data A[1] and A[0] using ascending logic, Figure 3B is a logic diagram showing the sorting of data A[1] and A[0] using descending logic.
[0067] In some implementations, dividing the intermediate sorting results obtained from the previous sorting into M / 2 data groups includes: merging the intermediate sorting results corresponding to every two data groups in the previous sorting into one data group to obtain M / 2 data groups.
[0068] For example, if the input data includes A[0], A[1], ..., A[7]. In the first sorting, A[0] and A[1] are taken as one data group, A[2] and A[3] are taken as one data group, and so on. In the second sorting, the sorting results of A[0] and A[1] and the sorting results of A[2] and A[3] are merged into one data group, and the sorting results of A[4] and A[5] and the sorting results of A[6] and A[7] are merged into one data group. In the third sorting, the sorting results of A[0] to A[3] are merged into one data group, and the sorting results of A[4] to A[7] are merged into one data group.
[0069] See also Figure 4A In some implementations, dividing the intermediate sorting result obtained from the previous sorting into M / 2 data groups and sorting the data in each data group to obtain a new intermediate sorting result includes the following steps S41 and S42.
[0070] In step S41, a third data group is obtained by combining the first data group and the second data group in the previous sorting.
[0071] In step S42, the data in the third data group is sorted to obtain a third data group sorting result.
[0072] See also Figure 4B Sorting the data in the third data group to obtain the third data group sorting result includes the following steps S421 to S422.
[0073] In step S421, K rounds of comparison are performed based on the first data group and the second data group. For example, for the CMP4 module, the number of the first data group is 2, and 2 rounds of comparison are required. For another example, for the CMP8 module, the number of the first data group is 4, and 4 rounds of comparison are required.
[0074] In step S422, obtain the sorting result of the third data group according to the maximum data and the minimum data obtained in each round of comparison, where K is the number of data included in the first data group.
[0075] In some implementation manners, performing K rounds of comparison based on the first data group and the second data group includes: in the first round of comparison, compare the data with the same sorting in the first data group and the second data group and perform re-sorting according to the comparison result to obtain the sorting sub-result of the first round; in the k-th round of comparison, perform adjacent data comparison on the data other than the maximum data and the minimum data in the sorting sub-result of the (k - 1)-th round, and perform re-sorting according to the comparison result to obtain the sorting sub-result of the k-th round, where k is an integer greater than 1 and less than or equal to K.
[0076] Exemplarily, if the data sorting method performs sorting using an ascending order logic, the first data group includes C[0] to C[3] and C[0] < C[1] < C[2] < C[3], and the second data group includes C[4] to C[7] and C[4] < C[5] < C[6] < C[7], then:
[0077] In the first round of comparison, compare C[0] and C[4] with the same sorting in the first data group and the second data group, compare C[1] and C[5], compare C[2] and C[6], and compare C[3] and C[7]. Assume the comparison results are C[4] < C[0], C[1] < C[5], C[2] < C[6], C[7] < C[3], then after re-sorting, the sorting sub-result of the first round is: C[4], C[0], C[1], C[5], C[2], C[6], C[7], C[3]. Among them, C[4] and C[3] are respectively the minimum data and the maximum data obtained in the first round of sorting.
[0078] In the second round of comparison, perform adjacent data comparison on the data other than the maximum data and the minimum data in the sorting sub-result of the first round (C[0], C[1], C[5], C[2], C[6], C[7]), that is, compare C[0] with C[1], compare C[5] with C[2], and compare C[6] with C[7]. Assume C[0] < C[1], C[2] < C[5], C[6] < C[7], then after re-sorting, the sorting sub-result of the second round is: C[0], C[1], C[2], C[5], C[6], C[7]. Among them, C[0] and C[7] are respectively the minimum data and the maximum data obtained in the second round of sorting.
[0079] In the third round of comparison, for the data other than the maximum and minimum data in the sorting sub-result of the second round (C[1], C[2], C[5], C[6]), adjacent data comparison is performed, that is, C[1] is compared with C[2], and C[5] is compared with C[6]. Assuming C[1]<C[2] and C[5]<C[6], the sorting sub-result after reordering is C[1], C[2], C[5], C[6]. Among them, C[1] and C[6] are the minimum and maximum data obtained from the third round of sorting, respectively.
[0080] In the fourth round of comparison, for the data other than the maximum and minimum data in the sorting sub-result of the third round (C[2], C[5]), comparison is performed. Assuming C[2]<C[5], the sorting sub-result after reordering is C[2], C[5]. Among them, C[2] and C[5] are the minimum and maximum data obtained from the fourth round of sorting, respectively.
[0081] In some implementation manners, the data sorting method sorts using an ascending order logic, and performing K rounds of comparison based on the first data group and the second data group includes: in the first round of comparison, using K comparison units CMP1_0 to CMP1_(K - 1) in a comparator to compare the data with the same sorting in the first data group and the second data group, where CMP1_m1 is used to compare the data with sorting m1 in the first data group and the second data group, the smaller value output by CMP1_0 is the minimum data of the first round of comparison, the larger value output by CMP1_(K - 1) is the maximum data of the first round of comparison, and m1 is an integer greater than or equal to 0 and less than K; in the k-th round of comparison, using K - k + 1 comparison units CMPk_0 to CMPk_(K - k) in the comparator to compare the data other than the maximum and minimum data in the comparison result of the (k - 1)-th round, where CMPk_m2 is used to compare the larger value output by CMP(k - 1)_m2 and the smaller value output by CMP(k - 1)_(m2 + 1) in the (k - 1)-th round of comparison, the smaller value output by CMPk_0 is the minimum data of the k-th round of comparison, the larger value output by CMPk_(K - k) is the maximum data of the k-th round of comparison, k is an integer greater than 1 and less than or equal to K, and m2 is an integer greater than or equal to 0 and less than or equal to K - k.
[0082] Exemplarily, Figure 5 is a schematic diagram showing the CMP8 module sorting the third data group. The first data group includes C[0] to C[3] and C[0]<C[1]<C[2]<C[3], and the second data group includes C[4] to C[7] and C[4]<C[5]<C[6]<C[7].
[0083] In the first round of comparison, four comparison units CMP1_0 to CMP_3 are used to compare the data with the same sorting in the first data group and the second data group. Among them, CMP1_0 is used to compare the two data with sorting 0 in the first data group and the second data group, that is, C[0] and C[4]. CMP1_1 is used to compare the two data with sorting 1 in the first data group and the second data group, that is, C[1] and C[5], and so on. Assuming C[4]<C[0], C[1]<C[5], C[2]<C[6], C[7]<C[3], the smaller value C[4] output by CMP1_0 is the smallest data in the first round of comparison, and the larger value C[3] output by CMP1_3 is the largest data in the first round of comparison.
[0084] In the second round of comparison, three comparison units CMP2_0 to CMP2_2 are used to compare the data other than the largest data and the smallest data in the results of the first round of comparison. Specifically, CMP2_0 is used to compare the larger value (C[0]) output by CMP1_0 and the smaller value (C[1]) output by CMP1_1. CMP2_1 is used to compare the larger value (C[5]) output by CMP1_1 and the smaller value (C[2]) output by CMP1_2. CMP2_2 is used to compare the larger value (C[6]) output by CMP1_2 and the smaller value (C[7]) output by CMP1_3. Assuming C[0]<C[1], C[2]<C[5], C[6]<C[7], the smaller value (C[0]) output by CMP2_0 is the smallest data in the second round of comparison, and the larger value (C[7]) output by CMP2_2 is the largest data in the second round of comparison.
[0085] In the third round of comparison, two comparison units CMP3_0 and CMP3_1 are used to compare the data other than the largest data and the smallest data in the results of the second round of comparison. Specifically, CMP3_0 is used to compare the larger value (C[1]) output by CMP2_0 and the smaller value (C[2]) output by CMP2_1. CMP3_1 is used to compare the larger value (C[5]) output by CMP2_1 and the smaller value (C[6]) output by CMP2_2. Assuming C[1]<C[2], C[5]<C[6], the smaller value (C[1]) output by CMP3_0 is the smallest data in the third round of comparison, and the larger value (C[6]) output by CMP3_1 is the largest data in the third round of comparison.
[0086] In the 4th round of comparison, one comparison unit CMP4_0 is used to compare the data other than the maximum data and the minimum data in the comparison results of the 3rd round. Specifically, CMP4_0 is used to compare the larger value (C[2]) output by CMP3_0 and the smaller value (C[5]) output by CMP3_1. Assuming C[2] < C[5], the smaller value (C[2]) output by CMP4_0 is the minimum data of the 4th round of comparison, and the larger value (C[5]) output by CMP4_0 is the maximum data of the 4th round of comparison.
[0087] In some implementation manners, the data sorting method sorts according to the ascending order logic, and obtaining the sorting result of the third data group based on the maximum data and the minimum data obtained in each round of comparison includes: in the sorting result of the third data group, the sorting of the minimum data and the maximum data obtained in the 1st round of comparison are 0 and 2K - 1 respectively, and the sorting of the minimum data and the maximum data obtained in the kth round of comparison are k - 1 and 2K - k respectively. For example, in Figure 5 the example shown, the sorting of the minimum data C[4] obtained in the 1st round of comparison is 0, and the sorting of the maximum data C[3] is 7. The sorting of the minimum data C[0] obtained in the 2nd round of comparison is 1, and the sorting of the maximum data C[7] is 6. The sorting of the minimum data C[1] obtained in the 3rd round of comparison is 2, and the sorting of the maximum data C[6] is 5. The sorting of the minimum data C[2] obtained in the 4th round of comparison is 3, and the sorting of the maximum data C[5] is 4. Therefore, the final sorting of the third data is: C[4], C[0], C[1], C[2], C[5], C[6], C[7], C[3].
[0088] In some implementations, the data sorting method uses descending logic for sorting, and performs K rounds of comparison based on the first data group and the second data group, including: in the first round of comparison, using K comparison units CMP1_0 to CMP1_(K-1) in a comparator to compare the data with the same sorting in the first data group and the second data group, wherein CMP1_m1 is used to compare the data sorted as m1 in the first data group and the second data group, the larger value output by CMP1_0 is the maximum data of the first round of comparison, the smaller value output by CMP1_(K-1) is the minimum data of the first round of comparison, and m1 is greater than or equal to 0 and less than In the k-th round of comparison, the K-k+1 comparison units CMPk_0 to CMPk_(Kk) in the comparator are used to compare the data other than the maximum data and the minimum data in the k-1th round of comparison results, wherein CMPk_m2 is used to compare the smaller value output by CMP(k-1)_m2 in the k-1th round of comparison with the larger value output by CMP(k-1)_(m2+1), the larger value output by CMPk_0 is the maximum data of the k-th round of comparison, the smaller value output by CMPk_(Kk) is the minimum data of the k-th round of comparison, and m2 is an integer greater than or equal to 0 and less than or equal to Kk. The K-th round of comparison based on the first data group and the second data group when using descending logic is similar to that when using ascending logic, which will not be repeated here.
[0089] In some implementations, the data sorting method uses descending logic for sorting, and the sorting result of the third data obtained according to the maximum data and the minimum data obtained in each round of comparison includes: in the sorting result of the third data, the maximum data and the minimum data obtained in the first round of comparison are sorted as 0 and 2K-1 respectively, and the maximum data and the minimum data obtained in the kth round of comparison are sorted as k-1 and 2K-k respectively. The sorting result of the third data obtained according to the maximum data and the minimum data obtained in each round of comparison when using descending logic is similar to that when using ascending logic, which will not be repeated here.
[0090] In some implementations, the number of original input data is an integer power of 2, and in this case, the original input data can be used as the multiple input data in step S11.
[0091] In some implementations, the number of original input data is not an integer power of 2. In this case, the original input data can be expanded using the expanded data to obtain expanded data whose number is an integer power of 2, and the expanded data is used as the multiple input data in step S11. Figure 1A The data sorting method shown is used to sort the expanded data, and the sorting result of the original input data can be obtained by deleting the expanded data from the final sorting result.
[0092] In some implementations, the expanded data may be the maximum value or the minimum value that can be represented by the plurality of original input data, but the present disclosure is not limited thereto.
[0093] For example, if the original input data is an 8-bit signed number, the maximum value it can represent is 127 and the minimum value is -128. In this case, 127 or -128 can be used as the extended data.
[0094] For example, if the original input data is A={A[0], A[1], A[2], A[3], A[4], A[5]}, it is padded with the maximum value MAX to obtain A'={A[0], A[1], A[2], A[3], A[4], A[5], MAX, MAX}. After A' is used as the input data and sorted, it is assumed that E'={MAX, MAX, A[3], A[1], A[2], A[5], A[0], A[4]} is obtained. After deleting the extended data MAX, the sorting result of the original input data is E={A[3], A[1], A[2], A[5], A[0], A[4]}.
[0095] According to the above, the data sorting method provided by the embodiment of the present disclosure can sort data whose number is an integer power of 2, and can also sort any number of data, thereby being able to meet the sorting requirements in different scenarios.
[0096] The protection scope of the data sorting method provided by the embodiment of the present disclosure is not limited to the execution order of the steps listed in the embodiment. All solutions implemented by adding, reducing or replacing steps in the prior art based on the principles of the present disclosure are included in the protection scope of the present disclosure.
[0097] The embodiments of the present disclosure also provide a data sorting device, which can implement the data sorting method provided by the embodiments of the present disclosure. However, the implementation device of the data sorting method provided by the embodiments of the present disclosure includes but is not limited to the structure of the data sorting device listed in the embodiments of the present disclosure. All structural deformations and replacements of the prior art made according to the principles of the embodiments of the present disclosure are included in the protection scope of the present disclosure.
[0098] Figure 6 Schematic diagram showing the structure of a data sorting device provided according to an embodiment of the present disclosure. Figure 6 As shown, the data sorting device 6 includes a data acquisition module 61 and a data sorting module 62 .
[0099] The data acquisition module 61 is configured to acquire a plurality of input data, where the number of the input data is 2 to the Nth power, where N is an integer greater than 1.
[0100] The data sorting module 62 includes a multi-stage comparator array, wherein the number of comparators in each stage of the multi-stage comparator array is twice the number of comparators in the next stage of the comparator array. The data sorting module 62 is configured to: divide the plurality of input data into two N-1 data groups, through the first stage comparator 2 N-1 The comparators are used to sort the data in each data group to obtain an intermediate sorting result; the intermediate sorting result obtained in the previous sorting is divided into M / 2 data groups, and the data in each data group is sorted by the M / 2 comparators in the next level of comparators to obtain a new intermediate sorting result, and this step is repeated N-1 times to obtain the final sorting result, where M is the number of data groups in the previous sorting.
[0101] It should be noted that Figure 6 The modules of the data sorting device 6 shown correspond to the steps of the data sorting method described above, and are not described in detail here.
[0102] In the several embodiments provided in the embodiments of the present disclosure, it should be understood that the disclosed system, device or method can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of modules / units is only a logical function division. There may be other division methods in actual implementation. For example, multiple modules or units can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or modules or units, which can be electrical, mechanical or other forms.
[0103] The modules / units described as separate components may or may not be physically separated, and the components displayed as modules / units may or may not be physical modules, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules / units may be selected according to actual needs to achieve the purpose of the embodiments of the present disclosure. For example, the functional modules / units in the various embodiments of the present disclosure may be integrated into one processing module, or each module / unit may exist physically separately, or two or more modules / units may be integrated into one module / unit.
[0104] Those of ordinary skill in the art should further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in terms of function in the above description. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this disclosure.
[0105] The embodiments of the present disclosure may also provide an electronic device. Figure 7 The structure diagram of the electronic device 7 in the embodiment of the present disclosure is shown. Figure 7 As shown, in this embodiment, the electronic device 7 includes a memory 71 and a processor 72 .
[0106] The memory 71 is used to store computer programs. In some implementations, the memory 71 may include a computer system readable medium in the form of a volatile memory, such as a RAM and / or a cache memory. The electronic device 7 may further include other removable / non-removable, volatile / non-volatile computer system storage media. The memory 71 may include at least one program product having a set (e.g., at least one) of program modules that are configured to perform the functions of the various embodiments of the present disclosure.
[0107] The processor 72 is connected to the memory 71 and is used to execute the computer program stored in the memory 71 so that the electronic device 7 executes the data sorting method provided by the embodiment of the present disclosure.
[0108] In some implementations, the processor 72 may be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc. In other embodiments, the processor 72 may also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.
[0109] The descriptions of the processes or structures corresponding to the above-mentioned figures have different emphases. For parts that are not described in detail in a certain process or structure, please refer to the relevant descriptions of other processes or structures.
[0110] In summary, the data sorting method provided by the embodiment of the present disclosure can achieve fast and parallel output of sorting results through a hardware structure with low hardware consumption and a small data dependency chain, and can adapt to the commonly used pipeline structure of hardware, and has the advantages of easy implementation and strong scalability. In addition, the data sorting method provided by the embodiment of the present disclosure can sort data whose number is an integer power of 2, and can sort any number of data, so it can meet the sorting requirements in different scenarios.
[0111] The above embodiments are merely illustrative of the principles and effects of the present disclosure, and are not intended to limit the present disclosure. Anyone familiar with the technology may modify or change the above embodiments without violating the spirit and scope of the present disclosure. Therefore, all equivalent modifications or changes made by a person of ordinary skill in the art without departing from the spirit and technical ideas disclosed in the present disclosure shall still be covered by the claims of the present disclosure.
Claims
1. A data sorting method, characterized in that: include: Acquire a plurality of input data, wherein the number of the plurality of input data is a power of 2, where N is an integer greater than 1; Divide the multiple input data into 2 N-1 data groups and sorting the data in each data group to obtain an intermediate sorting result; and The intermediate sorting result obtained in the previous sorting is divided into M / 2 data groups and the data in each data group is sorted to obtain a new intermediate sorting result, and this step is repeated N-1 times to obtain the final sorting result, where M is the number of data groups in the previous sorting.
2. The data sorting method according to claim 1, characterized in that: Getting multiple input data includes: Acquire a plurality of original input data, wherein the number of the plurality of original input data is not an integer power of 2; and The plurality of original input data are expanded by using the expanded data to obtain expanded data whose number is an integer power of 2, and the expanded data are used as the plurality of input data, The data sorting method further comprises: deleting the extended data in the final sorting result.
3. The data sorting method according to claim 2, characterized in that: The extended data is the maximum value or the minimum value that can be represented by the plurality of original input data.
4. The data sorting method according to claim 1, characterized in that: Divide the multiple input data into 2 N-1 data groups and sorting the data in each data group to obtain the intermediate sorting results include: Taking every two data among the plurality of input data as a data group; Enter the two data in each data group into 2 N-1 a corresponding comparator among the comparators; and Pass 2 N-1 The comparator output has 2 N-1 The intermediate sorting result of each data group includes two data arranged in order of size.
5. The data sorting method according to claim 4, characterized in that: Dividing the intermediate sorting result obtained from the previous sorting into M / 2 data groups and sorting the data in each data group to obtain a new intermediate sorting result includes: Taking every two data groups in the intermediate sorting results of each data group as a next-level data group; Enter the two data groups in each lower level data group into 2 N-2 a corresponding comparator among the comparators; and Pass 2 N-2 The comparator output has 2 N-2 The new intermediate sorting results of the data groups, each of which includes four data arranged in order of size, Among them, through 2 N-2 The comparator output has 2 N-2 The new intermediate sorting results of the data groups include: N-2 Each of the comparators compares the first sequence data in one data group with the first sequence data in another data group, and compares the second sequence data in the one data group with the second sequence data in the other data group, so as to output sorting results each including four data arranged in order of size, and the sorting results serve as the output data group corresponding to one comparator.
6. The data sorting method according to claim 5, characterized in that: Repeat this step N-1 times to get the final sorting results including: Take every two data groups in the intermediate sorting results obtained from the previous sorting as a next-level data group; Inputting two data groups in each next-level data group into corresponding comparators in the M / 2 comparators respectively; The new intermediate sorting result having M / 2 data groups is outputted by M / 2 comparators, and the new intermediate sorting result of each data group includes 2 N Divide by M / 2 data; and Repeat the above steps to include 2 N The sorting result of each data is taken as the final sorting result. The outputting of the new intermediate sorting result having M / 2 data groups through M / 2 comparators includes: comparing the data arranged in order of size in one data group with the data arranged in order of size in another data group through a plurality of comparing units of each of the M / 2 comparators. N Divide by M rounds of comparison to output 2 N The sorting result is divided by M / 2 data, and the sorting result is used as an output data group corresponding to a comparator.
7. The data sorting method according to claim 1, characterized in that: Divide the multiple input data into 2 N-1 data groups and sorting the data in each data group to obtain the intermediate sorting results include: The two data in each data group are sorted in ascending logic or descending logic to obtain the intermediate sorting result.
8. The data sorting method according to claim 1, characterized in that: Divide the intermediate sorting results obtained from the previous sorting into M / 2 data groups including: The intermediate sorting results corresponding to every two data groups in the previous sorting are merged into one data group to obtain the M / 2 data groups.
9. The data sorting method according to claim 8, characterized in that: Dividing the intermediate sorting result obtained from the previous sorting into M / 2 data groups and sorting the data in each data group to obtain a new intermediate sorting result includes: A third data group is obtained by combining the first data group and the second data group in the previous sorting; and Sorting the data in the third data group to obtain the sorting result of the third data group includes: performing K rounds of comparison based on the first data group and the second data group, and obtaining the sorting result of the third data group according to the maximum data and the minimum data obtained in each round of comparison, where K is the number of data contained in the first data group.
10. The data sorting method according to claim 9, characterized in that: Performing K rounds of comparison based on the first data group and the second data group includes: In the first round of comparison, the data with the same ranking in the first data group and the second data group are compared and re-ranked according to the comparison result to obtain the first round of ranking sub-results; and In the kth round of comparison, adjacent data comparison is performed for data other than the maximum data and the minimum data in the k-1th round of sorting sub-results, and reordering is performed according to the comparison results to obtain the kth round of sorting sub-results, where k is an integer greater than 1 and less than or equal to K.
11. The data sorting method according to claim 9, characterized in that: The data sorting method adopts ascending logic to sort, and performs K rounds of comparison based on the first data group and the second data group, including: In the first round of comparison, K comparison units CMP1_0 to CMP1_(K-1) in a comparator are used to compare the data with the same order in the first data group and the second data group, wherein CMP1_m1 is used to compare the data with the order m1 in the first data group and the second data group, the smaller value output by CMP1_0 is the minimum data of the first round of comparison, the larger value output by CMP1_(K-1) is the maximum data of the first round of comparison, and m1 is an integer greater than or equal to 0 and less than K; and In the k-th round of comparison, the K-k+1 comparison units CMPk_0 to CMPk_(Kk) in the comparator are used to compare the data other than the maximum data and the minimum data in the k-1th round of comparison results, wherein CMPk_m2 is used to compare the larger value output by CMP(k-1)_m2 and the smaller value output by CMP(k-1)_(m2+1) in the k-1th round of comparison, the smaller value output by CMPk_0 is the minimum data of the k-th round of comparison, the larger value output by CMPk_(Kk) is the maximum data of the k-th round of comparison, k is an integer greater than 1 and less than or equal to K, and m2 is an integer greater than or equal to 0 and less than or equal to Kk.
12. The data sorting method according to any one of claims 9 to 11, characterized in that: The data sorting method adopts ascending logic to sort, and obtains the sorting result of the third data group according to the maximum data and the minimum data obtained in each round of comparison, including: In the sorting results of the third data group, the smallest data and the largest data obtained in the first round of comparison are sorted as 0 and 2K-1 respectively, and the smallest data and the largest data obtained in the kth round of comparison are sorted as k-1 and 2K-k respectively.
13. The data sorting method according to claim 9, characterized in that: The data sorting method adopts descending logic to sort, and performs K rounds of comparison based on the first data group and the second data group, including: In the first round of comparison, K comparison units CMP1_0 to CMP1_(K-1) in a comparator are used to compare the data with the same order in the first data group and the second data group, wherein CMP1_m1 is used to compare the data with the order m1 in the first data group and the second data group, the larger value output by CMP1_0 is the maximum data of the first round of comparison, the smaller value output by CMP1_(K-1) is the minimum data of the first round of comparison, and m1 is an integer greater than or equal to 0 and less than K; and In the k-th round of comparison, the K-k+1 comparison units CMPk_0 to CMPk_(Kk) in the comparator are used to compare the data other than the maximum data and the minimum data in the k-1-th round of comparison results, wherein CMPk_m2 is used to compare the smaller value output by CMP(k-1)_m2 in the k-1-th round of comparison with the larger value output by CMP(k-1)_(m2+1), the larger value output by CMPk_0 is the maximum data of the k-th round of comparison, the smaller value output by CMPk_(Kk) is the minimum data of the k-th round of comparison, and m2 is an integer greater than or equal to 0 and less than or equal to Kk.
14. The data sorting method according to claim 9, 10 or 13, characterized in that: The data sorting method adopts descending logic to sort, and obtaining the sorting result of the third data according to the maximum data and the minimum data obtained in each round of comparison includes: In the sorting result of the third data, the maximum data and the minimum data obtained in the first round of comparison are sorted as 0 and 2K-1 respectively, and the maximum data and the minimum data obtained in the kth round of comparison are sorted as k-1 and 2K-k respectively.
15. A data sorting device, characterized in that: include: A data acquisition module is configured to acquire a plurality of input data, wherein the number of the plurality of input data is a power of 2, wherein N is an integer greater than 1; as well as A data sorting module comprises a multi-stage comparator array, wherein the number of comparators in each stage of the multi-stage comparator array is twice the number of comparators in the next stage of the comparator array, and the data sorting module is configured as follows: Divide the multiple input data into 2 N-1 data groups, through the first stage comparator 2 N-1 A comparator sorts the data in each data group to obtain an intermediate sorting result; The intermediate sorting result obtained from the previous sorting is divided into M / 2 data groups, and the data in each data group is sorted by the M / 2 comparators in the next level comparator to obtain a new intermediate sorting result. This step is repeated N-1 times to obtain the final sorting result, where M is the number of data groups in the previous sorting.
16. An electronic device, characterized in that: include: a memory configured to store an executable program; as well as A processor is configured to call the program to execute the data sorting method according to any one of claims 1 to 14.
Citation Information
Cited By
Data sorting method and device, electronic equipment and chip
CN120723159A
