Logtalk 2.28.2 files.
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1713 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
15
Logtalk/examples/threads/primes/NOTES.txt
Normal file
15
Logtalk/examples/threads/primes/NOTES.txt
Normal file
@@ -0,0 +1,15 @@
|
||||
=================================================================
|
||||
Logtalk - Object oriented extension to Prolog
|
||||
Release 2.28.2
|
||||
|
||||
Copyright (c) 1998-2006 Paulo Moura. All Rights Reserved.
|
||||
=================================================================
|
||||
|
||||
|
||||
To load this example and for sample queries, please see the SCRIPT file.
|
||||
|
||||
This folder contains a simple multi-threading example with calculating
|
||||
prime numbers on a given interval. Try to run the example in single and
|
||||
multi-processor (or multi-core) computers and compare the results. Most
|
||||
Prolog compilers allows you to measure the time taken for proving a goal
|
||||
using proprietary predicates.
|
31
Logtalk/examples/threads/primes/SCRIPT.txt
Normal file
31
Logtalk/examples/threads/primes/SCRIPT.txt
Normal file
@@ -0,0 +1,31 @@
|
||||
=================================================================
|
||||
Logtalk - Object oriented extension to Prolog
|
||||
Release 2.28.2
|
||||
|
||||
Copyright (c) 1998-2006 Paulo Moura. All Rights Reserved.
|
||||
=================================================================
|
||||
|
||||
|
||||
% start by loading the loading the example:
|
||||
|
||||
| ?- logtalk_load(primes(loader)).
|
||||
...
|
||||
|
||||
|
||||
% calculate the prime numbers in a given interval using a single thread:
|
||||
|
||||
| ?- primes::st_prime_numbers(1, 200000, Primes).
|
||||
|
||||
Primes = [199999, 199967, 199961, 199933, 199931, 199921, 199909, 199889, 199877|...]
|
||||
|
||||
Yes
|
||||
|
||||
|
||||
% calculate the prime numbers in a given interval by splitting the interval
|
||||
% in two sub-intervals and using a thread pere interval:
|
||||
|
||||
| ?- primes::mt_prime_numbers(1, 200000, Primes).
|
||||
|
||||
Primes = [199999, 199967, 199961, 199933, 199931, 199921, 199909, 199889, 199877|...]
|
||||
|
||||
Yes
|
13
Logtalk/examples/threads/primes/loader.lgt
Normal file
13
Logtalk/examples/threads/primes/loader.lgt
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
:- initialization(
|
||||
logtalk_load(
|
||||
primes)).
|
||||
|
||||
/*
|
||||
If you intend to use the FOP XSL:FO processor for generating PDF documenting
|
||||
files, comment the directive above and uncomment the directive below
|
||||
|
||||
:- initialization(
|
||||
logtalk_load(
|
||||
primes, [xmlsref(standalone)])).
|
||||
*/
|
62
Logtalk/examples/threads/primes/primes.lgt
Normal file
62
Logtalk/examples/threads/primes/primes.lgt
Normal file
@@ -0,0 +1,62 @@
|
||||
|
||||
:- object(primes).
|
||||
|
||||
:- info([
|
||||
version is 1.0,
|
||||
author is 'Paulo Moura',
|
||||
date is 2006/04/14,
|
||||
comment is 'Simple example for comparing single and multi-threading calculation of prime numbers.']).
|
||||
|
||||
:- threaded.
|
||||
|
||||
:- public(st_prime_numbers/3).
|
||||
:- mode(st_prime_numbers(+integer, +integer, -list), one).
|
||||
:- info(st_prime_numbers/3, [
|
||||
comment is 'Returns all prime numbers in the given interval using a single calculation thread.',
|
||||
argnames is ['Inf', 'Sup', 'Primes']]).
|
||||
|
||||
:- public(mt_prime_numbers/3).
|
||||
:- mode(mt_prime_numbers(+integer, +integer, -list), one).
|
||||
:- info(mt_prime_numbers/3, [
|
||||
comment is 'Returns all prime numbers in the given interval using two calculation threads.',
|
||||
argnames is ['Inf', 'Sup', 'Primes']]).
|
||||
|
||||
st_prime_numbers(N, M, Primes) :-
|
||||
M > N,
|
||||
prime_numbers(N, M, [], Primes).
|
||||
|
||||
mt_prime_numbers(N, M, Primes) :-
|
||||
M > N,
|
||||
N1 is N + (M - N) // 2,
|
||||
N2 is N1 + 1,
|
||||
threaded_call(prime_numbers(N, N1, [], Acc)),
|
||||
threaded_call(prime_numbers(N2, M, Acc, Primes)),
|
||||
threaded_exit(prime_numbers(N, N1, [], Acc)),
|
||||
threaded_exit(prime_numbers(N2, M, Acc, Primes)).
|
||||
|
||||
prime_numbers(N, M, Primes, Primes) :-
|
||||
N > M,
|
||||
!.
|
||||
prime_numbers(N, M, Acc, Primes) :-
|
||||
( is_prime(N) ->
|
||||
Acc2 = [N| Acc]
|
||||
; Acc2 = Acc),
|
||||
N2 is N + 1,
|
||||
prime_numbers(N2, M, Acc2, Primes).
|
||||
|
||||
is_prime(2) :- !.
|
||||
is_prime(Prime):-
|
||||
Prime > 2,
|
||||
Prime mod 2 =:= 1,
|
||||
Sqrt is sqrt(Prime),
|
||||
is_prime(3, Sqrt, Prime).
|
||||
|
||||
is_prime(N, Sqrt, Prime):-
|
||||
( N > Sqrt ->
|
||||
true
|
||||
; Prime mod N > 0,
|
||||
N2 is N + 2,
|
||||
is_prime(N2, Sqrt, Prime)
|
||||
).
|
||||
|
||||
:- end_object.
|
Reference in New Issue
Block a user