Logtalk 2.29.5 files.

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1859 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
pmoura 2007-03-28 22:50:26 +00:00
parent 4569fca292
commit a7cfc6e799
7 changed files with 296 additions and 0 deletions

View File

@ -0,0 +1,45 @@
% simple example of defining an operator local to a source file
:- op(200, xfx, edge). % global operator, visible within all
% entities defined in this source file
:- object(graph).
:- public(path/3).
:- public((edge)/2).
path(Start, End, [Start, End]) :-
::(Start edge End).
path(Start, End, [Start| Path]) :-
::(Start edge Node),
path(Node, End, Path).
:- end_object.
:- object(graph1,
extends(graph)).
a edge b.
a edge c.
b edge d.
c edge d.
:- end_object.
:- object(graph2,
extends(graph)).
v1 edge v2.
v1 edge v3.
v2 edge v4.
v3 edge v4.
:- end_object.
:- op(0, xfx, edge). % "undefine" the operator, effectively
% making it local to this source file

View File

@ -0,0 +1,32 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.29.5
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
=================================================================
To load this example and for sample queries, please see the SCRIPT file.
This folder contains a simple single-threaded and multi-threaded versions
of the merge sort algorithm. Depending on the size of the lists that are
ordered, the single-threaded version is often faster due not only to the
overhead of thread creation on the multi-threaded version but also because
the merge of the sorted sub-lists, where the bulk of the sorting work is
performed, does not take advantage of multi-threading.
This example uses a simple implementation of the merge sort algorithm,
intended only to illustrate Logtalk multi-threading features. For any
other purpose, you may find the following paper a worthwhile reading:
@incollection{ apt93modular,
author = "Krzysztof R. Apt and Dino Pedreschi",
title = "Modular Termination Proofs for Logic and Pure Prolog Programs.",
booktitle = "116",
month = "31",
publisher = "Centrum voor Wiskunde en Informatica (CWI)",
address = "ISSN 0169-118X",
pages = "35",
year = "1993",
url = "citeseer.ist.psu.edu/apt93modular.html" }

View File

@ -0,0 +1,50 @@
=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.29.5
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
=================================================================
% start by loading the example and the required library files:
| ?- logtalk_load(msort(loader)).
...
% NOTE: the example queries below use a SWI-Prolog proprietary predicate
% time/1 in order to get accurate goal times. For other Prolog compilers,
% replace the time/1 call by any appropriate timing calls (e.g. cputime/0).
% generate a big list of random floats and then sort it using a single thread:
?- generator::list(75000, List), time(msort(1)::msort(List, Sorted)).
% 4,864,942 inferences, 3.25 CPU in 3.42 seconds (95% CPU, 1496905 Lips)
List = [0.0923009, 0.443585, 0.72304, 0.945816, 0.501491, 0.311327, 0.597448, 0.915656, 0.666957|...],
Sorted = [1.39358e-06, 3.65916e-05, 4.01297e-05, 4.06822e-05, 5.07434e-05, 5.89827e-05, 6.09007e-05, 0.00010463, 0.000105771|...]
Yes
% generate a big list of random floats and then sort it using two threads:
?- generator::list(75000, List), time(msort(2)::msort(List, Sorted)).
% 300,072 inferences, 1.77 CPU in 1.15 seconds (153% CPU, 169532 Lips)
List = [0.0796181, 0.891391, 0.117642, 0.497486, 0.559702, 0.225095, 0.974507, 0.890696, 0.342357|...],
Sorted = [2.99566e-07, 5.43309e-06, 1.05778e-05, 3.20893e-05, 4.16823e-05, 4.74887e-05, 7.47632e-05, 0.00010165, 0.000111517|...]
Yes
% generate a big list of random floats and then sort it using four threads:
?- generator::list(75000, List), time(msort(4)::msort(List, Sorted)).
% 375,166 inferences, 1.65 CPU in 0.86 seconds (191% CPU, 227373 Lips)
List = [0.720675, 0.0274524, 0.430037, 0.335113, 0.381095, 0.448501, 0.176328, 0.447618, 0.353423|...],
Sorted = [7.91942e-06, 1.90435e-05, 2.04143e-05, 4.50762e-05, 6.8435e-05, 6.99459e-05, 8.55454e-05, 9.07958e-05, 0.000101131|...]
Yes

View File

@ -0,0 +1,19 @@
:- object(generator).
:- info([
version is 1.0,
author is 'Paulo Moura',
date is 2007/03/22,
comment is 'Simple object defining a predicate for generating lists of random values.']).
:- public(list/2).
list(0, []).
list(N, [R| Rs]) :-
N > 0,
N2 is N - 1,
random::random(R),
list(N2, Rs).
:- end_object.

View File

@ -0,0 +1,4 @@
:- initialization((
logtalk_load(library(random_loader), [reload(skip)]),
logtalk_load([generator, msort]))).

View File

@ -0,0 +1,73 @@
:- object(msort(_Threads)).
:- info([
version is 1.0,
author is 'Paulo Moura',
date is 2007/03/24,
comment is 'Single-threaded and multi-threaded versions of the merge sort algorithm.',
parameters is ['Threads'- 'Number of threads to use in sorting. Valid values are 1, 2, and 4.']]).
:- threaded.
:- public(msort/2).
:- mode(msort(+list, -list), one).
:- info(msort/2, [
comment is 'Sorts a list of terms into ascending order.',
argnames is ['List', 'Sorted']]).
msort(List, Sorted) :-
parameter(1, Threads),
msort(Threads, List, Sorted).
msort(1, List, Sorted) :-
st_msort(List, Sorted).
msort(2, List, Sorted) :-
mt_msort_2(List, Sorted).
msort(4, List, Sorted) :-
mt_msort_4(List, Sorted).
st_msort([], []).
st_msort([X], [X]).
st_msort([X, Y| Xs], Ys) :-
split([X, Y| Xs], X1s, X2s),
st_msort(X1s, Y1s),
st_msort(X2s, Y2s),
merge(Y1s, Y2s, Ys).
mt_msort_2(L, S) :-
split(L, L1, L2),
threaded((
st_msort(L1, S1),
st_msort(L2, S2))),
merge(S1, S2, S).
mt_msort_4(L, S) :-
split(L, L1, L2),
split(L1, L11, L12),
split(L2, L21, L22),
threaded((
st_msort(L11, S11),
st_msort(L12, S12),
st_msort(L21, S21),
st_msort(L22, S22))),
threaded((
merge(S11, S12, S1),
merge(S21, S22, S2))),
merge(S1, S2, S).
split([], [], []).
split([X| Xs], [X| Ys], Zs) :-
split(Xs, Zs, Ys).
merge([X| Xs], [Y| Ys], [X| Zs]) :-
X @=< Y, !,
merge(Xs, [Y| Ys], Zs).
merge([X| Xs], [Y| Ys], [Y| Zs]) :-
X @> Y, !,
merge([X | Xs], Ys, Zs).
merge([], Xs, Xs) :- !.
merge(Xs, [], Xs).
:- end_object.

View File

@ -0,0 +1,73 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="application/xml+xhtml; charset=utf-8" />
<title>Logtalk built-in predicate: threaded/1</title>
<link rel="stylesheet" href="../../screen.css" type="text/css" media="screen"/>
<link rel="stylesheet" href="../../print.css" type="text/css" media="print"/>
</head>
<body>
<div class="top-left">Logtalk reference manual</div>
<div class="top-right">Built-in predicate: threaded/1</div>
<div class="bottom-left"><span class="page"/></div>
<div class="bottom-right"><span class="page"/></div>
<div class="navtop"><a href="../../index.html">contents</a> &gt; <a href="../index.html">reference manual</a> &gt; <a href="../index.html#builtins">built-in predicates</a></div>
<h2 class="codenp">threaded/1<span id="builtins_threaded1"/></h2>
<h4>Description</h4>
<pre>threaded(Conjunction)</pre>
<p>
Proves each goal in a conjunction of goals in its own thread. The argument can also be a conjunction of messages, either to <em>self</em> or to an explicit object. A call to this predicate blocks until all individual threads terminate. This predicate is transparent to cuts and exceptions.
</p>
<h4>Template and modes</h4>
<pre>threaded(+callable)</pre>
<h4>Errors</h4>
<dl>
<dt>Conjunction is a variable:</dt>
<dd><code>instantiation_error</code></dd>
<dt>A goal in Conjunction is a variable:</dt>
<dd><code>instantiation_error</code></dd>
<dt>Conjunction is neither a variable nor a callable term:</dt>
<dd><code>type_error(callable, Conjunction)</code></dd>
<dt>A goal Goal in Conjunction is neither a variable nor a callable term:</dt>
<dd><code>type_error(callable, Goal)</code></dd>
</dl>
<h4>Examples</h4>
<dl>
<dt>Prove a conjunction of goals, each one in its own thread:</dt>
<dd><code>threaded((Goal, Goals))</code></dd>
<dt>Prove a conjunction of messages to <em>self</em>, each one in its own thread:</dt>
<dd><code>threaded(::(Message, Messages))</code></dd>
<dt>Prove a conjunction of messages to an object, each one in its own thread:</dt>
<dd><code>threaded(Object::(Message, Messages))</code></dd>
</dl>
<div class="footer">
<div class="copyright">
<span>Copyright &copy; <a href="mailto:pmoura@logtalk.org">Paulo Moura</a> &mdash; <a href="http://logtalk.org">Logtalk.org</a></span><br/>
<span>Last updated on: March 24, 2007</span>
</div>
<div class="navbottom">
<span><a href="../index.html#builtins">previous</a> | <a href="../../glossary.html">glossary</a> | <a href="threaded_once1.html">next</a></span><br/>
<span><a href="http://validator.w3.org/check/referer">XHTML</a> + <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a></span>
</div>
</div>
</body>
</html>