A fast sorting method and device based on FPGA

By dividing the data to be sorted into multiple data blocks and using RAM cache, combined with parallel processing technology, the problem of high resource consumption of traditional FPGA sorting methods is solved, and rapid sorting and performance improvement of very large data are achieved.

CN114416020BActive Publication Date: 2024-10-15ZG TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202210019642.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-01-10
Publication Date
2024-10-15
Estimated Expiration
2042-01-10

AI Technical Summary

Technical Problem

Traditional FPGA-based parallel processing to implement sorting requires a large amount of FPGA resources and cannot adapt to the sorting of very large data, resulting in a sharp increase in costs.

Method used

The data to be sorted is divided into multiple data blocks and processed in parallel through multiple comparison units. RAM is used for data caching and address control to reduce the input complexity of the comparison unit, simplify the FPGA implementation structure, and reduce resource consumption.

Benefits of technology

It achieves rapid sorting of very large data, reduces FPGA resource consumption, improves sorting algorithm performance, and achieves the best balance between speed and area.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114416020B_ABST
    Figure CN114416020B_ABST
Patent Text Reader

Abstract

The present invention discloses a fast sorting method and device based on FPGA, the method comprising: writing n to-be-sorted data into a to-be-sorted sequence into RAM for storage; dividing the to-be-sorted sequence into multiple data blocks of size m; reading the to-be-sorted data in a data block from the RAM and storing them in m registers respectively; instantiating m comparison units, in each comparison unit, comparing the to-be-sorted data in the corresponding register with each to-be-sorted data in the to-be-sorted sequence in the RAM respectively, calculating the number of data in the to-be-sorted sequence that is smaller than / greater than the to-be-sorted data in the corresponding register, and obtaining the sequence number of the to-be-sorted data after being arranged in ascending / descending order in the to-be-sorted sequence; synthesizing the sequence numbers calculated by all comparison units, obtaining the sorting result of the to-be-sorted data in a data block; repeating the above sorting process, and obtaining the sorting result of the to-be-sorted sequence. The present invention realizes data sorting in combination with RAM, thereby reducing FPGA resource consumption.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention belongs to the technical field of data processing, and in particular relates to a fast sorting method and device based on FPGA. Background Art

[0002] With the development of information technology, the amount of data has also increased dramatically. The speed of data processing has seriously affected the response time of the system. Sorting algorithm is an essential basic data processing operation in the computer field. Sorting can make the data fully arranged in a certain order, reducing the time of subsequent operations.

[0003] At present, there have been many studies and technologies to improve the sorting algorithm to further improve performance. However, most of the improvements to this technology are based on the existing CPU hardware environment. On the one hand, it takes up CPU resources and affects system performance; on the other hand, the CPU-based improvement methods are all implemented serially, which has bottlenecks and cannot significantly improve the performance of the sorting algorithm.

[0004] Traditional CPU-based sorting algorithms are not only slow, but also occupy CPU system resources. In recent years, many FPGA-based sorting algorithms or systems have emerged, which make full use of the parallel processing capabilities of FPGAs to greatly improve the speed of the algorithms and solve the shortcomings of traditional CPU-based sorting algorithms. However, the current FPGA-based sorting algorithms simply accelerate the sorting of traditional sorting algorithms through FPGA parallelization. Although it can improve the algorithm speed, it consumes a large amount of FPGA resources, increases the cost, and cannot adapt to the sorting of very large data. It is only suitable for applications with less data. For applications with a large amount of data to be sorted, such as the sorting of 4096 32-bit data commonly used in image processing, if the sorting is simply accelerated through parallel processing, a large amount of FPGA resources will be consumed, resulting in a sharp increase in costs. Summary of the invention

[0005] In view of this, the present invention proposes a fast sorting method and device based on FPGA, which is used to solve the problem that traditional FPGA-based parallel processing to achieve sorting requires consumption of a large amount of FPGA resources.

[0006] In a first aspect, the present invention discloses a fast sorting method based on FPGA, the method comprising:

[0007] S1, write n to-be-sorted data into a to-be-sorted sequence into RAM storage, record the address of each to-be-sorted data; divide the to-be-sorted sequence into multiple data blocks;

[0008] S2, reading the data to be sorted in a data block from the RAM, and storing them in multiple registers respectively;

[0009] S3, instantiate multiple comparison units, use the data to be sorted in each register as the first input of each comparison unit, and read the data to be sorted in the RAM in sequence according to the address of the data to be sorted as the second input of each comparison unit;

[0010] S4, in each comparison unit, respectively compare the data to be sorted in the corresponding register with each data to be sorted in the sequence to be sorted in the RAM, calculate the number of data in the sequence to be sorted that is smaller than / greater than the data to be sorted in the corresponding register, and use the number of data as the sequence number of the data to be sorted in the corresponding register after being arranged in ascending / descending order in the sequence to be sorted; and complete the sorting of the data to be sorted in a data block by combining the sequence numbers calculated by all comparison units;

[0011] S5. Repeat steps S2 to S4 to obtain the sorting result of the sequence to be sorted.

[0012] Based on the above technical solution, preferably, in step S1, dividing the sequence to be sorted into a plurality of data blocks specifically includes:

[0013] According to the size of the data to be sorted n and the specific application requirements, the sequence to be sorted is divided into There are data blocks of size m, m≤n, Indicates rounding up.

[0014] On the basis of the above technical solution, preferably, in step S3, the plurality of registers correspond to the plurality of comparison units one by one, each comparison unit has two inputs, and the data to be sorted data in the i-th register is i As the first input of the i-th comparison unit, the address of each data to be sorted in the RAM is obtained in sequence, and each data to be sorted in the RAM read out in sequence is used as the second input of the i-th comparison unit, i=1, 2, ..., m.

[0015] On the basis of the above technical solution, preferably, in step S4, in each comparison unit, the data to be sorted in the corresponding register is compared with each data to be sorted in the sequence to be sorted in the RAM, and the number of data in the sequence to be sorted that is smaller than the data to be sorted in the corresponding register is calculated, which specifically includes:

[0016] Let the data to be sorted in the register corresponding to the i-th comparison unit be data i ;

[0017] Using the time-sharing processing principle, the data to be sorted in the sequence to be sorted is read one by one by controlling the RAM address. j , j = 1, 2, ..., n, each time a data to be sorted is readj The data to be sorted, data, in the corresponding register i is compared with each data j respectively; when performing ascending sorting, count the number of data to be sorted in the sequence to be sorted that is less than data i ; when performing descending sorting, count the number of data to be sorted in the sequence to be sorted that is greater than data i .

[0018] Based on the above technical solution, preferably, the step S4 specifically includes:

[0019] Let the addresses of each data to be sorted in the RAM be addr1, addr2,..., addr n ;

[0020] At time t1, set the RAM address to addr1, then the output of the RAM is the first number data1 in the data to be sorted. If data i > data1, set res i-1 = 1; if data i < data1, set res i-1 = 0; if data i = data1, define res i-1 = 1 or res i-1 = 0 according to the actual application, and calculate sum i = res i-1 ;

[0021] At time t2, set the RAM address to addr2, then the output of the RAM is the second number data2 in the data to be sorted. If data i > data2, set res i-2 = 1; if data i < data2, set res i-2 = 0; if data i = data2, define res i-2 = 1 or res i-2 = 0 according to the actual application, and calculate sum i = sum i + res i-2 ;

[0022] At time t j , j = 3,..., n, set the RAM address to addr j , then the output of the RAM is the jth number data j in the data to be sorted. If data i > data j , set res i-j= 1; If data i <data j , let res i-j = 0; If data i = data j , define res i-j = 1 or res i-j = 0 according to the actual application, and calculate sum i = sum i + res i-j ;

[0023] sum i is the number of data less than data i in the to-be-sorted sequence, that is, the serial number of data i after the to-be-sorted sequence is sorted in ascending order.

[0024] Through m comparison units, calculate sum1, sum2,..., sum m corresponding to data1, data2,..., data m simultaneously, that is, the serial numbers of m data after the to-be-sorted sequence is sorted in ascending order, and the sorting of these m data is completed.

[0025] On the basis of the above technical solution, preferably, when performing descending sorting, step S4 specifically includes:

[0026] Set the addresses of each to-be-sorted data in the RAM as addr1, addr2,..., addr n ;

[0027] At time t1, set the RAM address to addr1, then the output of the RAM is the first number data1 in the to-be-sorted numbers. If data i < data1, let res i-1 = 1; If data i > data1, let res i-1 = 0; If data i = data1, define res i-1 = 1 or res i-1 = 0 according to the actual application, and calculate sum i = res i-1 ;

[0028] At time t2, set the RAM address to addr2, then the output of the RAM is the second number data2 in the to-be-sorted numbers. If data i < data2, let res i-2 = 1; If data i > data2, let resi-2 =0; if data i =data2, define res according to actual application i-2 =1 or res i-2 =0, calculate sum i =sum i +res i-2 ;

[0029] In t j At time j=3,…,n, let the RAM address be addr j , then the output of RAM is the jth number data in the number to be sorted j , if data i <data j , let res i-j =1; if data i >data j , let res i-j =0; if data i =data j , define res according to the actual application i-j =1 or res i-j =0, calculate sum i =sum i +res i-j ;

[0030] sum i That is, the calculated sequence to be sorted is greater than data i The number of data, that is, data i The serial number after the sequence to be sorted is arranged in descending order;

[0031] Through m comparison units, data1, data2, ..., data are calculated simultaneously. m Corresponding sum1, sum2, ..., sum m , that is, the sequence numbers of the m data after they are arranged in descending order in the sequence to be sorted, that is, the sorting of the m data in a data block is completed.

[0032] On the basis of the above technical solution, preferably, the step S5 specifically includes: repeating steps S2 to S4 to calculate the sum value corresponding to the remaining number nm, that is, all data1, data2, ..., data n Corresponding sum1, sum2, ..., sum n , get the serial numbers of all n data to be sorted after they are arranged in ascending / descending order in the sequence to be sorted, that is, the sorting of these n data is completed.

[0033] In a second aspect of the present invention, a fast sorting device based on FPGA is disclosed, the device comprising:

[0034] Data storage module: write n to-be-sorted data into a to-be-sorted sequence and store them in RAM, record the address of each to-be-sorted data; divide the to-be-sorted sequence into multiple data blocks;

[0035] Block reading module: reads the data to be sorted in a data block from RAM and stores them in multiple registers respectively;

[0036] Unit instantiation module: instantiate multiple comparison units, use the data to be sorted in each register as the first input of each comparison unit, and read the data to be sorted in the RAM in sequence as the second input of each comparison unit;

[0037] Data sorting module: In each comparison unit, the data to be sorted in the corresponding register is compared with each data to be sorted in the sequence to be sorted in the RAM, and the number of data in the sequence to be sorted that is smaller than / greater than the data to be sorted in the corresponding register is calculated, and the number of data is used as the sequence number of the data to be sorted in the corresponding register after being arranged in ascending / descending order in the sequence to be sorted; the sequence numbers output by all comparison units are combined to complete the sorting of the data to be sorted in a data block;

[0038] Circular comparison module: used to repeat the block reading module, unit instantiation module, and data sorting module to obtain the sorting result of the sequence to be sorted.

[0039] In a third aspect of the present invention, an electronic device is disclosed, comprising: at least one processor, at least one memory, a communication interface and a bus;

[0040] Wherein, the processor, memory, and communication interface communicate with each other via the bus;

[0041] The memory stores program instructions that can be executed by the processor, and the processor calls the program instructions to implement the method described in the first aspect of the present invention.

[0042] According to a fourth aspect of the present invention, a computer-readable storage medium is disclosed, wherein the computer-readable storage medium stores computer instructions, and the computer instructions enable a computer to implement the method described in the first aspect of the present invention.

[0043] Compared with the prior art, the present invention has the following beneficial effects:

[0044] 1) The present invention uses FPGA parallel processing to achieve fast sorting, which can be applied to the sorting of very large data and improve the performance of the sorting algorithm;

[0045] 2) The present invention combines RAM to implement data sorting. On the one hand, the data to be sorted can be cached, reducing the risk of FPGA timing non-convergence and improving the operating frequency; on the other hand, by controlling the RAM address, the purpose of controlling the input of the comparison unit is achieved, which greatly reduces the complexity of the input of the comparison unit, simplifies the FPGA implementation structure, and greatly reduces the consumption of logic resources such as the number of registers and wiring in common FPGA implementation algorithms, thereby reducing costs;

[0046] 3) The present invention adopts the principle of time-sharing processing, and divides the data to be sorted into multiple data blocks according to the size of the data amount n and the specific application requirements. Each data sorting cycle completes the sorting of the data in a data block, and the sorting of the entire data to be sorted is completed after multiple sorting cycles. The present invention can instantiate the number of comparison units according to the actual application situation and requirements, so as to accelerate the sorting while controlling the FPGA resource consumption, and achieve the best balance between speed and area. BRIEF DESCRIPTION OF THE DRAWINGS

[0047] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0048] Figure 1 The structural diagram of the traditional FPGA to implement the sorting of n data in parallel;

[0049] Figure 2 A schematic diagram of the structure of a quick sort based on FPGA implementation provided by an embodiment of the present invention;

[0050] Figure 3 A schematic diagram of input and output of a comparison unit provided in an embodiment of the invention;

[0051] Figure 4 A schematic flow chart of a quick sorting method based on FPGA implementation provided in an embodiment of the present invention. DETAILED DESCRIPTION

[0052] The following will be combined with the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0053] Traditional FPGAs need at least n comparison units, n data registers, and a large number (about n 2 ) between the register and the comparison unit, such as Figure 1 As shown, it only uses a simple parallel processing method to accelerate sorting, which consumes a lot of FPGA resources, increases costs, and cannot adapt to the sorting of ultra-large data.

[0054] The present invention proposes a fast sorting method based on FPGA, which improves the performance of the sorting algorithm and reduces the consumption of FPGA resources. Figure 2 The structure shown is composed of RAM (memory), m (m≤n) data registers (register) and m comparison units (Comparator).

[0055] The present invention proposes a fast sorting method based on FPGA implementation, which is mainly divided into the following steps:

[0056] S1. Store the data to be sorted

[0057] Write n data to be sorted into a sequence to be sorted and store them in RAM, record the address of each data to be sorted, and record the addresses as addr1, addr2…addr n . Divide the sequence to be sorted into n / m data blocks of size m. When n / m cannot be divided evenly, round n / m upwards to get data blocks, before The size of the first data block is m, and the size of the last data block is n%m. The sorting process of the last data block is the same as the previous one. The sorting process for each data block is the same.

[0058] S2. Read the data to be sorted in a data block

[0059] Read the data to be sorted in a data block from RAM (data1, data2...data m ), the total number of data to be sorted in the data block is m, which are stored in m registers respectively;

[0060] S3. Instantiate m comparison units

[0061] Instantiate m comparison units Comparator1, Comparator2, ..., Comparator m , register each register, namely register1, register2, ..., register m The data to be sorted in data1, data2, ..., data mAs the first input among the two inputs of each comparison unit, successively read each data to be sorted in the RAM according to the address of the data to be sorted, and use the output of the RAM as the second input of each comparison unit, as Figure 3 shown.

[0062] S4. Sort m data to be sorted

[0063] In each comparison unit, compare the data to be sorted (the first input) in the corresponding register with each data to be sorted (the second input) in the data sequence to be sorted in the RAM respectively, calculate the number of data in the data sequence to be sorted that is less than / greater than the data to be sorted in the corresponding register, and use the number of data as the serial number of the data to be sorted in the corresponding register after being arranged in ascending / descending order in the data sequence to be sorted; comprehensively calculate the serial numbers calculated by all comparison units to complete the sorting of the data to be sorted in a data block;

[0064] Take the working process (ascending order) of a single comparison unit Comparator i as an example:

[0065] At time t1, set the RAM address to addr1, then the output dataout of the RAM is the first number data1 in the data to be sorted. If data i > dataout (i.e., data1), set res i-1 = 1; if data i < dataout, set res i-1 = 0; if data i = dataout, res i-1 can be defined as 1 or res i-1 = 0 according to the actual application. Calculate sum i = res i-1 ;

[0066] At time t2, set the RAM address to addr2, then the output dataout of the RAM is the second number data2 in the data to be sorted. If data i > dataout (i.e., data2), set res i-2 = 1; if data i < dataout, set res i-2 = 0; if data i = dataout, res i-2 can be defined as 1 or res i-2 = 0 according to the actual application. Calculate sum i = sum i + res i-2 ;

[0067] ……

[0068] At time t j when the RAM address is addr j , the output dataout of the RAM is the j-th number data in the numbers to be sorted j . If data i > dataout (i.e., data j ), let res i-j = 1; if data i < dataout, let res i-j = 0; if data i = dataout, define res i-j = 1 or res i-j = 0 according to the actual application, and calculate sum i = sum i + res i-j ;

[0069] ……

[0070] At time t n when the RAM address is addr n , the output dataout of the RAM is the n-th number data in the numbers to be sorted n . If data i > dataout (i.e., data n ), let res i-n = 1; if data i < dataout, let res i-n = 0; if data i = dataout, res i-n = 1 or res i-n = 0 can be defined according to the actual application. Calculate sum i = sum i + res i-n .

[0071] Among them, j = 1, 2,..., n. Through the above process, the number sum i of data less than data i in the numbers to be sorted is calculated, that is, the serial number of data i after the numbers to be sorted are arranged in ascending order is found. Through m comparison units, the corresponding sum1, sum2,..., sum m of data1, data2,..., data m can be calculated simultaneously, that is, the serial numbers of m data after the numbers to be sorted are arranged in ascending order, which means the sorting of these m data is completed.

[0072] Similarly, if the data to be sorted is sorted in descending order, taking the working process of a single comparison unit Comparator i as an example:

[0073] At time t1, let the RAM address be addr1. Then the output dataout of the RAM is the first number data1 in the data to be sorted. If data i < dataout (i.e., data1), let res i-1 = 1; if data i > dataout, let res i-1 = 0; if data i = dataout, res i-1 can be defined as 1 or res i-1 = 0 according to the actual application. Calculate sum i = res i-1 ;

[0074] At time t2, let the RAM address be addr2. Then the output dataout of the RAM is the second number data2 in the data to be sorted. If data i < dataout (i.e., data2), let res i-2 = 1; if data i > dataout, let res i-2 = 0; if data i = dataout, res i-2 can be defined as 1 or res i-2 = 0 according to the actual application. Calculate sum i = sum i + res i-2 ;

[0075] ......

[0076] At time t j , let the RAM address be addr j . Then the output dataout of the RAM is the jth number data j in the data to be sorted. If data i < dataout (i.e., data j ), let res i-j = 1; if data i > dataout, let res i-j = 0; if data i = dataout, res i-j is defined as 1 or res i-j= 0, calculate sum i = sum i + res i-j ;

[0077] ……

[0078] At time t n , let the RAM address be addr n , then the output dataout of the RAM is the nth number data in the numbers to be sorted n , if data i < dataout (i.e., data n ), let res i-n = 1; if data i > dataout, let res i-n = 0; if data i = dataout, res can be defined as 1 or res i-n = 0 according to the actual application. Calculate sum i-n = sum i + res i + res i-n .

[0079] Among them, j = 1, 2,..., n. Through the above process, calculate the number sum i of the data greater than data i in the numbers to be sorted, that is, find the serial number of data i after the numbers to be sorted are arranged in descending order. Through m comparison units, the corresponding sum1, sum2,..., sum m of data1, data2,..., data m can be calculated simultaneously, that is, the serial numbers of m data after the numbers to be sorted are arranged in descending order, and the sorting of these m data is completed. S5. Repeat steps S2 to S4 to obtain the sorting result of the numbers to be sorted

[0080] Return to step S2 to read the next data block, and repeat steps S2 to S4 for a total of times to calculate the sum values corresponding to the remaining n - m numbers, that is, calculate all the corresponding sum1, sum2,..., sum n of data1, data2,..., data n , and obtain the serial numbers of all n data to be sorted after the numbers to be sorted are arranged in ascending / descending order, that is, complete the sorting of these n data

[0081] Figure 4The figure is a flow chart of the fast sorting method based on FPGA of the present invention, which corresponds to the above steps S1 to S5. The present invention adopts time-sharing processing to divide the input n data to be sorted into The output of RAM is used as an input of the comparison unit. By controlling the address of RAM, the purpose of controlling the input of the comparison unit is achieved. The m data in the data block are compared with the n data to be sorted, and the sequence number of the m data after being sorted in descending (or ascending) order among the n data to be sorted is obtained. In this way, the sorting of m data is completed each time, and the sorting results of all comparison units are combined to obtain the final sorting.

[0082] Corresponding to the above method embodiment, the present invention also proposes a fast sorting device based on FPGA, the device comprising:

[0083] Data storage module: write n to-be-sorted data into a to-be-sorted sequence into RAM storage, record the address of each to-be-sorted data; divide the to-be-sorted sequence into multiple data blocks of size m;

[0084] Block reading module: reads the data to be sorted in a data block from the RAM and stores them in m registers respectively;

[0085] Unit instantiation module: instantiate m comparison units, use the data to be sorted in each register as the first input of each comparison unit, and read the data to be sorted in the RAM in sequence as the second input of each comparison unit;

[0086] Data sorting module: In each comparison unit, the data to be sorted in the corresponding register is compared with each data to be sorted in the sequence to be sorted in the RAM, and the number of data in the sequence to be sorted that is smaller than / greater than the data to be sorted in the corresponding register is calculated, and the number of data is used as the sequence number of the data to be sorted in the corresponding register after being arranged in ascending / descending order in the sequence to be sorted; the sequence numbers output by all comparison units are combined to complete the sorting of the data to be sorted in a data block;

[0087] Circular comparison module: used to repeat the block reading module, unit instantiation module, and data sorting module to obtain the sorting result of the sequence to be sorted.

[0088] The above device embodiments and method embodiments correspond one to one, and for a brief description of the device embodiments, please refer to the method embodiments.

[0089] The technical solutions and effects of the present invention are described in detail below in conjunction with specific embodiments.

[0090] Taking a 3D scanner as an example, it needs to sort the processing intermediate values ​​corresponding to each row of image data in the image algorithm, that is, 4096 32-bit data. If a traditional CPU is used for sorting, it will consume a lot of time because the CPU can only complete the comparison of a small amount of data at a single time. If a simple FPGA parallel processing is used, 4096 32-bit comparison units need to be instantiated, and there are a large number of connections between the comparison units and the input data, which will consume a lot of FPGA resources.

[0091] Embodiment 1

[0092] This embodiment uses XCZU3EG as the control chip, instantiates 256 comparison units, each comparison unit has two inputs and one output, arranges the data to be sorted in ascending order, and completes the following operations:

[0093] If the first input is greater than the second input, the output res=1;

[0094] If the first input is less than the second input, the output res=0;

[0095] If the first input is equal to the second input, the RAM addresses corresponding to the first input and the second input are compared: if the RAM address corresponding to the first input is less than the RAM address corresponding to the second input, the output is res=1; otherwise, the output is res=0.

[0096] The specific implementation process is as follows:

[0097] Step 1: Store the data to be sorted into RAM in sequence, and their storage addresses are 0, 1, ..., 4095 respectively.

[0098] Step 2: Control the RAM read port address to 0, 1, ..., 255 respectively, that is, the first 256 data to be sorted (recorded as a1, a2, ..., a 256 ) are read out and stored in the register as the first input of 256 comparison units respectively.

[0099] This step takes 256 clks.

[0100] Step 3: Enable the first comparison unit, control the RAM read port address to 0, and use the RAM read port output as the second input of 256 comparison units, that is, connect the first input of the 4096 data to be sorted to the second input of the comparison unit. Compare and get res0. Sequentially control the RAM read port address to 1, 2, ..., 4095, and you can get res1, ..., res 4095 , let sum1=res0+res1+…+res 4095 , we can calculate the sequence number of a1 in the entire data to be sorted (4096 numbers) in ascending order.

[0101] At the same time, the remaining 255 comparison units can also calculate a2, a3, ..., a 256 The sequence number of the entire data to be sorted (4096 numbers) in ascending order.

[0102] This step takes 4096 clks.

[0103] Step 4: Analogously to step 2 and step 3, control the RAM read port address to 256~511, 512~767, ..., 3840~255 respectively, and perform 15 cycles in total to calculate the sum corresponding to the remaining 3840 data to be sorted 257 ,sum 258 ,…,sum 4096 , we can calculate the sequence numbers of the remaining 3840 data to be sorted in the entire data to be sorted (4096 numbers) in ascending order. So far, the sorting of the entire data to be sorted is completed.

[0104] The whole process takes (256+4096)*(4096 / 256)=69632 clks. When the clock frequency is 300Mhz, it takes about 69632*(1÷300Mhz)=232.1us to complete the sorting of 4096 32-bit data.

[0105] Embodiment 2

[0106] This embodiment uses XC7A200T as the control chip to achieve descending order of 64 8-bit data. 64 comparison units are instantiated, each comparison unit has two inputs and one output, and the data to be sorted is sorted in descending order to complete the following operations:

[0107] If the first input is greater than the second input, the output res=0;

[0108] If the first input is less than the second input, the output res=1;

[0109] If the first input is equal to the second input, the RAM addresses corresponding to the first input and the second input are compared: if the RAM address corresponding to the first input is less than the RAM address corresponding to the second input, the output is res=0; otherwise, the output is res=1.

[0110] The specific implementation process is as follows:

[0111] Step 1: Store the data to be sorted into RAM in sequence, and the storage addresses are 0, 1, ..., 63 respectively;

[0112] Step 2: Control the RAM read port addresses to 0, 1, ..., 63 respectively, that is, the data to be sorted (recorded as a1, a2, ..., a 64) are read out and stored in the register as the first input of 64 comparison units respectively.

[0113] This step takes 64 clks;

[0114] Step 3: Enable the comparison unit, control the RAM read port address to 0, and use the RAM read port output as the second input of the 64 comparison units, that is, connect the first input of the 64 data to be sorted to the second input of the comparison unit. Calculate res0. Sequentially control the RAM read port address to 1, 2, ..., 63, and you can get res1, ..., res 63 Let sum1 = res0 + res1 + ... + res 63 , we can calculate the sequence number of a1 in the entire data to be sorted (64 numbers) in descending order.

[0115] At the same time, the remaining 63 comparison units can also calculate a2, a3, ..., a 64 The sequence number of the entire data to be sorted (64 numbers) is arranged in descending order. So far, the sorting of 64 data to be sorted has been completed, and this step takes 64 clks.

[0116] The whole process takes 64+64=128 clks. When the clock frequency is 300Mhz, it takes about 128*(1÷300Mhz)=0.4267us to complete the sorting of 64 8-bit data.

[0117] The present invention also discloses an electronic device, comprising: at least one processor, at least one memory, a communication interface and a bus; wherein the processor, memory, and communication interface communicate with each other through the bus; the memory stores program instructions that can be executed by the processor, and the processor calls the program instructions to implement the aforementioned method of the present invention.

[0118] The present invention also discloses a computer-readable storage medium, wherein the computer-readable storage medium stores computer instructions, wherein the computer instructions enable the computer to implement all or part of the steps of the method described in the embodiment of the present invention. The storage medium includes: a USB flash drive, a mobile hard disk, a read-only memory ROM, a random access memory RAM, a magnetic disk or an optical disk, and other media that can store program codes.

[0119] The device / system embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be distributed to multiple network units. A person skilled in the art may select some or all of the modules according to actual needs to achieve the purpose of the solution of this embodiment without creative effort.

[0120] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the protection scope of the present invention.

Claims

1. A fast sorting method based on FPGA, characterized in that: The method comprises: S1, write n to-be-sorted data into a to-be-sorted sequence into RAM storage, record the address of each to-be-sorted data; divide the to-be-sorted sequence into multiple data blocks; S2, reading the data to be sorted in a data block from the RAM, and storing them in multiple registers respectively; S3, instantiate multiple comparison units, use the data to be sorted in each register as the first input of each comparison unit, and read the data to be sorted in the RAM in sequence according to the address of the data to be sorted as the second input of each comparison unit; S4, in each comparison unit, respectively compare the data to be sorted in the corresponding register with each data to be sorted in the sequence to be sorted in the RAM, calculate the number of data in the sequence to be sorted that is smaller than / greater than the data to be sorted in the corresponding register, and use the number of data as the sequence number of the data to be sorted in the corresponding register after being arranged in ascending / descending order in the sequence to be sorted; and complete the sorting of the data to be sorted in a data block by combining the sequence numbers calculated by all comparison units; S5. Repeat steps S2 to S4 to obtain the sorting result of the sequence to be sorted.

2. The fast sorting method based on FPGA implementation according to claim 1 is characterized in that: In the step S1, dividing the sequence to be sorted into a plurality of data blocks specifically includes: According to the size of the data to be sorted n and the specific application requirements, the sequence to be sorted is divided into data blocks of size m, m≤n, Indicates rounding up.

3. The fast sorting method based on FPGA according to claim 1 is characterized in that: In step S3, the plurality of registers correspond to the plurality of comparison units one by one, each comparison unit has two inputs, and the data to be sorted data in the i-th register is i As the first input of the i-th comparison unit, the address of each data to be sorted in the RAM is obtained in sequence, and each data to be sorted in the RAM read out in sequence is used as the second input of the i-th comparison unit, i=1,2,…,m, m is the number of data to be sorted in a read data block.

4. The fast sorting method based on FPGA implementation according to claim 3 is characterized in that: In the step S4, in each comparison unit, the data to be sorted in the corresponding register is compared with each data to be sorted in the sequence to be sorted in the RAM, and the number of data in the sequence to be sorted that is smaller than / greater than the data to be sorted in the corresponding register is calculated, specifically including: Let the data to be sorted in the register corresponding to the i-th comparison unit be data i ; Using the time-sharing processing principle, the data to be sorted in the sequence to be sorted is read one by one by controlling the RAM address. j , j = 1, 2, ..., n, each time a data to be sorted is read j , the data to be sorted in the corresponding register i Respectively with each data j For comparison; when sorting in ascending order, count the numbers in the sequence to be sorted that are smaller than data i The number of data to be sorted; when sorting in descending order, count the data in the sequence to be sorted that is greater than data i The number of data to be sorted.

5. The fast sorting method based on FPGA implementation according to claim 4 is characterized in that: When arranging in ascending order, step S4 specifically includes: Assume that the address of each data to be sorted in RAM is addr1, addr2, ..., addr n ; At time t1, let the RAM address be addr1. Then the output of the RAM is the first number data1 in the numbers to be sorted. If data i > data1, let res i-1 = 1; if data i < data1, let res i-1 = 0; if data i = data1, define res i-1 = 1 or res i-1 = 0 according to the actual application, and calculate sum i = res i-1 ; At time t2, let the RAM address be addr2. Then the output of the RAM is the second number data2 among the numbers to be sorted. If data i > data2, let res i-2 = 1; if data i < data2, let res i-2 = 0; if data i = data2, define res i-2 = 1 or res i-2 = 0 according to the actual application, and calculate sum i = sum i + res i-2 ; In t j At time j=3,…,n, let the RAM address be addr j , then the output of RAM is the jth number data in the number to be sorted j , if data i >data j , let res i-j =1; if data i <data j , let res i-j =0; if data i =data j , define res according to the actual application i-j =1 or res i-j =0, calculate sum i =sum i +res i-j ; sum i That is, the calculated sequence to be sorted is smaller than data i The number of data, that is, data i The serial number after the sequence to be sorted is arranged in ascending order; Through m comparison units, data1, data2, ..., data are calculated simultaneously. m Corresponding sum1, sum2, ..., sum m , that is, the sequence numbers of the m data after they are arranged in ascending order in the sequence to be sorted, that is, the sorting of the m data in a data block is completed.

6. The fast sorting method based on FPGA according to claim 4 is characterized in that: When descending arrangement is performed, the step S4 specifically includes: Assume that the address of each data to be sorted in RAM is addr1, addr2, ..., addr n ; At time t1, let the RAM address be addr1. Then the output of the RAM is the first number data1 among the numbers to be sorted. If data i < data1, let res i-1 = 1; if data i > data1, let res i-1 = 0; if data i = data1, define res i-1 = 1 or res i-1 = 0 according to the actual application, and calculate sum i = res i-1 ; At time t2, let the RAM address be addr2, then the output of the RAM is the second number data2 in the numbers to be sorted. If data i < data2, let res i-2 = 1; if data i > data2, let res i-2 = 0; if data i = data2, define res i-2 = 1 or res i-2 = 0 according to the actual application, and calculate sum i = sum i + res i-2 ; In t j At time j=3,…,n, let the RAM address be addr j , then the output of RAM is the jth number data in the number to be sorted j , if data i <data j , let res i-j =1; if data i >data j , let res i-j =0; if data i =data j , define res according to the actual application i-j =1 or res i-j =0, calculate sum i =sum i +res i-j ; sum i That is, the calculated sequence to be sorted is greater than data i The number of data, that is, data i The serial number after the sequence to be sorted is arranged in descending order; Through m comparison units, data1, data2, ..., data are calculated simultaneously. m Corresponding sum1, sum2, ..., sum m , that is, the sequence numbers of the m data after they are arranged in descending order in the sequence to be sorted, that is, the sorting of the m data in a data block is completed.

7. The fast sorting method based on FPGA implementation according to claim 5 or 6, characterized in that: The step S5 specifically includes: Repeat steps S2 to S4 to calculate the sum value corresponding to the remaining number nm, that is, all data1, data2, ..., data n Corresponding sum1, sum2, ..., sum n , get the serial numbers of all n data to be sorted after they are arranged in ascending / descending order in the sequence to be sorted, that is, the sorting of n data is completed.

8. A fast sorting device based on FPGA, characterized in that: The device comprises: Data storage module: write n to-be-sorted data into a to-be-sorted sequence and store them in RAM, record the address of each to-be-sorted data; divide the to-be-sorted sequence into multiple data blocks; Block reading module: reads the data to be sorted in a data block from RAM and stores them in multiple registers respectively; Unit instantiation module: instantiate multiple comparison units, use the data to be sorted in each register as the first input of each comparison unit, and read the data to be sorted in the RAM in sequence as the second input of each comparison unit; Data sorting module: In each comparison unit, the data to be sorted in the corresponding register is compared with each data to be sorted in the sequence to be sorted in the RAM, and the number of data in the sequence to be sorted that is smaller than / greater than the data to be sorted in the corresponding register is calculated, and the number of data is used as the sequence number of the data to be sorted in the corresponding register after being arranged in ascending / descending order in the sequence to be sorted; the sequence numbers calculated by all comparison units are combined to complete the sorting of the data to be sorted in a data block; Circular comparison module: used to repeat the block reading module, unit instantiation module, and data sorting module to obtain the sorting result of the sequence to be sorted.

9. An electronic device, characterized in that: include: at least one processor, at least one memory, a communication interface, and a bus; Wherein, the processor, memory, and communication interface communicate with each other via the bus; The memory stores program instructions that can be executed by the processor, and the processor calls the program instructions to implement the method according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and the computer instructions enable a computer to implement the method according to any one of claims 1 to 7.