Method and equipment for optimizing general computing based on GPU (Graphics Processing Unit)

By optimizing OpenCL and CUDA programming standards, obtaining the maximum number of threads and performing data division processing, it solves the problem of limited existing GPU computing programming methods and achieves efficient GPU computing performance and operating efficiency improvements.

CN118710484APending Publication Date: 2024-09-27马晓菲
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202310355886.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-03-25
Publication Date
2024-09-27

AI Technical Summary

Technical Problem

The existing GPU computing programming method is limited by fixed limits on the total task volume and the number of threads, resulting in increased code complexity, low operating efficiency, and hardware design that cannot fully utilize the GPU computing power.

Method used

By optimizing OpenCL and CUDA programming standards, the maximum number of threads in the work group is obtained, and a fixed number of threads is set in the kernel function for data division processing. Thread synchronization is achieved by combining the obstacle function, allowing threads to handle different task volumes, and improving GPU computing efficiency.

Benefits of technology

It greatly improves GPU computing performance, reduces code complexity, improves operating efficiency, can handle any workload, and even implements larger thread group sizes on hardware to release GPU computing power.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118710484A
    Figure CN118710484A
Patent Text Reader

Abstract

The invention discloses a method and equipment for optimizing general computing based on a GPU (Graphics Processing Unit), which are characterized in that according to OpenCL (Open Computing Language) and CUDA (Compute Unified Device Architecture) programming standards, the number of threads is fixed when a kernel runs, then data is divided, corresponding data is allocated to each thread, the data is processed, the next batch of data is continuously allocated, the threads are always in a running state until all tasks are processed, and the threads are distributed to the next batch of data; according to the method, the GPU can continuously execute programs of any task load, the dependence on a CPU is reduced, the number of access times to a host memory is reduced, the operation efficiency is improved, the usability is improved, the code quantity is reduced, a new scheme for improving GPU equipment and OpenCL and CUDA design is provided, and the general calculation efficiency of the GPU is greatly improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention discloses a method and device for optimizing general computing based on GPU. According to OpenCL and CUDA programming standards, relevant algorithms are designed to improve computing efficiency and usability, and the design of GPU equipment is improved to accelerate GPU general computing efficiency. Background Art

[0002] Based on OpenCL or CUDA computing tool library, current GPU computing first determines the total task volume, specifies the total number of threads, and then specifies the number of threads in each work group (group) or thread block (block), which is group in OpenCL and block in CUDA. This programming method leads to the establishment of a kernel function when the total task volume is constant, and another kernel function must be established when the task volume changes later. The CPU is required to participate in calling these different kernel functions, which increases the code volume of the program, increases complexity, reduces ease of use, and has low operating efficiency. Programming in GPU cannot be as free as in CPU, where continuous programming can be performed to accept any number of different tasks, but is limited by the total task volume. In addition, in hardware design, the number of threads in a group or block of existing GPUs is limited. For example, NVIDIA's RTX3090 graphics card has 10496 stream processors, but a group can only run up to 1024 threads, and the barrier function can only block 1024 threads in a group from being synchronized. In the case of using the algorithm described in the present invention, the hardware device will be optimized to greatly improve the GPU computing performance. Summary of the invention

[0003] The present disclosure provides a method and device for optimizing general computing based on GPU, based on OpenCL and CUDA programming standards, to optimize algorithms, improve computing efficiency, and optimize GPU hardware, further improving computing efficiency. Because OpenCL and CUDA are basically equivalent, the following only uses OpenCL as an example for explanation.

[0004] Steps to perform optimization:

[0005] S1: Add the following function in the host program to get the maximum number of work items in a group and save the result in the variable lsize. clGetDeviceInfo(devices[0], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(lsize), &lsize, NULL);

[0006] S2: When executing the kernel function, call the following function. The fifth and sixth parameters of the function must be set to the variable lsize in step S1. clEnqueueNDRangeKernel(commandQueue, sum, 1, NULL, &lsize, &lsize, 0, NULL, NULL);

[0007] S3: lsize threads are always in running state, the number of threads does not change, the tasks are not divided, only the data is divided, and different threads process their own data. A fixed number of threads are required to continuously process tasks of different task amounts.

[0008] S4: Divide the data to be processed. Assuming the task size is T, the number of threads is still the variable lsize in step S1. The data to be processed is divided into two parts. Assuming the data size of the first part is m, then m = (T / lsize) * lsize. Assuming the data size of the second part is s, s = T% lsize, that is, m is the part of T that can divide lsize, and s is the remaining part of T after deducting m. Where T = m + s.

[0009] S5: The two parts of data are processed differently. The m part is usually processed by a loop statement, and the s part is usually processed by a judgment statement. In this way, the GPU can always process any task volume T. Combined with the barrier function barrier (CLK_LOCAL_MEM_FENCE), thread synchronization is achieved, and the next program with a different task volume can continue to run. The thread that does not execute the task waits at the barrier function, and then continues to execute the next task. In this way, any program that can run on the CPU can always be modified to run on the GPU.

[0010] According to the above steps, the hardware design can be optimized as follows to further improve the computing speed:

[0011] 1. The lsize variable of most GPUs, that is, the maximum number of work items in the group, is too small. Most of them are 1024 threads, which is much smaller than the number of stream processors. GPU hardware or OpenCL and CUDA computing tools should allow a larger lsize value. lsize can be equal to the number of stream processors or slightly smaller than the number of stream processors (for convenience of calculation, it can be 2 to the power of n). Combined with the methods provided in the above 5 steps, the GPU computing power will be completely released and the running speed will be greatly improved.

[0012] Taking the array summation program as an example, assuming that the GPU used for calculation is NVIDIA's RTX3090, with 10496 stream processors, according to the traditional method, it takes an average of 0.085 nanoseconds to calculate the addition of each float (single-precision floating point number) type of data. Using the method provided by the present invention, it takes an average of 0.031 nanoseconds to calculate the addition of each data. If lsize is increased to 8192 (1024*8) threads, the running speed can be increased by 8 times, and it takes 0.0039 (0.031÷8) nanoseconds to calculate each number, which is 21 times that of the above-mentioned traditional calculation method.

[0013] 2. It is also possible to enable the barrier function to pause all stream processor tasks, which is feasible in hardware and can also achieve the above results.

[0014] 3. Based on the previous method, we can make a subversive design for the computer hardware system. The GPU chip can strengthen some of the cores, increase the cache of these cores, improve the performance of these cores, connect the north and south bridge chips, and directly control the peripherals and host memory without going through the CPU. In this way, the operating system and application programs can run in the GPU, thereby eliminating the CPU. Such computers will further increase the operating speed and reduce economic costs. BRIEF DESCRIPTION OF THE DRAWINGS

[0015] Now we will take the array summation problem and a neural network training as examples to explain the principle of the algorithm in detail.

[0016] Figure 1 The process of summing an array

[0017] Figure 2 Schematic diagram of neural network operation for any node

[0018] Figure 3 Schematic diagram of neural network operation with the number of hidden layer nodes equal to lsize

[0019] Assume that the variable lsize in step S1 of the invention is 1024, and assume that the total amount of array data is T, such as Figure 1 , there are m data from data block a to data block b, each of which has 1024 data, a total of m / 1024 data blocks, and data block c has s data, s=T%1024.

[0020] Figure 1 The local memory in the kernel is also 1024 data size. The source code of the kernel function is as follows:

[0021]

[0022] As shown in the kernel function source code, the function parameter *data is the array data to be summed, the function parameter *n is the number of array data, the function parameter *layer is the local memory, the x-line statement is the m data summation process, each data block is added to the local memory layer, the y-line statement is the remaining s data added to the local memory layer, at this time there are 1024 data in the layer, the z-line loop statement is the merging and summing of the 1024 data in the layer, that is, the folded summation. The last statement is to store the calculation result in data[0] output.

[0023] From the above source code, we can see that if the lsize value of the hardware or OpenCL or CUDA computing tool is increased several times, the running speed of this sum function will be increased several times. This sum function is widely used, for example, in neural networks, such as Figure 2 , the input node is multiplied by the weight and then summed in the hidden layer nodes. This is the calculation method for a neural network with any number of nodes. If the hidden layer nodes are 1024, the neural network code can be further simplified, and the training function fit code is as follows:

[0024]

[0025]

[0026] The above code takes handwritten digit recognition as an example. The handwritten digit is a grayscale image with a 28×28 dot matrix, with a total of 784 pixel values, 784 nodes in the input layer, 1024 nodes in the hidden layer, and 10 nodes in the output layer. All input layer pixel values ​​are divided by 255 to convert to decimals. The activation function is the sigmoid function. The target ten node values ​​are also converted to decimals. The value of the node with the label as the subscript is 0.99, and the values ​​of the other nine nodes are 0.01. Because the number of hidden layer nodes is lsize, that is, 1024, it is not necessary to follow the traditional algorithm and sum the hidden nodes one by one, such as Figure 2 , the storage method of weights is also different, which is equivalent to the transposition of the traditional method, such as Figure 3 , each input node multiplied by the weight generates 1024 values, which are added to the hidden layer. There are 784 input nodes, so a total of 784 additions are required. This neural network can also have multiple hidden layers. The algorithm is the same and will not be repeated here. This neural network can be used in situations where the requirements are not high because of its simplicity and efficiency. DETAILED DESCRIPTION

[0027] After the host program creates the kernel, or after initializing OpenCL, add the following statement:

[0028] clGetDeviceInfo(devices[0], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(lsize), &lsize, NULL);

[0029] The purpose is to obtain the maximum number of threads in the work group, i.e., the value of lsize, and then transfer the data to be processed into the video memory. Finally, in the function that executes the kernel, the fifth and sixth parameters are modified to &lsize.

[0030] Taking array summation as an example, we also need to set the third parameter to apply for a local memory block of size lsize.

[0031] The code is as follows:

[0032] status=clSetKernelArg(sum, 0, sizeof(mem_data), (void*)&mem_data);

[0033] status=clSetKernelArg(sum, 1, sizeof(mem_n), (void*)&mem_n);

[0034] / / The following statement applies for a local memory block of size lsize. Calculations are performed in local memory as much as possible.

[0035] status=clSetKerneiArg(sum, 2, sizeof(float)*lsize, NULL);

[0036] status=clEnqueueNDRangeKernel(commandQueue,sum,1,NULL,&lsize,&lsize,0,NULL,NULL);

[0037] After such settings, a fixed number of threads must continuously process tasks of different task amounts. Divide the data to be processed, calculate the address of the data processed by each thread, and divide the data to be processed into two parts. Assuming the amount of the first part of the data is m, then m = (T / lsize) * lsize, assuming the amount of the second part of the data is s, s = T% lsize, that is, m is the part of T that can divide lsize, and s is the remaining part of T after deducting m. Where T = m + s. The two parts of data are processed differently. The m part is usually processed with a loop statement, and all threads participate in the calculation. The s part is processed with a judgment statement, and the remaining small part of the threads participate in the calculation.

[0038] After the calculation is completed, the next batch of tasks can be continued, and the data can be divided and processed in the same way. In this way, any task can be processed continuously until all tasks are processed. The traditional method can only process one batch of tasks, and other kernel functions need to be started to process the next batch of tasks.

[0039] By using such algorithms, GPU hardware or OpenCL and CUDA computing tools should allow a larger lsize value, which can be equal to the number of stream processors or slightly less than the number of stream processors (which can be 2 to the power of n for convenient calculation), which can greatly speed up the calculation process. If the algorithm of the present invention is not used, increasing the lsize value is meaningless and cannot improve the calculation speed.

[0040] It is also possible to enable the barrier function to pause all stream processor tasks, which is feasible in hardware and can also achieve the above effect of increasing computing speed.

Claims

1. A method and device for optimizing general computing based on GPU, characterized in that: Based on OpenCL or CUDA, the local thread quantity, i.e., the thread quantity of a group or a block, is set equal to the total thread quantity, wherein the fixed thread quantity continuously processes any different task quantities.

2. According to claim 1, it is characterized in that Divide the data to be processed and assign tasks to each thread. The data to be processed is divided into two parts. Assuming the amount of the first part is m, then m = (T / lsize)*lsize. Assuming the amount of the second part is s, s = T%lsize, that is, m is the part of T that can divide lsize, and s is the remaining part of T after deducting m. Where T = m+s.

3. According to claim 2, it is characterized in that Divide the data into two parts, apply this method to the summation method and kernel function code.

4. According to claim 1, it is characterized in that A fixed amount of threads is required to continuously process any different amount of tasks, and this method is used to write neural network code.

5. According to claim 1 and claim 2, it is characterized in that To optimize GPU hardware or opencl and CUDA, increase the lsize value to the number of stream processors or slightly less than the number of stream processors.

6. According to claim 5, it is characterized in that The lsize value is set to a power of 2.

7. According to claim 1 and claim 2, it is characterized in that Improve GPU hardware or opencl and CUDA so that the barrier function can block threads with the number of stream processors or slightly less than the number of stream processors.

8. According to claim 1 and claim 2, it is characterized in that Make a revolutionary design to the computer system, strengthen all or part of the core of the graphics card, connect peripherals and memory, and eliminate the CPU.