High-level optimization of mathematical programs based on inductive inference from execution traces

A method for optimization of a program stored in non-transitory storage media includes generating traces for a programmed formula using a hardware processing system and selecting a subset of the traces. One or more substitute formulae are inferred from a plurality of formulae that yield a similar set of traces to the subset of traces. The programmed formula is transformed with a best matched substitute formula to reduce computational complexity.

BACKGROUND

Technical Field

The present invention relates to program optimization, and more particularly to systems and methods for using traces to inductively infer substitute program elements to reduce computational complexity.

Description of the Related Art

Program optimization can be considered a process to transform a program by applying prepared rules. For example, in peephole optimization, rules such as: “x*=2→x<<=1”, can be prepared, and an entire program can be scanned to apply the rules where applicable. However, to perform higher-level optimizations, the combination of rule applications causes the computation to explode. The problem is not only in the large number of rules, but also it is necessary to apply multiple rules. This is why searching is necessary, but the searching results in combinatorial explosion. Heuristics do not work well, because each rule by itself does not speed up the program, so it is difficult to decide on a search direction in the search space.

SUMMARY

A method for optimization of a program stored in non-transitory storage media includes generating traces for a programmed formula using a hardware processing system and selecting a subset of the traces. One or more substitute formulae are inferred from a plurality of formulae that yield a similar set of traces to the subset of traces. The programmed formula is transformed with a best matched substitute formula to reduce computational complexity.

A hardware processing system configured to reduce computation complexity of a programmed formula stored in non-transitory storage media includes a processor configured to generate a selection of traces for a programmed formula. An inferring device is coupled to the processor to search for one or more substitute formulae from a plurality of stored formulae to determine a similar set of traces to the selection of traces. A transformed program is generated using the processor that matches the programmed formula with a best matched substitute formula to reduce computational complexity.

A non-transitory computer readable storage medium comprising a computer readable program for optimization of a formula in a program, wherein the computer readable program when executed on a computer causes the computer to perform the steps of generating traces for a programmed formula using a hardware processing system; selecting a subset of the traces; inferring one or more substitute formulae from a plurality of formulae that yields a similar set of traces to the subset of traces; and transforming the programmed formula with a best matched substitute formula to reduce computational complexity.

DETAILED DESCRIPTION

In accordance with the present principles, systems and methods provide search clues for program optimization. The present principles search for candidate programs from a given mathematical program before optimization. The search that is conducted is influenced by traces computed for portions of the original program. These traces are employed to assist in the search to match for equivalent substitutes that will reduce computational complexity. The program can be transformed to become more efficient. Suppose that an optimized program is given after being transformed. A verification is needed to determine whether the program is equivalent before and after the transformation, by a verification method. The verification method is also a difficult problem that needs to be solved. However, verification problems have been studied broadly in the fields of automatic theorem proving and program verification. It is easier to employ verification information (in that a search target is given) than to perform a search by blind searching. The present principles aim at obtaining candidate programs after optimization from a given mathematical program before optimization.

Referring now to the drawings in which like numerals represent the same or similar elements and initially toFIG. 1, a block/flow diagram shows an exemplary processing system100for program optimization. The processing system104includes at least one processor (CPU)104operatively coupled to other components via a system bus102. A cache106, a Read Only Memory (ROM)108, a Random Access Memory (RAM)110, an input/output (I/O) adapter120, a network adapter140, a user interface adapter150, and a display adapter160are operatively coupled to the system bus102.

A user input device152may be operatively coupled to system bus102by the user interface adapter150. The user input device(s)152can be any of a keyboard, a mouse, a keypad, an image capture device, a motion sensing device, a microphone, a device incorporating the functionality of at least two of the preceding devices, and so forth. Other types of input devices can also be employed, e.g., a touch screen display. The user input device152is employed to input and output information to and from system100. A display162is coupled to the display adapter160.

A first storage device122is operatively coupled to system bus102(and may be coupled directly or through the I/O adapter120). The storage device122can be any of a disk storage device (e.g., a magnetic or optical disk storage device), a solid state magnetic device, etc. The first storage device122stores one or more programs124that will be run on the system100, The program124may be written in any suitable computer language, e.g., C, C++, etc. The program124is compiled and executed in whole or in part by an optimizer126.

The optimizer126is configured to generate traces125from portions of the program124to provide search information to an inference device127. The inference device127is configured to generate search queries based upon the traces125. The inference device127may be configured to conduct the search or may communicate the queries to a search engine180. The inference device127may create a trace or a subset of traces from the traces125. The traces125may be employed to provide information to narrow the search needed to find substitute code or formulas for the running the program124more efficiently (e.g., less computationally complex or less cost). The search engine180may employ the traces125to identify candidate substitutions for portions of the program. The search may be conducted on a database129or may access a separate corpus131of formulas, etc.

Once a substitute or substitutes are identified, the optimizer126searches for a best candidate code or formula(s) to be are substituted in the program124. The optimizer126can generate substitute code to provide a transformed or optimized program128.

The search engine180may be included locally or may access wide area or global networks. The search engine180may be configured to focus on a particular subject or a particular area of endeavor. The search engine180may include a commercially available search engine or be a specially designed search engine.

Moreover, it is to be appreciated that system200described below with respect toFIG. 2is a system for implementing respective embodiments of the present principles. Part or all of processing system100may be implemented in one or more of the elements of system200.

Referring toFIG. 2, a program optimization is shown in greater detail. The program optimizer126may include a processor with memory and is employed to transform a program by matching program traces with prepared formulae instead of employing prepared rules. In one example, (Example 1) a program202computes a sum of all the multiples of 3 or 5 below 1000. A naive execution takes O(N) time, while after transformation in accordance with the present principles, a program204takes virtually O(1) time, using a substitute formula for summation of an arithmetic sequence.

The optimizer126generates traces on program202that assist in obtaining candidate programs from a given (mathematical) program.

Program202includes a formula: sum({x|(x % 3==0∥x % 5==0) && natural(x) && x<N}). Traces can be computed for a given set of values of (different N's). Using these traces, a biased search can be performed. Through a search, the following formula is identified (program204) by employing verification of traces between programs202and204.

If one traces the execution of a mathematical program before optimization, one can sometimes find it easy to apply a known mathematical formula to the execution results (or to the states immediately before computing the results), even if the intermediate computation is complicated. In other words, to solve a mathematical problem, a guess can be made first by experimenting with small cases, before proving the guess is indeed true. The present principles trace the execution results or slices of the execution results by specifying various input values to a program before optimization, and then infer candidate programs after optimization by checking whether it is possible to reproduce the traces by a simple combination of known formulae.

So in Example 1, trace the slice immediately before computing sum( ), for example with N=50. This yields a trace: 3+5+6+9+10+12+15+18+20+21+24+25+27+30+33+35+36+39+40+42+45+48.

Then, check whether the formulae of the number series whose partial sum can be easily computed can generate the obtained trace, by matching the formulae with the trace. For example, prepare in advance the formula of the summation of arithmetic sequences, the formula of the summation of geometric sequences, etc. The matching does not need to be strict; partial matching suffices. In this example, first the arithmetic sequence with the initial term “3” and the common difference3partially matches the trace to provide formula (1). Next, a part of the arithmetic sequence with the initial term “5” and the common difference5matches the trace to provide formula (2). Finally, the arithmetic sequence with the initial term “15” and the common difference15matches the remaining trace to provide formula (3).
sum({x|3<=x&&x<50&&(x−3)%3==0})  (1)
sum({x|5<=x&&x<50&&(x−5)%5==0})  (2)
sum({x|15<=x&&x<50&&(x−15)%15==0})  (3)

As a result, the candidate program corresponding to program204is inferred. This process of matching206is a searching process based on traces. This searching discovers small combinations of formulae, each of which is known to simplify the computation. The present principles can take advantage of existing string matching algorithms and set cover algorithms.

Mathematical programs are employed in optimization problems, such as numerical computation, operations research, machine learning, etc. Increased computational speed is one important aspect for the mathematical programs. The previous example (Example 1) demonstrated a reduction in computational complexity; however the present principles are applicable in other aspects. For example, as described in Example 2 hereinafter, computations involving integer solutions of, e.g., b/a×(b−1)/(a−1)=½ (Eq. 2) can be reduced.

It takes more than several hours for a naive program to compute the solutions of the formula: b/a×(b−1)/(a−1)=½ (Eq. 2), when “a” is larger than N and N=1,000,000,000,000 (N=iteration counter). By transforming Eq. 2 to (2a−1)2−2(2b−1)2=−1, Pell's equation X2−2Y2=−1 can be applied, and the integer solutions of X and Y can be easily computed. However, since there are infinite ways to transform the equation, it is difficult to reach (2a−1)2−2(2b−1)2=−1 from b/a×(b−1)/(a−1)=½ by searching.

In the meantime, by executing the naive program with small N's, the following sequence will be generated: (4, 3), (21, 15), (120, 85), . . . . This sequence can be obtained by a linear transformation (e.g., the transformation may include the operations of plus 1, followed by division by 2) of the following solution sequence is computed by Pell's equation: (7, 5), (41, 29), (239, 169), . . . . As a result, the present principles infer a candidate program after optimization, by using Pell's equation with X=2a−1 and Y=2b−1. To verify whether the inferred program is correct, it is sufficient to substitute X=2a−1 and Y=2b−1 for X2−2Y2=−1 and to check the equation after the substitution is equivalent to b/a×(b−1)/(a−1)=½, by using a formula manipulation program.

In one embodiment, program204may be generated in accordance with the following matching/optimizing method (Method A) illustratively presented in pseudocode.

optimize(prog_before : Program before optimization) : Program after optimization {for (depth = 0; depth < depth_threshold; depth++) {Traces = Empty set;prog_after = nil;for (N = N_start; N < N_threshold; N += N_increment) {Traces = Traces ∪ trace(prog_before, N, depth);Formulae = matching_formulae(Traces);prog_after = search_for_matching(Traces, Formulae);if (prog_after == nil)!break;}if (prog_after != nil)break;}return prog_after;}trace(prog_before : Program before optimization, N, depth) : Trace of program {Invoke prog_before with N as a parameter, and return a trace depending on depth.The bigger the depth, the more detailed the trace. For example, if depth==0, then returnonly the return value of the program. For instance, (21, 15) in Example 2.If depth==1, then return the slice immediately before computing the program returnvalue. For instance, 3+5+6+9+10+12+15+... in Example 1.}matching_formulae(Traces) : Set of formulae {Return the set of the formulae that possibly match the given traces.In Example 1, return the set of the formulae of the sequences whose partialsummations are known to be easily computed.In Example 2, return the set of the formulae that generate the sequence of integerpairs.}search_for_matching(Traces, Formulae) : Program after optimization {If a set of formulae in Formulae can cover Traces, then return a programconsisting of the formulae. Otherwise, return nil.}

In accordance with the present principles, mathematical formulae are considered as computation components. The present embodiments do not need to search all mathematical formulae. Instead by employing traces or traces of slices, the present embodiments search only a subset of the formulae. For example, in Example 1, only the formulae of the sequences whose partial summations are known to be easily computed are used. In other words, the present principles perform matching to sequences generated by formulae. The present principles generate an optimized program from the intermediate results of computations.

Referring toFIG. 3, a system/method for optimization of a program stored in non-transitory storage media is illustratively shown. In block302, traces are generated for a programmed formula using a hardware processing system. Traces are generated by running the program and storing the output. It should be noted that the entire program need not be executed and portions of the program associated with a formula or portion of the program to be optimized only need to be executed. The traces may include a series of numbers, tuples, a group of numbers, etc. computed by the programmed formula.

In block304, traces may be generated at different depths in the program where larger depths provide greater detail for the traces. The greater depth will be employed in matching operations to provide a more accurate result. Depth is known to those skilled in the art.

In block306, a subset of the traces are selected. The selection process may be a random selection; however, selecting traces that are simpler, typical or known solutions are preferred. The number of traces selected will be dependent on the accuracy desired.

In block308, the subset of the traces may be selected by employing an iteration counter N and selecting the subset of traces based on a plurality of values of N. For example, N's may be selected randomly, every 100thN, every Z+40 N, etc.

In block310, one or more substitute formulae are inferred from a plurality of formulae that yield a similar set of traces to the subset of traces. Similarity may be defined based on accuracy, type or other criteria related to the traces. The similarity may be based on a general trend of the traces, e.g., increasing in value, a same statistical distribution, etc. In block312, the inferring may include searching a database or other corpus for replacement formulas based on the subset of traces. The search will be performed in accordance with the selected depth.

In block314, the programmed formula is transformed with a best matched substitute formula to reduce computational complexity. This may include substituted computer code, altering the programmed formula, substituting a formula and adding operations, etc. The best match depends on the accuracy (similarity) desired. The inductive inference may need exact similarity or may need a simple trend. The inference or search may result in multiple results. The most accurate substitute or the formula meeting the particular criteria may be selected as the best match.

In block316, transforming the programmed formula with the best matched substitute formula may include conducting one or more mathematical operations on the best matched substitute formula to match the programmed formula. For example, the substitute formula may need to be reduced or increased using an additional mathematical operation to provide the desired similarity with the programmed formula.

In block318, a verification process may be performed to verify that the best matched substitute formula matches the programmed formula.

Having described preferred embodiments for high-level optimization of mathematical programs based on inductive inference from execution traces (which are intended to be illustrative and not limiting), it is noted that modifications and variations can be made by persons skilled in the art in light of the above teachings. It is therefore to be understood that changes may be made in the particular embodiments disclosed which are within the scope of the invention as outlined by the appended claims. Having thus described aspects of the invention, with the details and particularity required by the patent laws, what is claimed and desired protected by Letters Patent is set forth in the appended claims.