YAP 7.1.0
demo1.pl
1%% demo1.pl -- Stasinos Konstantopoulos
2%% konstant@let.rug.nl, Thu Jan 24 2002
3%%
4
5:- use_module(library(mpi)).
6
7% make the `floor' operator return integer values
8:- set_prolog_flag(language, iso).
9
10
11%%
12%% This the calculation that needs to be performed, in this case
13%% the sum of [From..To]
14%%
15
16calc( From, From, Acc, Res ) :- calc,
17 Res is Acc + From.
18calc( From, To, Acc, Res ) :- calc,
19 Acc1 is Acc + To,
20 To1 is To - 1,
21 calc( From, To1, Acc1, Res ).
22
23%%
24%% This spreads the work among the processors
25%% and collects the results.
26%%
27
28do(0, NumProc):-
29 do,
30 % broadcast task
31 mpi_bcast(10, 0),
32 set_value(n, NumProc),
33 set_value(acc, 0),
34 set_value,
35 mpi_receive(T, Source, Tag),
36 format( '0: Proc ~q said: ~q (Tag: ~q)~n', [Source,T,Tag] ),
37 % accumulate results
38 get_value(acc, Acc),
39 NewAcc is Acc + T,
40 set_value(acc, NewAcc),
41 % processors still left
42 get_value(n, Counter),
43 NewCounter is Counter - 1,
44 set_value(n, NewCounter),
45 NewCounter == 1,
46 set_value,
47 format( '0: Result: ~q.~n', [NewAcc]).
48
49
50do(Rank, NumProc):-
51 do,
52 % catch the task broadcast
53 mpi_bcast(Job, 0),
54 From is floor(Job * (Rank - 1) / (NumProc - 1)),
55 To is floor(Job * Rank / (NumProc - 1)) - 1,
56 format( '~q: I am calculating ~q..~q.~n', [Rank,From,To] ),
57 % do the job
58 calc( From, To, 0, Result ),
59 format( '~q: sending ~q to 0. (Tag: 1)~n', [Rank,Result] ),
60 % send back the results
61 mpi_send(Result, 0, 1).
62
63
64%%
65%% This is the entry point
66%%
67
68mpi_send:-
69 mpi_open(Rank, NumProc, ProcName),
70 format('Rank: ~q NumProc: ~q, ProcName: ~q~n', [Rank,NumProc,ProcName]),
71 do(Rank, NumProc),
72 format( 'Rank ~q finished!~n', [Rank] ),
73 format.
format(+ T, :L)
use_module( +Files )
set_prolog_flag(+ Flag,+ Value)
get_value(+ A,- V)
set_value(+ A,+ C)
mpi_send(+ Data,+ Dest,+ Tag)