Code generation methods, apparatus, computer equipment, and readable storage media
By generating initialization variables, target expressions, and code snippets, the code generation process of the parallel computing platform is simplified, and the code generation efficiency is improved.
Patent Information
- Application Number
- CN202411207491.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-30
- Publication Date
- 2025-10-28
- Estimated Expiration
- 2044-08-30
AI Technical Summary
The complex process of writing code for parallel computing platforms results in low code generation efficiency.
Initialization variables are generated by obtaining variable parameters, target expressions are obtained by operating according to variable configuration, and various code snippets are generated based on code configuration information, which are finally combined into target code.
It simplifies the code generation process and improves code generation efficiency.
Smart Images

Figure CN119025090B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a code generation method, apparatus, computer device, computer-readable storage medium, and computer program product. Background Technology
[0002] With the rapid development of computer technology, fields such as big data, artificial intelligence (AI), and machine learning (ML) are undergoing unprecedented transformations, profoundly impacting the production methods and business models of various industries. With the explosive growth of data volume and the dramatic increase in computational complexity, traditional computing models are struggling to meet the demands of efficiently processing big data and complex algorithms. Against this backdrop, parallel computing technology has rapidly become a key means of improving computational efficiency. Parallel computing decomposes computational tasks into multiple sub-tasks and simultaneously utilizes multiple processors or computing units for independent computation, thereby achieving effective utilization of computing resources and a significant increase in computational speed.
[0003] However, the process of writing code on a parallel computing platform to accelerate parallel computing is complex, resulting in low code generation efficiency. Summary of the Invention
[0004] Therefore, it is necessary to provide a code generation method, apparatus, computer equipment, computer-readable storage medium, and computer program product that can improve code generation efficiency in response to the above-mentioned technical problems.
[0005] Firstly, this application provides a code generation method, including:
[0006] Obtain the variable parameters and generate the corresponding initialization variables based on the variable parameters;
[0007] The target expression is obtained based on the variable configuration operations performed on the initialization variables;
[0008] Obtain code configuration information and generate corresponding code snippets based on the code configuration information and target expression;
[0009] The target code is obtained by combining the various code snippets.
[0010] In some embodiments, generating corresponding code fragments based on code configuration information and target expression includes: generating function parameter code fragments and function signature code fragments based on the global pointer list in the code configuration information; generating function declaration code fragments based on object information in the code configuration information; generating function computation code fragments based on computation logic information and target expression in the code configuration information; and combining the code fragments to obtain target code, including: combining the function parameter code fragments, function signature code fragments, function declaration code fragments, and function computation code fragments to obtain target code.
[0011] In some embodiments, obtaining variable parameters and generating corresponding initialization variables based on the variable parameters includes: obtaining variable parameters obtained based on variable initialization operations, wherein the variable parameters include variable type and variable name; and generating corresponding initialization variables based on the variable type and variable name.
[0012] In some embodiments, obtaining a target expression based on a variable configuration operation for an initialization variable includes: determining a variable configuration operation for an initialization variable; and generating a corresponding target expression based on the operation information of the variable configuration operation.
[0013] In some embodiments, the operation information includes the operator and operand types; generating a corresponding target expression based on the operation information of the variable configuration operation includes: generating a corresponding target expression based on the initialization variables according to the operator and operand types.
[0014] In some embodiments, the code generation method further includes: when the target code meets the optimization conditions, performing code optimization processing on the target code to obtain optimized target code; wherein the code optimization processing includes at least one of loop unrolling, register allocation, memory alignment, thread block adjustment, or grid adjustment.
[0015] Secondly, this application also provides a code generation apparatus, comprising:
[0016] The variable initialization module is used to obtain variable parameters and generate corresponding initial variables based on the variable parameters;
[0017] The expression retrieval module is used to obtain the target expression based on the variable configuration operation applied to the initialization variable;
[0018] The code snippet acquisition module is used to obtain code configuration information and generate corresponding code snippets based on the code configuration information and target expression;
[0019] The code snippet combination module is used to combine various code snippets to obtain the target code.
[0020] Thirdly, this application also provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to perform the following steps:
[0021] Obtain the variable parameters and generate the corresponding initialization variables based on the variable parameters;
[0022] The target expression is obtained based on the variable configuration operations performed on the initialization variables;
[0023] Obtain code configuration information and generate corresponding code snippets based on the code configuration information and target expression;
[0024] The target code is obtained by combining the various code snippets.
[0025] Fourthly, this application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, performs the following steps:
[0026] Obtain the variable parameters and generate the corresponding initialization variables based on the variable parameters;
[0027] The target expression is obtained based on the variable configuration operations performed on the initialization variables;
[0028] Obtain code configuration information and generate corresponding code snippets based on the code configuration information and target expression;
[0029] The target code is obtained by combining the various code snippets.
[0030] Fifthly, this application also provides a computer program product, including a computer program that, when executed by a processor, performs the following steps:
[0031] Obtain the variable parameters and generate the corresponding initialization variables based on the variable parameters;
[0032] The target expression is obtained based on the variable configuration operations performed on the initialization variables;
[0033] Obtain code configuration information and generate corresponding code snippets based on the code configuration information and target expression;
[0034] The target code is obtained by combining the various code snippets.
[0035] The aforementioned code generation method, apparatus, computer device, computer-readable storage medium, and computer program product generate corresponding initialization variables based on acquired variable parameters, obtain a target expression based on variable configuration operations on the initialization variables, generate corresponding code fragments based on code configuration information and the target expression, and then combine the code fragments to obtain the target code. By generating initialization variables through variable parameters, obtaining the target expression through variable configuration operations on the initialization variables, generating code fragments based on code configuration information and the target expression, and combining the code fragments to obtain the target code, the code generation process can be simplified, thereby improving the efficiency of code generation. Attached Figure Description
[0036] To more clearly illustrate the technical solutions in the embodiments of this application or related technologies, the drawings used in the description of the embodiments of this application or related technologies will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0037] Figure 1 This is a diagram illustrating the application environment of the code generation method in one embodiment;
[0038] Figure 2 This is a flowchart illustrating a code generation method in one embodiment;
[0039] Figure 3 This is a flowchart illustrating the process of generating code snippets and obtaining target code in one embodiment;
[0040] Figure 4 This is a schematic diagram of the architecture of a code generation system in one embodiment;
[0041] Figure 5 This is a flowchart illustrating the code generation method in yet another embodiment;
[0042] Figure 6 This is a structural block diagram of a code generation device in one embodiment;
[0043] Figure 7 This is an internal structural diagram of a computer device in one embodiment;
[0044] Figure 8 This is an internal structural diagram of a computer device in one embodiment. Detailed Implementation
[0045] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0046] The code generation method provided in this application embodiment can be applied to, for example, Figure 1 In the application environment shown, terminal 102 communicates with server 104 via a network. A data storage system can store the data that server 104 needs to process. The data storage system can be integrated onto server 104 or placed in the cloud or on another network server. Users can edit variable parameters through terminal 102, and terminal 102 sends the edited variable parameters to server 104, which then generates corresponding initialization variables based on the variable parameters. Users can trigger variable configuration operations through terminal 102 to configure the generated initialization variables. Terminal 102 can send the user-triggered variable configuration operations to server 104, and server 104 can obtain the target expression based on the variable configuration operations for the initialization variables. Server 104 can obtain code configuration information, which can be sent by the user to server 104 through terminal 102 after configuration. Server 104 generates corresponding code fragments based on the code configuration information and the target expression, and then combines these code fragments to obtain the target code. Server 104 can feed the obtained target code back to terminal 102, so that terminal 102 can execute the target code to perform corresponding computational tasks, such as parallel computations like data preprocessing, feature extraction, and model training. In some embodiments, code generation can also be implemented independently by terminal 102 or server 104, that is, terminal 102 or server 104 can directly execute each step of the code generation method.
[0047] The terminal 102 can be, but is not limited to, various personal computers, laptops, smartphones, tablets, IoT devices, and portable wearable devices. IoT devices can include smart speakers, smart TVs, smart air conditioners, smart in-vehicle systems, and projection devices. Portable wearable devices can include smartwatches, smart bracelets, and head-mounted displays. Head-mounted displays can be virtual reality (VR) devices, augmented reality (AR) devices, and smart glasses. The server 104 can be a standalone physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing cloud computing services.
[0048] In one exemplary embodiment, such as Figure 2 As shown, a code generation method is provided, which can be applied to... Figure 1 Taking a computer device such as terminal 102 or server 104 as an example, the explanation includes the following steps 202 to 208. Wherein:
[0049] Step 202: Obtain the variable parameters and generate the corresponding initialization variables based on the variable parameters.
[0050] In programming, variable parameters are the information about the variables to be generated. This information may include, but is not limited to, variable names and types. Variable parameters allow for the initialization of variables, such as declaring their initial values, types, and names. In programming, a variable is a container for storing information, such as numbers, text, or images. Variables allow data to be stored and modified during program execution; that is, variables allow the program to process input, store data, perform calculations, and generate output.
[0051] Optionally, the computer device can acquire variable parameters, which can be edited by the user according to actual coding needs, such as by inputting them through an input device. Based on the acquired variable parameters, the computer device generates corresponding initialization variables, thereby declaring and initializing the variables so that they can be used for code editing. In some embodiments, code generation can be achieved through a parallel computing platform, such as CUDA (Compute Unified Device Architecture). Specifically, the computer device can generate corresponding initialization variables based on the variable parameters using the CUDA platform. These initialization variables can be matched with the CUDA platform; that is, the generated initialization variables can be CUDA variables. CUDA variables can be categorized according to their storage location and characteristics, such as global memory variables, shared memory variables, constant memory variables, texture memory variables, and register variables.
[0052] Step 204: Obtain the target expression based on the variable configuration operation for the initialization variables.
[0053] Variable configuration operations are configuration operations for initialization variables to configure corresponding target expressions based on the initialization variables. Variable configuration operations can include configuration operations for operation information such as operators and operand types for the initialization variables, allowing for the combination of expressions based on operators and operand types to obtain the target expression. An expression is a sequence of operators (such as addition, subtraction, multiplication, and division) and operands (such as variables, constants, and literals) used to represent a calculation process or value. Expressions can include, but are not limited to, arithmetic expressions performing basic mathematical operations such as addition, subtraction, multiplication, and division; relational expressions comparing the size or equality of two values; logical expressions controlling program flow; assignment expressions; conditional expressions; function call expressions; and complex expressions composed of multiple sub-expressions combined by operators. The target expression is an expression determined based on variable configuration operations and initialization variables, representing the calculation process or value constructed by the initialization variables.
[0054] For example, variable configuration operations can be triggered by the user according to actual programming needs. For instance, a user can input an expression built using a high-level language or assembly language on a computer device, thereby triggering a variable configuration operation. The computer device can then generate a corresponding target expression based on the variable configuration operation and the initialization variables. For example, the initialization variables may include variables p1, p2, and p3. If the user-triggered variable configuration operation is "assign the sum of variables 1 and 2 to variable 3", the resulting target expression will be "variable p3 = variable p1 + variable p2". In some embodiments, the user can trigger different variable configuration operations on the initialization variables according to actual programming needs, such as the needs of the computational task. Through these variable configuration operations, a corresponding target expression can be constructed based on the initialization variables to represent the corresponding computational process.
[0055] Step 206: Obtain code configuration information and generate corresponding code snippets based on the code configuration information and target expression.
[0056] The code configuration information refers to the configuration details provided by the user for the code to be generated. This may include, but is not limited to, defined pointer information, object buffering information, and computational logic information. The user can configure this information according to their actual programming needs. Code snippets are generated local code segments; combining multiple snippets yields complete code, which can then be executed to complete the corresponding computational tasks.
[0057] For example, the computer device can obtain code configuration information, which can be pre-configured by the user, such as by the user accessing the CUDA platform through the computer device. The computer device can generate code based on the code configuration information and the target expression to obtain various code snippets. Different code snippets can be generated for different types of configuration information, allowing for relatively independent parallel generation of each code snippet. For example, various code snippets such as function parameter code snippets, function signature code snippets, function declaration code snippets, and function computation code snippets can be generated.
[0058] Step 208: Obtain the target code by combining the various code snippets.
[0059] The target code is the generated code. A specific computer device can combine these code fragments, such as according to code structure conditions, to obtain the target code. These code structure conditions can be determined based on coding rules, and may include various structural conditions such as the order and nesting relationships between different code fragments.
[0060] In some embodiments, after obtaining each code segment, the computer device can analyze each code segment separately to determine whether each code segment meets the optimization conditions. When there are code segments that meet the optimization conditions, the computer device can optimize the code segments and then combine the code segments to obtain the target code. This allows for code optimization at the code segment level, which helps ensure the programming quality of the target code.
[0061] In the code generation method described above, corresponding initialization variables are generated based on the acquired variable parameters. A target expression is obtained through variable configuration operations on these initialization variables. Then, corresponding code snippets are generated based on the code configuration information and the target expression. Finally, the target code is obtained by combining these code snippets. This method simplifies the code generation process by generating initialization variables from variable parameters, obtaining the target expression through variable configuration operations on these initialization variables, generating code snippets based on the code configuration information and the target expression, and then combining these code snippets to obtain the target code. This approach improves the efficiency of code generation.
[0062] In one exemplary embodiment, such as Figure 3 As shown, the process of generating code snippets and combining them to obtain target code involves generating corresponding code snippets based on code configuration information and target expressions, and then combining these snippets to obtain the target code, including steps 302 to 308. Wherein:
[0063] Step 302: Generate function parameter code snippets and function signature code snippets based on the global pointer list in the code configuration information.
[0064] The global pointer list is a collection of one or more pointers defined in the global scope of the program code. These pointers can point to dynamically allocated memory, data structures (such as arrays, linked lists, trees, etc.), or other types of objects. The global pointer list can be used, but is not limited to, for managing resources and implementing data sharing across functions or modules. The function parameter code snippet is the code snippet corresponding to the function's parameters in the code to be generated; the function signature code snippet is the code snippet corresponding to the function's signature information in the code to be generated. A function is a block of code that encapsulates reusable code. A function can be used to complete a specific task or computation. A function can accept input (called parameters or arguments), perform a series of operations, and may return output (called a return value).
[0065] For example, the code configuration information may include a global pointer list, which may include pointers defined in the global scope. The computer device can generate corresponding function parameter code snippets and function signature code snippets based on the global pointer list. For instance, in the CUDA platform, the function parameter code snippet may include the code snippet corresponding to the parameter list of the CUDA kernel function, and the function signature code snippet may include the code snippet corresponding to the function signature of the CUDA kernel function.
[0066] Step 304: Generate function declaration code snippets based on the object information in the code configuration information.
[0067] In this context, an object is an entity composed of attributes and methods. Attributes are the object's state information, used to describe the object's characteristics; methods are the object's behavior, defining the operations the object can perform. Object information refers to the information carried by the object, specifically including information such as the object's attributes and methods. Function declaration code snippets are code fragments corresponding to the function declarations in the code to be generated.
[0068] For example, the code configuration information may include object information, and the computer device can generate corresponding function declaration code snippets based on the object information. For instance, in the CUDA platform, the computer device can generate code snippets corresponding to variables and memory declarations in CUDA kernel functions based on edge and buffer information in graph objects.
[0069] Step 306: Generate function calculation code snippets based on the calculation logic information and target expression in the code configuration information.
[0070] The computational logic information describes the processing logic required to perform computations in the code to be generated. The function computation code snippet is the code segment in the code to be generated that performs the computation. For example, the code configuration information may include computational logic information, and the computer device can generate the corresponding function computation code snippet based on the computational logic information and the target expression. For instance, in the CUDA platform, the computer device can generate the main loop code and offset calculation code in the CUDA kernel function by calling the target expression and following the computational logic information.
[0071] Step 308: Combine the function parameter code snippet, function signature code snippet, function declaration code snippet, and function calculation code snippet to obtain the target code.
[0072] For example, a computer device can combine various code snippets to obtain target code. Specifically, it can combine function parameter code snippets, function signature code snippets, function declaration code snippets, and function computation code snippets to obtain target code. For instance, in a CUDA platform, a computer device can combine function parameter code snippets, function signature code snippets, function declaration code snippets, and function computation code snippets to obtain complete CUDA kernel function code. In some embodiments, the computer device can combine various code snippets according to code structure conditions, specifically, it can combine the various code snippets in the order of "function parameter code snippet - function signature code snippet - function declaration code snippet - function computation code snippet" to obtain target code.
[0073] In this embodiment, the computer device generates function parameter code snippets and function signature code snippets based on the global pointer list in the code configuration information, generates function declaration code snippets based on the object information in the code configuration information, and generates function calculation code snippets based on the computation logic information and target expression in the code configuration information. The function parameter code snippets, function signature code snippets, function declaration code snippets, and function calculation code snippets are then combined to obtain the target code. This simplifies the code writing process and improves code generation efficiency by generating various code snippets and combining them using the code configuration information and target expression.
[0074] In an exemplary embodiment, obtaining variable parameters and generating corresponding initialization variables based on the variable parameters includes: obtaining variable parameters obtained based on variable initialization operations, wherein the variable parameters include variable type and variable name; and generating corresponding initialization variables according to the variable type and variable name.
[0075] The variable initialization operation is a user-triggered operation that initializes variables, such as user-triggered variable editing. The variable parameters obtained from the initialization operation include the variable type and variable name. The variable type determines the kinds of data the variable can store and the operations that can be performed on that data; variable types can include, but are not limited to, numeric, character, boolean, composite, or vector types. The variable name is an identifier assigned by the programmer to the variable, used to reference and manipulate the variable in the program.
[0076] For example, a user can initialize a variable by triggering a variable initialization operation. The computer device can detect the user-triggered variable initialization operation and determine the variable parameters based on the operation, including the variable type and variable name. The computer device can then generate a corresponding initialized variable based on the variable type and name. For instance, the computer device can define a primitive variable and initialize it according to its type and name to obtain the initialized variable.
[0077] In some embodiments, when at least one of the variable type and variable name is missing from the variable parameters obtained based on the variable initialization operation, the computer device can perform prediction to supplement the missing variable. Specifically, the computer device can obtain the variable parameters obtained based on the variable initialization operation. When the variable parameters include one of the variable type and variable name, i.e., when the variable parameters are missing either the variable name or the variable type, the computer device can perform prediction to supplement the missing variable. For example, when the variable parameters include the variable name but lack the variable type, the computer device can predict the corresponding variable type based on the variable name and the actual application scenario of the code to be generated. For example, if the variable name is age, the predicted variable type can be numeric. Similarly, when the variable parameters include the variable type but lack the variable name, the computer device can predict the corresponding variable name based on the variable type and the actual application scenario of the code to be generated. For example, if the variable type is numeric and its value range is [0, 200], and the actual application scenario of the code to be generated is to count the number of people in each age group, the predicted variable name can be age.
[0078] In this embodiment, the computer device can generate corresponding initialization variables according to the variable type and variable name, thereby initializing the variables in the code generation process. This simplifies the variable initialization operation and helps improve the efficiency of code generation.
[0079] In one exemplary embodiment, obtaining a target expression based on a variable configuration operation for an initialization variable includes: determining the variable configuration operation for the initialization variable; and generating a corresponding target expression based on the operation information of the variable configuration operation.
[0080] Variable configuration operations can be triggered by the user for initializing variables, and can specifically include configuration operations for operators and operand types. Operation information describes the variable configuration operation, and may include, but is not limited to, operation information such as the operator and operand types for the initializing variable. Furthermore, variable configuration operations can also include various configuration operations such as assignment operations, indexing operations, and vector type variable configuration operations. In such cases, the operation information for variable configuration operations can include the corresponding assignment data, index information, vector type information, and other operation information.
[0081] For example, the computer device can determine the user's variable configuration operation on the initialization variable and determine the operation information of the variable configuration operation, such as the operator, operand type, etc., for the initialization variable. The computer device can generate a corresponding target expression based on the operation information of the variable configuration operation. For example, when the variable configuration operation includes an assignment operation, the computer device can assign a value to the initialization variable based on the assignment data of the assignment operation, thereby obtaining a corresponding assignment expression; when the variable configuration operation includes an indexing operation, the computer device can generate a corresponding indexing expression based on the indexing information of the indexing operation.
[0082] In this embodiment, the computer device generates a corresponding target expression based on the operation information of the variable configuration operation for the initialization variable, which can simplify the expression generation operation and improve the efficiency of code generation.
[0083] In an exemplary embodiment, the operation information includes the operator and operand types; generating a corresponding target expression based on the operation information of the variable configuration operation includes: generating a corresponding target expression based on the initialized variables according to the operator and operand types.
[0084] For example, when the operation information of the variable configuration operation includes the operator and operand types, it indicates that the corresponding expression needs to be constructed according to the operator and operand types. The computer device can generate the corresponding target expression based on the operator and operand types and the initialization variables.
[0085] In this embodiment, the computer device generates a corresponding target expression based on the operator and operand types, which simplifies the expression generation process and improves code generation efficiency.
[0086] In an exemplary embodiment, the code generation method further includes: when the target code meets the optimization conditions, performing code optimization processing on the target code to obtain optimized target code; wherein the code optimization processing includes at least one of loop unrolling, register allocation, memory alignment, thread block adjustment, or grid adjustment.
[0087] The optimization criteria are used to determine whether further adjustments and optimizations are needed for the generated target code. For example, optimization analysis can be performed on the target code. If the analysis results indicate that there is optimizable code within the target code, then the target code is considered to meet the optimization criteria, meaning it needs to be optimized and updated. Loop unrolling can be achieved using the `#pragma unroll` directive to reduce loop overhead. Register allocation can be optimized by optimizing register allocation and usage to reduce memory access latency. Memory alignment can be optimized by ensuring memory access alignment to improve access speed. Thread block adjustment or mesh adjustment can be automated and optimized using machine learning.
[0088] For example, a computer device can acquire pre-set optimization conditions and determine whether the target code meets these conditions. For instance, the computer device can analyze loops in the target code to determine if loop optimization is possible; it can analyze register allocation strategies during target code execution to determine if register allocation strategies can be optimized; and it can analyze the memory state during target code execution to determine if memory mismatch issues exist, thereby determining whether optimization is necessary. When the target code is determined to meet the optimization conditions, the computer device can perform code optimization processing on the target code to obtain optimized target code, which can then be executed to achieve the corresponding computational task. The code optimization processing includes at least one of loop unrolling, register allocation, memory alignment, thread block adjustment, or grid adjustment, thereby ensuring the execution efficiency of the target code.
[0089] In this embodiment, when it is determined that the target code meets the optimization conditions, the computer device can perform code optimization processing on the target code, including loop unrolling, register allocation, memory alignment, thread block adjustment, or grid adjustment, thereby improving the execution efficiency of the target code.
[0090] This application also provides an application scenario in which the above-described code generation method is applied. Specifically, the code generation method is applied in this scenario as follows:
[0091] In programming platforms, such as Java programming platforms, users can edit variable parameters using a computer device. The computer device then generates corresponding initialization variables based on these user-edited parameters. Users can trigger variable configuration operations on these initialization variables to configure them, and the computer device can obtain the target expression based on these user-triggered variable configuration operations. Users can also configure the code to be generated, obtaining code configuration information. The computer device can then generate corresponding code snippets based on the user-configured code information and the target expression, and combine these code snippets to obtain the target code. The target code can be Java code, and running this Java code can perform big data processing and analysis.
[0092] This application also provides an application scenario in which the above-described code generation method is applied. Specifically, the code generation method is applied in this scenario as follows:
[0093] This embodiment relates to the fields of computer software and hardware, specifically to an expression and code generation method based on CUDA (Compute Unified Device Architecture) for high-performance computing (HPC) and graphics processing unit (GPU) acceleration scenarios. CUDA is a general-purpose parallel computing architecture that uses GPUs for high-performance computing; high-performance computing is a technology that utilizes parallel computing resources to solve complex computational problems.
[0094] With the development of big data, artificial intelligence, and machine learning, GPU-accelerated computing has become one of the main means to improve computing efficiency. However, existing CUDA programming is complex and error-prone, necessitating a simplification of the CUDA programming process and an increase in the automation of code generation. Specifically, existing CUDA programming mainly involves manually writing CUDA code and using some rudimentary code generation tools. These tools are often limited in functionality and struggle to handle complex expression generation and code optimization. Manually writing CUDA code is prone to inefficiency and errors, while existing tools lack sufficient support for complex expressions and dynamic memory operations. In other words, manually writing CUDA code in traditional techniques is complex, error-prone, and difficult to maintain and optimize. Traditional code generation tools are limited in functionality and cannot support complex expressions and dynamic memory operations. Furthermore, traditional methods struggle to handle expression generation and operations for different types of CUDA vectors (such as float2, float4, and int4), lacking versatility.
[0095] Based on this, the code generation method provided in this embodiment, by introducing a general expression generation class (CUDAExpression) and a CUDA-specific vector expression class (TensorExpression), achieves automated generation of complex CUDA expressions and code, improving the efficiency of CUDA programming and the maintainability of the code. Specifically, CPPExpression is a class used to generate and manage expressions, while TensorExpression is a class used to process CUDA vector types.
[0096] Specifically, such as Figure 4 As shown, the architecture of the code generation system to which the code generation method provided in this embodiment belongs includes a variable management module, an expression generation module, a code generation module, and an optimization module. The variable management module manages and generates CUDA variables, including basic variables and vector type variables; the expression generation module generates and manages various CUDA expressions, including variables, operators, pointers, and strings; the code generation module automatically generates complete CUDA kernel function code based on the generated expressions and variables; and the optimization module optimizes the generated CUDA code to improve execution efficiency. The code generation method provided in this embodiment can generate CUDA expressions and code, solving the complexity and error-proneness problems in existing technologies, and improving code maintainability and versatility.
[0097] Furthermore, the variable management module is responsible for managing and generating CUDA variables, supporting both basic variables and vector type variables. Its main functions include:
[0098] Variable initialization: Generate corresponding CUDA variables based on the variable type and name input by the user.
[0099] Variable assignment: Supports assigning values to variables and generating corresponding assignment expressions.
[0100] Variable indexing: Supports indexing operations on array type variables and generates corresponding index expressions.
[0101] Vector type handling: Supports CUDA vector types (such as float2, float4, int4) and generates access expressions for corresponding components (such as .x, .y, .z, .w).
[0102] Furthermore, the expression generation module is responsible for generating and managing various CUDA expressions, and its main functions include:
[0103] Expression parsing: Parses the input expression and generates corresponding CUDA expressions based on different operators and operand types.
[0104] Operator support: Supports a variety of operators (such as addition, subtraction, multiplication, division, assignment, etc.), and generates corresponding CUDA code snippets based on the operators.
[0105] Furthermore, for the parsing and generation of variables and expressions, in addition to generating variables and expressions through template functions and static methods, a parser based on an Abstract Syntax Tree (AST) can be used to provide more flexible and scalable expression generation capabilities.
[0106] Furthermore, the code generation module is responsible for automatically generating complete CUDA kernel function code based on the generated expressions and variables. Its main functions include:
[0107] Function signature generation: Based on the input list of global pointers, generate the parameter list and function signature of the CUDA kernel function.
[0108] Variable and memory declarations: Generate variable and memory declaration code for CUDA kernel functions based on edge and buffer information in the input graph object.
[0109] Loop and calculation code generation: Generates the main loop code and offset calculation code, and calls the expression generation module to generate the corresponding CUDA expression code.
[0110] Kernel function composition: Combine all generated code snippets to form complete CUDA kernel function code.
[0111] Furthermore, the optimization module is responsible for optimizing the generated CUDA code to improve execution efficiency. The main optimization techniques include:
[0112] Loop unrolling: Loops are unrolled using the #pragmaunroll directive, reducing loop overhead.
[0113] Register allocation: Optimize register usage and reduce memory access latency.
[0114] Memory alignment: Ensures memory access is aligned, improving access speed.
[0115] In addition to optimizations such as loop unrolling, register allocation, and memory alignment, machine learning-based code optimizations can be introduced to automatically adjust thread blocks and grid sizes, thereby further improving the execution efficiency of CUDA code.
[0116] In specific implementations, such as Figure 5As shown, in the initialization phase, the user inputs variable types and names, and the system generates corresponding CUDA variables; the user inputs expressions, and the system parses and generates corresponding CUDA expressions. In the code generation phase, the system generates the parameter list and function signature of the CUDA kernel function based on the input global pointer list; the system generates variable and memory declaration code in the CUDA kernel function based on the edge and buffer information in the input graph object; the system generates the main loop code and offset calculation code, and calls the expression generation module to generate the corresponding CUDA expression code; the system combines all generated code snippets to form the complete CUDA kernel function code. In the optimization phase, the system optimizes the generated CUDA code, including techniques such as loop unrolling, register allocation, and memory alignment. In the output phase, the system outputs the generated CUDA kernel function code, which users can use in applications such as high-performance computing, artificial intelligence and machine learning, and big data processing and analysis.
[0117] In the code generation method provided in this embodiment, data generation includes user input of variable types, names, and expressions; data processing includes the system parsing variables and expressions, generating corresponding CUDA code snippets, and performing optimization; and data output includes generating complete CUDA kernel function code for user use. The code generation method provided in this embodiment can be executed by a terminal, meaning the terminal performs variable and expression parsing, code generation, and optimization. In the execution sequence, variable and expression initialization is performed first; then code generation is performed, including function signature generation, variable and memory declaration, loop and computation code generation, and kernel function composition; finally, optimization is performed, outputting the final CUDA kernel function code.
[0118] The code generation method provided in this embodiment can be applied to high-performance computing, specifically large-scale scientific computing. The CUDA code generated by this method is used for numerical calculations and simulations to improve computational efficiency and accuracy. This method can also be applied to artificial intelligence and machine learning. Specifically, during the training and inference phases, the CUDA code generated by this method accelerates the computation of deep learning models and shortens training time. Furthermore, this method can be applied to big data processing and analysis. Specifically, during the data preprocessing and analysis phases, the CUDA code generated by this method accelerates the data processing flow and improves processing speed.
[0119] The code generation method provided in this embodiment overcomes the problems of complexity and error-proneness in manually writing CUDA code in traditional technologies. It automates the generation of complex CUDA expressions and code, improving programming efficiency and code maintainability. Furthermore, it supports the processing of various CUDA vector types, enhancing the versatility and flexibility of the code generation tool.
[0120] It should be understood that, although the various steps in the flowcharts involved in the various embodiments described above are displayed in sequence according to the instructions of the arrows, these steps are not necessarily executed in sequence in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order restriction on the execution of these steps, and these steps can be executed in other orders. Moreover, at least a portion of the steps in the flowcharts involved in the various embodiments described above can include multiple steps or multiple stages, and these steps or stages are not necessarily executed and completed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a portion of steps or stages in other steps.
[0121] Based on the same inventive concept, this application also provides a code generation apparatus for implementing the code generation method described above. The solution provided by this apparatus is similar to the implementation scheme described in the above method; therefore, the specific limitations in one or more code generation apparatus embodiments provided below can be found in the limitations of the code generation method described above, and will not be repeated here.
[0122] In one exemplary embodiment, such as Figure 6 As shown, a code generation device 600 is provided, including: a variable initialization module 602, an expression acquisition module 604, a code fragment acquisition module 606, and a code fragment combination module 608, wherein:
[0123] The variable initialization module 602 is used to obtain variable parameters and generate corresponding initialization variables based on the variable parameters;
[0124] Expression acquisition module 604 is used to obtain a target expression based on variable configuration operations for the initialization variables;
[0125] The code snippet acquisition module 606 is used to acquire code configuration information and generate corresponding code snippets based on the code configuration information and the target expression;
[0126] The code snippet combination module 608 is used to obtain target code by combining the various code snippets.
[0127] In some embodiments, the code snippet obtaining module 606 is further configured to generate function parameter code snippets and function signature code snippets based on the global pointer list in the code configuration information; generate function declaration code snippets based on the object information in the code configuration information; and generate function calculation code snippets based on the computation logic information in the code configuration information and the target expression. The code snippet combining module 608 is further configured to combine the function parameter code snippets, the function signature code snippets, the function declaration code snippets, and the function calculation code snippets to obtain the target code.
[0128] In some embodiments, the variable initialization module 602 is further configured to obtain variable parameters obtained based on the variable initialization operation, the variable parameters including variable type and variable name; and generate corresponding initialization variables according to the variable type and variable name.
[0129] In some embodiments, the expression obtaining module 604 is further configured to determine a variable configuration operation for the initialization variable; and generate a corresponding target expression based on the operation information of the variable configuration operation.
[0130] In some embodiments, the operation information includes the operator and operand type; the expression obtaining module 604 is further configured to generate a corresponding target expression based on the initialization variable according to the operator and the operand type.
[0131] In some embodiments, an optimization module is further included, which is used to perform code optimization processing on the target code when the target code meets the optimization conditions, so as to obtain optimized target code; wherein the code optimization processing includes at least one of loop unrolling, register allocation, memory alignment, thread block adjustment or grid adjustment.
[0132] Each module in the aforementioned code generation device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in or independent of the processor in a computer device, or stored in the memory of a computer device as software, so that the processor can call and execute the operations corresponding to each module.
[0133] In one exemplary embodiment, a computer device is provided, which may be a server, and its internal structure diagram may be as follows: Figure 7As shown, this computer device includes a processor, memory, input / output (I / O) interfaces, and a communication interface. The processor, memory, and I / O interfaces are connected via a system bus, and the communication interface is also connected to the system bus via the I / O interfaces. The processor provides computational and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system, computer programs, and a database. The internal memory provides the environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The database stores data involved in the code generation method. The I / O interfaces are used for exchanging information between the processor and external devices. The communication interface is used for communicating with external terminals via a network connection. When the computer program is executed by the processor, it implements a code generation method.
[0134] In one exemplary embodiment, a computer device is provided, which may be a terminal, and its internal structure diagram may be as follows: Figure 8 As shown, the computer device includes a processor, memory, input / output interfaces, a communication interface, a display unit, and an input device. The processor, memory, and input / output interfaces are connected via a system bus, and the communication interface, display unit, and input device are also connected to the system bus via the input / output interfaces. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The input / output interfaces are used for exchanging information between the processor and external devices. The communication interface is used for wired or wireless communication with external terminals; wireless communication can be achieved through Wi-Fi, mobile cellular networks, Near Field Communication (NFC), or other technologies. When the computer program is executed by the processor, it implements a code generation method. The display unit is used to form a visually visible image and can be a display screen, a projection device, or a virtual reality imaging device. The display screen can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer device can be a touch layer covering the display screen, or a button, trackball or touchpad set on the computer device casing, or an external keyboard, touchpad or mouse.
[0135] Those skilled in the art will understand that Figure 7 or Figure 8The 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.
[0136] In one exemplary embodiment, a computer device is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps in the above-described method embodiments.
[0137] In one exemplary embodiment, a computer-readable storage medium is provided storing a computer program that, when executed by a processor, implements the steps in the above-described method embodiments.
[0138] In one exemplary embodiment, a computer program product is provided, including a computer program that, when executed by a processor, implements the steps in the above-described method embodiments.
[0139] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, data stored, data displayed, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of the relevant data must comply with relevant regulations.
[0140] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. Any references to memory, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile memory and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can take many forms, such as Static Random Access Memory (SRAM) or Dynamic Random Access Memory (DRAM). The databases involved in the embodiments provided in this application may include at least one type of relational database and non-relational database. Non-relational databases may include, but are not limited to, blockchain-based distributed databases. The processors involved in the embodiments provided in this application may be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, quantum computing-based data processing logic devices, artificial intelligence (AI) processors, etc., and are not limited to these.
[0141] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this application.
[0142] The above-described embodiments merely represent several implementation methods of the present application. While the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the present application. It should be noted that a person of ordinary skill in the art may make various modifications and improvements without departing from the spirit of the present application, and these modifications and improvements fall within the scope of protection of the present application. Therefore, the scope of protection of the present application shall be determined by the appended claims.
Claims
1. A code generation method, characterized in that, The method includes: The system obtains variable parameters based on variable initialization operations. When the variable parameters include a variable name but lack a variable type, it predicts the corresponding variable type based on the variable name and the actual application scenario of the code to be generated. When the variable parameters include a variable type but lack a variable name, it predicts the corresponding variable name based on the variable type and the actual application scenario of the code to be generated. The variable type is used to determine the types of data and operations that the variable supports storing, and the variable name is used to reference and manipulate the variable in the program. Based on the CUDA platform (Computing Unified Device Architecture), the system generates corresponding initialization variables according to the variable type and variable name included in the variable parameters. These initialization variables are CUDA variables. The target expression is obtained based on the variable configuration operations performed on the initialization variables; the variable configuration operations include assignment operations, indexing operations, and vector type variable configuration operations. Obtain code configuration information; based on the global pointer list in the code configuration information, generate function parameter code snippets and function signature code snippets. The function parameter code snippets include code snippets corresponding to the parameter list of the CUDA kernel function, and the function signature code snippets include code snippets corresponding to the function signature of the CUDA kernel function. Based on object information in the code configuration information, generate function declaration code snippets. The object information includes edge and buffer information in the graph object, and the function declaration code snippets include code snippets corresponding to variable and memory declarations in the CUDA kernel function. Based on computation logic information in the code configuration information and the target expression, generate function computation code snippets. The function computation code snippets include the main loop code and offset calculation code in the CUDA kernel function. The code snippets are combined in the following order: function parameter code snippet - function signature code snippet - function declaration code snippet - function evaluation code snippet to obtain the target code. The target code is subjected to optimization analysis. When the optimization analysis results indicate that there is optimizable code in the target code, code optimization processing is performed on the optimizable code in the target code to obtain the optimized target code. The code optimization processing includes at least one of loop unrolling, register allocation, memory alignment, thread block adjustment, or grid adjustment.
2. The method according to claim 1, characterized in that, The step of obtaining the target expression based on the variable configuration operation for the initialization variables includes: Determine the variable configuration operation for the initialization variable; The corresponding target expression is generated based on the operation information of the variable configuration operation.
3. The method according to claim 2, characterized in that, The operation information includes the operator and operand types; the step of generating the corresponding target expression based on the operation information configured by the variable includes: Based on the operator and the operand type, a corresponding target expression is generated based on the initialization variable.
4. A code generation device, characterized in that, The device includes: A variable initialization module is used to obtain variable parameters obtained from variable initialization operations. When the variable parameters include a variable name but lack a variable type, the module predicts the corresponding variable type based on the variable name and the actual application scenario of the code to be generated. When the variable parameters include a variable type but lack a variable name, the module predicts the corresponding variable name based on the variable type and the actual application scenario of the code to be generated. The variable type is used to determine the types of data and operation types that the variable supports storing, and the variable name is used to reference and manipulate the variable in the program. Based on the CUDA platform of the Computing Unified Device Architecture, the module generates corresponding initialization variables according to the variable types and variable names included in the variable parameters. The initialization variables are CUDA variables of the Computing Unified Device Architecture. An expression acquisition module is used to obtain a target expression based on variable configuration operations for the initialization variables; the variable configuration operations include assignment operations, indexing operations, and vector type variable configuration operations; The code snippet acquisition module is used to obtain code configuration information, and generate function parameter code snippets and function signature code snippets based on the global pointer list in the code configuration information. The function parameter code snippets include code snippets corresponding to the parameter list of the CUDA kernel function, and the function signature code snippets include code snippets corresponding to the function signature of the CUDA kernel function. Based on the object information in the code configuration information, it generates function declaration code snippets. The object information includes edge and buffer information in the graph object, and the function declaration code snippets include code snippets corresponding to variable and memory declarations in the CUDA kernel function. Based on the computation logic information and the target expression in the code configuration information, it generates function computation code snippets. The function computation code snippets include the main loop code and offset calculation code in the CUDA kernel function. The code snippet combination module is used to combine various code snippets in the order of function parameter code snippet, function signature code snippet, function declaration code snippet, and function evaluation code snippet to obtain the target code; The target code is subjected to optimization analysis. When the optimization analysis results indicate that there is optimizable code in the target code, code optimization processing is performed on the optimizable code in the target code to obtain the optimized target code. The code optimization processing includes at least one of loop unrolling, register allocation, memory alignment, thread block adjustment, or grid adjustment.
5. The apparatus according to claim 4, characterized in that, The expression acquisition module is used to determine the variable configuration operation for the initialization variable; The corresponding target expression is generated based on the operation information of the variable configuration operation.
6. The apparatus according to claim 5, characterized in that, The expression acquisition module is used to generate a corresponding target expression based on the initialization variables according to the operator and operand types.
7. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 3.
8. A 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 steps of the method according to any one of claims 1 to 3.
9. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 3.
Citation Information
Patent Citations
Source code generation program, source code generation method, and source code generation device
WO2007116490A1