From a7cfc6e7994e08356644aecaf3fef962ef55e336 Mon Sep 17 00:00:00 2001 From: pmoura Date: Wed, 28 Mar 2007 22:50:26 +0000 Subject: [PATCH] Logtalk 2.29.5 files. git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1859 b08c6af1-5177-4d33-ba66-4b1c6b8b522a --- Logtalk/examples/operators/local.lgt | 45 ++++++++++++ Logtalk/examples/threads/msort/NOTES.txt | 32 ++++++++ Logtalk/examples/threads/msort/SCRIPT.txt | 50 +++++++++++++ Logtalk/examples/threads/msort/generator.lgt | 19 +++++ Logtalk/examples/threads/msort/loader.lgt | 4 + Logtalk/examples/threads/msort/msort.lgt | 73 +++++++++++++++++++ .../manuals/refman/builtins/threaded1.html | 73 +++++++++++++++++++ 7 files changed, 296 insertions(+) create mode 100644 Logtalk/examples/operators/local.lgt create mode 100644 Logtalk/examples/threads/msort/NOTES.txt create mode 100644 Logtalk/examples/threads/msort/SCRIPT.txt create mode 100644 Logtalk/examples/threads/msort/generator.lgt create mode 100644 Logtalk/examples/threads/msort/loader.lgt create mode 100644 Logtalk/examples/threads/msort/msort.lgt create mode 100644 Logtalk/manuals/refman/builtins/threaded1.html diff --git a/Logtalk/examples/operators/local.lgt b/Logtalk/examples/operators/local.lgt new file mode 100644 index 000000000..f90d86802 --- /dev/null +++ b/Logtalk/examples/operators/local.lgt @@ -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 diff --git a/Logtalk/examples/threads/msort/NOTES.txt b/Logtalk/examples/threads/msort/NOTES.txt new file mode 100644 index 000000000..8f371311b --- /dev/null +++ b/Logtalk/examples/threads/msort/NOTES.txt @@ -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" } + diff --git a/Logtalk/examples/threads/msort/SCRIPT.txt b/Logtalk/examples/threads/msort/SCRIPT.txt new file mode 100644 index 000000000..bba2d3ec3 --- /dev/null +++ b/Logtalk/examples/threads/msort/SCRIPT.txt @@ -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 diff --git a/Logtalk/examples/threads/msort/generator.lgt b/Logtalk/examples/threads/msort/generator.lgt new file mode 100644 index 000000000..bd7679f69 --- /dev/null +++ b/Logtalk/examples/threads/msort/generator.lgt @@ -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. diff --git a/Logtalk/examples/threads/msort/loader.lgt b/Logtalk/examples/threads/msort/loader.lgt new file mode 100644 index 000000000..f6e35ed0f --- /dev/null +++ b/Logtalk/examples/threads/msort/loader.lgt @@ -0,0 +1,4 @@ + +:- initialization(( + logtalk_load(library(random_loader), [reload(skip)]), + logtalk_load([generator, msort]))). diff --git a/Logtalk/examples/threads/msort/msort.lgt b/Logtalk/examples/threads/msort/msort.lgt new file mode 100644 index 000000000..ce410e6f3 --- /dev/null +++ b/Logtalk/examples/threads/msort/msort.lgt @@ -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. + diff --git a/Logtalk/manuals/refman/builtins/threaded1.html b/Logtalk/manuals/refman/builtins/threaded1.html new file mode 100644 index 000000000..6bdf02aa8 --- /dev/null +++ b/Logtalk/manuals/refman/builtins/threaded1.html @@ -0,0 +1,73 @@ + + + + + + + + Logtalk built-in predicate: threaded/1 + + + + + + +
Logtalk reference manual
+
Built-in predicate: threaded/1
+
+
+ + +

threaded/1

+ + +

Description

+ +
threaded(Conjunction)
+

+Proves each goal in a conjunction of goals in its own thread. The argument can also be a conjunction of messages, either to self or to an explicit object. A call to this predicate blocks until all individual threads terminate. This predicate is transparent to cuts and exceptions. +

+ +

Template and modes

+ +
threaded(+callable)
+ +

Errors

+ +
+
Conjunction is a variable:
+
instantiation_error
+
A goal in Conjunction is a variable:
+
instantiation_error
+
Conjunction is neither a variable nor a callable term:
+
type_error(callable, Conjunction)
+
A goal Goal in Conjunction is neither a variable nor a callable term:
+
type_error(callable, Goal)
+
+ +

Examples

+ +
+
Prove a conjunction of goals, each one in its own thread:
+
threaded((Goal, Goals))
+
Prove a conjunction of messages to self, each one in its own thread:
+
threaded(::(Message, Messages))
+
Prove a conjunction of messages to an object, each one in its own thread:
+
threaded(Object::(Message, Messages))
+
+ + + + + +