This invention relates to a program conversion device, a program conversion method, and a recording medium.
There are cases where program conversions, such as source-level program conversion, are performed.
For example, the language conversion device described in Patent Document 1 searches for locations in the source program that are described by the same character string as the reserved word, and determines whether the searched character string is used as a reserved word or as the character string of variables. If it is determined that the searched string is not used as a reserved word and is used as the character string of variables, this language converter converts the searched string into another string.
It is desirable to be able to convert programs so that they can be executed more efficiently.
An example of an object of the present invention is to provide a program conversion device, a program conversion method, and a recording medium that can solve the above-mentioned problems.
According to the first example aspect of the present invention, a program conversion device is provided with an analysis means that analyzes a program and detects a portion that includes a conditional branch and calculates a value according to an execution result of the conditional branch; and a program generation means that generates, on the basis of the analysis result, a program which is provided with a plurality of combinations of a conditional expression and a calculation portion indicating a value and configured without including a conditional branch, and which uses a value indicated by the calculation portion combined with the conditional expression that becomes true, where only one conditional expression from among the plurality of conditional expressions becomes true.
According to the second example aspect of the present invention, a program conversion method includes a computer analyzing a program and detecting a portion that includes a conditional branch and calculates a value according to an execution result of the conditional branch; and generating, on the basis of the analysis result, a program which is provided with a plurality of combinations of a conditional expression and a calculation portion indicating a value and configured without including a conditional branch, and which uses a value indicated by the calculation portion combined with the conditional expression that becomes true, where only one conditional expression from among the plurality of conditional expressions becomes true.
According to the third example aspect of the present invention, a recording medium is one that records a program for causing a computer to analyze a program and detect a portion that includes a conditional branch and calculates a value according to an execution result of the conditional branch; and generates, on the basis of the analysis result, a program which is provided with a plurality of combinations of a conditional expression and a calculation portion indicating a value and configured without including a conditional branch, and which uses a value indicated by the calculation portion combined with the conditional expression that becomes true, where only one conditional expression from among the plurality of conditional expressions becomes true.
According to the present invention, programs can be converted so that program execution can be made more efficient.
The following is a description of example embodiments of the present invention, but the following example embodiments do not limit the claimed invention. Not all of the combinations of features described in the example embodiments are essential to the solution of the invention.
The program conversion device 100 converts programs. In particular, the program conversion device 100 converts a program which includes a conditional branch and calculates a value according to an execution result of the conditional branch into a program which is provided with a plurality of combinations of a conditional expression and a calculation portion presenting a value and configured without including a conditional branch, and which uses a value indicated by the calculation portion combined with the conditional expression that becomes true, where only one conditional expression from among the plurality of conditional expressions becomes true.
The calculation portion is configured as a calculation equation that does not include a conditional branch. The calculation equation here may be a constant. That is, the calculation equation may be an equation consisting only of constant terms. Therefore, the value indicated by the calculation portion may be a value of a calculation equation (calculated value) or a constant value. The constant value can be interpreted as a kind of calculation equation value.
The pre-conversion program and the post-conversion program output the same data for the same data input.
The post-conversion program has a structure suitable for faster computation, for example, parallel processing, in that the computation portion does not include conditional branches. According to the program conversion device 100, calculations can be performed at relatively high speeds.
The conversion of a program here may be to rewrite the program to be converted or to generate a post-conversion program separately from the program to be converted.
Both rewriting the program to be converted and generating the post-conversion program separately from the program to be converted can be said to generate the post-conversion program. Rewriting the program to be converted can be said to be generating the post-conversion program using the program to be converted.
A program prior to conversion by the program conversion device 100 (the program to be converted) is also referred to as the pre-conversion program. A program after conversion by the program conversion device 100 is also referred to as the post-conversion program.
The communication portion 110 communicates with other devices. For example, if the storage device storing the pre-conversion program is included in a device other than the program conversion device 100, the communication portion 110 may receive the pre-conversion program from that device. If the execution environment for the post-conversion program is provided in a device other than the program conversion device 100, the communication portion 110 may transmit the post-conversion program to that device.
The display portion 120 is provided with a display screen, such as a liquid crystal panel or LED (Light Emitting Diode) panel, for example, and displays various images. For example, the display portion 120 may display one or more of the pre-conversion program, the data at the time of program conversion, and the post-conversion program.
The operation input portion 130 is provided with input devices such as a keyboard and mouse, for example, and accepts user operations. For example, the operation input portion 130 may accept user operations that indicate the conversion of the program and the acquisition source of the pre-conversion program.
The memory portion 180 stores various data. For example, the memory portion 180 stores the algorithm for the process of analyzing the pre-conversion program and the algorithm for the process of converting the program. The memory portion 180 may store these algorithms in a program.
In addition, the memory portion 180 stores the pre-conversion program. When the program conversion device 100 acquires the pre-conversion program from another device, the memory portion 180 may temporarily store the acquired pre-conversion program.
In addition, the memory portion 180 stores the post-conversion program. When the program conversion device 100 outputs the post-conversion program to other devices, the memory portion 180 may temporarily store the post-conversion program.
The control portion 190 controls the various parts of the program conversion device 100 to perform various processes. The functions of the control portion 190 are performed, for example, by the CPU (Central Processing Unit) provided by the program conversion device 100, which reads the program from the memory portion 180 and executes it.
The analysis portion 191 analyzes the pre-conversion program. In particular, the analysis portion 191 detects in the analysis a portion of the pre-conversion program that includes a conditional branch and calculates a value according to an execution result of the conditional branch. The analysis portion 191 is an example of an analysis means.
The portion detected by the analysis portion 191 that includes a conditional branch and calculates a value according to an execution result of the conditional branch can be the part to be converted in the conversion of the program.
The analysis portion 191 may use an abstract syntax tree to analyze the program. For example, if the program conversion device 100 uses the abstract syntax tree in processes other than program conversion, such as a compiler or interpreter, the abstract syntax tree or the abstract syntax tree generation algorithm may also be used for program conversion by the program conversion device 100. This eliminates the need to separately generate abstract trees or separately prepare algorithms for program conversion by the program conversion device 100.
The program generation portion 192 converts the program based on the results of analysis by the analysis portion 191. In particular, the program generation portion 192 replaces assignment statements based on conditional branching with conditional assignment statements. A conditional assignment statement consists of a combination of a conditional expression and a calculation portion that is composed without a conditional branch and indicates a value.
If the pre-conversion program contains an assignment statement with an if-then-else conditional branch, or an assignment statement with a nested structure of if-then-else conditional branches, the program generation portion 192 replaces this assignment statement with multiple conditional assignment statements. if-then-else conditional branches may be explicitly indicated using an if-then-else statement, or they may be implicitly indicated by, for example, using a return command in a function.
The program generation portion 192 sets the conditional expressions of multiple conditional assignment statements so that only one of the conditional expressions becomes true exclusively. In the post-conversion program, the value indicated by the calculation portion combined with the conditional expression that has become true is used as the value to be assigned by the assignment statement.
Tabular data can be understood as data in matrix form. The rows of the table correspond to the rows of the matrix, and the columns of the table correspond to the columns of the matrix. Each cell in the rows and columns of the table corresponds to an element of the matrix.
In the example in
However, the data handled by the program converted by the program conversion device 100 is not limited to specific content data. The data handled by the program converted by the program conversion device 100 is not limited to data in a specific format.
In the data shown in
Specifically, if the value in the “open” column is less than 1200, the value in the “diff” column is “−1”.
On the other hand, if the value in the “open” column is equal to or greater than 1200, when the value in the “close” column is greater than or equal to the value in the “open” column, the value of the difference obtained by subtracting the value in the “open” column from the value in the “close” column is stored in the “diff” column. If the value in the “open” column is equal to or greater than 1200 and the value in the “close” column is less than the value in the “open” column, the value in the “diff” column is “0”.
However, the processing that the pre-conversion program or post-conversion program performs on matrix-format data (tabular data) is not limited to adding columns. Furthermore, the data handled by the pre-conversion program or post-conversion program is not limited to data in matrix form.
If the pre-conversion program or post-conversion program handles data in matrix form, it may add columns, delete columns, or update the value of each element in a column, or perform other processing on the columns. Alternatively, the pre-conversion program or post-conversion program may perform processing on rows, such as adding rows, deleting rows, or updating the value of each element in the row.
The pre-conversion program or post-conversion program may perform both row and column processing, or only one of row or column processing.
Any one of the rows and columns of the matrix is also denoted as first data series. The rows and columns of the matrix that differ from the first data series are also denoted as the second data series.
In the case of processing for columns, such as the addition of columns illustrated in
In the program shown in
“tbl[‘diff’]=tbl.apply(func)” indicates that the user-defined function “func” is called for every row of tabular data “tbl” to create a column consisting of the return value of each row and assign it to the “diff” column of tabular data “tbl”.
In the function call “tbl.apply(func)”, one line of data from the tabular data “tbl” is passed to the user-defined function “func” as an argument. Rows of tabular data “tbl” that are passed to the user-defined function “func” as arguments are also called rows to be processed.
In the definition of the user-defined function “func”, the row to be processed is denoted as “row”.
The computer executing the user-defined function “func” compares the value of the “open” column of the row “row” to be processed with 1200 in “if row[‘open’]<1200 return−1”. If the value in the “open” column is less than 1200, the computer sets the return value to “−1” and terminates the execution of the user-defined function “func” for one line of data in the data “tbl”.
On the other hand, if the value in the “open” column is 1200 or more, the process proceeds to “if row[‘open’]<row[‘close’] . . . ”.
Thus, “if . . . return . . . ” implicitly indicates an if-then-else conditional branch. Here, “if row[‘open’]<1200 return−1 . . . ” is an example of the implication of an if-then-else conditional branch.
A computer executing the user-defined function “func” compares the value in the “open” column of the row to be processed with the value in the “close” column in “if row[‘open’]<row[‘close’] return row[‘close’]−row[‘open’]”. If the value in the “open” column is less than the value in the “close” column, the computer terminates execution of the user-defined function “func” for one line of data in the data “tbl” with the difference obtained by subtracting the value in the “open” column from the value in the “close” column as the return value.
On the other hand, if the value in the “open” column is greater than or equal to the value in the “close” column, the process proceeds to “return 0”. “if row[‘open’]<1200 return−1 . . . ” is another example of an if-then-else conditional implication.
At “return 0”, the computer executing the user-defined function “func” sets the return value to “0” and terminates the execution of the processing of the user-defined function “func” for one line of data in data “tbl”.
The following describes an example of the case in which the pre-conversion program makes a call to a user-defined function, as in the example in
For example, the pre-conversion program may be configured to call a subroutine defined by the user and assign the return value to a global variable within the subroutine. In this case, the return value of the user-defined function can be read as the return value assigned to the global variable in the following explanation.
User-defined functions and user-defined subroutines are collectively referred to as user-defined modules.
Alternatively, the pre-conversion program may be configured without a user-defined module. Specifically, a formula for calculating the desired value, such as calculating the value of each element of a new column, may be embedded in the body of the program in the pre-conversion program. In this case, the return value of the user-defined function can be read as the desired value in the following explanation.
In the example in
As shown above, in the pre-conversion program, “if . . . return . . . ” implicitly indicates an if-then-else conditional branch, which is structured as an if-cond-then-else tree structure in the abstract syntax tree shown in
The square nodes in the abstract syntax tree in
In the program conversion device 100, the analysis portion 191 generates the abstract syntax tree illustrated in
Alternatively, as described above, if the program conversion device 100 also uses the abstract syntax tree in processes other than program conversion, such as a compiler or interpreter, the abstract syntax tree or the abstract syntax tree generation algorithm may also be used for program conversion by the program conversion device 100.
The analysis portion 191 can use the abstract syntax tree to generate the pair consisting of the return value and the conditional expression illustrated in
For example, if the analysis portion 191 traces the tree from node n34, where “−” is surrounded by a square in the abstract syntax tree in
When the analysis portion 191 further traces the tree to the parent side, node n23 of the above if statement is set to else in the tree structure of a higher-level if statement (the if statement indicated by node n11). Therefore, the analysis portion 191 refers to node n21 of cond and the descendant nodes thereof in the tree structure of this if statement to obtain the conditional expression “row[‘open’]<1200”.
Node n34, where the analysis portion 191 starts its search, is included in the else of the if statement indicated by node n11. Therefore, the analysis portion 191 adds “not” to the conditional expression “row[‘open’]<1200” to make it “not(row[‘open’]<1200)”.
Then, the analysis portion 191 combines the conditional expressions “not(row[‘open’]<1200)” and “row[‘open’]<row[‘close’]” with and to generate the conditional expressions “(not(row[‘open’]<1200)) and (row[‘open’]<row[‘close’])”.
As shown in
The process in which the analysis portion 191 traces the tree from the node indicating the return value to detect the node indicating the if statement and the branching condition is an example of the process of detecting the portion of the pre-conversion program that includes a conditional branch and calculates a value according to the execution result of the conditional branch.
For example, the analysis portion 191 traces the abstract syntax tree shown in
The combination of these conditional branches “if row[‘open’]<row[‘close’]” and “if row[‘open’]<1200” and the return value calculation formula “row[‘close’]−row[‘open’]” indicated at node n34 is an example of the portion of the pre-conversion program that includes a conditional branch and calculates a value according to the execution result of the conditional branch.
The analysis portion 191 detecting the portion of the abstract syntax tree by tracing the abstract syntax tree from node n22 and detecting the portion of the abstract syntax tree by tracing the abstract syntax tree from node n35 are each examples of detecting the portion of the pre-conversion program that includes a conditional branch and calculates a value according to the execution result of the conditional branch.
A conditional expression naming statement is represented as an assignment statement that assigns the value of a conditional expression to a logical variable vector. The name of the logical variable vector is also used as the name of the conditional expression.
The logical variable vector here is a vector with logical variables as elements. When executing the conditional expression naming statement included in the post-conversion program, the computer executing the post-conversion program performs the conditional expression calculation for each row of the matrix to be processed, calculates the truth value vector for one column, and assigns it to the logical variable vector. The truth value vector here is a vector whose element values are truth values (i.e., values representing true or false).
A value representing true is also denoted simply as true, and a value representing false is also denoted simply as false. For example, the fact that a logical variable takes a value that represents true is also denoted as a logical variable value being true.
The program generation portion 192 may perform a mechanical conversion from the notation according to the format of the pre-conversion program to the notation according to the format of the post-conversion program when generating the conditional expression naming statement.
In the example in
“row[ ]” in the pre-conversion program represents the elements of the row to be processed and the elements of the column whose column name is indicated in [ ], among the elements contained in the input data in matrix format. On the other hand, “tbl[ ]” in the conditional expression naming statement represents the columns included in the matrix format data “tbl” for which the column name is indicated in [ ].
In the pre-conversion program, the computer executing the program calls a user-defined function for each line of input data to perform processing. In contrast, in the post-conversion program, portions of input data are specified by column, and the computer executing the program performs processing on the specified columns. The processing for a column in this case may be row-by-row processing for that column, or the computer may perform the same processing for all rows in parallel.
Thus, the processing methods assumed in the pre-conversion program and the post-conversion program may differ, and the notation in the pre-conversion program and the notation in the post-conversion program (here, in the conditional expression naming statement) may differ due to the difference in processing methods.
For example, the program generation portion 192 may perform a mechanical conversion from the notation according to the format of the pre-conversion program to the notation according to the format of the post-conversion program based on conversion rules, such as converting a fixed string to a fixed string.
The conditional expression naming statements generated by the program generation portion 192 are not limited to those by logical variable assignment statements as described above. For example, a conditional expression naming statement may be specified as a string conversion rule.
Furthermore, for example, the conditional expression naming statement “cond1=tbl[‘open’]<1200” in
Then, before or at the time of execution of the post-conversion program, the string “cond1” appearing in the post-conversion program may be replaced by the string “tbl[‘open’]<1200” representing the conditional expression.
The program generation portion 192 may use the names of conditional expressions specified in other conditional expression naming statements when generating the conditional expression naming statements. For example, in the example in
In the example in
The right-hand side of the conditional assignment statement corresponds to the example in the calculation portion.
The conditional assignment statement col[cond_var]=ret_var indicates that the value of each element of one row of the matrix for which the value of cond_var is true is the value indicated by ret_var.
In the example in
The computer that executes the post-conversion program shown in
“col=column(tbl.index)” indicates that an empty column “col” is to be generated for one column of data “tbl”. Here, an empty column for one column of data “tbl” is a column that has the same number of elements as one column of data “tbl” and each element is empty (value unset).
In the execution of “col=column(tbl.index)”, column “col” is generated as one column of data, separate from the data “tbl”. The column “col” can be treated as a vector. Column “col” is also denoted as vector “col”.
The elements of the vector “col” are mapped one-to-one to the rows of the data “tbl”. This is done, for example, by tying the elements of the vector “col” to the same index as the row index of the data “tbl”. The row index here is the identification information that identifies the row. The row index may be provided as a separate data from the elements as a matrix of data “tbl”. Alternatively, if one of the columns of data “tbl” has a fixed (constant) value and the value of each row is different, the value of that column may be used as the row index.
“col[cond1]=−1” indicates that the value “−1” shall be assigned to the element of vector “col” in the row for which the value of the logical variable vector “cond1” is true.
“col[cond2]=tbl[‘close’]−tbl[‘open’]” indicates that the difference obtained by subtracting the element value of the “open” column from the element value of the “close” column of the row is assigned to the element of the row for which the value of the logical variable vector “cond2” is true among the elements of vector “col”.
“col[cond3]=0” indicates that the value “0” shall be assigned to the element of the row for which the value of the logical variable vector “cond3” is true among the elements of the vector “col”.
The setting of the conditional expression in the conditional expression naming statement causes only one of the logical variables “cond1”, “cond2”, and “cond3” used in the conditional assignment statement to be true for each row of data “tbl”. This means that for each row of data “tbl”, only one of the three conditional assignment statements will be used.
This means that, for example, if a computer executing the post-conversion program shown in
“tbl[‘diff’]=col” indicates that the vector “col” is to be added to the data “tbl” as a column with the name “diff”. When the post-conversion program is executed, a new “diff” column is added to the data “tbl” by executing “tbl[‘diff’]=col”.
Comparing the pre-conversion program shown in
In the post-conversion program, only the conditional expression of any one of the multiple conditional assignment statements becomes true, and the value indicated by the calculation portion included in the conditional assignment statement whose conditional expression becomes true is used, ensuring that, during parallel processing, a unique value is obtained regardless of the execution order of the multiple conditional assignment statements. The post-conversion program is also suitable for parallel processing in this respect.
In the post-conversion program, a unique value is obtained regardless of the order in which multiple conditional assignment statements are executed, which not only allows multiple conditional assignment statements to be executed in parallel processing, but also allows the same processing to be performed in the column direction all at once when processing each row of data in matrix format.
For example, when the computer executes “cond1=tbl[‘open’]<1200” in the post-conversion program in
The computer, when executing “col[cond1]=−1”, may read the truth values from the logical variable vector “cond1” row by row at once. The computer may then assign −1 to the elements of the vector “col” in a batch for rows where the truth value is true.
Thus, if the computer performs the same process in a batch in the column direction, it will execute “col[cond1]=−1”, “col[cond2]=tbl[‘close’]−tbl[‘open’]” and “col[cond3]=0” for all rows in the same order. Such processing is made possible by the fact that unique values can be obtained regardless of the order in which multiple conditional assignment statements are executed.
A computer may batch the same process in parallel processing in the column direction. This reduces program execution time.
Alternatively, in an environment where the program is executed in an interpreted fashion, the portion of the program that is converted to a post-conversion program may be executed as a native function executed in binary code. In this case, the program execution time can also be reduced.
It is also possible that the programmer is familiar with the format of pre-conversion programs that use conditional branching, but is unfamiliar with the format of post-conversion programs that use conditional assignment statements. For example, the pre-conversion program has a high degree of freedom, such as the ability to use user-defined functions, whereas the post-conversion program is library-based and has a relatively low degree of freedom, and the programmer may not be familiar with the library for programming the post-conversion program.
In this case, the programmer can reduce program execution time by creating a pre-conversion program and converting it to a post-conversion program using the program conversion device 100.
In the process shown in
Next, the analysis portion 191 searches the abstract syntax tree to generate a list of return values (Step S112). The column of return values in the tabular data in
Next, the analysis portion 191 selects one return value from the list of return values (Step S113). In the loop from Step S113 to Step S122, the analysis portion 191 generates a pair of each of the return values in the list of return values and a conditional expression indicating the condition under which the return value is employed. In Step S313, the analysis portion 191 selects a return value in the list of return values for which the pair of the return value and conditional expression has not yet been generated.
Next, the analysis portion 191 identifies the conditions under which the return value selected in Step S113 is adopted (Step S114).
For example, the analysis portion 191 traces the abstract syntax tree from the return value node identified in Step S113 to the parent side. Then, the analysis portion 191 obtains the conditional expression of the branch each time a node of the if statement is reached. If the path traced by the analysis portion 191 corresponds to the else of the if statement, the analysis portion 191 takes the negation (NOT) of the conditional expression obtained.
The analysis portion 191 repeats obtaining the conditional expression for the branch and taking the negation of the conditional expression in the case of else until the root of the abstract syntax tree is reached. Upon reaching the abstract syntax tree, the analysis portion 191 combines the acquired conditional expressions (negated in the case of else) with and.
After Step S114, the analysis portion 191 determines whether it succeeded in identifying the condition under which the return value is employed (Step S115).
An example of a case in which the analysis portion 191 fails to identify the condition under which the return value is to be employed is when the conditions of the branch are complex. A further example of a case in which the analysis portion 191 fails to identify the condition under which the return value is to be employed is when an external function is called in the conditional expression of a branch, and the analysis portion 191 does not even analyze the external function. Examples of cases in which the analysis portion 191 does not analyze external functions include cases in which the operating environment of the analysis portion 191 does not allow reference to external function definitions, and cases in which the analysis portion 191 is set not to perform analysis of external functions.
If the analysis portion 191 determines that it failed to identify the condition under which the return value is adopted (Step S115: YES), the program conversion device 100 performs a predetermined process as the processing for program conversion failure (Step S141).
For example, the display portion 120 may display, in accordance with the control of the control portion 190, a message indicating that the conversion of the program was interrupted due to the occurrence of an error, and information indicating the cause of the failed conversion, such as an error code.
Alternatively, if the program conversion device 100 performs program conversion as part of the execution of the pre-conversion program, the pre-conversion program may be executed as is.
After Step S141, the program conversion device 100 terminates the process in
On the other hand, if it is determined in Step S115 that the condition under which the return value is employed is successfully identified (Step S115: YES), the analysis portion 191 generates a pair of the return value selected in Step S113 and the condition identified in Step S114 (Step S121). One line of tabular data shown in
Next, the analysis portion 191 determines whether there are any unprocessed return values (Step S122). Specifically, the analysis portion 191 determines whether there are any return values for which the pair of the return value and conditional expression has not yet been generated.
If the analysis portion 191 determines that there are unprocessed return values, the process returns to Step S113.
On the other hand, if it is determined that there are no unprocessed return values, the analysis portion 191 outputs a set of pairs of return values and conditional expressions generated in Step S121 to the program generation portion 192 (Step S131). The tabular data shown in
After Step S131, the program conversion device 100 terminates the process shown in
In the process of
Next, the program generation portion 192 determines whether the conditions indicated by the conditional expressions in the selected pairs can be described in the format of the updated program (Step S212).
An example of a case in which the conditions indicated by the conditional expressions included in the selected pairs cannot be described in the format of the updated program is when the updated program or a part thereof is composed using a library, and the conditions indicated by the conditional expressions included in the selected pairs cannot be described under constraint conditions on the description of conditional expressions in the library.
If the program generation portion 192 determines that the conditions indicated by the conditional expressions included in the selected pair cannot be described in the format of the updated program (Step S212: NO), the program conversion device 100 performs the processing predetermined as processing for program conversion failure (Step S251).
For example, the display portion 120 may display, in accordance with the control of the control portion 190, a message indicating that the conversion of the program was interrupted due to the occurrence of an error, and information indicating the cause of the failed conversion, such as an error code.
Alternatively, if the program conversion device 100 performs program conversion as part of the execution of the pre-conversion program, the pre-conversion program may be executed as is.
After Step S251, the program conversion device 100 terminates the process shown in
On the other hand, if it is determined in Step S212 that the conditions indicated by the conditional expressions included in the selected pair can be described in the format of the updated program (Step S212: YES), the program generation portion 192 generates a conditional expression naming statement based on the conditional expressions included in the selected pair (Step S221). As explained with reference to
Next, the program generation portion 192 determines whether the return values in the selected pairs can be described in the format of the updated program (Step S222).
An example of a case in which the return values in the selected pairs cannot be described in the format of the post-update program is when the post-update program or part thereof is composed using a library and the return values in the selected pairs cannot be described under constraint conditions of the description of the calculation formula in the library.
If the program generation portion 192 determines that the return values in the selected pairs cannot be described in the format of the updated program (Step S212: NO), the process proceeds to Step S251.
After Step S251, the program conversion device 100 terminates the process shown in
On the other hand, if it is determined in Step S222 that the return values included in the selected pairs can be described in the format of the updated program (Step S212: NO), the program generation portion 192 generates a conditional assignment statement based on the return values included in the selected pairs and the conditional expression naming statement generated in Step S221 (Step S231). As explained with reference to
Next, the program generation portion 192 determines whether there are any unprocessed pairs (Step S232). Specifically, the program generation portion 192 determines whether there are any pairs for which conditional assignment statements have not yet been generated.
If the program generation portion 192 determines that there are unprocessed pairs (Step S232: YES), the process returns to Step S211.
On the other hand, upon determining that there are no unprocessed pairs (Step S232: NO), the program generation portion 192 generates a post-conversion program using the conditional expression naming statement generated in Step S221 and the conditional assignment statement generated in Step S231 (Step S241).
For example, the program generation portion 192 may insert a conditional expression naming statement and a conditional assignment statement into a pre-prepared template, set the name and the like required by the template, such as the name “diff” for the new column shown in
After Step S241, the program conversion device 100 terminates the process shown in
As described above, the analysis portion 191 analyzes the pre-conversion program and detects a portion of the pre-conversion program that includes a conditional branch and calculates a value according to the execution result of the conditional branch. The program generation portion 192 generates a post-conversion program in which multiple combinations of conditional expressions and calculation portions that are configured without conditional branches and that indicate values are provided, only one of the multiple conditional expressions becomes true, and the value indicated by the calculation portion combined with the conditional expression that becomes true is used, based on the analysis results.
The post-conversion program generated by the program conversion device 100 can be said to be a program suitable for parallel processing in that it does not include conditional branches in the combination of conditional expressions and calculation portions.
In the post-conversion program generated by the program conversion device 100, only one of the multiple conditional expressions becomes true, and the value indicated by the calculation portion combined with the conditional expression that becomes true is used, ensuring that in parallel processing, a unique value is obtained regardless of the execution order of the multiple combinations. The post-conversion program generated by the program conversion device 100 is also suitable for parallel processing in this respect.
Thus, according to the program conversion device 100, the program can be converted so that program execution can be made more efficient. For example, it is expected that the post-conversion program will be processed in parallel, resulting in a shorter program execution time than for the pre-conversion program.
In addition, it is expected that the reader of the program can easily ascertain the structure of the program in that the post-conversion program is described not in a nested structure of conditional branches, but in a format in which the value is adopted by any of several combinations of conditional expressions and calculation portions.
For example, when the program conversion device 100 converts a pre-conversion program without a library into a post-conversion program with a library, a person who wants to learn how to describe a program with a library can compare the pre-conversion program with the post-conversion program to use it as a reference for describing programs using a library.
The portion of the pre-conversion program that includes a conditional branch and calculates a value according to the execution result of the conditional branch is configured as a user-defined module. The program generation portion 192 replaces an assignment statement that uses the user-defined module to calculate for each first data series, which is one of the row or column of matrix-formatted data, the value of an element in one second data series, which is the different one of the row or column from the first data series, for each first data series, with multiple conditional assignment statements combining a conditional expression and a calculation portion constituted without a conditional branch and indicating a value. In the replaced program (post-conversion program), only the conditional expression of any one of the multiple conditional assignment statements becomes true, and the value indicated by the calculation portion of the conditional assignment statement whose conditional expression becomes true is assigned as the value of an element of one second data series.
Thus, when the pre-conversion and post-conversion programs process data in matrix form, the effect due to increased efficiency of program execution may be increased in that processing is performed for each of multiple rows or multiple columns.
For example, consider the case where row-by-row processing is performed sequentially in both the pre-conversion and post-conversion programs for processing matrix data columns. In this case, it is expected that the reduction in program execution time in the post-conversion program relative to the pre-conversion program for one row of data will be multiplied by the number of rows for the entire data in matrix form.
If the post-conversion program is written using a library that performs matrix processing in parallel, or if the post-conversion program also performs row-by-row processing in parallel, the execution time of the post-conversion program is further reduced.
When the program conversion device 100 converts the part of the pre-conversion program that is executed by calling a user-defined module into a part that is embedded in the program body of the post-conversion program, the post-conversion program is expected to further reduce program execution time to the extent that module calls are no longer required.
The program generation portion 192 replaces the portion of a pre-conversion program executed in an interpreted manner that includes a conditional branch and calculates a value according to the execution result of the conditional branch into a portion which is provided with a plurality of combinations of a conditional expression and a calculation portion presenting a value and configured without including a conditional branch, and which uses a value indicated by the calculation portion combined with the conditional expression that becomes true, where only one conditional expression from among the plurality of conditional expressions becomes true.
As mentioned above, the post-conversion program is suitable for parallel processing. In this regard, it is conceivable that the computer executing the post-conversion program or a portion of the post-conversion program may use an accelerator to execute the post-conversion program or a portion of the post-conversion program. In an environment where programs are executed in an interpreted manner, the effect of faster processing using accelerators is expected to be particularly noticeable.
The analysis portion 191 also analyzes the pre-conversion program using an abstract syntax tree.
If an abstract syntax tree of the pre-conversion program has already been generated for purposes other than program conversion, such as for compilation or interpreter execution, the analysis portion 191 can use this abstract syntax tree and does not need to separately generate an abstract syntax tree for program conversion.
According to the program conversion device 100, the processing load is expected to be light and the processing time is expected to be reduced in this respect.
Alternatively, when the analysis portion 191 generates an abstract syntax tree for program conversion, if an abstract syntax tree generation algorithm is provided for purposes other than program conversion, such as for compilation or interpreter execution, the analysis portion 191 can generate an abstract syntax tree using that algorithm. In this case, according to the program conversion device 100, the storage capacity of the program conversion device 100 is relatively small in that there is no need to prepare a separate abstract syntax tree generation algorithm for program conversion.
In the configuration shown in
In such a configuration, the analysis portion 611 analyzes the program and detects a portion that includes a conditional branch and calculates a value according to the execution result of the conditional branch. The program generation portion 612 generates a program in which multiple combinations of conditional expressions and calculation portions that are configured without conditional branches and that indicate values are provided, only one of the multiple conditional expressions becomes true, and the value indicated by the calculation portion combined with the conditional expression that becomes true is used, based on the analysis results.
The analysis portion 611 is an example of an analysis means. The program generation portion 612 is an example of a program generation means.
The program generated by the program conversion device 610 can be said to be a program suitable for parallel processing in that it does not include conditional branches in the combination of conditional expressions and calculation portions.
In the program generated by the program conversion device 610, only one of the multiple conditional expressions becomes true, and the value indicated by the calculation portion combined with the conditional expression that becomes true is used, ensuring that in parallel processing, a unique value is obtained regardless of the execution order of the multiple combinations. The program generated by the program conversion device 610 is also suitable for parallel processing in this respect.
Thus, according to the program conversion device 610, the program can be converted so that program execution can be made more efficient. For example, it is expected that the program generated by the program conversion device 610 will be processed in parallel, resulting in a shorter program execution time than for the pre-conversion program.
In addition, it is expected that the reader of the program can easily ascertain the structure of the program in that the program generated by the program conversion device 610 is described not in a nested structure of conditional branches, but in a format in which the value is adopted by any of several combinations of conditional expressions and calculation portions.
For example, when the program conversion device 610 converts a program without a library into a program with a library, a person who wants to learn how to describe a program with a library can compare the program prior to conversion by the program conversion device 610 with the program after conversion to use it as a reference for describing programs using a library.
The process shown in
In performing an analysis (Step S611), the computer analyzes the program and detects a portion that includes a conditional branch and calculates a value according to the execution result of the conditional branch.
In generating the program (Step S612), a computer generates a program in which multiple combinations of conditional expressions and calculation portions that are configured without conditional branches and that indicate values are provided, only one of the multiple conditional expressions becomes true, and the value indicated by the calculation portion combined with the conditional expression that becomes true is used, based on the analysis results.
The program obtained by the program conversion method shown in
In the program obtained by the program conversion method shown in
Thus, according to the program conversion method shown in
In addition, it is expected that the reader of the program can easily ascertain the structure of the program in that the program obtained by the program conversion method shown in
For example, when a program without a library is converted into a program with a library by the program conversion method shown in
In the configuration shown in
Either one or more of the above program conversion device 100 and program conversion device 610, or any part thereof, may be implemented in the computer 700. In that case, the operations of each of the above-mentioned processing portions are stored in the auxiliary memory device 730 in the form of a program. The CPU 710 reads the program from the auxiliary memory device 730, deploys it in the main memory device 720, and executes the above processing according to the program. The CPU 710 also reserves a storage area in the main memory device 720 according to the program. Communication between each device and other devices is performed by the interface 740, which has a communication function and performs communication according to the control of the CPU 710. The interface 740 also has a port for the nonvolatile recording medium 750 and reads information from and writes information to the nonvolatile recording medium 750.
When the program conversion device 100 is implemented in the computer 700, the operations of the control portion 190 and the various parts thereof are stored in the auxiliary storage device 730 in the form of a program. The CPU 710 reads the program from the auxiliary memory device 730, deploys it in the main memory device 720, and executes the above processing according to the program.
The CPU 710 also allocates the storage area corresponding to the memory portion 180 in the main memory device 720 according to the program.
Communication with other devices by the communication portion 110 is performed by the interface 740, which has communication functions and operates according to the control of the CPU 710.
The display by the display portion 120 is performed by the interface 740 having a display device and displaying various images according to the control of the CPU 710.
Acceptance of user operations by the operation input portion 130 is performed by the interface 740 having input devices such as a keyboard and mouse, for example, to accept user operations and output information indicating accepted user operations to the CPU 710.
When the program conversion device 610 is implemented in the computer 700, the operations of the analysis portion 611 and the program generation portion 612 are stored in the auxiliary memory device 730 in the form of programs. The CPU 710 reads the program from the auxiliary memory device 730, deploys it in the main memory device 720, and executes the above processing according to the program.
The CPU 710 also reserves a storage area in the main memory device 720 for the program conversion device 610 to perform processing according to the program.
Communication between the program conversion device 610 and other devices is performed by the interface 740, which has communication functions and operates according to the control of the CPU 710.
Interaction between the program conversion device 610 and the user is performed by the interface 740, which has a display device and input device and operates according to the control of the CPU 710.
Any one or more of the above programs may be recorded on the nonvolatile recording medium 750. In this case, the interface 740 may read the program from the nonvolatile recording medium 750. The CPU 710 may then directly execute the program read by the interface 740, or it may be stored once in the main memory device 720 or the auxiliary memory device 730 and then executed.
A program for executing all or part of the processing performed by the program conversion device 100 and the program conversion device 610 may be recorded on a computer-readable recording medium, and the computer system may read the program recorded on this recording medium and execute it to perform the processing of each part. The term “computer system” here shall include an operation system and hardware such as peripherals.
In addition, “computer-readable recording medium” means a portable medium such as a flexible disk, magneto-optical disk, ROM (Read Only Memory), CD-ROM (Compact Disc Read Only Memory), or other storage device such as a hard disk built into a computer system. The above program may be used to realize some of the aforementioned functions, and may also be used to realize the aforementioned functions in combination with programs already recorded in the computer system.
While the above example embodiments of this invention have been described in detail with reference to the drawings, it should be understood that specific configurations are not limited to these example embodiments, with designs and the like also included to the extent that they do not depart from the gist of this invention.
The present invention may be applied to a program conversion device, a program conversion method, and a recording medium.
Filing Document | Filing Date | Country | Kind |
---|---|---|---|
PCT/JP2021/044518 | 12/3/2021 | WO |