...PredicateA(x) & PredicateB(y) ...Now imagine that PredicateA takes really long time to calculate, and PredicateB eventually fails. Since expressions are evaluated left to right, the execution time depends on the order of the predicates, although the result of the calculation will remain the same. Clearly, the sequence
...PredicateB(y) & PredicateA(x) ...will be calculated much quicker. Currently, the onus on determing the best order of the predicates is on the programmer. However, the programmer cannot easily determine the ideal order (nor should he care). Now if each of the two predicates was executed in parallel in a separate thread, the order of the predicates would be irrelevant. The compiler could dispatch suitable predicates to various threads automatically based on a cost/benefit analysis, or the programmer would use multithreading syntax such as
...PredicateB(y) && PredicateA(x) ...On a single core CPU threads do not really run in parallel, but the program can still benefit immensely: If one of the predicates fails, there is no need to finish calculating the other one, as the result is a logical AND of both predicates. (Obviously, logical OR of predicates would benefit from multithreading in similar fashion.) The biggest win would be to run individual threads each on a separate CPU. As a matter of fact constraint logic languages are particularily suitable to take advantage of multi-core CPUs, as the compiler can relatively easily determine code that can run in a separate thread.
While the concept of multithreading is easy to explain and the benefits are easy to see, the actual implementation is quite difficult. The long term goal here is to have a compiler that can generate application that can automatically take advantage of multiple CPU cores and scale performance accordingly. In other words, if you run the same application on a system with more CPU cores, the application performance should improve accordingly. While this may sound obvious, it is important to realize that at the present time the vast majority of applications do not benefit from multi-core CPUs, you only benefit by being able to run multiple applications concurrently.
Page last updated: December 8, 2012