GPU-based thread arrangement method, device, equipment and storage medium
By allocating threads to one-dimensional grids and mapping arrays through kernel functions, the problem of inefficiency caused by improper thread allocation in three-dimensional thread computing is solved, on-demand allocation and efficient utilization of threads are achieved, and the performance of heterogeneous programs is improved. .
Patent Information
- Application Number
- CN202110835793.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-07-23
- Publication Date
- 2024-10-18
- Estimated Expiration
- 2041-07-23
AI Technical Summary
The existing GPU computing method based on three-dimensional threads is inefficient when accessing global memory, resulting in slow memory access, and improper thread allocation wastes resources and affects the performance of heterogeneous programs.
A GPU-based thread arrangement method is adopted to allocate threads to a one-dimensional grid, and map the array to be calculated to the threads through the kernel function, thereby allocating threads on demand and improving thread utilization.
It improves thread utilization and improves the performance of kernel functions. Especially when calculating three-dimensional arrays, it can significantly reduce thread idleness and startup overhead, and is suitable for parallel computing of arrays with dimensions greater than 3.
Smart Images

Figure CN113641470B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of computer technology, and in particular, relates to a GPU-based thread arrangement method, device, equipment and storage medium. Background Art
[0002] CUDA (Compute Unified Device Architecture) introduces the concepts of thread, block, and grid, and they are all three-dimensional. Generally, when computing three-dimensional arrays, three-dimensional threads and blocks are used by default to develop heterogeneous computing codes, because the mapping is clear enough, and the calculation of each array dimension in the three-dimensional array corresponds to a thread of one dimension.
[0003] However, this thread arrangement often has the following disadvantages:
[0004] 1) Although threads, blocks, and grids can be three-dimensional in logical concepts, they are still one-dimensional when they correspond to the SP units that actually perform calculations. When a large number of three-dimensional threads access global memory, it is easy for some policy issues of the scheduler to cause memory access to slow down, which will seriously affect the execution efficiency of heterogeneous programs. A large number of threads are idle while waiting to read data, which is inefficient.
[0005] 2) Depending on the algorithm, some of the opened threads will be wasted; because the number of opened threads is positively correlated with the kernel function startup time, so assuming that we have opened 96*128 threads, but only used 96*96, then these wasted threads will also have overhead when they are started, affecting the performance of heterogeneous programs.
[0006] 3) Since the number of three-dimensional thread blocks must be rounded up when calculating each dimension, the number of threads in each dimension cannot be allocated on demand, and the thread blocks in each dimension will be wasted. Summary of the invention
[0007] The present invention aims to solve one of the technical problems in the related art at least to a certain extent. To this end, one object of the present invention is to propose a GPU-based thread arrangement method, device, equipment and storage medium that can optimize thread arrangement and thus improve thread utilization.
[0008] In order to solve the above technical problems, the embodiments of the present invention provide the following technical solutions:
[0009] A GPU-based thread arrangement method, comprising:
[0010] Get the required thread;
[0011] assigning the threads into a one-dimensional grid;
[0012] The array to be calculated is mapped to the thread through the kernel function.
[0013] Optionally, the obtaining the required thread includes:
[0014] Determining the number of threads required; wherein the number of threads is determined according to the number of elements of the array;
[0015] According to the number, the threads are generated.
[0016] Optionally, after obtaining the required thread, the process includes:
[0017] Start the kernel function.
[0018] Optionally, allocating the threads to a one-dimensional grid includes:
[0019] Determine the label of each of the threads in the one-dimensional grid;
[0020] The threads are allocated into a one-dimensional grid according to the labels.
[0021] Optionally, mapping the array to be calculated to the thread through a kernel function includes:
[0022] Determining the sum of the number of elements of the array;
[0023] According to the sum of the numbers of the elements, the elements are mapped to the threads through a sum function.
[0024] Optionally, the array is an N-dimensional array, where N is a positive integer greater than or equal to 3.
[0025] An embodiment of the present invention further provides a GPU-based thread arrangement device, comprising:
[0026] Acquisition module, used to obtain the required threads;
[0027] An allocation module, used for allocating the threads into a one-dimensional grid;
[0028] A mapping module is used to map the array to be calculated to the thread through a kernel function.
[0029] An embodiment of the present invention further provides a computer device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the above method when executing the computer program.
[0030] An embodiment of the present invention further provides a computer-readable storage medium having a computer program stored thereon, and the computer program implements the above method when executed by a processor.
[0031] The embodiments of the present invention have the following technical effects:
[0032] The above technical solution of the present invention, 1) by optimizing the arrangement of threads, mapping the threads to a one-dimensional grid, so that the threads can be allocated on demand, improving the utilization rate of the threads, can make the performance of many kernel functions whose calculations are not very complex to a certain extent improved, and there is no need to modify the calculation code in the kernel function, only the mapping code of the elements needs to be modified, which can reduce the difficulty of operation and reduce the workload.
[0033] 2) At the same time, the mapping method provided by the present invention can not only calculate three-dimensional arrays, but also easily realize parallel calculation of array elements with a dimension greater than 3, and has a wide range of applications.
[0034] Additional aspects and advantages of the present invention will be given in part in the following description and in part will be obvious from the following description, or will be learned through practice of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] Figure 1 It is a schematic diagram of a flow chart provided by an embodiment of the present invention;
[0036] Figure 2 is a schematic diagram of a three-dimensional grid structure provided by an embodiment of the present invention;
[0037] Figure 3 is a schematic diagram of the structure of a one-dimensional grid provided by an embodiment of the present invention;
[0038] Figure 4 It is a schematic diagram of the structure of a computer device provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0039] Embodiments of the present invention are described in detail below, examples of which are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are intended to be used to explain the present invention, and should not be construed as limiting the present invention.
[0040] The present invention mentions GPU (graphics processing unit) graphics processor; CUDA (Compute Unified Device Architecture) a computing platform; thread: thread; block: block; grid: grid; streaming processor (streaming processor, SP) and GPU streaming multiprocessor (streaming multiprocessor, SM) in hardware concepts; and kernel: kernel function.
[0041] like Figure 1 As shown, an embodiment of the present invention provides a GPU-based thread arrangement method, comprising:
[0042] Step S1: Get the required thread;
[0043] Step S2: Allocate the threads into a one-dimensional grid;
[0044] Step S3: Mapping the array to be calculated onto the thread through a kernel function.
[0045] Specifically, Figure 2 As shown in the figure, it is an xyz three-dimensional grid, which shows that in the process of heterogeneous computing program transplantation and development, the mapping relationship between SP, SM and three-dimensional threads, blocks and grids is analyzed in the fine-grained parallel computing process of GPU accelerator cards, and combined with the hot spot calculation code in the transplanted weather forecast program, as shown in the figure. Figure 3 As shown, the three-dimensional array to be calculated is mapped to a one-dimensional grid.
[0046] This embodiment of the present invention optimizes the arrangement of threads and maps the threads to a one-dimensional grid, so that threads can be allocated on demand, thereby improving thread utilization. It can improve the performance of many kernel functions whose calculations are not very complex to a certain extent, and there is no need to modify the calculation code in the kernel function, only the mapping code of the elements needs to be modified, which can reduce the difficulty of operation and the workload.
[0047] In an optional embodiment of the present invention, in step S1, obtaining the required thread includes:
[0048] Step S11: determining the required number of threads; wherein the number of threads is determined according to the number of elements of the array;
[0049] Step S12: Generate the thread according to the quantity.
[0050] Specifically, the total number of threads generally corresponds to the number of elements in the kernel function calculation array, which avoids idle threads, prevents waste of thread resources, reduces unnecessary overhead generated when threads are started, and improves the performance of heterogeneous programs.
[0051] In an optional embodiment of the present invention, in step S1, after obtaining the required thread, the following steps are performed:
[0052] Step S13: Start the kernel function.
[0053] In an optional embodiment of the present invention, in step S2, allocating the threads to a one-dimensional grid includes:
[0054] Step S21: determining the label of each thread in the one-dimensional grid;
[0055] The specific label is tid.
[0056] Step S22: Allocate the threads to a one-dimensional grid according to the labels.
[0057] In an optional embodiment of the present invention, in step S3, mapping the array to be calculated to the thread through a kernel function includes:
[0058] Step S31: determining the sum of the number of elements of the array;
[0059] Step S32: Mapping the elements to the threads through a sum function according to the sum of the numbers of the elements.
[0060] In an optional embodiment of the present invention, in step S3, the array is an N-dimensional array, and N is a positive integer greater than or equal to 3.
[0061] Specifically, taking N as 3 as an example, the sum ijkdim of the number of all array elements of the entire three-dimensional array (i, j, k) is calculated, and then the three dimensions of the array are mapped on the thread through the mapping formula, that is, the array is mapped on a one-dimensional grid.
[0062] Take the following code as an example:
[0063] int tid=(blockIdx.x)*blockDim.x+threadldx.x;
[0064] int ijkdim=(ied-isd+1)*(jed-jsd+1)*npz;
[0065] int k=(tid) / ((ied-isd+1)*(jed-jsd+1));
[0066] Int j=(tid-(k)*((ied-isd+1)*(jed-jsd+1)) / (ied-isd+1);
[0067] int i=(tid)-(j)*(ied-isd+1)-(k)*((ied-isd+1)*(jed-jsd+1));
[0068] This embodiment of the present invention, through the above-mentioned mapping method, can not only calculate a three-dimensional array, but also realize parallel calculation of array elements with a dimension greater than 3, and has a wide range of applications.
[0069] The data listed in Table 1 below are the performance test results of a large number of kernel functions in the heterogeneous computing weather forecast software.
[0070] Table 1 Performance test results of some three-dimensional array calculation kernel functions
[0071]
[0072]
[0073] By comparing the data in Table 1, we can see that among the kernel functions tested in this software, the maximum speedup can be 26%, and the average speedup of all kernel functions is 16.15%. It can be seen that in most kernel functions, this mapping method is quite effective and the performance improvement effect of the kernel function is significant.
[0074] An embodiment of the present invention further provides a GPU-based thread arrangement device, comprising:
[0075] Acquisition module, used to obtain the required threads;
[0076] An allocation module, used for allocating the threads into a one-dimensional grid;
[0077] A mapping module is used to map the array to be calculated to the thread through a kernel function.
[0078] In this embodiment of the present invention, when an array is computed in parallel using a GPU, the operation of mapping elements to specific threads is implemented in the kernel function code rather than by the compiler, thereby optimizing thread arrangement and improving thread utilization.
[0079] like Figure 4 As shown, an embodiment of the present invention further provides a computer device 40, including a memory 42, a processor 41, and a computer program stored in the memory 42 and executable on the processor 41, wherein the processor 41 implements the method described above when executing the computer program.
[0080] In addition, other functions of the thread arrangement of the embodiment of the present invention are known to those skilled in the art and are not described here in detail to reduce redundancy.
[0081] It should be noted that the logic and / or steps represented in the flowchart or otherwise described herein, for example, can be considered as a sequenced list of executable instructions for implementing logical functions, and can be specifically implemented in any computer-readable medium for use by an instruction execution system, device or apparatus (such as a computer-based system, a system including a processor, or other system that can fetch instructions from an instruction execution system, device or apparatus and execute instructions), or in combination with these instruction execution systems, devices or apparatuses. For the purposes of this specification, "computer-readable medium" can be any device that can contain, store, communicate, propagate or transmit a program for use by an instruction execution system, device or apparatus, or in combination with these instruction execution systems, devices or apparatuses. More specific examples of computer-readable media (a non-exhaustive list) include the following: an electrical connection portion with one or more wirings (electronic device), a portable computer disk box (magnetic device), a random access memory (RAM), a read-only memory (ROM), an erasable and programmable read-only memory (EPROM or flash memory), an optical fiber device, and a portable compact disk read-only memory (CDROM). In addition, the computer-readable medium may even be paper or other suitable medium on which the program is printed, since the program may be obtained electronically, for example, by optically scanning the paper or other medium and then editing, interpreting or processing in other suitable ways if necessary, and then stored in a computer memory.
[0082] It should be understood that the various parts of the present invention can be implemented by hardware, software, firmware or a combination thereof. In the above-mentioned embodiments, a plurality of steps or methods can be implemented by software or firmware stored in a memory and executed by a suitable instruction execution system. For example, if implemented by hardware, as in another embodiment, it can be implemented by any one of the following technologies known in the art or their combination: a discrete logic circuit having a logic gate circuit for implementing a logic function for a data signal, a dedicated integrated circuit having a suitable combination of logic gate circuits, a programmable gate array (PGA), a field programmable gate array (FPGA), etc.
[0083] In the description of this specification, the description with reference to the terms "one embodiment", "some embodiments", "examples", "specific examples", or "some examples" means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present invention. In this specification, the schematic representation of the above terms does not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described may be combined in any one or more embodiments or examples in a suitable manner.
[0084] In the description of the present invention, it is to be understood that the terms “center”, “longitudinal”, “lateral”, “length”, “width”, “thickness”, “up”, “down”, “front”, “back”, “left”, “right”, “vertical”, “horizontal”, “top”, “bottom”, “inside”, “outside”, “clockwise”, “counterclockwise”, “axial”, “radial”, “circumferential”, etc., indicating orientations or positional relationships based on the orientations or positional relationships shown in the accompanying drawings, are only for the convenience of describing the present invention and simplifying the description, and do not indicate or imply that the referred device or element must have a specific orientation, be constructed and operated in a specific orientation, and therefore should not be understood as limiting the present invention.
[0085] In addition, the terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features defined as "first" and "second" may explicitly or implicitly include at least one of the features. In the description of the present invention, the meaning of "plurality" is at least two, such as two, three, etc., unless otherwise clearly and specifically defined.
[0086] In the present invention, unless otherwise clearly specified and limited, the terms "installed", "connected", "connected", "fixed" and the like should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection or an indirect connection through an intermediate medium, it can be the internal connection of two elements or the interaction relationship between two elements, unless otherwise clearly defined. For ordinary technicians in this field, the specific meanings of the above terms in the present invention can be understood according to specific circumstances.
[0087] In the present invention, unless otherwise clearly specified and limited, a first feature being "above" or "below" a second feature may mean that the first and second features are in direct contact, or the first and second features are in indirect contact through an intermediate medium. Moreover, a first feature being "above", "above" or "above" a second feature may mean that the first feature is directly above or obliquely above the second feature, or simply means that the first feature is higher in level than the second feature. A first feature being "below", "below" or "below" a second feature may mean that the first feature is directly below or obliquely below the second feature, or simply means that the first feature is lower in level than the second feature.
[0088] Although the embodiments of the present invention have been shown and described above, it is to be understood that the above embodiments are exemplary and are not to be construed as limitations of the present invention. A person skilled in the art may change, modify, replace and vary the above embodiments within the scope of the present invention.
Claims
1. A thread arrangement method based on GPU, characterized in that: include: Get the required thread; assigning the threads into a one-dimensional grid; Mapping the array to be calculated to the thread through a kernel function; The allocating the threads into a one-dimensional grid comprises: Determine the label of each of the threads in the one-dimensional grid; Allocating the threads into a one-dimensional grid according to the labels; The obtaining of the required thread comprises: Determining the number of threads required; wherein the number of threads is determined according to the number of elements of the array; According to the number, the threads are generated.
2. The method according to claim 1, characterized in that After obtaining the required thread, the method includes: Start the kernel function.
3. The method according to claim 1, characterized in that Mapping the array to be calculated to the thread through a kernel function includes: Determining the sum of the number of elements of the array; According to the sum of the numbers of the elements, the elements are mapped to the threads through a sum function.
4. The method according to claim 3, characterized in that The array is an N-dimensional array, where N is a positive integer greater than or equal to 3.
5. A GPU-based thread arrangement device, characterized in that: include: Acquisition module, used to obtain the required threads; An allocation module, used for allocating the threads into a one-dimensional grid; A mapping module, used to map the array to be calculated to the thread through a kernel function The allocation module is further used to determine the label of each thread in the one-dimensional grid; Allocating the threads into a one-dimensional grid according to the labels; The acquisition module is further used to determine the required number of threads; wherein the number of threads is determined according to the number of elements of the array; According to the number, the threads are generated.
6. A computer device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the method according to any one of claims 1 to 4 is implemented.
7. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 4 is implemented.
Citation Information
Patent Citations
Ball mill medium motion simulation method based on boundary box search method and GPU
CN109753726A
