% This LaTeX document was generated using the LaTeX backend of PlDoc, % The SWI-Prolog documentation system \section{library(thread): High level thread primitives} \label{sec:thread} \begin{tags} \tag{author} Jan Wielemaker \end{tags} This module defines simple to use predicates for running goals concurrently. Where the core multi-threaded API is targeted at communicating long-living threads, the predicates here are defined to run goals concurrently without having to deal with thread creation and maintenance explicitely. Note that these predicates run goals concurrently and therefore these goals need to be thread-safe. As the predicates in this module also abort branches of the computation that are no longer needed, predicates that have side-effect must act properly. In a nutshell, this has the following consequences: \begin{itemize} \item Nice clean Prolog code without side-effects (but with cut) works fine. \item Side-effects are bad news. If you really need assert to store intermediate results, use the \predref{thread_local}{1} declaration. This also guarantees cleanup of left-over clauses if the thread is cancelled. For other side-effects, make sure to use \predref{call_cleanup}{2} to undo them should the thread be cancelled. \item Global variables are ok as they are thread-local and destroyed on thread cancellation. Note however that global variables in the calling thread are \textbf{not} available in the threads that are created. You have to pass the value as an argument and initialise the variable in the new thread. \item Thread-cancellation uses \predref{thread_signal}{2}. Using this code with long-blocking foreign predicates may result in long delays, even if another thread asks for cancellation. \end{itemize} \vspace{0.7cm} \begin{description} \predicate[semidet]{concurrent}{3}{+N, :Goals, +Options} Run \arg{Goals} in parallel using \arg{N} threads. This call blocks until all work has been done. The \arg{Goals} must be independent. They should not communicate using shared variables or any form of global data. All \arg{Goals} must be thread-safe. Execution succeeds if all goals have succeeded. If one goal fails or throws an exception, other workers are abandoned as soon as possible and the entire computation fails or re-throws the exception. Note that if multiple goals fail or raise an error it is not defined which error or failure is reported. On successful completion, variable bindings are returned. Note however that threads have independent stacks and therefore the goal is copied to the worker thread and the result is copied back to the caller of \predref{concurrent}{3}. Choosing the right number of threads is not always obvious. Here are some scenarios: \begin{itemize} \item If the goals are CPU intensive and normally all succeeding, typically the number of CPUs is the optimal number of threads. Less does not use all CPUs, more wastes time in context switches and also uses more memory. \item If the tasks are I/O bound the number of threads is typically higher than the number of CPUs. \item If one or more of the goals may fail or produce an error, using a higher number of threads may find this earlier. \end{itemize} \begin{arguments} \arg{N} & Number of worker-threads to create. Using 1, no threads are created. If \arg{N} is larger than the number of \arg{Goals} we create exactly as many threads as there are \arg{Goals}. \\ \arg{Goals} & List of callable terms. \\ \arg{Options} & Passed to \predref{thread_create}{3} for creating the workers. Only options changing the stack-sizes can be used. In particular, do not pass the detached or alias options. \\ \end{arguments} \begin{tags} \tag{See also} In many cases, \predref{concurrent_maplist}{2} and friends is easier to program and is tractable to program analysis. \end{tags} \predicate[semidet]{concurrent_forall}{2}{:Generate, :Action} \nodescription \predicate[semidet]{concurrent_forall}{3}{:Generate, :Action, +Options} True when \arg{Action} is true for all solutions of \arg{Generate}. This has the same semantics as \predref{forall}{2}, but the \arg{Action} goals are executed in multiple threads. Notable a failing \arg{Action} or a \arg{Action} throwing an exception signals the calling thread which in turn aborts all workers and fails or re-throws the generated error. \arg{Options}: \begin{description} \termitem{threads}{+Count} Number of threads to use. The default is determined by the Prolog flag \prologflag{cpu_count}. \end{description} \begin{tags} \tag{To be done} Ideally we would grow the set of workers dynamically, similar to dynamic scheduling of HTTP worker threads. This would avoid creating threads that are never used if \arg{Generate} is too slow or does not provide enough answers and would further raise the number of threads if \arg{Action} is I/O bound rather than CPU bound. \end{tags} \predicate{concurrent_and}{2}{:Generator, :Test} \nodescription \predicate{concurrent_and}{3}{:Generator, :Test, +Options} Concurrent version of \verb$(Generator,Test)$. This predicate creates a thread providing solutions for \arg{Generator} that are handed to a pool of threads that run \arg{Test} for the different instantiations provided by \arg{Generator} concurrently. The predicate is logically equivalent to a simple conjunction except for two aspects: (1) terms are \textit{copied} from \arg{Generator} to the test \arg{Test} threads while answers are copied back to the calling thread and (2) answers may be produced out of order. If the evaluation of some \arg{Test} raises an exception, \predref{concurrent_and}{2},3 is terminated with this exception. If the caller commits after a given answer or raises an exception while \predref{concurrent_and}{2},3 is active with pending choice points, all involved resources are reclaimed. \arg{Options}: \begin{description} \termitem{threads}{+Count} Create a worker pool holding \arg{Count} threads. The default is the Prolog flag \prologflag{cpu_count}. \end{description} This predicate was proposed by Jan Burse as \verb$balance((Generator,Test))$. \predicate[semidet]{concurrent_maplist}{2}{:Goal, +List} \nodescription \predicate[semidet]{concurrent_maplist}{3}{:Goal, +List1, +List2} \nodescription \predicate[semidet]{concurrent_maplist}{4}{:Goal, +List1, +List2, +List3} Concurrent version of \predref{maplist}{2}. This predicate uses \predref{concurrent}{3}, using multiple \textit{worker} threads. The number of threads is the minimum of the list length and the number of cores available. The number of cores is determined using the prolog flag \verb$cpu_count$. If this flag is absent or 1 or \arg{List} has less than two elements, this predicate calls the corresponding maplist/N version using a wrapper based on \predref{once}{1}. Note that all goals are executed as if wrapped in \predref{once}{1} and therefore these predicates are \textit{semidet}. Note that the the overhead of this predicate is considerable and therefore \arg{Goal} must be fairly expensive before one reaches a speedup. \predicate[semidet]{first_solution}{3}{-X, :Goals, +Options} Try alternative solvers concurrently, returning the first answer. In a typical scenario, solving any of the goals in \arg{Goals} is satisfactory for the application to continue. As soon as one of the tried alternatives is successful, all the others are killed and \predref{first_solution}{3} succeeds. For example, if it is unclear whether it is better to search a graph breadth-first or depth-first we can use: \begin{code} search_graph(Grap, Path) :- first_solution(Path, [ breadth_first(Graph, Path), depth_first(Graph, Path) ], []). \end{code} \arg{Options} include thread stack-sizes passed to thread_create, as well as the options \verb$on_fail$ and \verb$on_error$ that specify what to do if a solver fails or triggers an error. By default execution of all solvers is terminated and the result is returned. Sometimes one may wish to continue. One such scenario is if one of the solvers may run out of resources or one of the solvers is known to be incomplete. \begin{description} \termitem{on_fail}{Action} If \const{stop} (default), terminate all threads and stop with the failure. If \const{continue}, keep waiting. \termitem{on_error}{Action} As above, re-throwing the error if an error appears. \end{description} \begin{tags} \tag{bug} \predref{first_solution}{3} cannot deal with non-determinism. There is no obvious way to fit non-determinism into it. If multiple solutions are needed wrap the solvers in \predref{findall}{3}. \end{tags} \predicate[semidet]{call_in_thread}{2}{+Thread, :Goal} Run \arg{Goal} as an interrupt in the context of \arg{Thread}. This is based on \predref{thread_signal}{2}. If waiting times out, we inject a \verb$stop(Reason)$ exception into \arg{Goal}. Interrupts can be nested, i.e., it is allowed to run a \predref{call_in_thread}{2} while the target thread is processing such an interrupt. This predicate is primarily intended for debugging and inspection tasks. \end{description}