A heap sorting system and method based on FPGA

By grouping and processing data in parallel on the FPGA, heap sorting within and between groups is solved, the problem of high resource consumption of existing heap sorting algorithms is solved, and fast and efficient data sorting is achieved.

CN118642680BActive Publication Date: 2024-11-01TAISHAN UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411123976.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-08-16
Publication Date
2024-11-01
Estimated Expiration
2044-08-16

AI Technical Summary

Technical Problem

The existing heap sorting algorithm consumes a lot of resources when sorting large data volumes, which cannot meet the real-time requirements, and it occupies too much resources when implementing FPGA.

Method used

Using a heap sorting system based on FPGA, the data grouping is used to sort in parallel within groups, and the forward and reverse order of the inter-groups is combined and heap sorting is performed, and the heap sorting algorithm is optimized to adapt to the parallel circuit structure. The specific steps include obtaining the data to be sorted, sorting within groups and merging between groups, and gradually building the sorting results using the large top heap and small top heap structure.

Benefits of technology

It significantly shortens the sorting time, reduces the use of hardware resources, improves the sorting speed, and can complete the sorting of large data volumes in a short clock cycle.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118642680B_ABST
    Figure CN118642680B_ABST
Patent Text Reader

Abstract

The present invention relates to the field of digital integrated circuit technology, specifically to a heap sorting system and method based on FPGA, which obtains data to be sorted, takes each x data as a group, and sorts y groups of x×y data in the group to obtain y groups of sorted arrays; selects the maximum value in each group of sorted arrays, constructs a max-heap structure, wherein the top element of the heap is the maximum value of the x×y data, takes out the remaining data to replace the current top element of the heap, and reconstructs the max-heap structure to obtain the second largest value of the x×y data, and obtains the positive sorting data part of the x×y data through sequential cyclic iteration; during the positive sorting, when only one data is left in any group of the obtained y groups of sorted arrays, reverse sorting is performed; the positive sorting data part and the reverse sorting data part are associated to obtain the final sorting result. After the data are grouped, the consumption of sorting time is saved by parallel group sorting + inter-group merge sorting.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of digital integrated circuit design, and in particular to a stack sorting system and method based on FPGA. Background Art

[0002] The statements in this section merely provide background information related to the present invention and do not necessarily constitute prior art.

[0003] Sorting is a common algorithm in engineering implementation. It is often used in software design and based on serial implementation. The sorted data can be arranged in a certain order to facilitate fast search and retrieval. For example, in the database, the index is built based on the principle of sorting to speed up query operations; or in the process of feature extraction, matching and result screening of face recognition algorithms, relevant sorting algorithms are applied.

[0004] Heap Sort is a comparison-based sorting algorithm that uses the heap data structure to design a sorting algorithm. Compare and initialize the heap, using Compare times, delete elements from the top of the heap and rebuild the heap, and finally arrange all elements in order. The time complexity of the heap sort algorithm is , the space complexity is This results in a large delay in sorting, and a large amount of time is consumed in sorting large amounts of data, which cannot meet the needs of applications with high real-time requirements.

[0005] For the above reasons, when sorting a large amount of data, too many resources are occupied by the hardware (such as FPGA, field programmable gate array, which is often the core component of embedded systems), resulting in excessive sorting time. Summary of the invention

[0006] In order to solve the technical problems existing in the above-mentioned background technology, the present invention provides a heap sorting system and method based on FPGA, which performs parallel intra-group sorting after data grouping, and then performs inter-group forward and reverse order merge heap sorting, and optimizes the heap sorting algorithm to make it suitable for parallel circuit structure.

[0007] In order to achieve the above object, the present invention adopts the following technical solution:

[0008] A first aspect of the present invention provides a stack sorting method based on FPGA, comprising the following steps:

[0009] Get the data to be sorted, take each x data as a group, sort y groups of x×y data within each group, and get y groups of sorted arrays;

[0010] Use the obtained y groups of sorted arrays to perform positive sorting, specifically: select the maximum value in each group of sorted arrays, build a max-heap structure, where the top element of the heap is the maximum value of the x×y data, take out the remaining data to replace the current top element of the heap and re-build the max-heap structure to obtain the second largest value of the x×y data, and iterate in sequence to obtain the positive sorting data part of the x×y data;

[0011] During the forward sorting, when there is only one data left in any of the y groups of sorted arrays, reverse sorting is performed, specifically: the minimum value in each group of sorted arrays is selected to construct a small top heap structure, in which the top element of the heap is the minimum value of the x×y data, and the remaining data is taken to replace the current top element of the heap to reconstruct the small top heap structure to obtain the second smallest value of the x×y data, and after sequentially iterating in a loop, the reversed sorted data part of the x×y data is obtained;

[0012] The positive sorting data part and the reverse sorting data part are associated to obtain the final sorting result.

[0013] Furthermore, taking every x data as a group, y groups of x×y data are sorted within the group to obtain y groups of sorted arrays. Specifically, the data to be sorted is constructed into a max-heap structure, and the root node of the current heap is the largest element in the data to be sorted; the remaining nodes and data zero are used to reconstruct the max-heap structure, and the elements constructed each time are still x data; through repeated iterations, the maximum value of a data is obtained each time, and the data is output and zeros are added to the array to be sorted, and finally an array sorted from large to small is obtained, which is the required sorted array.

[0014] Furthermore, the data to be sorted is constructed into a large top heap structure, specifically:

[0015] The first level input is three data to be sorted, and the output is the maximum value among the three and the remaining two node data. The maximum value output is used as a node, and the remaining two node data are the left and right child nodes of the maximum value.

[0016] The input of the next stage is the two maximum values ​​output by the previous stage and another data to be sorted;

[0017] By analogy, after multiple levels of processing, the value of each node is greater than or equal to the values ​​of its left and right child nodes, resulting in a max-heap structure of x data in the group.

[0018] A second aspect of the present invention provides a FPGA-based heap sorting system, comprising:

[0019] The grouping heap sorting module has y unit processors, each of which has a data selector Mux, a Heap sort_PEi module, a register Register_PEi and a memory RAM_PEi. The data selector Mux obtains the data to be sorted, selects the data output by the register Register_PEi, and uses the Heap sort_PEi module with a four-level Max_PE circuit structure to perform heap sorting on the x data in each group and store them in the memory RAM_PEi.

[0020] The merged heap sorting module includes a register Register_Max, a register Register_Min, a secondary Max_M module and a secondary Min_M module, which is used to merge the positive and negative sorting according to the data of the memory RAM_PEi in the grouped heap sorting module, and obtain the final sorting result and store it in the memory RAM_out;

[0021] The control module is used to coordinate the grouping heap sorting module and the merging heap sorting module.

[0022] Furthermore, the Heap sort_PEi module is a multi-level Max_PE circuit structure having multiple groups of Max_PE modules;

[0023] The input of the first-level Max_PE module is three data to be sorted, and the output is the maximum value among the three and the remaining two node data. The maximum value output is used as a node, and the remaining two node data are the left and right child nodes of the maximum value;

[0024] The input of the next-level Max_PE module is the two maximum values ​​output by the two Max_PE modules of the previous level and another data to be sorted;

[0025] By analogy, after multi-level Max_PE module heap sorting, the value of each node is greater than or equal to the values ​​of its left and right child nodes, and a large top heap structure of x data in the group is obtained.

[0026] Furthermore, the data of the mth storage unit of the memory RAM_PEi is always zero, and the data of the 0th storage unit is always the maximum value;

[0027] When all the elements of the array have participated in the positive sort, that is, sorted from large to small, the address pointer has pointed to the mth storage unit, and the address pointer is no longer modified. The data zero in the storage unit continues to participate in the sorting to construct the max-heap until the sorting is completed;

[0028] When all the elements of the array have participated in the reverse sorting, that is, sorted from small to large, the address pointer has pointed to the 0th storage unit, and the address pointer is no longer modified. The maximum value of the data in the storage unit continues to participate in the sorting to construct the small top heap until the sorting is completed.

[0029] Furthermore, in the i-th clock cycle, the x data in the y-th group of the data to be sorted are sorted within the group, and a max-heap is constructed after being temporarily stored. Starting from the i+2-th clock cycle, the maximum value, the second largest value, etc. in the group are output in sequence until a sorted array is formed; where i=y.

[0030] Furthermore, in the i+2th clock cycle, the maximum value in each group of sorted arrays enters the secondary Max_M module in the merged heap sort module to construct a big top heap, and from the i+4th clock cycle, the maximum value, the second largest value, etc. of the final sorting result are output in sequence, which is the positive sorting data part, and the obtained sorting data is written in sequence from the first port of the memory RAM_out.

[0031] Furthermore, in the i+32th clock cycle, there is only one data left in one group of sorted arrays. After temporary storage, the data in each group of sorted arrays enters the secondary Min_M module in the merged heap sort module to build a small top heap. Starting from the i+34th clock cycle, the minimum value, the second minimum value, etc. of the final sorting result are output in sequence, which is the reverse sorted data part, and the obtained sorted data is written in sequence from the second port of the memory RAM_out.

[0032] Furthermore, in the i+126th clock cycle, the positively sorted data written from the first port of the memory RAM_out is extracted, and in the i+127th clock cycle, the reversely sorted data written from the second port of the memory RAM_out is extracted, and the final sorting result of x×y data is obtained after association.

[0033] Compared with the prior art, one or more of the above technical solutions have the following beneficial effects:

[0034] After the data is grouped, the data in all groups are sorted in parallel, and then merged and sorted between groups. The parallel processing of data can save sorting time, and during the merged sorting between groups, the forward sorting and reverse sorting are performed in parallel in some clock cycles, which further shortens the sorting time, thereby improving the sorting speed and reducing the resources occupied by hardware (such as FPGA, programmable gate array). BRIEF DESCRIPTION OF THE DRAWINGS

[0035] The accompanying drawings in the specification, which constitute a part of the present invention, are used to provide a further understanding of the present invention. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute improper limitations on the present invention.

[0036] Figure 1 is a schematic diagram of a sorting circuit structure provided by one or more embodiments of the present invention;

[0037] Figure 2 is a schematic diagram of a circuit structure of a group parallel sorting module provided by one or more embodiments of the present invention;

[0038] Figure 3 is a schematic diagram of the structure of a Heap sort_PE module provided by one or more embodiments of the present invention;

[0039] Figure 4 is a schematic diagram of the structure of a control_PE module in a controller controller provided by one or more embodiments of the present invention;

[0040] Figure 5 is a schematic diagram of the structure of control_Max (Min) in the controller controller provided by one or more embodiments of the present invention;

[0041] Figure 6 is a control flow chart of a control module control_Max provided by one or more embodiments of the present invention;

[0042] Figure 7 is a control flow chart of a control module control_Min provided by one or more embodiments of the present invention;

[0043] Figure 8 is a flow chart of a control module control_PE provided by one or more embodiments of the present invention;

[0044] Fig. 9 It is a schematic diagram of the spatiotemporal relationship between seven PE modules and output sorting results provided by one or more embodiments of the present invention. DETAILED DESCRIPTION

[0045] The present invention will be further described below in conjunction with the accompanying drawings and embodiments.

[0046] It should be noted that the following detailed descriptions are exemplary and are intended to provide further explanation of the present invention. Unless otherwise specified, all technical and scientific terms used herein have the same meanings as those commonly understood by those skilled in the art to which the present invention belongs.

[0047] Terminology explanation:

[0048] Heap Sort is a comparison-based sorting algorithm designed using a heap data structure. A heap is a structure that approximates a complete binary tree and satisfies the heap property: the key value or index of a child node is always smaller than (or larger than) its parent node. In the heap sorting algorithm, a maximum heap (or maximum priority queue) is generally used for ascending sorting, or a minimum heap (or minimum priority queue) is used for descending sorting.

[0049] A big heap is a data structure based on a complete binary tree. In a big heap, the value of each node is greater than or equal to the value of its child node. The root node of the big heap (also called the top of the heap) always contains the maximum value in the tree.

[0050] Suppose there is an array representation of a max-heap as [85, 68, 99, 55, 92, 57, 98, 66, 56] (in the array representation, the root node is usually located at index 1 to conform to the properties of a complete binary tree). According to the definition of a max-heap, the root node (index 1, value 85) is the maximum value in the heap, and its value is greater than or equal to the values ​​of its child nodes.

[0051] In a small heap, the value of each node is less than or equal to the value of its child node. The root node of a small heap (i.e., the top of the heap) is the smallest element in the entire heap.

[0052] As introduced in the background technology, when sorting a large amount of data, the use of a heap sorting algorithm occupies too many resources of the hardware (such as FPGA, programmable gate array), resulting in excessive consumption of sorting time. The following embodiment provides a heap sorting system and method based on FPGA, which performs intra-group sorting in parallel after data grouping, and then performs heap sorting in parallel in forward and reverse order between groups, and optimizes the heap sorting algorithm to make it suitable for parallel circuit structure.

[0053] Embodiment 1:

[0054] A heap sorting method based on FPGA comprises the following steps:

[0055] Get the data to be sorted, take each x data as a group, sort y groups of x×y data within each group, and get y groups of sorted arrays;

[0056] Use the obtained y groups of sorted arrays to perform positive sorting, specifically: select the maximum value in each group of sorted arrays, build a max-heap structure, where the top element of the heap is the maximum value of the x×y data, take out the remaining data to replace the current top element of the heap and re-build the max-heap structure to obtain the second largest value of the x×y data, and iterate in sequence to obtain the positive sorting data part of the x×y data;

[0057] During the forward sorting, when there is only one data left in any of the y groups of sorted arrays, reverse sorting is performed, specifically: the minimum value in each group of sorted arrays is selected to construct a small top heap structure, in which the top element of the heap is the minimum value of the x×y data, and the remaining data is taken to replace the current top element of the heap to reconstruct the small top heap structure to obtain the second smallest value of the x×y data, and after sequentially iterating in a loop, the reversed sorted data part of the x×y data is obtained;

[0058] The positive sorting result and the reverse sorting result are associated to obtain the final sorting result.

[0059] This embodiment takes the sorting of 217 data as an example. After receiving all the data to be sorted, the serial heap sorting program needs about 1600 clock cycles to obtain all the final sorting results. This embodiment uses 7 groups to merge in parallel and then performs heap sorting. After inputting 7 groups of 217 data in the 7th clock cycle, all the final sorting results can be obtained in the 134th clock cycle, which greatly reduces the sorting time.

[0060] The design idea of ​​the circuit is to "optimize the heap sorting algorithm suitable for hardware implementation Sorting within a group Merge sort between groups".

[0061] The following optimizations are made to the intra-group sorting algorithm:

[0062] (1) The data to be sorted is constructed into a big-top heap. According to the properties of the big-top heap, the root node (top of the heap) of the current heap is the largest element in the data to be sorted;

[0063] (2) Output the top element of the heap so that it no longer participates in the subsequent sorting process; the remaining nodes and a data zero are reconstructed into a big top heap (data zero is the minimum value of an unsigned number; if the input data of the circuit is a signed number, it can be set to the minimum value of the signed number, such as the complement 10000000). In this way, the elements of each big top heap construction are still 31 data;

[0064] (3) Repeat step 2, starting from the first construction of the max-heap. Each time you build it, you can get the maximum value of a data, output the data, and fill zeros in the array to be sorted. Finally, you get an ordered array.

[0065] The inter-group heap sort algorithm is optimized as follows:

[0066] The data in the 32nd storage unit of the memory RAM_PEi is always zero, and the data in the 0th storage unit is always 255 (ie, the maximum value of an 8-bit unsigned number).

[0067] When all the elements of the array have participated in the forward sort (sorting from large to small), the address pointer has pointed to the 32nd storage unit, so the address pointer is no longer modified, and the data 0 in the storage unit continues to participate in the sorting to build the big top heap until the sorting is completed. Similarly, when all the elements of the array have participated in the reverse sort (sorting from small to large), the address pointer has pointed to the 0th storage unit, so the address pointer is no longer modified, and the data 255 in the storage unit continues to participate in the sorting to build the small top heap until the sorting is completed.

[0068] Sorting within a group: 31 data are grouped into one array, and at most 7 groups of data are sorted in parallel in each clock cycle of the circuit. Each group of data is heap sorted within the group, and a maximum value is obtained in each clock cycle. After 31 sorts, a positive sorted array is obtained.

[0069] Inter-group merge sort: Inter-group merge sort has two processes: forward sort and reverse sort. These two processes are performed in parallel in some clock cycles, thereby shortening the sorting time.

[0070] When the maximum values ​​Max31_i in the seven groups of the grouped heap sorting module have all been written into the memory RAM_PEi, the inter-group merge sorting can be performed. Select the current maximum value data of each ordered array to build a max-heap, and the top element of the heap is the maximum value of these data. Modify the address pointer of the memory where the top element of the heap is located, take out the data of the next storage unit to replace this top element of the heap and rebuild the max-heap... and sort in a loop. If all the elements of the array have participated in the sorting, the address pointer has pointed to the 32nd storage unit, so the address pointer is no longer modified, and the data zero in the storage unit continues to participate in the sorting to build the max-heap until the sorting is completed.

[0071] The reverse sorting is opposite to the forward sorting. When the last sorting value in the 7 groups of the grouping heap sorting module, that is, the minimum value Max1_i, has been written into the memory RAM_PEi, the current minimum value data of each ordered array is selected to construct a small top heap, and the minimum value is obtained, and the sorting is cyclically carried out in sequence. If all the elements of the array have participated in the sorting, the address pointer has pointed to the 0th storage unit, so the address pointer is no longer modified, and the data 255 in the storage unit continues to participate in the sorting to construct the small top heap until the sorting is completed.

[0072] In the i-th (i=1, ...7) clock cycle, the i-th group of 31 data of external sampling data enters the PEi module. After being temporarily stored for 1 clock cycle, a big top heap is constructed. Starting from the i+2-th clock cycle, the maximum value, the second largest value, etc. in the group are output in turn.

[0073] In the i+2th clock cycle, that is, the 9th clock cycle, PE7 outputs the first maximum value Max31_7 (that is, the maximum value among the 31 data). After 1 clock cycle of temporary storage, {Max31_1, Max31_2, Max31_3, Max31_4, Max31_5, Max31_6, Max31_7} enter the secondary Max_M module in the merge heap sort module to build a big top heap. Starting from the 11th clock cycle, the maximum value and the second largest value of the final sorting result are output in turn... and written into the 1st to 123rd storage units of the memory RAM_out from the 1st port of the memory RAM_out.

[0074] In the i+32th clock cycle, that is, the 39th clock cycle, PE7 outputs the 31st maximum value Max1_7 (that is, the minimum value among the 31 data), and then after 1 clock cycle of temporary storage, {Max1_1, Max1_2, Max1_3, Max1_4, Max1_5, Max1_6, Max1_7} enter the secondary Min_M module in the merge heap sort module to build a small top heap, and from the 41st clock cycle, the minimum value and the second minimum value of the final sorting result are output in turn... and written into the 217th to 124th storage units of the memory RAM_out from the second port of the memory RAM_out.

[0075] In the i+126th clock cycle, that is, the 133rd clock cycle, the sorting data Max95 of the 123rd storage unit is written from the first port of the memory RAM_out. In the 134th clock cycle, the sorting data Max94 of the 124th storage unit is written from the second port of the memory RAM_out. So far, 217 data sortings have been completed.

[0076] The above method performs parallel intra-group sorting after data grouping, and then performs inter-group merge sorting. After inputting 7 groups of 217 data in the 7th clock cycle, all final sorting results can be obtained in the 134th clock cycle, which greatly reduces the sorting time and can significantly improve the sorting speed, thereby reducing the resources occupied by hardware (such as FPGA, field programmable gate array).

[0077] Embodiment 2:

[0078] A heap sorting system based on FPGA, comprising:

[0079] The grouping heap sorting module has y unit processors, each of which has a data selector Mux, a Heap sort_PEi module, a register Register_PEi and a memory RAM_PEi. The data selector Mux obtains the data to be sorted, selects the data output by the register Register_PEi, and uses the Heap sort_PEi module with a four-level Max_PE circuit structure to perform heap sorting on the x data in each group and store them in the memory RAM_PEi.

[0080] The merged heap sorting module includes a register Register_Max, a register Register_Min, a secondary Max_M module and a secondary Min_M module, which is used to merge the positive and negative sorting according to the data of the memory RAM_PEi in the grouped heap sorting module, and obtain the final sorting result and store it in the memory RAM_out;

[0081] The control module is used to coordinate the grouping heap sorting module and the merging heap sorting module.

[0082] This embodiment takes the sorting of 217 data as an example. After receiving all the data to be sorted, the serial heap sorting program needs about 1600 clock cycles to obtain all the final sorting results. This embodiment uses 7 groups to merge in parallel and then performs heap sorting. After inputting 7 groups of 217 data in the 7th clock cycle, all the final sorting results can be obtained in the 134th clock cycle, which greatly reduces the sorting time.

[0083] Considering the number of FPGA input and output points and economic cost, this embodiment sets the input data volume of the circuit to 31 8-bit unsigned numbers in one clock cycle. That is, the external sampling data is n-bit unsigned data, n=8; each x data is 1 group, x=31; in one clock cycle, the external sampling device inputs the circuit in parallel, and the circuit can receive y groups of x×y data in m clock cycles, m=7, y=7, x×y=217.

[0084] The above design idea has the advantages of both software and hardware implementation, the design is easy to implement, highly reusable, and improves the working efficiency of the circuit. Specifically: the circuit structure is as follows Figure 1 As shown, the circuit includes three parts: a grouping heap sorting module, a merging heap sorting module and a control module.

[0085] The external sampling data is 8-bit unsigned data, with 31 data as one group. It is input into the circuit in parallel by the external sampling device in one clock cycle. The circuit can receive 7 groups of 217 data in total in 7 clock cycles.

[0086] The grouped heap sorting module sorts the 7 groups of data in parallel from large to small, and outputs them to the merged heap sorting module. The merged heap sorting module merges the 7 groups of sorted data and sorts them in reverse order and outputs the final sorting results in sequence. The controller controller includes three modules: control_PE, control_Max and control_Min, which realize the control of the memory register read and write, address pointer and timing of the above components, so that the circuit works in coordination.

[0087] The circuit structure of the packet heap sorting module is as follows: Figure 2 As shown. There are 7 unit processors PEi (where i=1, 2…, 7) inside the packet heap sort module. Each PE has a two-to-one data selector Mux, a Heap sort_PEi module, a register Register_PEi and a memory RAM_PEi. The data selector Mux initially selects the external sampled data, and then selects the data output by the register Register_PEi.

[0088] The Heap sort_PEi module is a four-level Max_PE circuit structure that performs heap sorting on 31 data.

[0089] Register Register_PEi is a 248-bit data register that temporarily stores the data of the remaining nodes after the current group sorting. The lower 8 bits of register Register_PEi are always zero to ensure that the last node can be filled with zeros in each heap adjustment.

[0090] The storage capacity of RAM_PEi is 33*8 bits, and the dual ports can read and write simultaneously. The initial address pointers of both ports are 1. The data of the 32nd storage unit of RAM_PEi is always zero, and the data of the 0th storage unit is always 255 (that is, the maximum value of an 8-bit unsigned number).

[0091] The seven unit processors PEi can process seven groups of data in parallel, thereby reducing the time required for sorting and increasing the speed of sorting.

[0092] The control_PE module in the controller controls the reading and writing of the memory registers, the address pointer and the timing in the above modules to make the circuit work in a coordinated manner.

[0093] The internal structure of the Heap sort_PE module is as follows Figure 3 As shown in the figure. The Max_PE module is the basic unit of sorting. The input of each Max_PE module is three 8-bit unsigned data to be sorted; the output is three data, which are the maximum value of the three and the remaining two node data. According to the properties of the max-heap, the maximum value is used as a node, and the remaining two node data are used as the left and right child nodes of the maximum value. The outputs of the two Max_PE modules and the other data to be sorted are sent to the next Max_PE module. After the 31 data are sorted by a total of 15 Max_PE modules to form a 4-level Max_PE module (with 8, 4, 2 and 1 Max_PE modules respectively), the value of each node is greater than or equal to the value of its left and right child nodes, thus establishing a max-heap of 31 data in the group. The 4-level structure can output a maximum value in each clock cycle, which significantly reduces the number of comparisons, reduces the time complexity of sorting, and improves the real-time performance of sorting.

[0094] The circuit structure of the merged heap sort module is as follows Figure 1As shown. The merged heap sorting module includes register Register_Max, register Register_Min, secondary Max_M module and secondary Min_M module. Its input data is the data of 7 RAM_PEi in the grouped heap sorting module. After merging the data of 7 RAM_PEi in positive and negative order, the final ordering result is stored in the memory RAM_out.

[0095] The bit width of register Register_Max and register Register_Min is 56 bits. The initial address pointers of the second ports of the 7 RAM_PEs are all 1. The Max_M module is different from the Max_PE module. It inputs 3 8-bit unsigned data and outputs only 1 data - the maximum value of the three. The Min_M module is the opposite of the Max_M module. It inputs 3 8-bit unsigned data and outputs only 1 data - the minimum value of the three. The memory RAM_out is a dual-port RAM with a storage capacity of 217*8bit. The first port writes the output value of the secondary Max_M module, and the second port writes the output value of the secondary Min_M module.

[0096] The above components of the merged heap sorting module all include the control_Max module and the control_Min module of the controller controller.

[0097] The circuit structure of the three modules of the controller controller, control_PE, control_Max and control_Min, is as follows Figure 4 and Figure 5 shown.

[0098] The control_PE module includes a loop count controller, a read / write controller, a Mux selection controller, and an address pointer controller. The loop count controller outputs control signals to other components, the read / write controller generates read / write control signals for the first port of the memory RAM_PEi and the read / write control signals for the register Register_PEi, the Mux selection controller outputs the Mux selection signal, and the address pointer controller outputs the address pointer for the first port of RAM_PEi.

[0099] The circuit structures of the control_Max module and the control_Min module are similar, and both have control components (Note: the content in the following brackets corresponds to the control_Min module): the loop count controller outputs control signals to other components, the same maximum (minimum) value controller outputs control signals to the read-write controller and the address pointer controller, the read-write controller generates the read-write control signal of the 2nd (1) port of RAM_PEi, the read-write control signal of the 1st (2) port of RAM_out, and the read-write control signal of the register Register_Max (Register_Min), and the address pointer controller outputs the address pointer of the 2nd (1) port of RAM_PEi and the address pointer of the 1st (2) port of RAM_out.

[0100] The workflows of the control_Max module and the control_Min module are as follows: Figure 6 and Figure 7 The workflows of the two are basically the same, involving steps such as initialization, reading and sorting the data to be sorted, correcting the sorting results, outputting the maximum value of the sorting, and looping.

[0101] Initialization: Set the number of loops and make the RAM address pointer point to the maximum (minimum) value of each ordered array.

[0102] Read the data to be sorted and sort them: the maximum (minimum) value of each ordered array in the current group is temporarily stored in the register Register_Max (Register_Min). Then the register Register_Max (Register_Min) is sent to the 7 input terminals of the three Max_M (Min_M) modules, and after the maximum (minimum) value comparison, the maximum (minimum) value data of this sort is output.

[0103] Correct the sorting result: Since there may be multiple identical maximum (minimum) values ​​in the data currently involved in the comparison, the output result of this sorting is corrected. First, determine the number of identical maximum (minimum) values, and then correct the address pointers of the RAM_PE where these identical maximum (minimum) values ​​are located.

[0104] Output the remaining identical maximum (minimum) values: Set the number of loops according to the number of identical maximum (minimum) values, and continue to output the identical maximum (minimum) values.

[0105] By repeating the above steps, the result is output after a certain number of cycles.

[0106] The workflow of the control_PE module of the controller is as follows Figure 8 As shown, the steps involved are initialization, reading and sorting the data to be sorted, modifying the data path and address pointer and looping.

[0107] Initialization: Set the number of loops so that the RAM address pointer points to the first storage unit of each array, set the select end of the Mux data selector to 0, and allow external sampled data to enter the circuit.

[0108] Read the data to be sorted and sort it: After sorting, the maximum value in the current group is temporarily stored in the memory RAM_PEi, and the remaining node data is written to the register Register_PEi. Read the remaining node data and zero-filled data from the register Register_PEi, and prepare to enter the next intra-group parallel heap sorting module.

[0109] Modify the data path, address pointer and number of loops: The address pointer of RAM_PE is increased by 1 to point to the next storage unit. Set the select terminal of the Mux data selector to 1 to allow the data in register Register_PEi to enter the circuit. The number of loops is reduced by 1.

[0110] Repeat the above steps and output the result after a certain number of cycles.

[0111] The temporal and spatial relationship between the 7 parallel unit processing modules and the output sorting results is as follows: Fig. 9 shown.

[0112] In this embodiment, when the data amount n=217, all modules in the circuit are in an effective working state and have the highest efficiency.

[0113] In the i-th clock cycle, the unit processor PEi receives the i-th group of 31 data {data31_i, data30_i, ...data1_i} in parallel.

[0114] In the i+1th clock cycle, the array enters the heap sorting process of the Heap sort_PEi module for the first time, establishes a max-heap, and obtains the maximum value Max31_i in {data31_i, data30_i, ... data1_i}. Max31_i is stored in the first storage unit of the memory RAM_PEi, and the address pointer is increased by 1. At the same time, the remaining 30 data after this sorting are temporarily stored in the register Register_PEi, and together with the lower 8-bit data 00000000 of Register_PEi, they form 248-bit data, ready to be sent to the unit processor PEi to adjust the heap and establish the max-heap for the second time.

[0115] In the i+2th clock cycle, the data in register Register_PEi is sent to the Heap sort_PEi module for heap sorting, and the second largest value Max30_i in {data31_i, data30_i, ... data1_i} is obtained. Max30_i is stored in the second storage unit of memory RAM_PEi, and the address pointer is increased by 1. At the same time, the remaining 29 data are temporarily stored in register Register_PEi. These 29 data already include the 8-bit data 00000000 that entered the loop sorting in the previous iteration. They and the lower 8-bit data 00000000 of register Register_PEi still form 248-bit data, which are ready to be sent to PEi for adjusting the heap and establishing the max-heap for the third time.

[0116] By analogy, in subsequent clock cycles, 31 data {Max31_i, Max30_i, ...Max1_i} can be obtained in sequence after being sorted from large to small in the group.

[0117] In the 9th clock cycle, PE7 outputs the first maximum value Max31_7, {Max31_1, Max31_2, Max31_3, Max31_4, Max31_5, Max31_6, Max31_7} enters the merged heap sorting module, and then after 1 clock cycle of register Register_Max temporary storage, merge the secondary Max_M module in the heap sorting module, build a big top heap for these 7 data, and get the maximum value Max217 of the final sorting result (that is, the maximum value among 217 data). In the 11th clock cycle, the data is written to the first storage unit from the first port of the memory RAM_out.

[0118] The data Max217 and the 7*8-bit data {Max31_1, Max31_2, Max31_3, Max31_4, Max31_5, Max31_6, Max31_7} temporarily stored in the register Register_Max enter the same maximum value controller of the control module control_Max for the same comparison. The number of the same maximum values ​​is determined according to the comparison result, and the number value is input into the other three control components in the controller control_Max.

[0119] If the number of identical maximum values ​​is 0, it means that there is only one maximum value in the data currently involved in the comparison. The read-write controller of the controller control_Max sends a read signal to the second port of RAM_PE, preparing to send new data to the register Register_Max of the merged heap sort module for comparison.

[0120] If the number of the same maximum values ​​is not 0, it means that there are multiple data participating in the comparison that are the same as the current maximum value, and the second port of RAM_PE does not need to send new data. The read-write controller of the controller control_Max sends a read signal to the register Register_Max, reads 7 data into the Max_M module, and in the subsequent clock cycle, the same maximum value is repeatedly written from the first port of the memory RAM_out to the subsequent storage unit. Among them, the number of repetitions is controlled by the cycle number controller, and the storage unit address is controlled by the address pointer controller.

[0121] After all the same maximum value outputs are completed, the loop count controller modifies the loop count and prepares new data to enter the next inter-group sorting.

[0122] In the subsequent clock cycles to the 133rd clock cycle, the above process is repeated to obtain the subsequent maximum value, and the XXth to 123rd storage cells of RAM_out are written in sequence from the 1st port of RAM_out.

[0123] In the 39th clock cycle, PE7 outputs the 31st maximum value Max1_7 (i.e. the minimum value among the 31 data), and then after 1 clock cycle of temporary storage, {Max1_1, Max1_2, Max1_3, Max1_4, Max1_5, Max1_6, Max1_7} enter the secondary Min_M module in the merge heap sort module. Similar to the positive order merge sort process, a small top heap is constructed, and from the 41st clock cycle, the minimum value and the second minimum value of the final sorting result are output in sequence... and written into the 217th to 124th storage units of RAM_out in sequence from the second port of RAM_out.

[0124] from Fig. 9 It can be seen that starting from the 41st clock cycle, the forward and reverse merge sorts are performed simultaneously. The first port of RAM_out is written with the result of the forward sort, and the second port is written with the result of the reverse sort, thus shortening the merge sort time.

[0125] In summary, the implementation method of the sorting circuit of this embodiment can obtain an ordered sequence with a relatively small number of clock cycles, which can significantly improve the sorting speed. This design method is also applicable to the design of other numbers of data sorting circuits, and has a significant effect in improving the sorting speed.

[0126] The above are only preferred embodiments of the present invention and are not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.

Claims

1. A heap sorting system based on FPGA, characterized in that: include: The grouped heap sorting module has y unit processors, each of which has a data selector Mux, a Heapsort_PEi module, a register Register_PEi and a memory RAM_PEi. The data to be sorted obtained by the data selector Mux is grouped with x data each, and y groups of x×y data are sorted within the group to obtain y groups of sorted arrays. The data output by the register Register_PEi is selected, and the Heap sort_PEi module with a four-level Max_PE circuit structure is used to perform heap sorting on the x data in each group and store them in the memory RAM_PEi. The merged heap sorting module includes a register Register_Max, a register Register_Min, a secondary Max_M module and a secondary Min_M module, which are used to merge forward and reverse sorting according to the data in the memory RAM_PEi in the grouped heap sorting module, and use the obtained y group sorting arrays to perform forward sorting; during the forward sorting, when there is only one data left in any group of the obtained y group sorting arrays, reverse sorting is performed to obtain the final sorting result and store it in the memory RAM_out; Specifically, the maximum value in each group of sorted arrays is selected to construct a max-heap structure, in which the top element of the heap is the maximum value of the x×y data, and the remaining data is taken out to replace the current top element of the heap to reconstruct the max-heap structure, and the second largest value of the x×y data is obtained. After iterative cycles, the positive sorted data part of the x×y data is obtained; Select the minimum value in each group of sorted arrays to construct a small top heap structure, in which the top element of the heap is the minimum value of the x×y data, take out the remaining data to replace the current top element of the heap and rebuild the small top heap structure to obtain the second smallest value of the x×y data, and iterate in sequence to obtain the reversed sorted data part of the x×y data; Associating the positive sorting data part and the reverse sorting data part to obtain the final sorting result; A control module, used for coordinating the grouping heap sorting module and the merging heap sorting module; Among them, taking each x data as a group, y groups of x×y data are sorted in the group respectively to obtain y groups of sorted arrays, specifically: the data to be sorted is constructed into a big top heap structure, and the root node of the current heap is the largest element in the data to be sorted; the remaining nodes and data zero are used to reconstruct the big top heap structure, and the elements constructed each time are still x data; through repeated iterations, the maximum value of a data is obtained each time, the data is output and zero is added to the array to be sorted, and finally an array sorted from large to small is obtained, which is the required sorted array; The data to be sorted is constructed into a big top heap structure, specifically: The first level input is three data to be sorted, and the output is the maximum value among the three and the remaining two node data. The maximum value output is used as a node, and the remaining two node data are the left and right child nodes of the maximum value. The input of the next stage is the two maximum values ​​output by the previous stage and another data to be sorted; By analogy, after multiple levels of processing, the value of each node is greater than or equal to the values ​​of its left and right child nodes, resulting in a max-heap structure of x data in the group.

2. A FPGA-based heap sorting system as claimed in claim 1, characterized in that: The Heap sort_PEi module is a multi-level Max_PE circuit structure with multiple groups of Max_PE modules; The input of the first-level Max_PE module is three data to be sorted, and the output is the maximum value among the three and the remaining two node data. The maximum value output is used as a node, and the remaining two node data are the left and right child nodes of the maximum value; The input of the next-level Max_PE module is the two maximum values ​​output by the two Max_PE modules of the previous level and another data to be sorted; By analogy, after multi-level Max_PE module heap sorting, the value of each node is greater than or equal to the values ​​of its left and right child nodes, and a large top heap structure of x data in the group is obtained.

3. A FPGA-based heap sorting system as claimed in claim 1, characterized in that: The data of the mth storage unit of the memory RAM_PEi is always zero, and the data of the 0th storage unit is always the maximum value; When all the elements of the array have participated in the positive sort, that is, sorted from large to small, the address pointer has pointed to the mth storage unit, and the address pointer is no longer modified. The data zero in the storage unit continues to participate in the sorting to construct the max-heap until the sorting is completed; When all the elements of the array have participated in the reverse sorting, that is, sorted from small to large, the address pointer has pointed to the 0th storage unit, and the address pointer is no longer modified. The maximum value of the data in the storage unit continues to participate in the sorting to construct the small top heap until the sorting is completed.

4. A FPGA-based heap sorting system as claimed in claim 1, characterized in that: In the i-th clock cycle, the x-th data in the y-th group of data to be sorted are sorted within the group, and a max-heap is constructed after being temporarily stored. Starting from the i+2-th clock cycle, the maximum value, the second largest value, and so on in the group are output in sequence until a sorted array is formed; Where i=y.

5. The FPGA-based heap sorting system according to claim 1, wherein: In the i+2th clock cycle, the maximum value in each group of sorted arrays enters the secondary Max_M module in the merge heap sort module to build a big top heap. Starting from the i+4th clock cycle, the maximum value, the second largest value, etc. of the final sorting result are output in sequence, which is the positive sorting data part, and the obtained sorting data is written in sequence from the first port of the memory RAM_out.

6. A FPGA-based heap sorting system as claimed in claim 1, characterized in that: In the i+32th clock cycle, there is only one data left in one group of sorted arrays. After temporary storage, the data in each group of sorted arrays enters the secondary Min_M module in the merged heap sort module to build a small top heap. Starting from the i+34th clock cycle, the minimum value, the second minimum value, etc. of the final sorting result are output in sequence, which is the reverse sorted data part, and the obtained sorted data is written in sequence from the second port of the memory RAM_out.

7. The FPGA-based heap sorting system according to claim 1, wherein: In the i+126th clock cycle, the positive sorted data written from the first port of the memory RAM_out is extracted, and in the i+127th clock cycle, the reverse sorted data written from the second port of the memory RAM_out is extracted, and the final sorting result of x×y data is obtained after association.