Thread allocation method and device, storage medium and computer device
By allocating threads according to a preset strategy during initial runtime and adjusting thread allocation after a preset number of rounds, the problem of insufficient system resource utilization is solved, and system throughput and resource utilization are improved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- GUANGZHOU WERIDE TECH LTD CO
- Filing Date
- 2022-12-16
- Publication Date
- 2026-04-24
AI Technical Summary
In existing technologies, manually setting the concurrency level for each step leads to insufficient utilization of system resources and reduces system throughput.
By determining the target task and the total number of threads, the threads are allocated according to a preset strategy during the initial runtime, and the thread allocation is readjusted based on the execution status after a preset number of rounds, until the total number of threads is reached.
It improved resource utilization and overall system throughput, and reduced allocation errors caused by small initial data volume.
Smart Images

Figure CN115904724B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a thread allocation method, apparatus, storage medium, and computer device. Background Technology
[0002] Compared to traditional multi-threaded serial task execution, multi-threaded parallel computing can handle a given number of tasks or jobs more efficiently.
[0003] For example, the process of data anonymization involves many steps, such as downloading data, extracting images from videos, anonymizing, compressing images into videos, and uploading. Each step has different resource requirements and can utilize different concurrency levels. If the concurrency level for each step is manually set, changes in the environmental resources relied upon by each step during actual task execution may lead to insufficient utilization of system resources and reduced system throughput. Summary of the Invention
[0004] The purpose of this application is to at least solve one of the aforementioned technical defects, particularly the technical defect in the prior art where if the concurrency of each step is manually set, the system resources may not be fully utilized and the system throughput may be reduced due to changes in the environmental resources on which each step depends during the actual execution of the task.
[0005] This application provides a thread allocation method, the method comprising:
[0006] Determine the target task to be executed and the total number of threads to be allocated. The target task includes multiple subtasks to be executed concurrently, and the number of subtasks in the target task is not greater than the total number of threads.
[0007] During initial runtime, each subtask is assigned a thread at least once according to a preset first allocation strategy until the number of allocated threads reaches the total number of threads.
[0008] After the threads are allocated, the current execution round number of the target task is monitored. When the current execution round number reaches the preset round interval, the thread allocation operation is performed on each subtask at least once according to the preset second allocation strategy until the number of allocated threads reaches the total number of threads.
[0009] Optionally, the step of performing thread allocation operations on each subtask at least once according to a preset first allocation strategy until the number of allocated threads reaches the total number of threads includes:
[0010] Based on the number of subtasks in the target task, a single thread is allocated to each subtask, and after the thread allocation is completed, the first execution time of each subtask under a single thread is calculated.
[0011] Calculate the quotient of the number of threads allocated to each subtask and its corresponding first execution duration, and reallocate threads to each subtask according to the proportion of the quotients of all subtasks, until the number of allocated threads reaches the total number of threads.
[0012] Optionally, the step of re-allocating threads to each subtask at least once according to a preset second allocation strategy until the number of allocated threads reaches the total number of threads includes:
[0013] Get the number of threads allocated to each subtask after the thread allocation operation was performed in the previous round of number intervals, and the second execution time of each subtask under the corresponding number of threads;
[0014] Calculate the quotient of the number of threads allocated to each subtask and its corresponding second execution duration, and reallocate threads to each subtask according to the proportion of the quotients of all subtasks, until the number of allocated threads reaches the total number of threads.
[0015] Optionally, the step of reallocating threads to each subtask according to the proportion of the quotient of all subtasks until the number of allocated threads reaches the total number of threads includes:
[0016] Threads are reallocated to each subtask according to the ratio of the quotients of all subtasks, and after the thread allocation is completed, it is determined whether the number of currently allocated threads has reached the total number of threads.
[0017] If the target is reached, then stop the thread allocation operation;
[0018] If the target is not reached, sort the subtasks in reverse order according to their quotients, and assign a single thread to each subtask from top to bottom according to the sorting order and the current number of remaining threads.
[0019] If the remaining number of threads has been allocated, then the thread allocation operation stops.
[0020] If the remaining number of threads has not been fully allocated, then return to the step of allocating a single thread to each subtask in order of sorting and the remaining number of threads from top to bottom.
[0021] This application also provides a thread allocation device, including:
[0022] The task and thread determination module is used to determine the target task to be executed and the total number of threads to be allocated. The target task includes multiple sub-tasks to be executed concurrently, and the number of sub-tasks in the target task is not greater than the total number of threads.
[0023] The thread allocation module is used to perform at least one thread allocation operation on each subtask according to a preset first allocation strategy during initial runtime, until the number of allocated threads reaches the total number of threads.
[0024] The thread rebalancing module is used to monitor the current execution round number of the target task after the thread allocation is completed, and when the current execution round number reaches a preset round interval, to re-allocate the thread to each subtask at least once according to a preset second allocation strategy, until the number of allocated threads reaches the total number of threads.
[0025] Optionally, the thread allocation module includes:
[0026] The first duration determination module is used to allocate a single thread to each subtask based on the number of subtasks in the target task, and after the thread allocation is completed, to calculate the first execution duration of each subtask under a single thread.
[0027] The first thread allocation submodule is used to calculate the quotient of the number of threads allocated to each subtask and its corresponding first execution duration, and to reallocate threads to each subtask according to the proportion of the quotients of all subtasks, until the number of allocated threads reaches the total number of threads.
[0028] Optionally, the thread rebalancing module includes:
[0029] The second duration determination module is used to obtain the number of threads allocated to each subtask after performing the thread allocation operation in the previous round of number intervals, and the second execution duration of each subtask under the corresponding number of threads;
[0030] The second thread allocation submodule is used to calculate the quotient of the number of threads allocated to each subtask and its corresponding second execution duration, and to reallocate threads to each subtask according to the proportion of the quotients of all subtasks, until the number of allocated threads reaches the total number of threads.
[0031] Optionally, the first thread allocation submodule or the second thread allocation submodule includes:
[0032] The reallocation module is used to reallocate threads to each subtask according to the ratio of the quotient of all subtasks, and after the thread allocation is completed, it determines whether the number of currently allocated threads has reached the total number of threads.
[0033] The first stop allocation module is used to stop thread allocation operations if the target is reached.
[0034] The first reverse allocation module is used to sort the subtasks in reverse order according to the quotient of each subtask if the target is not reached, and then allocate a single thread to each subtask from top to bottom according to the sorting order and the current number of remaining threads.
[0035] The second stop allocation module is used to stop the thread allocation operation if the current remaining number of threads has been allocated.
[0036] If the remaining number of threads has not been allocated, the second reverse allocation module returns to the step of allocating a single thread to each subtask in order of sorting and the remaining number of threads from top to bottom.
[0037] This application also provides a storage medium storing computer-readable instructions that, when executed by one or more processors, cause the one or more processors to perform the steps of the thread allocation method as described in any of the above embodiments.
[0038] This application also provides a computer device, including: one or more processors, and memory;
[0039] The memory stores computer-readable instructions, which, when executed by the one or more processors, perform the steps of the thread allocation method as described in any of the above embodiments.
[0040] As can be seen from the above technical solutions, the embodiments of this application have the following advantages:
[0041] The thread allocation method, apparatus, storage medium, and computer device provided in this application, when determining the target task to be executed and the total number of threads to be allocated, since the target task contains multiple concurrently executed subtasks and the number of subtasks is no greater than the total number of threads, can perform thread allocation operations on each subtask at least once according to a preset first allocation strategy during the initial system runtime, until the number of allocated threads reaches the total number of threads. This allows for real-time adjustment of the concurrency based on the specific execution status of each subtask during actual task execution, thereby improving resource utilization and overall system throughput. Furthermore, to avoid allocation errors caused by uneven data size, this application can pre-set a round interval and, after thread allocation is completed, monitor the current execution round number of the target task. When the current execution round number reaches the preset round interval, perform thread allocation operations on each subtask at least once again according to a preset second allocation strategy, until the number of allocated threads reaches the total number of threads. This not only solves the problem of small data volume and large errors during initial runtime but also further improves resource utilization. Attached Figure Description
[0042] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0043] Figure 1 A flowchart illustrating a thread allocation method provided in an embodiment of this application;
[0044] Figure 2 This is a schematic diagram of a multi-round allocation process for thread allocation provided in an embodiment of this application;
[0045] Figure 3 This is a schematic diagram of the structure of a thread allocation device provided in an embodiment of this application;
[0046] Figure 4 This is a schematic diagram of the internal structure of a computer device provided in an embodiment of this application. Detailed Implementation
[0047] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0048] Compared to traditional multi-threaded serial task execution, multi-threaded parallel computing can handle multiple given tasks or jobs more efficiently. For example, the process of data anonymization involves many steps, such as downloading data, extracting images from videos, anonymizing, compressing images into videos, and uploading. Each step has different resource requirements and a different number of concurrent connections. If the concurrency of each step is manually set, changes in the environmental resources relied upon by each step during actual task execution may lead to insufficient utilization of system resources and reduced system throughput. Based on this, this application proposes the following technical solution, as detailed below:
[0049] In one embodiment, such as Figure 1 As shown, Figure 1 This is a flowchart illustrating a thread allocation method provided in an embodiment of this application; this application provides a thread allocation method, which may include:
[0050] S110: Determine the target task to be executed and the total number of threads to be allocated.
[0051] In this step, when computing a given task in parallel, the target task to be executed and the total number of threads to be allocated can be determined first. Then, the target task can be computed in parallel based on the total number of threads.
[0052] Specifically, the target task in this application includes multiple concurrently executed subtasks, and the number of subtasks is no greater than the total number of threads. This ensures that each subtask can be assigned a corresponding thread, and also allows for the allocation of more threads to subtasks that require more thread processing, thereby effectively improving resource utilization.
[0053] For example, when the target task is a data desensitization task, this application can configure the corresponding number of threads according to the number of subtasks in the target task, or according to the expected processing time or task type of the target task, as long as the total number of configured threads is not less than the number of subtasks of the target task, other restrictions are not imposed here.
[0054] S120: During initial runtime, each subtask is assigned a thread at least once according to the preset first allocation strategy until the number of allocated threads reaches the total number of threads.
[0055] In this step, after determining the target task to be executed and the total number of threads to be allocated through S110, this application can perform thread allocation operation on each subtask at least once according to the preset first allocation strategy when the system is initially running, until the number of allocated threads reaches the total number of threads.
[0056] It is understood that since the total number of threads in this application is not less than the number of subtasks of the target task, when the total number of threads in this application is the same as the number of subtasks, a thread allocation operation can be performed on each subtask once. When the total number of threads in this application is greater than the number of subtasks, a thread allocation operation can be performed on each subtask more than once. When allocating threads, a preset first allocation strategy can be used to perform thread allocation operations until the cumulative number of allocated threads reaches the total number of threads.
[0057] In one specific implementation, this application can first allocate the same number of threads to each subtask. If there are still remaining threads, the number of threads allocated to each subtask in the next thread allocation can be determined based on the task execution time of each subtask. In this way, after multiple thread allocations, each subtask can be allocated a matching number of threads, thereby effectively improving resource utilization.
[0058] S130: After the thread allocation is completed, monitor the current execution round number of the target task, and when the current execution round number reaches the preset round interval, perform thread allocation operation on each subtask at least once according to the preset second allocation strategy until the number of allocated threads reaches the total number of threads.
[0059] In this step, during the initial system run in S120, each subtask is allocated at least once according to the preset first allocation strategy until the number of allocated threads reaches the total number of threads. This application can also continue to monitor the current execution round of the target task, and when the current execution round reaches the preset round interval, each subtask is re-allocated at least once according to the preset second allocation strategy until the cumulative number of allocated threads reaches the total number of threads.
[0060] It should be noted that, because the amount of data is relatively small during the initial runtime of this application when allocating threads for each subtask, the thread allocation error may be relatively large. Therefore, to reduce allocation error, this application can perform thread rebalancing operations at regular intervals to avoid allocation errors caused by differences in data size.
[0061] Specifically, after thread allocation is completed during the initial runtime, this application can continue to monitor the current execution round number of the target task. Assuming each subtask in the target task completes one round, this application can pre-set the corresponding round interval, such as setting it as a power of the round number, a power of 1, or other methods; no limitation is imposed here. Once the round interval is set, when the current execution round number reaches the preset interval, each subtask will be re-allocated at least once according to the preset second allocation strategy until the number of allocated threads reaches the total number of threads. For example, this application can redistribute threads according to a power of the round number, i.e., redistributing threads again in the second, fourth, eighth, ... rounds. This effectively solves the problem of small initial data volume and large errors.
[0062] In this application, when performing at least one thread allocation operation on each subtask according to the second allocation strategy, the number of threads for each subtask in the next allocation can be determined according to the number of threads currently allocated to each subtask and the execution time of each subtask. If the number of threads has been allocated, the process continues to wait for the next number of intervals before reallocating threads. If the number of threads has not been allocated, threads can continue to be allocated to each subtask according to the execution time of each subtask until the cumulative number of allocated threads reaches the total number of threads.
[0063] In the above embodiments, after determining the target task to be executed and the total number of threads to be allocated, since the target task contains multiple concurrently executed subtasks and the number of subtasks is no greater than the total number of threads, during the initial system runtime, this application can perform thread allocation operations on each subtask at least once according to a preset first allocation strategy until the number of allocated threads reaches the total number of threads. This allows for real-time adjustment of the concurrency based on the specific execution status of each subtask during actual task execution, thereby improving resource utilization and overall system throughput. Furthermore, to avoid allocation errors caused by uneven data size, this application can pre-set a round interval and monitor the current execution round of the target task after thread allocation is completed. When the current execution round reaches the preset round interval, each subtask is re-allocated at least once according to a preset second allocation strategy until the number of allocated threads reaches the total number of threads. This not only solves the problem of small data volume and large errors during initial runtime but also further improves resource utilization.
[0064] In one embodiment, S120, which involves performing thread allocation operations on each subtask at least once according to a preset first allocation strategy until the number of allocated threads reaches the total number of threads, may include:
[0065] S121: Allocate a single thread to each subtask based on the number of subtasks in the target task, and after the thread allocation is completed, calculate the first execution time of each subtask under a single thread.
[0066] S122: Calculate the quotient of the number of threads allocated to each subtask and its corresponding first execution duration, and reallocate threads to each subtask according to the proportion of the quotients of all subtasks, until the number of allocated threads reaches the total number of threads.
[0067] In this embodiment, when allocating threads, the thread allocation operation can be performed according to a preset first allocation strategy, and the allocation will continue until the cumulative number of allocated threads reaches the total number of threads.
[0068] For example, this application can first allocate the same number of threads to each subtask. If there are still remaining threads, the number of threads allocated to each subtask in the next thread allocation can be determined based on the task execution time of each subtask. In this way, after multiple thread allocations, each subtask can be allocated a matching number of threads, thereby effectively improving resource utilization.
[0069] In one specific implementation, this application can allocate a single thread to each subtask based on the number of subtasks in the target task, so that the system can execute each subtask concurrently through a single thread. Then, this application can calculate the first execution time of each subtask under a single thread, and calculate the quotient of the number of threads allocated to each subtask and its corresponding first execution time. In this way, the corresponding number of threads can be reallocated to each subtask according to the proportion of the quotient of all subtasks, until the number of allocated threads reaches the total number of threads.
[0070] For example, when the target task in this application has five subtasks, namely A, B, C, D, and E, and the total number of threads is 10, during the initial run, this application can allocate a single thread to each subtask and calculate the actual execution time of each subtask: A(2s)->B(2s)->c(2s)->D(2s)->E(2s). Then, this application can allocate threads according to the ratio between the number of threads in each subtask and the actual execution time, such as A(1 / 2s)->B(1 / 2s)->c(1 / 2s)->D(1 / 2s)->E(1 / 2s). After allocating according to the ratio and rounding, the result is 2:2:2:2:2, which means that two threads are allocated to each subtask. At this point, the threads are just allocated.
[0071] In one embodiment, step S130, which involves re-allocating threads to each subtask at least once according to a preset second allocation strategy until the number of allocated threads reaches the total number of threads, may include:
[0072] S131: Obtain the number of threads allocated to each subtask after the thread allocation operation was performed in the previous round of number intervals, and the second execution time of each subtask under the corresponding number of threads.
[0073] S132: Calculate the quotient of the number of threads allocated to each subtask and its corresponding second execution duration, and reallocate threads to each subtask according to the proportion of the quotients of all subtasks, until the number of allocated threads reaches the total number of threads.
[0074] In this embodiment, because the amount of data is relatively small during the initial runtime when allocating threads for each subtask, the thread allocation error may be relatively large. Therefore, to reduce allocation error, this application can perform thread rebalancing operations at regular intervals to avoid allocation errors caused by differences in data size.
[0075] Specifically, after the initial thread allocation is completed during runtime, this application can continue to monitor the current execution rounds of the target task. Assuming that each subtask in the target task is completed in one round, this application can pre-set the corresponding round interval. When the current execution round reaches the preset round interval, the application performs thread allocation operations on each subtask at least once according to the preset second allocation strategy until the number of allocated threads reaches the total number of threads. For example, this application can redistribute threads according to the exponent of the round, i.e., the second, fourth, eighth rounds, ... redistribute threads again. When redistributing threads, the application can first obtain the number of threads allocated to each subtask after the thread allocation operation in the previous round interval and the second execution duration of each subtask under the corresponding number of threads. Then, the application calculates the quotient between the number of threads of each subtask and its corresponding second execution duration, and redistributes threads to each subtask according to the proportion of the quotient of all subtasks until the number of allocated threads reaches the total number of threads, thereby effectively solving the problem of small data volume and large error at the beginning.
[0076] For example, when the target task in this application has five subtasks, namely A, B, C, D, and E, and the total number of threads is 10, during the initial runtime, this application can allocate a single thread to each subtask and calculate the actual execution time of each subtask: A(2s)->B(2s)->c(2s)->D(2s)->E(2s). Then, this application can allocate threads according to the ratio between the number of threads and the actual execution time of each subtask, such as A(1 / 2s)->B(1 / 2s)->c(1 / 2s)->D(1 / 2s)->E(1 / 2s). After allocating according to the ratio and rounding, it becomes 2:2:2:2:2, which means that two threads are allocated to each subtask. At this point, the threads are just allocated, and this application can start the current thread allocation. The thread allocation process, as the thread allocation operation under the first round interval, when the current execution round of the target task reaches the preset round interval, this application can obtain the number of threads allocated to each subtask under the previous round interval after the thread allocation operation was performed under the previous round interval, as well as the second execution time of each subtask under the corresponding number of threads, such as A(4s)->B(2s)->c(3s)->d(2s)->E(4s). The ratio of the quotients of all subtasks is 2 / 4:2 / 2:2 / 3:2 / 2:2 / 4=3:6:4:6:3. Threads are allocated according to the ratio and rounded to 1:2:1:2:1. After allocation according to this ratio, there are still 3 threads remaining. At this time, the thread allocation operation can continue to be performed until the cumulative number of allocated threads reaches the total number of threads.
[0077] In one embodiment, such as Figure 2 As shown, Figure 2This is a schematic diagram of a multi-round thread allocation process provided in the embodiments of this application; in S122 or S132, threads are reallocated to each subtask according to the proportion of the quotient of all subtasks until the number of allocated threads reaches the total number of threads, which may include:
[0078] S1221: Reassign threads to each subtask according to the ratio of the quotient of all subtasks, and after the threads are assigned, determine whether the number of threads assigned has reached the total number of threads.
[0079] S1222: If the condition is met, stop the thread allocation operation.
[0080] S1223: If not reached, sort the subtasks in reverse order according to their quotients, and assign a single thread to each subtask from top to bottom according to the sorting order and the current number of remaining threads.
[0081] S1224: If the remaining number of threads has been allocated, then stop the thread allocation operation.
[0082] S1225: If the remaining number of threads has not been allocated, return to the step of allocating a single thread to each subtask in order of sorting and the remaining number of threads from top to bottom.
[0083] In this embodiment, after reallocating threads to each subtask according to the proportion of the quotients of all subtasks, this application can determine whether the number of currently allocated threads has reached the total number of threads. If not, the subtasks can be sorted in reverse order according to the quotients of each subtask, and a single thread can be allocated to each subtask from top to bottom according to the sorting order and the current remaining number of threads. After the current remaining number of threads has been allocated, the thread allocation operation stops. If the current remaining number of threads has not been allocated, the allocation of a single thread to each subtask can continue from top to bottom according to the sorting order and the current remaining number of threads until the current remaining number of threads has been allocated. If the number of currently allocated threads has reached the total number of threads, the thread allocation operation can be stopped directly.
[0084] For example, during initial execution, this application can allocate a single thread to each subtask and calculate the actual execution time of each subtask. Then, this application can calculate the quotient between the number of threads and the actual execution time of each subtask, i.e., n1 / t1, n2 / t2, n3 / t3, ..., and calculate the ratio of all quotients according to the quotients of each subtask, i.e., n1 / t1:n2 / t2:n3 / t3, ..., and then allocate the required number of threads for each subtask according to this ratio. Since the number of threads can only be an integer, multiple rounds of allocation are required. In each round of allocation... Calculate the total number of threads: total_thread_num * ni / ti / (n1 / t1 + n2 / t2 + n3 / t3 + ...) and round down (ni and ti are the number of threads and the actual execution time of the i-th subtask, respectively). This will give you the number of threads for each subtask. If all threads are allocated, exit the process. If not, allocate one thread at a time in reverse order of ni / ti. If all threads are allocated, exit the process. If not, continue allocating threads in reverse order of ni / ti until all threads are allocated.
[0085] In one specific implementation, when the target task in this application has five subtasks, namely A, B, C, D, and E, and the total number of threads is 10, during initial runtime, this application can allocate a single thread to each subtask and calculate the actual execution time of each subtask: A(2s)->B(2s)->c(2s)->D(2s)->E(2s). Then, this application can allocate threads according to the ratio between the number of threads and the actual execution time of each subtask, such as A(1 / 2s)->B(1 / 2s)->c(1 / 2s)->D(1 / 2s)->E(1 / 2s). After allocating according to the ratio and rounding, the result is 2:2:2:2:2, which means that two threads are allocated to each subtask. At this point, the threads are just allocated. This application can use this thread allocation process as... In the thread allocation operation under the first round interval, when the current execution round of the target task reaches the preset round interval, this application can obtain the number of threads allocated to each subtask after the thread allocation operation under the previous round interval, as well as the second execution time of each subtask under the corresponding number of threads, such as A(4s)->B(2s)->c(3s)->d(2s)->E(4s). The ratio of the quotients of all subtasks is 2 / 4:2 / 2:2 / 3:2 / 2:2 / 4=3:6:4:6:3. Threads are allocated according to the ratio and rounded to 1:2:1:2:1. After allocation according to this ratio, there are 3 threads remaining. At this time, a thread can be allocated to the top three subtasks according to the reverse order of the ratio of the quotients of all subtasks, so the final number of threads is 1:3:2:3:1.
[0086] The thread allocation device provided in the embodiments of this application is described below. The thread allocation device described below can be referred to in correspondence with the thread allocation method described above.
[0087] In one embodiment, such as Figure 3 As shown, Figure 3 This is a schematic diagram of a thread allocation device provided in an embodiment of this application; this application also provides a thread allocation device, which may include a task and thread determination module 210, a thread allocation module 220, and a thread rebalancing module 230, specifically including the following:
[0088] The task and thread determination module 210 is used to determine the target task to be executed and the total number of threads to be allocated. The target task includes multiple subtasks to be executed concurrently, and the number of subtasks in the target task is not greater than the total number of threads.
[0089] The thread allocation module 220 is used to perform thread allocation operation on each subtask at least once according to a preset first allocation strategy during initial runtime, until the number of allocated threads reaches the total number of threads.
[0090] The thread rebalancing module 230 is used to monitor the current execution round number of the target task after the thread allocation is completed, and when the current execution round number reaches a preset round interval, to re-allocate the thread to each subtask at least once according to a preset second allocation strategy, until the number of allocated threads reaches the total number of threads.
[0091] In the above embodiments, after determining the target task to be executed and the total number of threads to be allocated, since the target task contains multiple concurrently executed subtasks and the number of subtasks is no greater than the total number of threads, during the initial system runtime, this application can perform thread allocation operations on each subtask at least once according to a preset first allocation strategy until the number of allocated threads reaches the total number of threads. This allows for real-time adjustment of the concurrency based on the specific execution status of each subtask during actual task execution, thereby improving resource utilization and overall system throughput. Furthermore, to avoid allocation errors caused by uneven data size, this application can pre-set a round interval and monitor the current execution round of the target task after thread allocation is completed. When the current execution round reaches the preset round interval, each subtask is re-allocated at least once according to a preset second allocation strategy until the number of allocated threads reaches the total number of threads. This not only solves the problem of small data volume and large errors during initial runtime but also further improves resource utilization.
[0092] In one embodiment, the thread allocation module 220 includes:
[0093] The first duration determination module is used to allocate a single thread to each subtask based on the number of subtasks in the target task, and after the thread allocation is completed, to calculate the first execution duration of each subtask under a single thread.
[0094] The first thread allocation submodule is used to calculate the quotient of the number of threads allocated to each subtask and its corresponding first execution duration, and to reallocate threads to each subtask according to the proportion of the quotients of all subtasks, until the number of allocated threads reaches the total number of threads.
[0095] In one embodiment, the thread rebalancing module 230 includes:
[0096] The second duration determination module is used to obtain the number of threads allocated to each subtask after performing the thread allocation operation in the previous round of number intervals, and the second execution duration of each subtask under the corresponding number of threads;
[0097] The second thread allocation submodule is used to calculate the quotient of the number of threads allocated to each subtask and its corresponding second execution duration, and to reallocate threads to each subtask according to the proportion of the quotients of all subtasks, until the number of allocated threads reaches the total number of threads.
[0098] In one embodiment, the first thread allocation submodule or the second thread allocation submodule includes:
[0099] The reallocation module is used to reallocate threads to each subtask according to the ratio of the quotient of all subtasks, and after the thread allocation is completed, it determines whether the number of currently allocated threads has reached the total number of threads.
[0100] The first stop allocation module is used to stop thread allocation operations if the target is reached.
[0101] The first reverse allocation module is used to sort the subtasks in reverse order according to their quotients if the target is not reached, and then allocate a single thread to each subtask from top to bottom according to the sorting order and the current number of remaining threads.
[0102] The second stop allocation module is used to stop the thread allocation operation if the current remaining number of threads has been allocated.
[0103] If the remaining number of threads has not been allocated, the second reverse allocation module returns to the step of allocating a single thread to each subtask in order of sorting and the remaining number of threads from top to bottom.
[0104] In one embodiment, this application also provides a storage medium storing computer-readable instructions that, when executed by one or more processors, cause the one or more processors to perform the steps of the thread allocation method as described in any of the above embodiments.
[0105] In one embodiment, this application also provides a computer device, including: one or more processors, and memory.
[0106] The memory stores computer-readable instructions, which, when executed by the one or more processors, perform the steps of the thread allocation method as described in any of the above embodiments.
[0107] Indicatively, such as Figure 4 As shown, Figure 4 This is a schematic diagram of the internal structure of a computer device 300 provided in an embodiment of this application. The computer device 300 can be provided as a server. (Refer to...) Figure 4 The computer device 300 includes a processing component 302, which further includes one or more processors, and memory resources represented by memory 301 for storing instructions, such as application programs, that can be executed by the processing component 302. The application programs stored in memory 301 may include one or more modules, each corresponding to a set of instructions. Furthermore, the processing component 302 is configured to execute instructions to perform the thread allocation method of any of the above embodiments.
[0108] The computer device 300 may also include a power supply component 303 configured to perform power management of the computer device 300, a wired or wireless network interface 304 configured to connect the computer device 300 to a network, and an input / output (I / O) interface 305. The computer device 300 may operate on an operating system stored in memory 301, such as Windows Server™, Mac OS X™, Unix™, Linux™, Free BSD™, or similar.
[0109] Those skilled in the art will understand that Figure 4 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0110] Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0111] The various embodiments in this specification are described in a progressive manner. Each embodiment focuses on the differences from other embodiments. The various embodiments can be combined as needed, and the same or similar parts can be referred to each other.
[0112] The above description of the disclosed embodiments enables those skilled in the art to make or use this application. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of this application. Therefore, this application is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A thread allocation method, characterized in that, The method includes: Determine the target task to be executed and the total number of threads to be allocated. The target task includes multiple subtasks to be executed concurrently, and the number of subtasks in the target task is not greater than the total number of threads. During initial runtime, each subtask is assigned a thread at least once according to a preset first allocation strategy until the number of allocated threads reaches the total number of threads. After the threads are allocated, the current execution round number of the target task is monitored, and when the current execution round number reaches the preset round interval, the thread allocation operation is performed on each subtask at least once according to the preset second allocation strategy until the number of allocated threads reaches the total number of threads. The step of performing thread allocation operations on each subtask at least once according to a preset first allocation strategy until the number of allocated threads reaches the total number of threads includes: Based on the number of subtasks in the target task, a single thread is allocated to each subtask, and after the thread allocation is completed, the first execution time of each subtask under a single thread is calculated. Calculate the quotient of the number of threads allocated to each subtask and its corresponding first execution duration. Reallocate threads to each subtask according to the proportion of the quotients of all subtasks. After the threads are allocated, determine whether the number of threads currently allocated has reached the total number of threads. If the target is reached, then stop the thread allocation operation; If the target is not reached, the subtasks are sorted in reverse order according to their quotients, and then a single thread is assigned to each subtask from top to bottom according to the sorting order and the current number of remaining threads. If the remaining number of threads has been allocated, then the thread allocation operation will stop. If the remaining number of threads has not been fully allocated, then return to the step of allocating a single thread to each subtask in order of sorting and the remaining number of threads from top to bottom.
2. The thread allocation method according to claim 1, characterized in that, The step of re-allocating threads to each subtask at least once according to a preset second allocation strategy until the number of allocated threads reaches the total number of threads includes: Get the number of threads allocated to each subtask after the thread allocation operation was performed in the previous round of number intervals, and the second execution time of each subtask under the corresponding number of threads; Calculate the quotient of the number of threads allocated to each subtask and its corresponding second execution duration, and reallocate threads to each subtask according to the proportion of the quotients of all subtasks, until the number of allocated threads reaches the total number of threads.
3. A thread allocation device, characterized in that, include: The task and thread determination module is used to determine the target task to be executed and the total number of threads to be allocated. The target task includes multiple sub-tasks to be executed concurrently, and the number of sub-tasks in the target task is not greater than the total number of threads. The thread allocation module is used to perform at least one thread allocation operation on each subtask according to a preset first allocation strategy during initial runtime, until the number of allocated threads reaches the total number of threads. The thread rebalancing module is used to monitor the current execution round number of the target task after the thread allocation is completed, and when the current execution round number reaches the preset round interval, to re-allocate the thread to each subtask at least once according to the preset second allocation strategy, until the number of allocated threads reaches the total number of threads. The thread allocation module includes: The first duration determination module is used to allocate a single thread to each subtask based on the number of subtasks in the target task, and after the thread allocation is completed, to calculate the first execution duration of each subtask under a single thread. The first thread allocation submodule is used to calculate the quotient of the number of threads allocated to each subtask and its corresponding first execution time. The reallocation module is used to reallocate threads to each subtask according to the ratio of the quotient of all subtasks, and after the thread allocation is completed, it determines whether the number of currently allocated threads has reached the total number of threads. The first stop allocation module is used to stop thread allocation operations if the target is reached. The first reverse allocation module is used to sort the subtasks in reverse order according to their quotients if the target is not reached, and then allocate a single thread to each subtask from top to bottom according to the sorting order and the current number of remaining threads. The second stop allocation module is used to stop the thread allocation operation if the current remaining number of threads has been allocated. If the remaining number of threads has not been allocated, the second reverse allocation module returns to the step of allocating a single thread to each subtask in order of sorting and the remaining number of threads from top to bottom.
4. The thread allocation device according to claim 3, characterized in that, The thread rebalancing module includes: The second duration determination module is used to obtain the number of threads allocated to each subtask after performing the thread allocation operation in the previous round of number intervals, and the second execution duration of each subtask under the corresponding number of threads; The second thread allocation submodule is used to calculate the quotient of the number of threads allocated to each subtask and its corresponding second execution duration, and to reallocate threads to each subtask according to the proportion of the quotients of all subtasks, until the number of allocated threads reaches the total number of threads.
5. A storage medium, characterized in that: The storage medium stores computer-readable instructions that, when executed by one or more processors, cause the one or more processors to perform the steps of the thread allocation method as described in any one of claims 1 to 2.
6. A computer device, characterized in that, include: One or more processors, and memory; The memory stores computer-readable instructions that, when executed by the one or more processors, perform the steps of the thread allocation method as described in any one of claims 1 to 2.
Citation Information
Patent Citations
Dynamic multi-thread scheduling method and system
CN112395063A
Intelligent task processing method based on distributed heterogeneous system
CN114968569A