Code generation method and device, computer equipment and readable storage medium

By generating initialization variables and target expressions, and generating code snippets with code configuration information, the problem of code generation complexity of parallel computing platform is solved, the code generation efficiency is improved, and it is suitable for high-performance computing and accelerated computing in graphics processing units.

CN119025090AActive Publication Date: 2024-11-26BEIJING QINGCHENG JIZHI TECHNOLOGY CO LTD

Patent Information

Application Number
CN202411207491.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-30
Publication Date
2024-11-26
Estimated Expiration
2044-08-30

Smart Images

  • Figure CN119025090A_ABST
    Figure CN119025090A_ABST
Patent Text Reader

Abstract

The invention relates to a code generation method and device, computer equipment, a computer readable storage medium and a computer program product. The method comprises the steps that variable parameters are acquired, and corresponding initialization variables are generated based on the variable parameters; obtaining a target expression according to a variable configuration operation for the initialized variable; code configuration information is obtained, and corresponding code snippets are generated according to the code configuration information and the target expression; and obtaining a target code according to the code snippets. By adopting the method, the code generation efficiency can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present 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 Art

[0002] With the rapid development of computer technology, fields such as big data, artificial intelligence (AI) and machine learning (ML) are undergoing unprecedented changes. The development of these fields has profoundly affected the production methods and business models of various industries. With the explosive growth of data volume and the sharp increase in computing complexity, traditional computing models can no longer meet the needs of efficient processing of big data and complex algorithms. In this context, parallel accelerated computing technology has quickly become a key means to improve computing efficiency. Parallel computing achieves effective utilization of computing resources and a significant increase in computing speed by decomposing computing tasks into multiple subtasks and using multiple processors or computing units to perform independent computing at the same time.

[0003] However, in the process of writing code through a parallel computing platform for parallel accelerated computing, the process of writing code for the parallel computing platform is complicated, resulting in low code generation efficiency. Summary of the invention

[0004] Based on this, it is necessary to provide a code generation method, apparatus, computer device, computer-readable storage medium and computer program product that can improve code generation efficiency in response to the above technical problems.

[0005] In a first aspect, the present application provides a code generation method, comprising:

[0006] Get variable parameters and generate corresponding initialization variables based on the variable parameters;

[0007] Obtaining a target expression according to a variable configuration operation on an initialized variable;

[0008] Obtain code configuration information, and generate corresponding code snippets according to the code configuration information and target expressions;

[0009] The target code is obtained by combining various code fragments.

[0010] In some embodiments, corresponding code snippets are generated according to code configuration information and target expressions, including: generating function parameter code snippets and function signature code snippets according to a global pointer list in the code configuration information; generating function declaration code snippets according to object information in the code configuration information; generating function calculation code snippets according to calculation logic information and target expressions in the code configuration information; and obtaining target code by combining the code snippets, including: combining function parameter code snippets, function signature code snippets, function declaration code snippets and function calculation code snippets to obtain target code.

[0011] In some embodiments, variable parameters are obtained, and corresponding initialization variables are generated based on the variable parameters, including: obtaining variable parameters obtained based on a variable initialization operation, the variable parameters including a variable type and a variable name; and generating corresponding initialization variables according to the variable type and the variable name.

[0012] In some embodiments, obtaining a target expression according to a variable configuration operation on an initialization variable includes: determining a variable configuration operation on the initialization variable; and generating a corresponding target expression according to operation information of the variable configuration operation.

[0013] In some embodiments, the operation information includes an operator and an operand type; generating a corresponding target expression according to the operation information of the variable configuration operation includes: generating a corresponding target expression based on the initialized variable according to the operator and the operand type.

[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 an 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] In a second aspect, the present application also provides a code generation device, comprising:

[0016] A variable initialization module is used to obtain variable parameters and generate corresponding initialization variables based on the variable parameters;

[0017] An expression acquisition module, used for obtaining a target expression according to a variable configuration operation on an initialization variable;

[0018] A code snippet acquisition module is used to obtain code configuration information and generate corresponding code snippets according to the code configuration information and the target expression;

[0019] The code snippet combination module is used to obtain the target code according to the combination of various code snippets.

[0020] In a third aspect, the present application further provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the following steps are implemented:

[0021] Get variable parameters and generate corresponding initialization variables based on the variable parameters;

[0022] Obtaining a target expression according to a variable configuration operation on an initialized variable;

[0023] Obtain code configuration information, and generate corresponding code snippets according to the code configuration information and target expressions;

[0024] The target code is obtained by combining various code fragments.

[0025] In a fourth aspect, the present application further provides a computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the following steps are implemented:

[0026] Get variable parameters and generate corresponding initialization variables based on the variable parameters;

[0027] Obtaining a target expression according to a variable configuration operation on an initialized variable;

[0028] Obtain code configuration information, and generate corresponding code snippets according to the code configuration information and target expressions;

[0029] The target code is obtained by combining various code fragments.

[0030] In a fifth aspect, the present application further provides a computer program product, including a computer program, which implements the following steps when executed by a processor:

[0031] Get variable parameters and generate corresponding initialization variables based on the variable parameters;

[0032] Obtaining a target expression according to a variable configuration operation on an initialized variable;

[0033] Obtain code configuration information, and generate corresponding code snippets according to the code configuration information and target expressions;

[0034] The target code is obtained by combining various code fragments.

[0035] The above-mentioned code generation method, apparatus, computer device, computer-readable storage medium and computer program product generate corresponding initialization variables based on the acquired variable parameters, obtain the target expression according to the variable configuration operation on the initialization variables, generate corresponding code fragments according to the code configuration information and the target expression, and then obtain the target code according to the combination of the code fragments. By generating initialization variables through variable parameters, obtaining the target expression through the variable configuration operation on the initialization variables, generating various code fragments according to the code configuration information and the target expression, and obtaining the target code by combining various code fragments, the code generation process can be simplified, thereby improving the efficiency of code generation. BRIEF DESCRIPTION OF THE DRAWINGS

[0036] In order to more clearly illustrate the technical solutions in the embodiments of the present application or related technologies, the drawings required for use in the embodiments of the present application or related technical descriptions will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.

[0037] Figure 1 An application environment diagram of a code generation method in an embodiment;

[0038] Figure 2 A schematic diagram of a flow chart of a code generation method in an embodiment;

[0039] Figure 3 A schematic diagram of a process of generating a code snippet and obtaining a target code in one embodiment;

[0040] Figure 4 A schematic diagram of the architecture of a code generation system in one embodiment;

[0041] Figure 5 is a flowchart of a code generation method in yet another embodiment;

[0042] Figure 6 is a structural block diagram of a code generating device in an embodiment;

[0043] Figure 7 is an internal structure diagram of a computer device in one embodiment;

[0044] Figure 8 FIG. 4 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION

[0045] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.

[0046] The code generation method provided in the embodiment of the present application can be applied to Figure 1 In the application environment shown. Among them, the terminal 102 communicates with the server 104 through the network. The data storage system can store the data that the server 104 needs to process. The data storage system can be integrated on the server 104, or it can be placed on the cloud or other network servers. The user can edit the variable parameters through the terminal 102, and the terminal 102 sends the variable parameters edited by the user to the server 104, and the server generates the corresponding initialization variables based on the variable parameters. The user can trigger the variable configuration operation through the terminal 102 to configure the generated initialization variables, and the terminal 102 can send the variable configuration operation triggered by the user to the server 104, and the server 104 can obtain the target expression according to the variable configuration operation for the initialization variable. The server 104 can obtain the code configuration information, and the code configuration information can be sent to the server 104 by the user through the terminal 102 after the terminal 102 configuration is completed. The server 104 generates the corresponding code fragments according to the code configuration information and the target expression, and then obtains the target code according to the combination of each code fragment. The server 104 can feed back the obtained target code to the terminal 102, so that the terminal 102 can execute the target code to perform corresponding computing tasks, such as performing parallel computing such as data preprocessing, feature extraction, and model training by executing the code. In some embodiments, code generation can also be implemented by the terminal 102 or the server 104 alone, that is, the terminal 102 or the server 104 directly executes each step of the code generation method.

[0047] The terminal 102 may be, but is not limited to, various personal computers, laptops, smart phones, tablet computers, IoT devices, and portable wearable devices. The IoT devices may be smart speakers, smart TVs, smart air conditioners, smart car devices, projection devices, etc. Portable wearable devices may be smart watches, smart bracelets, head-mounted devices, etc. Head-mounted devices may be virtual reality (VR) devices, augmented reality (AR) devices, smart glasses, etc. The server 104 may be an independent physical server, a server cluster or distributed system consisting of multiple physical servers, or a cloud server that provides cloud computing services.

[0048] In an exemplary embodiment, Figure 2 As shown, a code generation method is provided, which is applied to Figure 1 The terminal 102 or the server 104 or other computer devices are used as an example to illustrate, including the following steps 202 to 208. Among them:

[0049] Step 202: Obtain variable parameters, and generate corresponding initialization variables based on the variable parameters.

[0050] Among them, the variable parameters are the parameter information of the variables to be generated, which may include but are not limited to the variable name, variable type and other information. The variable parameters can be used to initialize the variable to obtain the initialized variable, such as declaring the initial value, type, name, etc. of the variable. In programming, a variable is a container for storing information, such as numbers, text, images and other information. Variables allow data to be stored and modified during program execution, that is, variables allow programs to process inputs, store data, perform calculations and generate outputs.

[0051] Optionally, the computer device can obtain variable parameters, and the variable parameters can be edited by the user according to actual coding needs, such as being input by the user through an input device. The computer device generates corresponding initialization variables based on the obtained variable parameters, thereby realizing declaration and initialization of the variables, so that code editing can be performed using the variables. In some embodiments, code generation can be achieved through a parallel computing platform, such as a CUDA (Compute Unified Device Architecture) platform. Specifically, the computer device can generate corresponding initialization variables based on the variable parameters based on the CUDA platform, and the initialization variables can match the CUDA platform, that is, the generated initialization variables can be CUDA variables. Among them, CUDA variables can be divided into global memory variables, shared memory variables, constant memory variables, texture memory variables, and register variables, etc. according to their storage location and characteristics.

[0052] Step 204, obtaining a target expression according to the variable configuration operation on the initialized variable.

[0053] Among them, the variable configuration operation is a configuration operation for the initialized variable, so as to configure the corresponding target expression based on the initialized variable. The variable configuration operation may include configuration operations for the operation information such as the operator and operand type of the initialized variable, so that the initialized variable can be combined and expressed according to the operator and operand type to obtain the target expression. An expression is a sequence composed of operators (such as addition, subtraction, multiplication, division, etc.) and operands (such as variables, constants, literals, etc.), which is used to represent a calculation process or value. The expression may include but is not limited to arithmetic expressions including basic mathematical operations such as addition, subtraction, multiplication, and division, relational expressions for comparing the size or equality of two values, logical expressions for controlling program flow, assignment expressions for assignment, conditional expressions for conditional judgment, function call expressions for calling functions, and complex expressions composed of multiple sub-expressions combined by operators. The target expression is an expression determined based on the variable configuration operation and the initialized variable to represent the calculation process or value constructed by the initialized variable.

[0054] Exemplarily, the variable configuration operation can be triggered by the user according to actual programming needs, such as the user can input an expression based on a high-level language or assembly language based on a computer device, thereby triggering the variable configuration operation. The computer device can generate a corresponding target expression based on the variable configuration operation in combination with the initialization variables. For example, the initialization variables may include variables p1, variables p2, and variables p3. The variable configuration operation triggered by the user is "assign the sum of variables 1 and 2 to variable 3", and the target expression can be obtained as "variable p3 = variable p1 + variable p2". In some embodiments, the user can trigger different variable configuration operations for the initialization variables according to actual programming needs, such as according to the needs of the computing task, so as to construct a corresponding target expression based on the initialization variables through the variable configuration operation, so as to represent the corresponding computing process through the target expression.

[0055] Step 206, obtaining code configuration information, and generating corresponding code snippets according to the code configuration information and the target expression.

[0056] The code configuration information is the configuration information of the user for the code to be generated, which may include but is not limited to the defined pointer information, the buffer information of the object, the calculation logic information, etc. The code configuration information can be configured by the user according to the actual programming needs. The code snippet is the generated partial code. Multiple code snippets can be combined to obtain the complete code, and the complete code can execute and complete the corresponding calculation task.

[0057] Exemplarily, the computer device can obtain code configuration information, and the code configuration information can be configured in advance by the user, such as being configured by the user through accessing the CUDA platform through the computer device. The computer device can generate code according to 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 in the code configuration information, so that relatively independent parallel generation can be performed for each code snippet, such as generating various different code snippets such as function parameter code snippets, function signature code snippets, function declaration code snippets, function calculation code snippets, etc.

[0058] Step 208, obtaining target code according to the combination of various code fragments.

[0059] The target code is the generated code, and the specific computer device can combine the various code fragments, such as the computer device can combine the various code fragments according to the code structure conditions to obtain the target code. The code structure conditions can be determined according to the code writing rules, such as the order and nesting relationship between different code fragments.

[0060] In some embodiments, after obtaining each code snippet, the computer device can analyze each code snippet separately to determine whether each code snippet meets the optimization conditions. When there is a code snippet that meets the optimization conditions, the computer device can optimize the code snippet and then combine the code snippets to obtain the target code, so that code optimization can be performed based on the code snippet level, which is conducive to ensuring the programming quality of the target code.

[0061] In the above code generation method, the corresponding initialization variables are generated based on the obtained variable parameters, the target expression is obtained according to the variable configuration operation on the initialization variables, and the corresponding code fragments are generated according to the code configuration information and the target expression, and then the target code is obtained by combining the code fragments. By generating the initialization variables through the variable parameters, obtaining the target expression through the variable configuration operation on the initialization variables, generating the corresponding code fragments according to the code configuration information and the target expression, and obtaining the target code by combining the code fragments, the code generation process can be simplified, thereby improving the efficiency of code generation.

[0062] In an exemplary embodiment, Figure 3 As shown, the process of generating code fragments and combining them to obtain the target code, that is, generating corresponding code fragments according to the code configuration information and the target expression, and obtaining the target code according to the combination of the code fragments, includes steps 302 to 308. Among them:

[0063] Step 302: Generate a function parameter code snippet and a function signature code snippet according to the global pointer list in the code configuration information.

[0064] Among them, 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 for, but not limited to, managing resources, implementing cross-function or cross-module data sharing, etc. The function parameter code snippet is the code snippet corresponding to the function parameter in the code to be generated; the function signature code snippet is the code snippet corresponding to the signature information of the function in the code to be generated. A function is a code block that encapsulates reusable code. A function can be used to complete a specific task or calculation. A function can accept input (called parameters or independent variables), perform a series of operations, and may return output (called return value).

[0065] Exemplarily, the code configuration information may include a global pointer list, and the global pointer list may include pointers defined in a global scope. The computer device may generate corresponding function parameter code snippets and function signature code snippets based on the global pointer list. For example, in a CUDA platform, the function parameter code snippet may include a code snippet corresponding to a parameter list of a CUDA kernel function, and the function signature code snippet may include a code snippet corresponding to a function signature of a CUDA kernel function.

[0066] Step 304: Generate a function declaration code snippet according to the object information in the code configuration information.

[0067] Among them, an object is an entity composed of attributes and methods. Attributes are the state information of an object, which are used to describe the characteristics of an object; methods are the behavior of an object, which define the operations that an object can perform. Object information is the information carried by an object, which may include information such as the attributes and methods of an object. The function declaration code snippet is the code snippet corresponding to the function declaration in the code to be generated.

[0068] Exemplarily, the code configuration information may include object information, and the computer device may generate corresponding function declaration code snippets based on the object information. For example, in a CUDA platform, the computer device may generate code snippets corresponding to variable and memory declarations in a CUDA kernel function based on edge and buffer information in a graph object.

[0069] Step 306: Generate a function calculation code snippet according to the calculation logic information and the target expression in the code configuration information.

[0070] The calculation logic information is used to describe the processing logic of the calculation required to be performed in the code to be generated. The function calculation code snippet is a code snippet for the function to perform calculations in the code to be generated. Exemplarily, the code configuration information may include calculation logic information, and the computer device may generate the corresponding function calculation code snippet based on the calculation logic information and the target expression. For example, in the CUDA platform, the computer device may generate the main loop code and the offset calculation code in the CUDA kernel function according to the calculation logic information by calling the target expression.

[0071] Step 308: Combine the function parameter code snippet, the function signature code snippet, the function declaration code snippet, and the function calculation code snippet to obtain a target code.

[0072] Exemplarily, the computer device may combine the various code snippets to obtain the target code, and specifically may combine the function parameter code snippet, the function signature code snippet, the function declaration code snippet, and the function calculation code snippet to obtain the target code. For example, in the CUDA platform, the computer device may combine the function parameter code snippet, the function signature code snippet, the function declaration code snippet, and the function calculation code snippet to obtain a complete CUDA kernel function code. In some embodiments, the computer device may combine the various code snippets according to the code structure conditions, and specifically may combine the various code snippets in the order of "function parameter code snippet-function signature code snippet-function declaration code snippet-function calculation code snippet" to obtain the target code.

[0073] In this embodiment, the computer device generates a function parameter code snippet and a function signature code snippet based on the global pointer list in the code configuration information, generates a function declaration code snippet based on the object information in the code configuration information, generates a function calculation code snippet based on the calculation logic information and the target expression in the code configuration information, and combines the function parameter code snippet, the function signature code snippet, the function declaration code snippet and the function calculation code snippet to obtain the target code, so that each code snippet can be generated through the code configuration information and the target expression and combined to obtain the target code, which simplifies the code writing operation and is conducive to improving the code generation efficiency.

[0074] In an exemplary embodiment, variable parameters are obtained, and corresponding initialization variables are generated based on the variable parameters, including: obtaining variable parameters obtained based on a variable initialization operation, the variable parameters including a variable type and a variable name; and generating corresponding initialization variables according to the variable type and the variable name.

[0075] The variable initialization operation is an operation triggered by the user to initialize the variable, such as a variable editing operation triggered by the user. The variable parameters obtained based on the variable initialization operation include the variable type and the variable name. The variable type determines the type of data that the variable can store and the operations that can be performed on the data. The variable type may include but is not limited to numeric, character, Boolean, composite or vector types; the variable name is an identifier specified by the programmer for the variable, which is used to reference and operate the variable in the program.

[0076] Exemplarily, a user may perform variable initialization by triggering a variable initialization operation, and the computer device may detect the variable initialization operation triggered by the user, and determine variable parameters based on the variable initialization operation, wherein the variable parameters include a variable type and a variable name. The computer device may generate a corresponding initialization variable based on the variable type and the variable name, such as defining an original variable, and initializing the original variable according to the variable type and the variable name, thereby obtaining an initialized variable.

[0077] In some embodiments, when at least one of the variable type and the variable name is missing in the variable parameter obtained based on the variable initialization operation, the computer device can perform a prediction supplement. Specifically, the computer device can obtain the variable parameter obtained based on the variable initialization operation, and when the variable parameter includes one of the variable type and the variable name, that is, when the variable parameter lacks the variable name or the variable type, the computer device performs a prediction supplement. For example, when the variable parameter includes the variable name but lacks 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, when the variable name is age, the predicted variable type can be a numerical type. For another example, when the variable parameter includes the variable type but lacks 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 a numerical type and the 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, so as to initialize the variables in the code generation, which can simplify the variable initialization operation and help improve the code generation efficiency.

[0079] In an exemplary embodiment, obtaining a target expression according to a variable configuration operation on an initialization variable includes: determining the variable configuration operation on the initialization variable; and generating a corresponding target expression according to operation information of the variable configuration operation.

[0080] Among them, the variable configuration operation can be triggered by the user for the initialization variable, and can specifically include the configuration operation for the operator and operand type. The operation information is used to describe the variable configuration operation, and can specifically include but is not limited to the operation information such as the operator and operand type for the initialization variable. In addition, the variable configuration operation can also include various configuration operations such as assignment operation, index operation, vector type variable configuration operation, and the operation information of the variable configuration operation can include various operation information such as the corresponding assignment data, index information, vector type information, etc.

[0081] Exemplarily, the computer device may determine the variable configuration operation of the user for the initialized variable, and determine the operation information of the variable configuration operation, such as the operation information such as the operator and operand type for the initialized variable. The computer device may generate a corresponding target expression according to the operation information of the variable configuration operation. For example, when the variable configuration operation includes an assignment operation, the computer device may assign a value to the initialized variable according to the assignment data of the assignment operation, thereby obtaining a corresponding assignment expression; when the variable configuration operation includes an index operation, the computer device may generate a corresponding index expression based on the index information of the index operation.

[0082] In this embodiment, the computer device generates a corresponding target expression according to the operation information of the variable configuration operation for the initialization variable, which can simplify the expression generation operation and help improve the code generation efficiency.

[0083] In an exemplary embodiment, the operation information includes an operator and an operand type; generating a corresponding target expression according to the operation information of the variable configuration operation includes: generating a corresponding target expression based on the initialized variable according to the operator and the operand type.

[0084] Exemplarily, when the operation information of the variable configuration operation includes an operator and an operand type, it indicates that a corresponding expression needs to be constructed according to the operator and operand type. The computer device can generate a corresponding target expression according to the operator and operand type and based on the initialized variables.

[0085] In this embodiment, the computer device generates a corresponding target expression according to the operator and operand type, which can simplify the expression generation operation and help improve the 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 an 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] Among them, the optimization conditions are used to determine whether further adjustments and optimizations need to be made to the generated target code. For example, optimization analysis can be performed on the target code. When the optimization analysis results show that there is optimizable code in the target code, it can be considered that the target code meets the optimization conditions, that is, the target code needs to be optimized and updated. Loop unrolling can be achieved through the #pragma unroll instruction to reduce loop overhead, register allocation can be optimized by optimizing the allocation and use of registers to reduce memory access latency, memory alignment can be optimized by ensuring memory access alignment to increase access speed, and thread block adjustment or grid adjustment can be optimized through automatic adjustment through machine learning.

[0088] Exemplarily, the computer device can obtain the preset optimization conditions and make a judgment on the target code to determine whether the target code meets the optimization conditions. For example, the computer device can analyze the loop in the target code to determine whether the loop can be optimized; for another example, the computer device can analyze the register allocation strategy when the target code is executed to determine whether the register allocation strategy can be optimized; for another example, the computer device can analyze the memory state when the target is executed to determine whether there is a memory misalignment problem, so as to determine whether optimization is needed. When it is determined that the target code meets the optimization conditions, the computer device can perform code optimization processing on the target code to obtain the optimized target code, so as to implement the corresponding computing task by executing the optimized target code. Among them, the code optimization processing includes at least one of loop unrolling, register allocation, memory alignment, thread block adjustment or grid adjustment, so as to ensure 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 at least one of loop unrolling, register allocation, memory alignment, thread block adjustment or grid adjustment, thereby improving the execution efficiency of the target code.

[0090] The present application also provides an application scenario, and the application scenario applies the above code generation method. Specifically, the application of the code generation method in the application scenario is as follows:

[0091] In a programming platform, such as a Java programming platform, a user can edit variable parameters through a computer device, and the computer device generates corresponding initialization variables based on the variable parameters edited by the user. The user can trigger a variable configuration operation for the initialization variable to configure the initialization variable, and the computer device can obtain a target expression based on the variable configuration operation triggered by the user. The user can configure the code to be generated to obtain code configuration information. The computer device can generate corresponding code snippets based on the code configuration information configured by the user and the target expression, and combine the code snippets to obtain the target code, which can be a Java code. By running the Java code, big data processing and analysis can be performed.

[0092] The present application also provides an application scenario, and the application scenario applies the above code generation method. Specifically, the application of the code generation method in the application scenario is as follows:

[0093] This embodiment relates to the field of computer software and hardware, and specifically to an expression generation and code generation method based on CUDA (Compute Unified Device Architecture), which is used in high performance computing (HPC) and graphics processing unit (GPU) accelerated computing scenarios. Among them, CUDA is a general parallel computing architecture that uses GPU for high performance computing; high performance computing is a technology that uses parallel computing resources to solve complex computing problems.

[0094] With the development of big data, artificial intelligence, machine learning and other fields, using GPU to accelerate computing has become one of the main means to improve computing efficiency. However, the existing CUDA programming is complex and error-prone. It is necessary to simplify the CUDA programming process and improve the automation of code generation. Specifically, the existing CUDA programming mainly includes manually writing CUDA code and using some preliminary code generation tools. These tools often have single functions and are difficult to handle complex expression generation and code optimization problems. Manually writing CUDA code is prone to inefficiency and errors, and existing tools do not provide sufficient support for complex expressions and dynamic memory operations. In other words, manually writing CUDA code in traditional technology is complex and error-prone, and difficult to maintain and optimize. Traditional code generation tools have single functions and cannot support complex expressions and dynamic memory operations. In addition, traditional methods are difficult to handle expression generation and operations of different types of CUDA vectors (such as float2, float4, int4), and lack versatility.

[0095] Based on this, the code generation method provided in this embodiment realizes the automatic generation of complex CUDA expressions and codes by introducing a general expression generation class (CUDAExpression) and a CUDA-specific vector expression class (TensorExpression), thereby improving the efficiency of CUDA programming and the maintainability of the code. Among them, CPPExpression is used to generate and manage expression classes, and TensorExpression is used to process CUDA vector type classes.

[0096] Specifically, 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. Among them, the variable management module is used to manage and generate CUDA variables, including basic variables and vector type variables; the expression generation module is used to generate and manage various CUDA expressions, including variables, operators, pointers and strings, etc.; the code generation module is used to automatically generate complete CUDA kernel function codes based on the generated expressions and variables; the optimization module is used to optimize the generated CUDA code to improve execution efficiency. The code generation method provided in this embodiment can realize the generation of CUDA expressions and codes, solve the complexity and error-proneness problems in the prior art, and improve the maintainability and versatility of the code.

[0097] Furthermore, the variable management module is responsible for managing and generating CUDA variables, supporting basic variables and vector type variables. Its main functions include:

[0098] Variable initialization: Generate corresponding CUDA variables according to the variable type and name entered by the user.

[0099] Variable assignment: supports variable assignment operations and generates corresponding assignment expressions.

[0100] Variable indexing: supports indexing operations on array type variables and generates corresponding index expressions.

[0101] Vector type processing: 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. Its main functions include:

[0103] Expression parsing: Parse the input expression and generate the corresponding CUDA expression according to different operators and operand types.

[0104] Operator support: supports multiple 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 also be used to provide a more flexible and extensible expression generation function.

[0106] Furthermore, the code generation module is responsible for automatically generating complete CUDA kernel function codes based on the generated expressions and variables. Its main functions include:

[0107] Function signature generation: Generates the parameter list and function signature of the CUDA kernel function based on the input global pointer list.

[0108] Variable and memory declaration: Generate variable and memory declaration code in CUDA kernel function 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 combination: Combine all generated code snippets to form a 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 technologies include:

[0112] Loop unrolling: Use the #pragmaunroll directive to unroll the loop and reduce loop overhead.

[0113] Register allocation: Optimize register usage and reduce memory access latency.

[0114] Memory alignment: Ensure memory access alignment to improve access speed.

[0115] In addition to optimizations such as loop unrolling, register allocation, and memory alignment, machine learning-based code optimization can also be introduced to automatically adjust thread blocks and grid sizes to further improve the execution efficiency of CUDA code.

[0116] In the specific implementation, Figure 5As shown in the figure, in the initialization phase, the user inputs the variable type and variable name, and the system generates the corresponding CUDA variable; the user inputs the expression, and the system parses and generates the corresponding CUDA expression. 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 the 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 the generated code fragments to form a complete CUDA kernel function code. In the optimization phase, the system optimizes the generated CUDA code, including 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 application scenarios 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 type, name and expression; data processing includes system parsing variables and expressions, generating corresponding CUDA code snippets, and performing optimization processing; 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, that is, the terminal performs parsing of variables and expressions, code generation and optimization processing. In the execution sequence, variables and expressions are initialized first, that is, the initialization operation of variables and expressions is performed first; code generation is then performed, that is, code generation operations are then performed, including function signature generation, variable and memory declaration, loop and calculation code generation, kernel function combination, etc.; finally, optimization processing is performed, that is, the optimization processing operation is finally performed to output the final CUDA kernel function code.

[0118] The code generation method provided in this embodiment can be applied to high-performance computing, specifically in large-scale scientific computing, using the CUDA code generated by the code generation method provided in this embodiment for numerical calculations and simulations to improve computing efficiency and accuracy. The code generation method provided in this embodiment can also be applied to artificial intelligence and machine learning, specifically in the training and reasoning stages, using the CUDA code generated by the code generation method provided in this embodiment to accelerate the calculation of deep learning models and shorten training time. The code generation method provided in this embodiment can also be applied to big data processing and analysis, specifically in the data preprocessing and analysis stages, using the CUDA code generated by the code generation method provided in this embodiment to accelerate the data processing process and improve the processing speed.

[0119] The code generation method provided in this embodiment can overcome the problem that manual writing of CUDA code in traditional technology is complex and error-prone, realizes automatic generation of complex CUDA expressions and codes, improves programming efficiency and code maintainability. At the same time, it supports the processing of multiple CUDA vector types, and enhances 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 above-mentioned embodiments are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps does not have a strict order restriction, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above-mentioned embodiments can include multiple steps or multiple stages, and these steps or stages are not necessarily executed 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 part of the steps or stages in other steps.

[0121] Based on the same inventive concept, the embodiment of the present application also provides a code generation device for implementing the code generation method involved above. The implementation scheme for solving the problem provided by the device is similar to the implementation scheme recorded in the above method, so the specific limitations in one or more code generation device embodiments provided below can refer to the limitations on the code generation method above, and will not be repeated here.

[0122] In an exemplary embodiment, 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] A variable initialization module 602, used to obtain variable parameters and generate corresponding initialization variables based on the variable parameters;

[0124] An expression obtaining module 604, configured to obtain a target expression according to a variable configuration operation on the initialization variable;

[0125] The code snippet obtaining module 606 is used to obtain code configuration information and generate corresponding code snippets according to the code configuration information and the target expression;

[0126] The code fragment combination module 608 is used to obtain the target code according to the combination of the code fragments.

[0127] In some embodiments, the code snippet acquisition module 606 is also used to generate function parameter code snippets and function signature code snippets according to the global pointer list in the code configuration information; generate function declaration code snippets according to the object information in the code configuration information; generate function calculation code snippets according to the calculation logic information and the target expression in the code configuration information; the code snippet combination module 608 is also used to combine the function parameter code snippet, the function signature code snippet, the function declaration code snippet and the function calculation code snippet to obtain the target code.

[0128] In some embodiments, the variable initialization module 602 is further used to obtain variable parameters based on the variable initialization operation, wherein the variable parameters include a variable type and a variable name; and generate corresponding initialization variables according to the variable type and the variable name.

[0129] In some embodiments, the expression acquisition module 604 is further used to determine a variable configuration operation for the initialized variable; and generate a corresponding target expression according to the operation information of the variable configuration operation.

[0130] In some embodiments, the operation information includes an operator and an operand type; the expression acquisition module 604 is further used to generate a corresponding target expression based on the initialized variables according to the operator and the operand type.

[0131] In some embodiments, an optimization module is also included, which is used to perform code optimization processing on the target code when the target code meets the optimization conditions to obtain an 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 above code generation device can be implemented in whole or in part by software, hardware, or a combination thereof. Each module can be embedded in or independent of a processor in a computer device in the form of hardware, or can be stored in a memory in a computer device in the form of software, so that the processor can call and execute operations corresponding to each module.

[0133] In an exemplary embodiment, a computer device is provided. The computer device may be a server, and its internal structure diagram may be as shown in FIG. Figure 7As shown. The computer device includes a processor, a memory, an input / output interface (Input / Output, referred to as I / O) and a communication interface. Among them, the processor, the memory and the input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store data involved in the code generation method. The input / output interface of the computer device is used to exchange information between the processor and an external device. The communication interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a code generation method is implemented.

[0134] In an exemplary embodiment, a computer device is provided. The computer device may be a terminal, and its internal structure diagram may be as shown in FIG. Figure 8 As shown. The computer device includes a processor, a memory, an input / output interface, a communication interface, a display unit and an input device. The processor, the memory and the input / output interface are connected through a system bus, and the communication interface, the display unit and the input device are connected to the system bus through the input / output interface. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The input / output interface of the computer device is used to exchange information between the processor and an external device. The communication interface of the computer device is used to communicate with an external terminal in a wired or wireless manner, and the wireless manner can be implemented through WIFI, a mobile cellular network, near field communication (NFC) or other technologies. When the computer program is executed by the processor, a code generation method is implemented. The display unit of the computer device is used to form a visually visible picture, which 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 shell, or an external keyboard, touchpad or mouse.

[0135] Those skilled in the art will understand that Figure 7 or Figure 8The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0136] In an exemplary embodiment, a computer device is provided, including a memory and a processor, wherein a computer program is stored in the memory, and the processor implements the steps in the above-mentioned method embodiments when executing the computer program.

[0137] In an exemplary embodiment, a computer-readable storage medium is provided, storing a computer program, and when the computer program is executed by a processor, the steps in the above method embodiments are implemented.

[0138] In an exemplary embodiment, a computer program product is provided, including a computer program, and when the computer program is executed by a processor, the steps in the above method embodiments are implemented.

[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, stored data, displayed data, 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 relevant data must comply with relevant regulations.

[0140] A person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment method can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to the memory, database or other medium used in the embodiments provided in the present 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. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in each embodiment provided in this application may include at least one of a relational database and a non-relational database. Non-relational databases may include distributed databases based on blockchains, etc., but are not limited to this. The processor involved in each embodiment provided in this application may be a general-purpose processor, a central processing unit, a graphics processor, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, an artificial intelligence (AI) processor, etc., but are not limited to this.

[0141] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, 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 only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be construed as limiting the scope of the present application. It should be noted that, for a person of ordinary skill in the art, several modifications and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.

Claims

1. A code generation method, characterized in that: The method comprises: Obtaining variable parameters, and generating corresponding initialization variables based on the variable parameters; Obtaining a target expression according to a variable configuration operation on the initialized variable; Acquire code configuration information, and generate corresponding code snippets according to the code configuration information and the target expression; The target code is obtained by combining the various code fragments.

2. The method according to claim 1, characterized in that The generating corresponding code snippets according to the code configuration information and the target expression includes: Generate a function parameter code snippet and a function signature code snippet according to the global pointer list in the code configuration information; Generate a function declaration code snippet according to the object information in the code configuration information; Generate a function calculation code snippet according to the calculation logic information in the code configuration information and the target expression; The step of obtaining the target code by combining the code fragments comprises: The function parameter code fragment, the function signature code fragment, the function declaration code fragment and the function calculation code fragment are combined to obtain a target code.

3. The method according to claim 1, characterized in that The obtaining of variable parameters and generating corresponding initialization variables based on the variable parameters includes: Obtaining variable parameters obtained based on the variable initialization operation, wherein the variable parameters include a variable type and a variable name; Generate corresponding initialization variables according to the variable type and the variable name.

4. The method according to claim 1, characterized in that: The step of obtaining a target expression according to the variable configuration operation on the initialization variable includes: Determining a variable configuration operation for the initialized variable; Generate a corresponding target expression according to the operation information of the variable configuration operation.

5. The method according to claim 4, characterized in that The operation information includes an operator and an operand type; The generating a corresponding target expression according to the operation information of the variable configuration operation includes: According to the operator and the operand type, a corresponding target expression is generated based on the initialized variables.

6. The method according to any one of claims 1 to 5, characterized in that: The method further comprises: When the target code meets the optimization condition, code optimization processing is performed on the target code to obtain an optimized target code; The code optimization process includes at least one of loop unrolling, register allocation, memory alignment, thread block adjustment or grid adjustment.

7. A code generating device, characterized in that: The device comprises: A variable initialization module, used to obtain variable parameters and generate corresponding initialization variables based on the variable parameters; An expression acquisition module, used for obtaining a target expression according to a variable configuration operation on the initialization variable; A code snippet acquisition module, used to obtain code configuration information, and generate corresponding code snippets according to the code configuration information and the target expression; The code fragment combination module is used to obtain the target code according to the combination of each of the code fragments.

8. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 6 are implemented.

9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.

10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.

Citation Information

Patent Citations

  • A method and apparatus for program generation

    CN109408043A

  • Game interface code generation method, device and equipment and storage medium

    CN112099786A

  • Code generation method based on machine learning, code editing system and storage medium

    CN113805861A

  • Code generation method and device, electronic equipment and computer readable storage medium

    CN116737160A

  • Method, device, and computer program product for generating program code

    US11106434B1

Cited By

  • Method and system for generating and optimizing CUDA (Compute Unified Device Architecture) code based on multi-dimensional feature search and enhancement

    CN120704683A