Work Distribution

This module provides meta-predicates to distribute work over multiple threads. This module provides meta-predicates to distribute work over multiple threads. The simplest meta-predicate horde/[1,2] collects the results of the spawned threads and leaves the work distribution to the spawned threads itself. The other predicates distribute work items among the spawned threads, but do this only on a local scale.

Example:
?- balance(between(1,10,X), Y is X*X).
X = 1,
Y = 1 ;
X = 3,
Y = 9 ;
Etc..

The meta-predicates balance/[2,3] and setup_balance/[3,4] allow work distribution of a gen-erate and test. The meta-predicates might change the order of the result set. If the meta-predicates are cancelled by a cut such as in a surrounding \+/1 or once/1 they will automatically cancel each spawned thread.

The meta-predicates balance/[2,3] and setup_balance/[3,4] assume a side effect free interac-tion between the generate and test. The only channels are the variables in the intersection of the generate and test and the instantiations are copied. Currently the copying doesn't support attribute variables.

The setup in the meta-predicates setup_balance/[3,4] is executed once per spawned thread. The setup is executed before the test and attribute variables can communicate between the setup and the test. The setup can for example be used to build a CLP(FD) model and the model will be available in the test for labelling.

The following work distribution predicates are provided:

horde(T):
horde(T, N):
The predicate succeeds whenever T succeeds. The predicate spawns threads over the available processors running copies of T. The binary predicate allows specifying the number N of requested threads.
balance(G, T):
balance(G, T, N):
The predicate succeeds whenever G, T succeeds. The predicate distributes the work generated by G over the available processors running copies of T. The ternary predicate allows specifying the number N of requested threads.
setup_balance(S, G, T):
setup_balance(S, G, T, N):
The predicate succeeds whenever S, G, T succeeds. The predicate distributes the work generated by G over the available processors running copies of S, T. The ter-nary predicate allows specifying the number N of requested threads.
submit(C, N):
The predicate succeeds in running a copy of the goal C in a new thread and unifies N with its new name.
cancel(N):
The predicate succeeds in stopping the thread with the name N.

Kommentare