A COStream-based method for estimating data flow workload
By fine-grained calculation of the workload of the data process in COStream, combined with syntax analysis trees and compilation optimization paths, the inaccuracy problem of the workload estimation method in COStream is solved, and the efficiency and parallelism of task division are improved.
Patent Information
- Application Number
- CN202210691390.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-17
- Publication Date
- 2024-10-18
- Estimated Expiration
- 2042-06-17
AI Technical Summary
The workload estimation method in the existing data flow compiler COStream is not precise enough and fails to accurately reflect the actual execution path of program statements and the impact of compilation optimization, resulting in large estimation errors and affecting the efficiency and parallelism of task division.
Using the COStream-based refined estimation method of data process workload, by obtaining the basic operations of different data types and the number of clock cycles of operations, a syntax analysis tree is generated to calculate the workload of each node in a fine-grained manner, and consider the compiled and optimized The actual execution path and detailed calculation of the workload of complex program statements.
It improves the accuracy of workload estimation, can more accurately reflect the running time of instructions, reduces estimation errors, and enhances the efficiency and parallelism of task division.
Smart Images

Figure CN115113875B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of task workload estimation of data flow compilers, and more specifically, relates to a COStream-based data flow workload refinement estimation method. Background Art
[0002] Workload estimation is an indispensable step in multi-core compilers. This step belongs to the front-end part of the compilation process, after lexical analysis, syntax analysis, and semantic analysis, and before task division. Task division is the prerequisite for parallel computing. In order to minimize the total completion time of all parallel tasks, in addition to improving the performance of the division algorithm, it is also necessary to improve the accuracy of task workload estimation. That is, the accuracy of program workload estimation will affect the effect of task division and the parallelism of the program. Before running a program statement, the compiler needs to perform an in-depth analysis of the estimated program structure to calculate the workload of the program statement.
[0003] In the prior art, different workload estimation methods are used for different application environments. For example, for tasks in a cloud computing environment, the task communication time is used as its workload, where the communication time is the size of the task output data divided by the bandwidth between resources. This method has low estimation accuracy. The workload estimation method for computing tasks using the data flow language StreamIt is to count the number of instructions of program statements executed on each computing node in a steady-state cycle of a pipeline. This estimation method has a high computational cost.
[0004] In the data flow compiler COStream, the existing workload estimation method is to analyze the statements of the calculation function in the steady-state cycle, define different workload values for different operations and calculations, and calculate the workload sentence by sentence. This method does not distinguish the basic operation types (such as arithmetic operations, relational operations, logical operations, bit operations, assignment operations, etc.) and data types in the program statements. For different data types, general estimation values are used, which leads to large estimation errors. In addition, the workload estimation method in COStream lacks analysis of the program structure and the actual execution path of the statement, and roughly defines the workload of the statement. For example, for the external function called in the computing node, COStream does not analyze the statements of the function body, and the workload of different functions is uniformly defined as a constant value. This estimation reduces the estimation cost, but increases the inaccuracy of the estimation.
[0005] In addition, COStream ignores the impact of compilation optimization when estimating the workload of a program. Specifically, the compiler ultimately executes the executable file compiled by the compiler. The compiler optimizes the source file during the compilation phase, which changes the actual execution path of the statement. In COStream, for the statement structure optimized by the COStream compiler, the existing workload estimation method in COStream still calculates according to the statement structure before optimization. That is, for program statements that will not be executed after compiler optimization, the estimation in COStream still calculates their workload. Such an estimation method has a large error compared to the actual workload. Summary of the invention
[0006] In view of the defects of the prior art and the need for improvement, the present invention provides a COStream-based data flow workload refinement estimation method, which aims to improve the accuracy of program workload estimation in COStream and make task division for the purpose of load balancing more efficient.
[0007] To achieve the above object, the present invention provides a COStream-based data flow workload refinement estimation method, comprising:
[0008] Obtain the number of clock cycles of basic operations and basic operations of different data types in the COStream data flow language; the basic operations include unary operations, binocular operations and mathematical functions, and the basic operations include constant assignment, variable naming, variable declaration, forced type conversion, and function call;
[0009] Generate a grammar analysis tree from the program statement structure to be tested, traverse all nodes in the grammar analysis tree, and calculate the workload of each node respectively. The workload of the program statement structure to be tested is the sum of the workloads of all nodes;
[0010] Among them, if the node type in the syntax analysis tree is the node type corresponding to the basic operation or basic calculation, the number of clock cycles of the basic operation or basic calculation is directly used as the workload of the node type; if the node type in the syntax analysis tree is a compound program statement node, constant propagation is used to analyze the execution logic of the compound program statement node, and the compound program statement node is decomposed into node types corresponding to the basic operation or basic calculation and then the workload is calculated.
[0011] Furthermore, the number of clock cycles of basic operations and basic operations of different data types in the COStream data flow language is obtained through a test program, and the test program includes:
[0012] The CPU is used to execute the operation to be tested N times in a loop, and the total execution time T of the CPU is obtained. The number of clock cycles of the operation to be tested is equal to T*f / N, where f is the main frequency of the CPU, and the operation to be tested is the basic operation or basic operation to be tested.
[0013] Further, obtaining the total execution time T of the CPU includes:
[0014] The CPU is caused to execute the operation to be tested N times in a loop through a first loop structure to obtain a first execution time; wherein the first loop structure includes a statement of the operation to be tested; when the CPU starts and ends executing the first loop structure, the gettimeofday() function is used to obtain a first initial time and a first end time of the system respectively, and the difference between the first end time and the first initial time is the first execution time;
[0015] The CPU is caused to execute the operation to be tested N times in a loop through a second loop structure to obtain a second execution time; wherein the second loop structure includes two identical statements of the operation to be tested; when the CPU starts and ends executing the second loop structure, the gettimeofday() function is used to obtain a second initial time and a second end time of the system respectively, and the difference between the second end time and the second initial time is the second execution time;
[0016] The difference between the second execution time and the first execution time is the total execution time T of the CPU.
[0017] Furthermore, the compound program statement nodes include ternary operation nodes, conditional branch statement nodes, loop statement nodes and user-defined function nodes;
[0018] The conditional branch statement nodes include if conditional statement nodes, if-else conditional statement nodes and switch-case conditional statement nodes; the loop statement nodes include for loop nodes, while loop nodes and do-while loop nodes.
[0019] Further, when the compound program statement node is a ternary operation node, the expression of the ternary operation node is recorded as a?b:c, and the value of expression a is calculated by constant propagation. If the value of expression a is true, the workload of the ternary operation node is the sum of the workload of expression a and the workload of expression b; if the value of expression a is false, the workload of the ternary operation node is the sum of the workload of expression a and the workload of expression c.
[0020] When the compound program statement node is an if or if-else conditional statement node, the workload is the sum of the workload of the statements in the branch and the workload of the conditional expression;
[0021] When the composite program statement node is a switch-case conditional statement node, the value of the switch variable expression is calculated by constant propagation, and the value of the switch variable expression is matched with the case expression. If the match is successful, the workload of the switch-case conditional statement node is the sum of the workload of the switch variable expression and the workload of the statements in the case expression; if the match fails, the workload of the switch-case conditional statement node is the sum of the workload of the switch variable expression and the workload of the statements in the default expression;
[0022] When the compound program statement node is a for loop node, the workload is the workload of a single for loop body statement multiplied by the number of loops, plus the workload of the statement in the for expression;
[0023] When the compound program statement node is a while loop node, converting the while loop node into a for loop node and calculating the workload;
[0024] When the compound program statement node is a do-while loop node, first calculate the workload of executing the while loop body once, then convert the while loop node into a for loop node, and calculate the corresponding workload, the workload of the do-while loop node is the sum of the workloads calculated twice;
[0025] When the compound program statement node is a user-defined function node, the workload is the sum of the workload of the function call and the workload of the function body.
[0026] Furthermore, for custom function nodes,
[0027] When the function body is a parameterless function, the workload of the function body is the sum of the clock cycles of the basic operations or basic calculations corresponding to each statement in the function body;
[0028] When the function body is a function with parameters, the workload of the function body is calculated as follows: the actual parameter value of the function is calculated through constant propagation; the formal parameters of the function are traversed, and the formal parameters and the actual parameter values corresponding to the formal parameters are inserted into the symbol table of the COStream compiler to obtain the actual parameter values corresponding to the formal parameters; the actual parameter values are used to replace the formal parameters of the function body, and the workload of each statement in the function body is calculated;
[0029] When the function body is a recursive function with parameters, the workload of the function body is (the workload of one recursion + the workload of function call) * the number of recursions, where the calculation method of the workload of one recursion is the same as the calculation method when the function body is a function with parameters.
[0030] Furthermore, the workload of the constant assignment is 0;
[0031] The workload of the variable naming is 1 clock cycle;
[0032] For variable declaration, the workload of declaring ordinary variables is 1 clock cycle; declaring array variables, only declaring without assigning initial values, the workload is 2 clock cycles, and declaring and assigning initial values, the workload is 2 clock cycles + number of variables * 1 clock cycle;
[0033] The workload of the forced type conversion is 1 clock cycle;
[0034] The workload of the function call is 10 clock cycles.
[0035] Furthermore, it also includes that in the basic operations, the workload is not calculated for the calculation operation of adding zero, the calculation operation of subtracting zero, the calculation operation of multiplying by 1, and the calculation operation of dividing by 1;
[0036] In multiplication, if at least one of the two multipliers is 2 n , n is a positive integer, and the workload of the multiplication operation is replaced by the workload of the corresponding addition or arithmetic left shift operation;
[0037] In the Boolean expressions in the conditional judgment statement, the values of the Boolean expressions are calculated in order. If the Boolean expression is an OR operation, when the value of one of the Boolean expressions is true, the workload of the remaining uncalculated expressions is not calculated; if the Boolean expression is an AND operation, when the value of one of the Boolean expressions is false, the workload of the remaining uncalculated expressions is not calculated;
[0038] In declaration nodes, the workload of useless variables is not calculated.
[0039] In general, the above technical solutions conceived by the present invention can achieve the following beneficial effects:
[0040] (1) The COStream-based data flow workload refinement estimation method of the present invention uses clock cycles as units to estimate the workload of program statements in fine granularity. For different data types, the clock cycle is used as the unit to test the number of clock cycles of basic operations and basic operations respectively. Based on the COStream program syntax analysis tree, the program statement structure to be tested is split into different node types, and the workload of each node type is calculated respectively. Since the method of the present invention considers the data type when calculating the workload of program statements, the workload estimation method is more accurate; and the clock cycle is used as the workload estimation granularity unit, compared with the existing use of the number of instructions as the estimation unit, the estimation granularity is smaller, the calculation cost is smaller, and the running time of the instruction can be more accurately reflected; wherein, for the complex program statement node, according to its actual execution path, the workload is refined and calculated. Compared with the estimation method in the prior art in which COStream does not analyze the statements of the function body and the workload of different functions is uniformly defined as a constant value, the estimation method of the present invention has higher accuracy.
[0041] (2) Furthermore, based on the existing compilation optimization methods of the current compiler (computation optimization, expression short-circuiting, loop invariant exceptions, and dead code removal optimization), the actual execution path of the COStream compiler after optimization is taken into account when performing workload estimation. For program statements that will not be executed after the compiler optimization, the corresponding workload will not be calculated, which further improves the accuracy of workload estimation in the COStream data flow language and can perform instruction-level workload estimation for program statements. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] Figure 1 It is a flow chart of a method for fine-grained estimation of data flow program workload in an embodiment of the present invention.
[0043] Figure 2 It is a flow chart of calculating the number of for loops in an embodiment of the present invention.
[0044] Figure 3 4 is a flow chart of identifying loop invariants in an embodiment of the present invention.
[0045] Figure 4 It is a flow chart of identifying and deleting useless variable workload in an embodiment of the present invention. DETAILED DESCRIPTION
[0046] In order to make the purpose, technical solutions and advantages of the present invention more clearly understood, the present invention 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 invention and are not intended to limit the present invention. In addition, the technical features involved in the various embodiments of the present invention described below can be combined with each other as long as they do not conflict with each other.
[0047] In the present invention, the terms "first", "second", etc. in the present invention and the accompanying drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence.
[0048] The COStream-based data flow workload refinement estimation method provided by the present invention includes:
[0049] Step S1, using a test program to test the basic operations and clock cycle numbers of basic operations in the COStream data flow language for different data types;
[0050] Specifically, different data types mainly include integer int, long integer long, super long integer long long, single-precision floating point float, double-precision floating point double; basic operations mainly include unary operations, binary operations and mathematical functions, among which binary operations include arithmetic operations, relational operations, logical operations, bit operations, elementary operations and assignment operations, and mathematical functions are cmath library functions; basic operations mainly include constant assignment, variable naming, variable declaration, forced type conversion, and function call.
[0051] Step S2: Based on the test result in step S1, generate a syntax analysis tree for the program statement structure to be tested, traverse all nodes in the syntax analysis tree, and calculate the workload of each node respectively. The workload of the program statement structure to be tested is the sum of the workloads of all nodes.
[0052] The types of syntax nodes of the syntax analysis tree include: unary operation nodes, binary operation nodes, constant nodes, identifier nodes, declaration nodes, forced type conversion nodes, ternary operation nodes, if conditional statement nodes, if-else conditional statement nodes, switch-case conditional statement nodes, for loop nodes, while loop nodes, do-while loop nodes and function call nodes;
[0053] like Figure 1 As shown, in the process of traversing all nodes in the syntax analysis tree, if the syntax node type is the node type corresponding to the basic operation or the node type corresponding to the basic operation in step S1, the test result in step S1 is directly used to calculate the workload of the corresponding node type. If a monocular operation node of a different data type is identified, the workload is calculated according to the number of monocular operation clock cycles of the data type measured by the test program;
[0054] If the syntax node type is a compound program statement node, it cannot be directly included in the workload. It is necessary to analyze the structure of the program statement, use the constant propagation mechanism to analyze the execution logic of the compound program statement structure, decompose it into the node type corresponding to the basic operation or the node type corresponding to the basic operation, and then calculate the workload; among them, the compound program statement node includes ternary operation node, conditional branch statement node, loop statement node and user-defined function node, among which the conditional branch statement node includes if conditional statement node, if-else conditional statement node, switch-case conditional statement node; loop statement node for loop node, while loop node, do-while loop node.
[0055] Through the above method, the workload of each grammatical structure such as unary operation, binary operation, ternary operation, conditional branch statement, loop statement, custom function, etc. is calculated precisely.
[0056] Specifically, in this embodiment, all test experiments are performed in the Linux operating system, and the compiler used is the COStream compiler.
[0057] In step S1, the sub-steps are included: let the CPU cyclically execute each basic calculation and basic operation to be tested (i.e., the calculation to be tested) N times to obtain the total time T required for the CPU to execute. The CPU's main frequency is f. Since the time of each clock cycle is the inverse of the main frequency, it can be deduced that the number of clock cycles required for a single operation = the total execution time T*main frequency f / number of executions N.
[0058] In this embodiment, the total time T of CPU execution is calculated using the library function gettimeofday, which is used to query the current date and time. In the Linux operating system, the library function uses a cycle counting method to time, which has a high accuracy. Before and after the CPU cycles N times to execute the operation to be tested, the library function gettimeofday is used to obtain the time, and the system time is recorded. The time required for the current execution of the operation is obtained by subtracting the two obtained times.
[0059] In this embodiment, the work of cyclically executing the operation to be tested is completed using a for loop statement, and the operation statement to be tested is placed in the for loop. The number of loops N can be controlled by the conditional statement of the for loop. In order to remove the influence of the calculation contained in the for loop itself, the test program runs two loops, wherein only one operation statement to be tested is written in one loop, and it is run N times. The system time is obtained once at the beginning of the loop, and the system time is obtained again at the end of the loop. The difference between the two times is the execution time of this time; two identical operation statements to be tested are written in the other loop, and they are run N times. The system time is obtained once at the beginning of the loop, and the system time is obtained again at the end of the loop. The difference between the two times is the execution time of this time; the difference between the two execution times is the total execution time T of the CPU. The number of clock cycles of a single operation is obtained through the above formula.
[0060] Specifically, for each operation to be tested, the total CPU execution time T is obtained, including:
[0061] The CPU is caused to execute the operation to be tested N times in a loop through a first loop structure to obtain a first execution time; wherein the first loop structure includes a statement of the operation to be tested; when the CPU starts and ends executing the first loop structure, the gettimeofday() function is used to obtain a first initial time and a first end time of the system respectively, and the difference between the first end time and the first initial time is the first execution time;
[0062] The CPU is caused to execute the operation to be tested N times in a loop through the second loop structure to obtain a second execution time; wherein the second loop structure includes two identical operation statements to be tested; when the CPU starts and ends executing the second loop structure, the gettimeofday() function is used to obtain the second initial time and the second end time of the system respectively, and the difference between the second end time and the second initial time is the second execution time;
[0063] The difference between the second execution time and the first execution time is the total CPU execution time T.
[0064] Specifically, after testing the test program, it was found that the same basic operation has different clock cycles for different data types; in the basic operations, the workload of constant assignment is 0, and the workload of variable naming is 1 clock cycle; for declaring variables, if it is a normal variable, whether it is only declared without assigning an initial value, or both declared and assigned an initial value, the workload is 1 clock cycle, if it is an array variable, only declared without assigning an initial value, the workload is 2 clock cycles, and for declaration and assignment of initial values, it is necessary to determine the number of variables contained in the array, and the workload is defined as 2 clock cycles + number of variables * 1 clock cycle; for forced type conversion nodes, the workload is 1 clock cycle.
[0065] In basic operations, the workload of single-eye operations and arithmetic operations of different data types is shown in Tables 1 and 2 below:
[0066] Table 1 The number of clock cycles of monocular operations for different data types
[0067] Data Types Bitwise inversion (!) Logical NOT (~) Increment (++) Decrement (--) Negative operation (-) int 1 1 1 1 1 long 1 1 2 2 1 long long 1 1 2 2 1
[0068] Table 2 Clock cycles of arithmetic operations of different data types
[0069] Data Types Addition (+) Subtraction (-) multiplication(*) division( / ) Remainder(%) int 1 1 2 2 3 long 2 2 3 5 6 long long 2 2 5 5 6 float 4 4 6 7 - double 4 4 6 8 -
[0070] In binary operations, elementary operations include subscript operations, operations pointing to structures, and operations on structure members. The workload of the first two is counted as 0, and the workload of the structure member operation is the workload of the left expression.
[0071] In step S2, in the process of traversing all nodes in the syntax analysis tree, if the identified syntax node type is the node type corresponding to the basic operation in step S1 or the node type corresponding to the basic calculation, the test result in step S1 is directly used to calculate the workload of the corresponding node type; for example: the COStream compiler recognizes a constant node and does not count the workload; recognizes an identifier node used to name a variable, and the workload is counted as 1 clock cycle; recognizes a declaration node, and calculates according to the specific workload of declaring the variable; recognizes a forced type conversion node, and the workload is counted as 1 clock cycle.
[0072] When a monocular operation node is identified, the workload is calculated according to the test results in Table 1; when a binocular operation node is identified, if it is an assignment operation, the assignment operation includes equal to (=), addition equal to (+=), subtraction equal to (-=), multiplication equal to (*=), division equal to ( / =), remainder equal to (%=), left shift equal to (<<=), right shift equal to (>>=), bitwise AND equal to (&=), bitwise OR equal to (|=), XOR equal to (^=). Since the workload of the assignment operation itself can be counted as 0 after testing, the assignment operation only calculates the workload of the operation after the equal sign, and calculates the workload according to the test results of the number of clock cycles corresponding to the operation after the equal sign; other binocular operations calculate the corresponding workload according to the above test results.
[0073] If a compound program statement node is identified, and the compound program statement node is an if or if-else conditional statement node, the workload is the sum of the workload of the statements in the branch and the workload of the conditional expression. First, calculate the workload of the if statement conditional expression, and use constant propagation to calculate the value of the if conditional expression. For the if conditional statement node, if the value of the if conditional expression is true, continue to calculate the workload of the statements in the if branch; if it is false, it indicates that the program will not execute the statements in the branch, and the workload calculation of this node ends, that is, the workload of the statements in the branch is 0. For the if-else conditional statement node, if the value of the if conditional expression is true, it indicates that the program executes the statements in the if branch, then calculate the workload of the statements in the if branch; if the value of the if conditional expression is false, it indicates that the program executes the statements in the else branch, then calculate the workload of the statements in the else branch.
[0074] When calculating the workload of statements in a branch and the workload of conditional expressions, the statements in the branch and conditional expressions are decomposed into node types corresponding to basic operations or node types corresponding to basic operations, and then the corresponding workloads are calculated respectively.
[0075] If a switch-case conditional statement node is identified, the workload of the switch variable expression is calculated first, the value of the switch variable expression is calculated with the help of constant propagation, and then matched with the constants of the case expression in turn. When the match is successful, the workload of the statements in the case is calculated, and the workload calculation of the node is ended. At this time, the workload of the switch-case conditional statement node is the sum of the workload of the switch variable expression and the workload of the statements in the case; if the value of the switch variable expression does not match all case expressions, the workload of the statements in the default is calculated. At this time, the workload of the switch-case conditional statement node is the sum of the workload of the switch variable expression and the workload of the statements in the default. When calculating the workload of the switch variable expression, the workload of the statements in the case, and the workload of the statements in the default, the switch variable expression, the statements in the case, and the statements in the default are respectively decomposed into node types corresponding to basic operations or node types corresponding to basic operations, and then the corresponding workloads are calculated respectively.
[0076] If a ternary operation node is identified, the general expression of the ternary operation is a? b:c. The workload of expression a is calculated first, and the value of expression a is calculated using constant propagation. If the value of expression a is true, indicating that the program executes expression b, the workload of expression b is calculated, and the workload of expression c is recorded as 0. The workload of the ternary operation is the workload of expression a plus the workload of expression b. If the value of expression a is false, indicating that the program executes expression c, the workload of expression c is calculated, and the workload of expression b is recorded as 0. The workload of the ternary operation is the workload of expression a plus the workload of expression c. The workload calculation of expression a, expression b, and expression c is performed by decomposing the corresponding expressions into the node types corresponding to the basic operation or basic operation and then calculating the corresponding workload.
[0077] If a for loop node is identified, the basic method for calculating the for loop workload is to multiply the workload of a single for loop body statement by the number of loops and add the workload in the for expression. The workload of a single loop is calculated by decomposing the loop body statement into the node types corresponding to the corresponding basic operations or the node types corresponding to the basic operations according to the above method, and calculating the corresponding workload sentence by sentence.
[0078] Without considering the complex situation where the loop body statement affects the loop condition, the number of loops is only controlled by the for loop statement. The for loop statement includes three sub-expressions: a single expression, a conditional expression, and a final loop body. The single expression is executed only once and does not participate in the loop. Its function is to assign an initial value to the loop control variable. The conditional expression is executed one more time than the loop body. It is a judgment statement. When the conditional expression is true, the loop body statement is executed, otherwise it is not executed. The final loop body is executed the same number of times as the middle loop body. It is executed after the middle loop body is executed to control the change of the conditional expression. To calculate the workload of the for expression, multiply the workload of the three sub-expressions by their respective execution times.
[0079] Calculating the number of loops is the key to calculating the workload of the entire for loop node. Figure 2 As shown in the figure, it is a flowchart for calculating the number of for loops. To calculate the number of loops, first calculate the value range of the control variable in the for loop, that is, the difference of the control variable. The difference of the control variable is related to the end value of the control variable and the initial value of the control variable when the loop ends, and the connection with the previous step. Then analyze the end loop body, obtain the operator of the end loop body and the constant value involved in the operation, and calculate according to different situations.
[0080] Specifically, the initial value and the end value of the control variable are obtained from the single expression through constant propagation. If the single expression directly assigns a constant value to the control variable, the constant value is taken as the initial value of the control variable; if the single expression is a calculation expression, the initial value of the single expression is calculated using constant propagation as the initial value of the control variable. If the conditional expression directly gives a constant, that is, the right side of the relational comparison operator of the conditional expression is a constant, then the constant is taken as the end value of the control variable, otherwise the end value is calculated with the help of constant propagation;
[0081] To calculate the difference of the control variable, it is necessary to analyze the operator of the conditional expression. If the operator of the conditional expression is less than or equal to (<=) or greater than or equal to (>=), add one to the absolute value of the difference between the end value and the initial value, otherwise take the absolute value of the difference between the end value and the initial value. After obtaining the difference of the control variable, analyze the end loop body. You need to first obtain the operator of the end loop body. If the operator is self-increment (++) or self-decrement (--), the number of loops is the difference of the control variable; otherwise, further calculations are required: obtain the change in the control variable each cycle, that is, the step length of the control variable. If the operator of the end loop body is addition (+, +=) or subtraction (-, -=), calculate the number of loops by dividing the difference of the control variable by the step length; if the operator symbol is multiplication (*, *=) or division ( / , / =), use the logarithmic function to calculate the number of loops, that is, the number of loops is Where K is the end value divided by the initial value, and M is the multiple of the control variable change in each cycle calculated by constant propagation.
[0082] If the while and do-while loop nodes are identified, the calculation method of the while loop structure workload is similar to that of the for loop structure, and the workload is the number of loops * the workload of one loop. For the while loop, first convert the while loop structure to a for loop structure, and then calculate it according to the workload calculation method of the for loop. For the do-while loop, since the loop will be executed unconditionally once, it is necessary to first calculate the workload of the while loop body once, then convert the loop to a for loop, calculate the workload according to the calculation method of the for loop, and finally add the two workloads to get the workload of the do-while loop structure.
[0083] If a function call node is identified, the workload of calling the function is the sum of the workload of the function call and the workload of the function body, wherein, after testing the test function, the overhead of the function call, that is, the workload is 10 clock cycles; the function body is divided into system-provided library functions and user-defined functions. If the function body is a system-provided library function, the mathematical function tested in step S1 calculates the workload of the corresponding library function; if the function body is a user-defined function, it can be divided into a function without parameters and a function with parameters.
[0084] If it is a parameterless function, since no variables are passed in, the estimated value of the workload of the function body is a constant, and you can calculate the workload corresponding to each statement separately.
[0085] If it is a function with parameters, the parameters will affect the workload of the function body. The method for calculating the workload of the function body is as follows: the first step is to calculate the actual parameter value in advance with the help of constant propagation; the second step is to traverse the formal parameters of the function, and insert the actual parameter values corresponding to the formal parameters into the symbol table of the COStream compiler to obtain the corresponding formal parameter values; the third step is to calculate the workload of each statement in the function body sentence by sentence. During the calculation process, when the formal parameter value is needed, it is obtained from the symbol table of the COStream compiler to accurately calculate the workload of the function, where the workload of each statement is the number of clock cycles corresponding to the basic calculation or basic operation corresponding to the statement.
[0086] If it is a recursive function with parameters, the basic method for calculating the workload of the recursive function is to multiply the workload of one recursion by the number of recursions. Among them, the calculation method of the workload of one recursion is the workload calculation method of the above-mentioned function with parameters; to obtain the number of recursions, it is necessary to obtain the initial value, the termination value and the change amount of the control variable of the recursive control variable each time the recursive function is called. The loop of the recursive function is controlled by the change of the actual parameter value, so the control variable is the parameter value of the function, the initial value of the control variable is the actual parameter value passed in when calling the recursive function, and the termination value is obtained in the termination condition in the if statement. In the recursive call statement of the recursive function, the step length of the change of the control variable each time it is called is obtained, and the absolute value of the difference between the initial value and the termination value of the control variable is divided by the step length as the number of recursions of the recursive function. In addition, it should be noted that since there is a system call overhead for each call of the recursive function, it is necessary to add the workload of the function call (10 clock cycles * number of recursions) to the calculation result, that is, the total workload is the workload of one recursion multiplied by the number of recursions + (10 clock cycles * number of recursions).
[0087] In the process of calculating the workload sentence by sentence, each sentence can be mapped to a corresponding basic calculation and a node type corresponding to the basic operation.
[0088] Through the above method, the number of clock cycles required for basic operations is defined as its workload, and the workload of program statements is estimated and the structure of program statements is analyzed. The workload of each grammatical structure such as unary operations, binary operations, ternary operations, conditional branch statements, loop statements, custom functions, etc. can be accurately calculated.
[0089] Furthermore, as a preferred embodiment, the present invention, on the basis of calculating the above workload, also takes into account the compilation optimization methods existing in the COStream data flow compiler, including calculation optimization, expression short-circuit optimization, loop invariant externalization, and dead code deletion optimization. The original workload estimation method is adjusted according to the calculation actually performed after the compilation optimization, and the workload is estimated according to the calculation path after the compilation optimization, which specifically includes:
[0090] Workload estimation optimization for computational optimization: computational optimization is an optimization method for basic operations. For single or multiple computational operations that do not produce any changes to the overall computation of the basic operations, the workload is recorded as 0; specifically, it includes computational operations of adding zero, subtracting zero, multiplying by 1, and dividing by 1. Specifically, if the operator is identified as addition (+) or subtraction (-), and the left subtree or right subtree of the node is a constant 0, the workload of the node is omitted and counted as 0. If the operator is identified as multiplication (*) or division ( / ), and the left subtree or right subtree of the node is a constant 1, the workload of the node is omitted and counted as 0.
[0091] For multiplication operations, where at least one of the two multipliers is 2 n , n is a positive integer, the compiler will optimize it to an equivalent addition or arithmetic left shift operation, and calculate the workload, that is, when the value of the left subtree or / and right subtree of the multiplication operation is 2 n , replacing the work of multiplication with the work of addition or left shift.
[0092] Workload estimation for short-circuiting expressions: For Boolean expressions in conditional statements, the compiler will usually execute jump instructions and omit the remaining calculations when it can determine the truth of the expression. For example, in an OR operation containing multiple sub-expressions, the values of each expression are calculated in sequence. If the result of one expression is true, the result of the entire expression is true, and the compiler does not need to calculate the values of the remaining expressions, that is, the workload of the remaining expressions is 0. Similarly, in an AND operation, the values of each expression are calculated in sequence. If the result of one expression is false, the values of the remaining expressions are not calculated, that is, the workload of the remaining expressions is 0.
[0093] When estimating the workload, constant propagation is used to pre-calculate the Boolean expression values in the conditional statement. In the OR operation, if the value of a sub-expression is calculated to be true, the workload of the remaining sub-expressions will not be calculated. Similarly, in the AND operation, if the value of a sub-expression is calculated to be false, the workload of the remaining sub-expressions will not be calculated.
[0094] Workload estimation optimization for loop invariant extraction: When calculating the workload of statements in the loop body, when a loop invariant expression is identified, that is, the loop control variable is a loop invariant expression, the workload of the loop invariant expression is calculated separately once, and its value is not included in the sum of the loop body workload. After calculating the loop body workload, the workload of running the loop invariant expression once is added. For the workload of other loop structures, the workload of the statements in the loop body is still calculated sentence by sentence, and the sum of the workload of the statements in the loop body is multiplied by the number of loops to obtain the workload of the entire loop structure. Specifically, the workload calculation method for the for loop structure containing loop invariant expressions is as follows: Figure 3 As shown, declare a linked list list and store the loop control variable in the linked list; traverse all variables to determine whether the variable is a loop variable related to the control variable. If the variable is a loop variable related to the control variable, add all the loop variables related to the control variable to the linked list list, traverse the statements in the loop body, and the expression that does not contain the loop variable is the loop invariant.
[0095] Effort estimation optimization for dead code removal: For declaration nodes, the effort of compiler-defined but unused variables is recorded as 0. Dead variables are identified first, and then the effort estimate of defining such variables is subtracted. Figure 4 This is a flowchart for identifying and deleting useless variable workload. First, declare a linked list list, save the identifier nodes defined in the declaration node in the linked list, if an identifier is identified in a non-declaration node, it means that the variable represented by the identifier is used by other statements, and the variable represented by the identifier is not a useless variable, then delete the variable represented by the identifier in the linked list; traverse the entire syntax analysis tree, all defined and used variables have been deleted from the linked list, and the remaining are useless variables that are defined but not used, traverse these useless variables, calculate the workload of defining these useless variables, and subtract the workload of this part of useless variables from the workload.
[0096] The COStream-based data flow workload refinement estimation method of the present invention uses clock cycles as units to estimate the workload of program statements in fine granularity. For different data types, the clock cycles are used as units to test the number of clock cycles of basic operations and basic operations respectively. Based on the COStream program syntax analysis tree, the program statement structure to be tested is split into different node types, and the workload of each node type is calculated respectively. Since the method of the present invention considers the data type when calculating the workload of program statements, the workload estimation method is more accurate; and the clock cycle is used as the workload estimation granularity unit, compared with the existing use of the number of instructions as the estimation unit, the estimation granularity is smaller, the calculation cost is smaller, and the running time of the instruction can be more accurately reflected; wherein, for the compound program statement node, according to its actual execution path, the workload is refined and calculated. Compared with the estimation method in the prior art in which COStream does not analyze the statements of the function body and the workload of different functions is uniformly defined as a constant value, the estimation method of the present invention has higher accuracy.
[0097] Furthermore, based on the existing compilation optimization methods of current compilers (computation optimization, expression short-circuiting, loop invariant exceptions, and dead code removal optimization), the actual execution path of the COStream compiler after optimization is taken into account when estimating the workload. For program statements that will not be executed after compiler optimization, the corresponding workload will not be calculated, which further improves the accuracy of workload estimation in the COStream data flow language.
[0098] It will be easily understood by those skilled in the art that the above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the protection scope of the present invention.
Claims
1. A COStream-based method for estimating the workload of data flows. It is characterized in that include: Get the number of clock cycles of basic operations and basic operations of different data types in COStream data flow language; The basic operations include unary operations, binocular operations and mathematical functions, and the basic operations include constant assignment, variable naming, variable declaration, forced type conversion and function call; Generate a grammar analysis tree from the program statement structure to be tested, traverse all nodes in the grammar analysis tree, and calculate the workload of each node respectively. The workload of the program statement structure to be tested is the sum of the workloads of all nodes; Wherein, if the node type in the grammar analysis tree is the node type corresponding to the basic operation or basic operation, the number of clock cycles of the basic operation or basic operation is directly used as the workload of the node type; if the node type in the grammar analysis tree is a compound program statement node, constant propagation is used to analyze the execution logic of the compound program statement node, and the compound program statement node is decomposed into the node type corresponding to the basic operation or basic operation and then the workload is calculated; The number of clock cycles of basic operations and basic operations of different data types in the COStream data flow language is obtained through a test program, and the test program includes: The CPU executes the operation to be tested N times in a loop, and the total execution time T of the CPU is obtained. The number of clock cycles of the operation to be tested is equal to T*f / N, where f is the main frequency of the CPU, and the operation to be tested is the basic operation or basic operation to be tested; Get the total execution time T of the CPU, including: The CPU is caused to execute the operation to be tested N times in a loop through a first loop structure to obtain a first execution time; wherein the first loop structure includes a statement of the operation to be tested; when the CPU starts and ends executing the first loop structure, the gettimeofday() function is used to obtain a first initial time and a first end time of the system respectively, and the difference between the first end time and the first initial time is the first execution time; The CPU is caused to execute the operation to be tested N times in a loop through a second loop structure to obtain a second execution time; wherein the second loop structure includes two identical statements of the operation to be tested; when the CPU starts and ends executing the second loop structure, the gettimeofday() function is used to obtain the second initial time and the second end time of the system respectively, and the difference between the second end time and the second initial time is the second execution time; The difference between the second execution time and the first execution time is the total execution time T of the CPU.
2. The method according to claim 1, It is characterized in that The compound program statement nodes include ternary operation nodes, conditional branch statement nodes, loop statement nodes and user-defined function nodes; The conditional branch statement nodes include if conditional statement nodes, if-else conditional statement nodes and switch-case conditional statement nodes; The loop statement nodes include a for loop node, a while loop node, and a do-while loop node.
3. The method according to claim 2, It is characterized in that When the compound program statement node is a ternary operation node, the expression of the ternary operation node is recorded as a?b:c, and the value of expression a is calculated by constant propagation. If the value of expression a is true, the workload of the ternary operation node is the sum of the workload of expression a and the workload of expression b; If the value of expression a is false, the workload of the ternary operation node is the sum of the workload of expression a and the workload of expression c; When the compound program statement node is an if or if-else conditional statement node, the workload is the sum of the workload of the statements in the branch and the workload of the conditional expression; When the composite program statement node is a switch-case conditional statement node, the value of the switch variable expression is calculated by constant propagation, and the value of the switch variable expression is matched with the case expression. If the match is successful, the workload of the switch-case conditional statement node is the sum of the workload of the switch variable expression and the workload of the statements in the case expression; If the match fails, the workload of the switch-case conditional statement node is the sum of the workload of the switch variable expression and the workload of the statements in the default expression; When the compound program statement node is a for loop node, the workload is the workload of a single for loop body statement multiplied by the number of loops, plus the workload of the statement in the for expression; When the compound program statement node is a while loop node, converting the while loop node into a for loop node and calculating the workload; When the compound program statement node is a do-while loop node, first calculate the workload of executing the while loop body once, then convert the while loop node into a for loop node, and calculate the corresponding workload, the workload of the do-while loop node is the sum of the workloads calculated twice; When the compound program statement node is a user-defined function node, the workload is the sum of the workload of the function call and the workload of the function body.
4. The method according to claim 3, It is characterized in that For custom function nodes, When the function body is a parameterless function, the workload of the function body is the sum of the clock cycles of the basic operations or basic calculations corresponding to each statement in the function body; When the function body is a function with parameters, the workload of the function body is calculated as follows: the actual parameter value of the function is calculated through constant propagation; the formal parameters of the function are traversed, and the formal parameters and the actual parameter values corresponding to the formal parameters are inserted into the symbol table of the COStream compiler to obtain the actual parameter values corresponding to the formal parameters; the actual parameter values are used to replace the formal parameters of the function body, and the workload of each statement in the function body is calculated; When the function body is a recursive function with parameters, the workload of the function body is (the workload of one recursion + the workload of function call) * the number of recursions, where the workload of one recursion is calculated in the same way as when the function body is a function with parameters.
5. The method according to claim 4, It is characterized in that The workload of the constant assignment is 0; The workload of the variable naming is 1 clock cycle; For variable declaration, the workload of declaring ordinary variables is 1 clock cycle; declaring array variables, only declaring without assigning initial values, the workload is 2 clock cycles, and declaring and assigning initial values, the workload is 2 clock cycles + number of variables * 1 clock cycle; The workload of the forced type conversion is 1 clock cycle; The workload of the function call is 10 clock cycles.
6. The method according to any one of claims 1 to 5, It is characterized in that It also includes that in the basic operations, the workload is not calculated for the calculation operation of adding zero, the calculation operation of subtracting zero, the calculation operation of multiplying by 1, and the calculation operation of dividing by 1; In multiplication, if at least one of the two multipliers is 2 n , n is a positive integer, and the workload of the multiplication operation is replaced by the workload of the corresponding addition or arithmetic left shift operation; In the Boolean expressions in the conditional judgment statement, the values of the Boolean expressions are calculated in order. If the Boolean expression is an OR operation, when the value of one expression in the Boolean expression is true, the workload of the remaining uncalculated expressions is not calculated; If the Boolean expression is an AND operation, when the value of one expression in the Boolean expression is false, the workload of the remaining unevaluated expressions is not calculated; In declaration nodes, the workload of useless variables is not calculated.
Citation Information
Patent Citations
Method and device for source code inspection
CN103294598A
Program compiling method and program compiler
CN106155755A
