Data sorting method, electronic device, storage medium, and computer program product
By using multiple processing units of a parallel processor to perform local sorting and hierarchical merge sort on the data, the problem of low efficiency of the traditional TopK operator under large data volumes is solved, and efficient TopK data filtering is achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- SHANGHAI BIREN TECH CO LTD
- Filing Date
- 2025-11-19
- Publication Date
- 2026-05-05
AI Technical Summary
Traditional TopK operators require global sorting of all data when dealing with massive amounts of data and relatively small K values, resulting in low computational efficiency and wasted resources.
The parallel processor performs local sorting of the data to be processed in parallel by multiple processing units, and performs one or more merge sorts depending on the amount of data and hardware resources. It only filters and merges local target data to avoid global sorting.
It significantly improves the efficiency and performance of TopK operations, especially in scenarios with a huge amount of data and a relatively small K value, thus reducing the waste of computing resources.
Smart Images

Figure CN121166069B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of artificial intelligence technology, and in particular to a data sorting method, electronic device, storage medium, and computer program product. Background Technology
[0002] The TopK operator (returns the top K values) is a common operator in data processing. Traditional TopK operator implementations typically involve globally sorting all the data to be processed and then selecting the top K values from the sorted results as the final result. However, when the data volume is huge and the value of K is relatively small, this method involves globally sorting all the data, which includes a large number of unnecessary sorting operations, resulting in low computational efficiency and wasted computing resources. Summary of the Invention
[0003] This invention provides a data sorting method, an electronic device, a storage medium, and a computer program product to address the deficiencies in related technologies.
[0004] This invention provides a data sorting method, comprising the following steps:
[0005] The parallel processor uses multiple processing units to perform local sorting of the data to be processed in parallel. Each processing unit obtains a portion of the data to be processed, sorts the obtained portion of the data, and selects a preset number of data as local target data.
[0006] Based on the amount of data to be processed and the hardware resources of the parallel processor, the local target data obtained by each processing unit is subjected to one or more merge sort operations, and the preset number of data is selected from the merge sorted data as global target data.
[0007] According to a data sorting method provided by the present invention, the step of performing one or more merge sorting operations on the local target data obtained by each processing unit based on the data volume of the data to be processed and the hardware resources of the parallel processor includes:
[0008] The local target data of each processing unit is merged and sorted at the first merging level, and the preset number of data is selected from the merged and sorted data.
[0009] Based on the amount of data to be processed and the hardware resources of the parallel processor, the data selected at the first merging level is selectively merged and sorted at a higher merging level, and the preset number of data is selected from the merged and sorted data until the global target data is obtained.
[0010] According to a data sorting method provided by the present invention, the merging hierarchy is determined based on the following steps:
[0011] Obtain the maximum amount of data that can be processed at each level;
[0012] When the amount of data to be processed is less than or equal to the upper limit of the amount of data to be processed at a certain level, each level from the local sorting level to the certain level will be determined as the merging level.
[0013] According to a data sorting method provided by the present invention, the parallel processor is a graphics processor and the processing unit is a thread;
[0014] The merge sort operation includes at least one of the following:
[0015] Merge and sort the local target data of multiple threads at the thread bundle level;
[0016] Merge and sort the data after merging multiple thread bundles at the thread block level;
[0017] At the device level, merge and sort the data after merging multiple thread blocks.
[0018] According to a data sorting method provided by the present invention, each of the plurality of processing units obtains a portion of data from the data to be processed, and each processing unit sorts the obtained portion of data and selects a preset number of data as local target data, including:
[0019] Each processing unit obtains 2K data points from the data to be processed, where K is the preset number;
[0020] Sort the first K data points in ascending order and the last K data points in descending order to obtain a bitonic sequence;
[0021] Perform a bitonic merge operation on the bitonic sequence to separate the K largest data points from the K smallest data points in the bitonic sequence;
[0022] The sorting operation is performed only on the K data points with the larger values to obtain the local target data.
[0023] According to a data sorting method provided by the present invention, the step of continuing the sorting operation only on the K data with larger values to obtain the local target data includes:
[0024] The subsequences at odd-numbered positions in the target sequence are sorted in ascending order, and the subsequences at even-numbered positions are sorted in descending order. The target sequence is a sequence consisting of the K largest data points.
[0025] The bitonic merge operation is recursively performed on each subsequence until the K data in each subsequence are arranged in ascending or descending order. The target sequence after the bitonic merge operation is then used as the local target data.
[0026] According to a data sorting method provided by the present invention, the step of performing local sorting of the data to be processed in parallel by multiple processing units of a parallel processor further includes:
[0027] Determine whether the preset quantity is a power of 2;
[0028] If the preset quantity is not a power of 2, then the preset quantity is rounded up to the smallest power of 2 value that is greater than or equal to the preset quantity.
[0029] According to a data sorting method provided by the present invention, selecting a preset number of data points as global target data from the merged sorted data includes:
[0030] Select a predetermined number of rounded data points from the merged and sorted data as the initial target data;
[0031] A preset number of data points are selected from the initial target data as the global target data.
[0032] The present invention also provides a data sorting device, comprising the following modules:
[0033] A local sorting unit is used to perform local sorting of data to be processed in parallel by multiple processing units of a parallel processor. Each of the multiple processing units obtains a portion of the data to be processed, sorts the obtained portion of data, and selects a preset number of data as local target data.
[0034] The global sorting unit is used to perform one or more merge sorting operations on the local target data obtained by each processing unit according to the amount of data to be processed and the hardware resources of the parallel processor, and select the preset number of data from the merged and sorted data as global target data.
[0035] The present invention also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement any of the data sorting methods described above.
[0036] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the data sorting method as described above.
[0037] The present invention also provides a computer program product, including a computer program that, when executed by a processor, implements any of the data sorting methods described above.
[0038] The data sorting method, electronic device, storage medium, and computer program product provided by this invention achieve rapid TopK filtering of massive amounts of data through parallelized local sorting and hierarchical merge sorting. Because this invention only performs local sorting on a portion of the data within each processing unit, and only processes local target data filtered from the previous stage—data whose quantity is much smaller than the original data—during the merge stage, it avoids the enormous computational overhead of globally sorting all the data to be processed. This significantly improves the efficiency and performance of TopK operations, especially in scenarios with a huge amount of data and a relatively small K value. Attached Figure Description
[0039] To more clearly illustrate the technical solutions in this invention or related technologies, the accompanying drawings used in the description of the embodiments or related technologies will be briefly introduced below. Obviously, the accompanying drawings described below are some embodiments of this invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0040] Figure 1 This is a flowchart illustrating the data sorting method provided by the present invention.
[0041] Figure 2 This is a flowchart illustrating another data sorting method provided by the present invention.
[0042] Figure 3 This is a flowchart illustrating the bitone merging operation provided by the present invention.
[0043] Figure 4 This is a schematic diagram of the data sorting device provided by the present invention.
[0044] Figure 5 This is a schematic diagram of the structure of the electronic device provided by the present invention. Detailed Implementation
[0045] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.
[0046] With the rapid development of data processing technology, data sorting has become a fundamental operation in the field of data processing. The TopK operator, as a commonly used data filtering operator, is used to quickly obtain the top K maximum or minimum values from massive amounts of data, and has wide applications in search engines, recommendation systems, data analysis, and other application scenarios.
[0047] Traditional TopK operator implementations typically employ a sorting algorithm to first arrange all the original data into an ordered sequence, and then extract the K largest data points as the TopK output. For example, the traditional bitonic sort algorithm performs a merge sort operation on all the data, and through multiple rounds of comparisons and swaps, finally obtains a globally ordered data sequence, from which the top K data points are then extracted.
[0048] However, the traditional TopK algorithm has significant performance drawbacks. Because this method requires a full sort of all data, while TopK operations only need to consider the first K data points, sorting the remaining data is unnecessary. Especially when the data size is large and the value of K is relatively small, performing sorting and merge sort operations on all data obviously wastes a lot of computational resources, resulting in low algorithm efficiency and making it unsuitable for applications with high real-time requirements.
[0049] In response, this invention provides a data sorting method that aims to optimize the performance of TopK operations and improve data processing efficiency by sorting only the necessary data and avoiding global sorting of all data.
[0050] in, Figure 1 This is a flowchart illustrating the data sorting method provided by the present invention, as shown below. Figure 1 As shown, the method includes steps 110 and 120.
[0051] Step 110: The data to be processed is sorted locally in parallel by multiple processing units of the parallel processor. Each processing unit obtains a portion of the data to be processed, sorts the obtained portion of data, and selects a preset number of data as local target data.
[0052] Here, a parallel processor can be understood as a hardware device capable of executing multiple computational tasks simultaneously. It can be, but is not limited to, a graphics processing unit (GPU), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or a central processing unit (CPU) containing multiple computing cores. This embodiment preferably uses a GPU because it has a massive number of computing cores, making it particularly suitable for executing such highly parallel data processing tasks. Correspondingly, multiple processing units can be understood as independent computing cores or execution threads within a parallel processor. For example, in a GPU, a processing unit can be a streaming multiprocessor (SM) or its compute units (CUs), etc.
[0053] The data to be processed can be understood as the original dataset that requires TopK operations. To achieve parallel processing, this dataset needs to be distributed among multiple processing units. As an optional implementation, the data to be processed can be distributed among the processing units using a preset data partitioning strategy. For example, if there are N data points and P processing units, a uniform partitioning approach can be used, allocating N / P data points to each processing unit.
[0054] After each processing unit obtains the portion of data it is responsible for processing, it performs a local sort on this portion of data. This local sort can be any known sorting algorithm, such as quicksort, mergesort, heapsort, etc., and this embodiment does not impose a specific limitation on it. The purpose of sorting is to select the candidate data most likely to become the global TopK result from this portion of data.
[0055] After local sorting is complete, each processing unit selects a preset number of data points from its sorting results as local target data. This "preset number" is a key parameter, and its value should be at least equal to the K value of the final TopK value to be obtained. For example, if the final goal is to obtain the Top 100 data (i.e., K=100), then each processing unit needs to select at least the first 100 data points from its local sorting results. In a preferred embodiment, to ensure that potential global TopK data is not lost due to uneven data distribution during subsequent merging, this preset number can be set to a value greater than K, such as K+M, where M is the redundancy.
[0056] For example, suppose the data to be processed contains 1,000,000 numbers, and the goal is to obtain the Top 1024 (i.e., K=1024). The parallel processor used has 16 processing units. First, the 1,000,000 data points can be evenly distributed among the 16 processing units, with each unit receiving 62,500 data points. Then, each processing unit performs a local sort on its 62,500 data points and selects the top 1024 largest data points from the sorted results as its local target data. After execution, a total of 16 sets of local target data will be obtained, each containing 1024 data points.
[0057] Step 120: Based on the amount of data to be processed and the hardware resources of the parallel processor, perform one or more merge sort operations on the local target data obtained by each processing unit, and select a preset number of data from the merge sorted data as the global target data.
[0058] After step 110, multiple sets of locally ordered target data are obtained. This step aims to merge these locally ordered target data to find the global Top K result. The merge sort operation here can be understood as the process of merging two or more sorted sequences into an ordered sequence.
[0059] It should be noted that the merge sort in this step is flexible and can be performed once or multiple times. The number of executions depends on the amount of data to be processed and the hardware resources of the parallel processor.
[0060] As an alternative implementation, when the total amount of all local target data is small and can be fully accommodated and efficiently processed by the memory of a single processing unit or a small group of processing units, only one merge sort operation needs to be performed. For example, all 16 groups of local target data (a total of 16 × 1024 data points) in the example above can be collected, a large-scale merge sort can be performed, and then the top 1024 data points from the final sorting result can be selected as the global target data.
[0061] As an alternative implementation, when the total amount of local target data is very large, or when the architecture of the parallel processor is more suitable for hierarchical processing, multiple merge sort operations can be performed. For example, taking the 16 sets of local target data in the example above, a first round of merging can be performed. Specifically, the 16 sets of local target data can be divided into 8 data pairs, and the merging operation can be performed on these 8 data pairs in parallel using multiple processing units of the parallel processor. In each merging operation, a pair of local target data sequences containing 1024 data points is merged into an intermediate ordered sequence containing 2048 data points. In order to control the amount of data to be processed later, the first 1024 data points are immediately selected from this intermediate ordered sequence to form a new intermediate merge result containing 1024 data points. Since this process is performed in parallel, after the first round of merging, 8 such intermediate merge results are obtained. Then, a second round of merging is performed, that is, the 8 intermediate results are paired up and merged in parallel 4 times to obtain 4 results. This process continues until only one set of data remains.
[0062] After one or more merge sort operations, a final ordered sequence containing all potential global TopK candidate values is obtained. Finally, a preset number of data points are selected from this ordered sequence as the global target data. This preset number is the K value of the TopK values the user ultimately needs to obtain. For example, if the target is Top1024, the first 1024 data points are selected from the final merge sort result. Through the above steps, this embodiment can significantly improve the performance of TopK operations without sacrificing accuracy.
[0063] The data sorting method provided in this embodiment achieves rapid TopK filtering of massive datasets through parallelized local sorting and hierarchical merge sorting. Because this embodiment only performs local sorting on a portion of the data within each processing unit, and only processes local target data selected in the previous stage that is much smaller than the original data during the merge phase, it avoids the huge computational overhead of globally sorting all the data to be processed. This significantly improves the efficiency and performance of TopK operations, especially in scenarios with a huge total data volume and a relatively small K value.
[0064] It should be noted that the execution entity in this embodiment can be any electronic device or computing system capable of performing parallel data processing, such as a server, workstation, personal computer, embedded system, etc. The data sorting method in this embodiment, as a fundamental high-performance computing operation, can be widely applied in fields such as big data processing, search engines, recommendation systems, machine learning, and database systems.
[0065] Taking a recommendation system as an example, the data to be processed can be a recommendation list containing a large number of candidate products or content and their corresponding recommendation scores, generated by the system for the user. By sorting this list using the method described in the above embodiment, the top K products or content with the highest scores can be efficiently obtained, thereby enabling real-time recommendations to the user and improving the user experience.
[0066] Taking a search engine as an example, the data to be processed can be a massive number of web pages returned for a user's query and their corresponding relevance scores. By sorting the scores using the method described in the above embodiment, the top K most relevant web page results can be quickly selected and presented on the first page of search results, thereby significantly reducing search latency.
[0067] Taking machine learning as an example, the data to be processed can be feature importance scores calculated during model training or outlier detection in data analysis. Based on the method in the above embodiments, the most important Top K features can be quickly identified for feature selection, or the most anomalous Top K data points can be identified for fault diagnosis, thereby accelerating model iteration and data analysis processes.
[0068] Furthermore, the execution entity in this embodiment is preferably a GPU. By distributing the data to be processed to each processing unit to perform local sorting in parallel, and designing the subsequent merge operation as a hierarchical parallel structure, the large-scale parallel computing capabilities of such parallel processors can be fully utilized to achieve performance far exceeding that of traditional serial algorithms, thereby efficiently completing the TopK data sorting task.
[0069] After acquiring the local target data of each processing unit in parallel in step 110, the total amount of this data may still be very large, making it impossible to efficiently complete global merging in a single processing step. Therefore, this embodiment introduces the concept of hierarchical merging. Specifically, based on the amount of data to be processed and the hardware resources of the parallel processor, one or more merging and sorting operations are performed on the local target data acquired by each processing unit, including:
[0070] First, the local target data of each processing unit is merged and sorted at the first merging level, and a preset number of data are selected from the merged and sorted data.
[0071] Here, the first merge level can be understood as the first round of merge processing for the local target data in step 110. At this level, the P groups of local target data are grouped, and merge sort is performed in parallel on each group.
[0072] As an optional implementation, each merge operation can combine M groups of sorted data into a larger ordered group. For example, when M=2, it is a pairwise merge. This operation takes two sorted data sequences as input, compares the head elements of the two sequences one by one, puts the one with the larger value into the output sequence, and moves the pointers of the corresponding input sequences until all elements are merged into the output sequence. M can be set according to the hardware characteristics of the parallel processor.
[0073] Taking the scenario in the example above, 16 sets of local target data (each set containing 1024 data points) were obtained from 16 processing units. In the first merging level, if pairwise merging (M=2) is used, 8 parallel merging tasks can be started. Each task merges 2 sets of local target data into an ordered sequence containing 2048 data points. Then, from this ordered sequence of 2048 data points, the data with the largest preset number (e.g., 1024) is selected as the output of this merge. After the first merging level is completed, the original 16 sets of data are merged into 8 new ordered sets of data, each still with a length of 1024.
[0074] Next, based on the amount of data to be processed and the hardware resources of the parallel processor, the data selected in the first merging level is selectively merged and sorted in a higher merging level. A preset number of data are selected from the merged and sorted data until the global target data is obtained.
[0075] The "optional" here refers to whether a higher merge level is needed, depending on the number of data groups remaining after the previous merge level. If only one data group remains after the first merge level, the merge process ends, and there is no need to proceed to a higher level. If the number of remaining data groups is greater than one, then merging needs to continue.
[0076] Higher merge levels can be understood as further merging of the output of the previous merge level. This process is iterative, taking P' groups of data generated in the previous level as input, and performing grouping and parallel merging again until only one group of data remains.
[0077] Using the example above, the first merging level produces 8 sets of data. Since 8 > 1, a higher merging level (the second merging level) is needed. In the second merging level, these 8 sets of data are merged pairwise, with 4 merging tasks executed in parallel, producing 4 new ordered sets of data. Since 4 > 1, the process continues to the third merging level, merging the 4 sets of data into 2 sets. Since 2 > 1, the process then continues to the fourth merging level, finally merging the 2 sets of data into a single sequence containing 2048 data points. At this point, since only 1 set of data remains, the condition "until the global target data is obtained" is met. Finally, a preset number (i.e., K value, 1024) of data points are selected from this final ordered sequence to obtain the global target data.
[0078] This embodiment introduces a hierarchical merge mechanism, decomposing a large-scale merge problem into multiple hierarchical, small-scale parallel merge subproblems. Each level significantly reduces the number of data sets to be processed. Since each merge task processes a relatively small amount of data, the computational complexity and memory pressure of a single merge operation are effectively reduced, greatly improving the utilization of parallel processors and overall processing throughput, enabling efficient processing of TopK computation tasks with larger-scale data.
[0079] Considering that the size of the data to be processed varies in practical applications, complex multi-level merging can introduce unnecessary scheduling overhead for smaller data volumes. Therefore, this embodiment aims to provide an adaptive merging level determination mechanism that selects the optimal merging path based on the actual size of the data to be processed.
[0080] Specifically, in this embodiment, the merging level is determined based on the following steps:
[0081] First, obtain the maximum amount of data that can be processed at each level.
[0082] Here, each level can be understood as a pre-defined computational flow or processing pipeline with different processing capabilities for different data scales. The upper limit of the amount of data that can be processed is the maximum amount of data that each level can efficiently process. This upper limit is usually pre-calibrated or configured based on the hardware resources of the parallel processor. For example, the upper limit of the amount of data that a thread can process is 2K, the upper limit of the amount of data that a thread bundle can process is 2K×32, and the upper limit of the amount of data that a thread block can process is 2K×1024.
[0083] The amount of data to be processed is compared with the upper limit of the amount of data to be processed at each level. If the amount of data to be processed is less than or equal to the upper limit of the amount of data to be processed at a certain level, it indicates that the hardware resources corresponding to that level are sufficient to efficiently complete the entire sorting task. At this time, the levels from the local sorting level to a certain level are determined as the merge level.
[0084] For example, suppose the maximum amount of data that can be processed in the first level is 20GB, and the maximum amount of data that can be processed in the second level is 240GB. If the amount of data to be processed is 15GB, that is, 15GB < 20GB, it means that the hardware resources corresponding to the first level are sufficient to process all the data to be processed. Therefore, the first level is used as the merge level, that is, a global merge sort is performed in the first level to directly obtain the global target data.
[0085] If the amount of data to be processed is 100GB, i.e. 100GB > 20GB and 100GB < 240GB, it means that the amount of data to be processed exceeds the memory capacity of the first level, but can be fully accommodated in the memory corresponding to the second level. Therefore, the first and second levels are used as merging levels. After the local sorting is completed in the first level to obtain the local target data of each processing unit, the local target data obtained in the first level is merged and sorted in the second level to obtain the global target data.
[0086] This embodiment achieves fine-grained management and scheduling of computing resources by pre-defining different processing capability levels based on hardware resources and dynamically selecting the optimal processing path according to the actual size of the data to be processed during task execution. Because this embodiment selects a simpler, lower-cost single-pass merge path for small-scale data and a more scalable multi-level merge path for large-scale data, it significantly improves overall computing performance and resource utilization while ensuring the ability to handle data of various sizes.
[0087] In a preferred embodiment, the parallel processor described above is a graphics processing unit (GPU), and the processing unit is a thread in the GPU. The GPU can execute a large number of threads simultaneously. By treating each thread as a processing unit, the parallel processing potential of the GPU can be maximized, enabling the local sorting process in step 110 to be completed under extremely high concurrency.
[0088] Accordingly, GPU threads are typically organized into a hierarchical structure of warps and blocks. A warp usually refers to a group of threads executed simultaneously by the scheduler and is the basic unit of GPU execution. A block is a larger unit composed of one or more warps. All threads within a block can access the same high-speed on-chip shared memory and can perform fast synchronization. Utilizing this hardware characteristic, merge sort operations can be decomposed into at least one of the following hierarchical operations:
[0089] ① At the thread bundle level, the local target data of multiple threads are merged and sorted, i.e., the first round of merging is performed within the thread bundle. Specifically, all threads within the same thread bundle (e.g., 32 threads) work collaboratively. Each thread writes its local target data (e.g., K data items) into the shared memory of its respective thread block. Subsequently, these threads use an efficient parallel merge algorithm to merge and sort the local target data contributed by all threads within the thread bundle in the shared memory. Since the access latency of shared memory is much lower than that of global memory, this process can be completed extremely quickly. After merging, a preset number (e.g., K data items) of data are selected again from the results as the local target data of the thread bundle.
[0090] ② Merge and sort the data after merging multiple thread bundles at the thread block level. After merging at the thread bundle level is complete, a higher-level merge can be performed at the thread block level. Specifically, all thread bundles within the same thread block place their local target data in shared memory. Then, all threads within the thread block collaborate again to merge and sort this data. For example, if a thread block contains 8 thread bundles, and after merging at the thread bundle level, 8 sets of ordered data are obtained, these 8 sets of data are merged into one group at the thread block level, and a predetermined number of data points are selected from it as the local target result of the thread block.
[0091] ③ At the device level, merge sort is performed on the merged data of multiple thread blocks. After all thread blocks have completed their internal merge sort, a final round of merge sort is performed at the device level, and the top K data items globally, i.e., the global target data, are selected from the final sorted list.
[0092] This embodiment achieves collaborative optimization between the algorithm and hardware by deeply mapping the merge sort algorithm operations to the GPU's three-layer hardware architecture of thread bundles, thread blocks, and devices. Since the merge sort operations at the thread bundle and thread block levels are completed in high-speed shared memory, the number of read / write operations and data transfers to slow global memory are greatly reduced, significantly lowering memory access latency and further improving the overall execution efficiency and throughput of the algorithm.
[0093] in, Figure 2 This is a flowchart illustrating another data sorting method provided by the present invention, as shown below. Figure 2 As shown, in the initial stage of data sorting, multiple threads (thread 0, thread 1, ..., thread 31) acting as processing units perform local sorting of the data to be processed in parallel. Each thread (e.g., threads 0 to 31) obtains a 2K data block from the data to be processed, executes the local sorting algorithm, and selects the K data with the largest values as the local target data.
[0094] Then, at the thread bundle level, the local target data of multiple threads are merge sorted. Within each thread bundle (thread bundle 0, ..., thread bundle 31), multiple threads perform a merge sort operation on their respective K local target data to obtain the TopK result of that thread bundle.
[0095] Next, the merged data of multiple thread bundles is merged and sorted at the thread block level. Within each thread block (thread block 0, ..., thread block n), multiple thread bundles merge and sort the merged results obtained in the previous stage again, and the data is aggregated using shared memory to obtain the TopK result of that thread block.
[0096] Finally, at the device level, the merged data from multiple thread blocks is merged and sorted. The device (such as a graphics processor) collects the TopK results from all thread blocks from global memory, performs the final merge sort operation, and selects a preset number of data from the final merge sorted data as the global target data to complete the entire TopK operation.
[0097] Based on any of the above embodiments, this embodiment provides a specific and efficient implementation method for the local sorting and filtering process in step 110, which cleverly utilizes the principle of bitonic sorting. Specifically, each of the multiple processing units obtains a portion of data from the data to be processed, and each processing unit sorts the obtained portion of data and selects a preset number of data as local target data, including:
[0098] First, each processing unit obtains 2K data points from the data to be processed, where K is a preset number.
[0099] Here, K is the preset quantity, which is the number of local target data that needs to be filtered out in each processing unit. In this embodiment, each processing unit does not acquire an arbitrary number of data, but rather selectively acquires 2K data points as input for subsequent processing. For example, if the final requirement is to acquire the global Top 1024 (i.e., K=1024), then each processing unit will acquire 2×1024=2048 data points in this step.
[0100] Next, sort the first K data points in ascending order and the last K data points in descending order to obtain a bitonic sequence. A bitonic sequence can be understood as a sequence that is first monotonically increasing and then monotonically decreasing, or first monotonically decreasing and then monotonically increasing. A bitonic sequence can be constructed by dividing the 2K data points into two parts and sorting each part in opposite directions.
[0101] For example, if the obtained 8 data points are {3, 8, 1, 5, 9, 2, 7, 4}, and K=4, then the first K data points {3, 8, 1, 5}, after being sorted in ascending order, become {1, 3, 5, 8}, and the last K data points {9, 2, 7, 4}, after being sorted in descending order, become {9, 7, 4, 2}. Concatenating these two results in a bitonic sequence {1, 3, 5, 8, 9, 7, 4, 2}.
[0102] Then, a bitonic merge operation is performed on the bitonic sequence to separate the K largest values from the K smallest values. The bitonic merge operation transforms a bitonic sequence into a completely ordered sequence through a series of fixed comparison-exchange operations.
[0103] It should be noted that in this embodiment, for a bitonic sequence of length 2K, only one round of the comparison-exchange phase in the complete bitonic merge network needs to be executed. That is, the first K elements in the sequence will contain the K elements with larger values in the entire sequence, and the last K elements will contain the K elements with smaller values.
[0104] Specifically, this operation compares the i-th element in the sequence with the (i+K)-th element and swaps the larger element to the front based on the sorting objective (e.g., obtaining the maximum value). For example, for the bitonic sequence {1, 3, 5, 8, 9, 7, 4, 2} above, comparing (1, 9), (3, 7), (5, 4), and (8, 2) and swapping all the larger values to the front, the sequence becomes {9, 7, 5, 8, 1, 3, 4, 2}. It can then be observed that the first four elements {9, 7, 5, 8} are exactly the four largest elements among the original eight data points, while the last four elements {1, 3, 4, 2} are the four smallest elements.
[0105] Therefore, through the above operations, we can focus only on the K largest separated data points, meaning the K smallest data points are directly discarded without any further processing. At this point, only the K largest separated data points need to be considered. It's important to note that although these K largest data points are the largest, they are not inherently completely ordered. Therefore, a final sorting operation needs to be performed on these K data points to obtain a completely ordered sequence. This ordered sequence is the final local target data output by this processing unit.
[0106] This embodiment achieves rapid halving of candidate data by constructing a bitonic sequence and utilizing the single-separation property of the bitonic merge operation. Since this embodiment replaces the complete sorting of 2K data points with a single efficient parallel compare-swap operation, and instead performs the final sorting only on the selected K data points, it avoids the overhead of completely sorting twice the number of candidate data points, further improving the computational efficiency of the local sorting stage.
[0107] Figure 3 This is a flowchart illustrating the bimodal merge operation provided by the present invention, as shown below. Figure 3 As shown, firstly, the first K data points are sorted in ascending order, and the last K data points are sorted in descending order. Then, the two ordered sequences are concatenated to form a bitoneic sequence of length 2K that is first monotonically increasing and then monotonically decreasing.
[0108] Then, a bitonic merge operation is performed on the bitonic sequence generated in the previous step. This operation compares the i-th element with the (i+K)-th element in the sequence through a parallel "compare-swap" operation, swapping smaller values to the first half and larger values to the second half, thus separating the data. After this stage, the input 2K data points are separated into a first half containing the K smaller values and a second half containing the K larger values. Figure 3 The area covered by the cross symbol represents the first K data points with smaller values that have been separated out.
[0109] Since the sorting objective is to obtain the Top K data with the largest values, the smaller data is useless to the final result. Therefore, this embodiment omits all sorting operations on this part of the data, greatly saving computing resources and avoiding the performance waste caused by sorting all of this data.
[0110] The area not covered by the cross represents the K largest data points in the latter half of the separated data. Since these K data points are the candidate set of the final local target data, they need to be completely sorted to obtain the K largest data points, which are the final local target data produced by this processing unit.
[0111] Based on any of the above embodiments, the sorting operation is continued only on the K data points with larger values to obtain local target data, including:
[0112] Sort the subsequences at odd positions in the target sequence in ascending order and the subsequences at even positions in descending order. The target sequence is a sequence of K data points with relatively large values.
[0113] Recursively perform bitonic merge operations on each subsequence until the K data in each subsequence are arranged in ascending or descending order. Then, use the target sequence after the bitonic merge operation as the local target data.
[0114] Specifically, the target sequence can be logically divided into multiple subsequences of equal length. For example, if the target sequence length K is 1024, it can be divided into 512 subsequences of length 2, or 256 subsequences of length 4, etc.
[0115] The distinction between "subsequences at odd positions" and "subsequences at even positions" is determined based on their logical indices within the target sequence. For example, if K data points are divided into K / 2 subsequences of length 2, then the 1st, 3rd, 5th... subsequences at odd positions are sorted in ascending order; the 2nd, 4th, 6th... subsequences at even positions are sorted in descending order. After this process, the entire target sequence consists of multiple alternating ascending and descending sequences.
[0116] Next, the bitonic merge operation is recursively performed on each subsequence until the K data in each subsequence are arranged in ascending or descending order. The target sequence after the bitonic merge operation is then used as the local target data.
[0117] For example, assuming K=8 and the target sequence is {3, 7, 8, 1, 9, 4, 5, 2}, the target sequence is divided into four subsequences of length 2. The odd-numbered subsequences {3, 7} and {9, 4} are sorted in ascending order, becoming {3, 7} and {4, 9}. The even-numbered subsequences {8, 1} and {5, 2} are sorted in descending order, becoming {8, 1} and {5, 2}. The resulting sorted target sequence is {3, 7, 8, 1, 4, 9, 5, 2}.
[0118] Next, three rounds of bitonic merging operations are performed on the target sequence {3, 7, 8, 1, 4, 9, 5, 2}. In the first round, {3, 7, 8, 1} and {4, 9, 5, 2} are treated as two bitonic sequences of length 4 and merged separately to obtain two ordered sequences {1, 3, 7, 8} and {2, 4, 5, 9}, resulting in the second round's bitonic sequence {1, 3, 7, 8, 9, 5, 4, 2}. In the second round, the bitonic sequence {1, 3, 7, 8, 9, 5, 4, 2} is merged to obtain the third round's bitonic sequence {1, 3, 4, 2, 9, 5, 7, 8}. In the third round, the bitonic sequence is merged to finally obtain the completely ascending sequence {1, 2, 3, 4, 7, 8, 9}. This fully sorted sequence is then used as the local target data.
[0119] Based on any of the above embodiments, the data to be processed is locally sorted in parallel by multiple processing units of a parallel processor, further comprising:
[0120] Determine if the preset quantity is a power of 2;
[0121] If the preset quantity is not a power of 2, then the preset quantity will be rounded up to the smallest power of 2 value that is greater than or equal to the preset quantity.
[0122] Considering that parallel sorting algorithms have the highest computational efficiency when processing data volumes that are powers of 2, if the preset quantity is not a regular power of 2 value, it may lead to problems such as inconsistent data block sizes, the need to handle complex boundary conditions, and uneven computational load among parallel processing units during algorithm execution. This will complicate the algorithm's control logic and ultimately reduce the overall sorting performance. Therefore, this embodiment requires regularization preprocessing of the preset quantity before performing local sorting on the data to be processed.
[0123] Specifically, first, it is determined whether the preset quantity is a power of 2. If so, it indicates that the preset quantity itself meets the data scale requirements of an efficient parallel sorting algorithm, and the data to be processed can be directly sorted locally in parallel by multiple processing units of the parallel processor. If not, it indicates that the preset quantity does not meet the optimal computational scale. In this case, the preset quantity is rounded up to the smallest power of 2 greater than or equal to the preset quantity, and the adjusted value is used as the new preset quantity in subsequent sorting steps, thereby ensuring that the sorting algorithm can run in the most efficient way.
[0124] For example, suppose we need to obtain the top 1000 data points, meaning the initial preset quantity K=1000. First, we determine that 1000 is not a power of 2. In this case, we round 1000 up to the smallest power of 2 greater than or equal to 1000, resulting in 1024. This adjusts the preset quantity to 1024. Further, after sorting according to the adjusted preset quantity, each processing unit receives 1024 data points as the global target data. In this case, we can extract the largest K (i.e., 1000) data points from these 1024 data points as the final global target data.
[0125] This embodiment preprocesses the preset quantity K by judging and rounding it up to a power of 2 before performing local sorting, so that the data block sizes processed by subsequent parallel sorting and merging operations are uniform and consistent. This avoids the additional logical overhead and complexity caused by processing irregular data block sizes, ensures a balanced load of computing tasks on the parallel processor, and further improves the overall performance and efficiency of data sorting.
[0126] The preset quantity can be adjusted based on the following formula:
[0127]
[0128] in, This indicates the adjusted preset quantity. This indicates the preset quantity before adjustment. Indicates rounding up. Indicates base 2, The logarithm of .
[0129] Based on any of the above embodiments, selecting a preset number of data points from the merged and sorted data as global target data includes:
[0130] Select a predetermined number of rounded data points from the merged and sorted data as the initial target data;
[0131] Select a preset number of data points from the initial target data as the global target data.
[0132] Here, the rounded preset quantity is the value K' obtained by rounding the original specified preset quantity K up to the smallest power of 2. After the local sorting in step 110 and the merge sorting in step 120, a highly ordered sequence containing the globally optimal candidate values is obtained. This step first selects the first K' data from this sequence and defines this part of the data as the initial target data.
[0133] Since the initial target data obtained in the previous step is already ordered, we only need to extract the first K data points from the beginning of the initial target data. These K data points are the final global target data that needs to be returned to the user.
[0134] For example, if a user needs to retrieve the Top 1000 data (K=1000), since K is not a power of 2, K is rounded up, resulting in K' being 1024. After the merge sort is completed according to step 120, the global sort result is obtained. The first K' data points are selected from this result as the initial target data, and then the top K data points are extracted from the initial target data as the final global target data.
[0135] The data sorting apparatus provided by the present invention will be described below. The data sorting apparatus described below can be referred to in correspondence with the data sorting method described above.
[0136] Based on any of the above embodiments Figure 4 This is a schematic diagram of the data sorting device provided by the present invention, as shown below. Figure 4 As shown, the device includes:
[0137] The local sorting unit 410 is used to perform local sorting of the data to be processed in parallel by multiple processing units of the parallel processor. Each processing unit in the multiple processing units obtains a portion of the data to be processed, sorts the obtained portion of the data, and selects a preset number of data as local target data.
[0138] The global sorting unit 420 is used to perform one or more merge sorting operations on the local target data obtained by each processing unit according to the amount of data to be processed and the hardware resources of the parallel processor, and select a preset number of data from the merged and sorted data as global target data.
[0139] Figure 5 This is a schematic diagram of the structure of the electronic device provided by the present invention, such as... Figure 5As shown, the electronic device may include a processor 510, a communications interface 520, a memory 530, and a communication bus 540, wherein the processor 510, the communications interface 520, and the memory 530 communicate with each other through the communication bus 540. The processor 510 can call logical instructions in the memory 530 to execute a data sorting method.
[0140] Furthermore, the logical instructions in the aforementioned memory 530 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to related technologies, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0141] On the other hand, the present invention also provides a computer program product, which includes a computer program that can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer is able to execute the data sorting methods provided by the above methods.
[0142] In another aspect, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to perform the data sorting methods provided by the methods described above.
[0143] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.
[0144] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the parts that contribute to the related technology, can be embodied in the form of software products. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.
[0145] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A data sorting method, characterized in that, include: Determine if the preset quantity is a power of 2; If the preset quantity is not a power of 2, then the preset quantity is rounded up to the smallest power of 2 value that is greater than or equal to the preset quantity. Multiple processing units of a parallel processor perform local sorting of the data to be processed in parallel. Each processing unit obtains 2K data points from the data to be processed, where K is the preset number. The first K data points are sorted in ascending order, and the last K data points are sorted in descending order to obtain a bitonic sequence. Only one round of the comparison and exchange phase of the complete bitonic merge network is performed on the bitonic sequence to ensure that the first K data points in the bitonic sequence contain the K data points with larger values in the entire sequence, and the last K data points contain the K data points with smaller values. The sorting operation is continued only on the K data points with larger values to obtain the local target data. Based on the amount of data to be processed and the hardware resources of the parallel processor, the local target data obtained by each processing unit is subjected to one or more merge sort operations, and the preset number of data is selected from the merge sorted data as global target data.
2. The data sorting method according to claim 1, characterized in that, The step of performing one or more merge sort operations on the local target data obtained by each processing unit based on the data volume of the data to be processed and the hardware resources of the parallel processor includes: The local target data of each processing unit is merged and sorted at the first merging level, and the preset number of data is selected from the merged and sorted data. Based on the amount of data to be processed and the hardware resources of the parallel processor, the data selected at the first merging level is selectively merged and sorted at a higher merging level, and the preset number of data is selected from the merged and sorted data until the global target data is obtained.
3. The data sorting method according to claim 2, characterized in that, The merging hierarchy is determined based on the following steps: Obtain the maximum amount of data that can be processed at each level; When the amount of data to be processed is less than or equal to the upper limit of the amount of data to be processed at a certain level, each level from the local sorting level to the certain level will be determined as the merging level.
4. The data sorting method according to any one of claims 1 to 3, characterized in that, The parallel processor is a graphics processor, and the processing unit is a thread; The merge sort operation includes at least one of the following: Merge and sort the local target data of multiple threads at the thread bundle level; Merge and sort the data after merging multiple thread bundles at the thread block level; At the device level, merge and sort the data after merging multiple thread blocks.
5. The data sorting method according to claim 1, characterized in that, The step of continuing the sorting operation only on the K data points with the larger values to obtain the local target data includes: The subsequences at odd-numbered positions in the target sequence are sorted in ascending order, and the subsequences at even-numbered positions are sorted in descending order. The target sequence is a sequence consisting of the K largest data points. The bitonic merge operation is recursively performed on each subsequence until the K data in each subsequence are arranged in ascending or descending order. The target sequence after the bitonic merge operation is then used as the local target data.
6. The data sorting method according to any one of claims 1 to 3, characterized in that, The process of locally sorting the data to be processed in parallel using multiple processing units of a parallel processor also includes: Determine whether the preset quantity is a power of 2; If the preset quantity is not a power of 2, then the preset quantity is rounded up to the smallest power of 2 value that is greater than or equal to the preset quantity.
7. The data sorting method according to claim 6, characterized in that, The step of selecting the preset number of data points as global target data from the merged and sorted data includes: Select a predetermined number of rounded data points from the merged and sorted data as the initial target data; A preset number of data points are selected from the initial target data as the global target data.
8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and running on the processor, characterized in that, When the processor executes the computer program, it implements the data sorting method as described in any one of claims 1 to 7.
9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the data sorting method as described in any one of claims 1 to 7.
10. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the data sorting method as described in any one of claims 1 to 7.
Citation Information
Patent Citations
Polar code decoding method, decoder and computer storage medium
CN111404558A
High-performance parallel implementation device for K-NN on GPU processor
CN112380003A
Comparator network-based topK data parallel acquisition method and device
CN117215780A