next up previous
Next: 4.3 Findall/3 with multiple Up: 4 Using BinProlog Previous: 4.1 The interactive toplevel

4.2 Using multiple logic engines

A new (experimental) feature added starting with version 3.39 alows launching multiple Prolog engines having their own stack groups (heap, local stack and trail). An engine can be seen as an abstract data-type which produces a (possibly infinite) stream of solutions as needed. To create an engine use:

  % :-mode create_engine(+,+,+,-).
  create_engine(HeapSize,StackSize,TrailSize,Handle)

The Handle is a unique integer denoting the engine for further processing. To `fuel' the engine with a goal and an expected answer variable do:

  % :-mode load_engine(+,+,+).
  load_engine(Handle,Goal,AnswerVariable)

No processing except the initialization of the engine takes place and no answer is returned with this operation. Think about it as simply puting gas in your car.

To get an answer from the engine do:

  % :-mode ask_engine(+,-).
  ask_engine(Handle,Answer)

When the stream of answers reaches its end, ask_engine/2 will simply fail.

If for some reasons you are not interested in the engine anymore, do

  % :-mode destroy_engine(+).
  destroy_engine(Handle)

to free the data areas alocated for the engine. If you want to submit another query before using the complet stream of answers, it is more efficient to reuse an existing engine with load_engine/3, instead of destroying it and creating a new one.

Try out the following example (see more in files library/engines, progs/engtest.pl):

 ?-create_engine(256,64,64,E),
   load_engine(E,append(As,Bs,[A,B,B,A]),As+Bs),
   ask_engine(E,R1),write(R1),nl,
   ask_engine(E,R2),write(R2),nl,
   load_engine(E,member(X,[1,2,3]),X),
   ask_engine(E,R3),write(R3),nl,
   ask_engine(E,R4),write(R4),nl,
   destroy_engine(E).

As engines will be assigned to real processors in future multi-threaded implementations this reusability of a given engine for execution of multiple goals is intended to allow precise control to the programmer over the resurces of a system. Preemptive multitasking handled either with thread libraries, when available and efficient, or by BinProlog itself otherwise, is planned in the near future.

This `super-scalar' performance improvement can already be tested with the companion (Solaris 2.x only) Linda implementation in directory multi (see file multi/myprogs/mcolor.pl).

If an engine fails due to the overflow of a given data area, it will warn you and fail. The warnings are disabled with quietness levels higher than 6 (command line switch q6). This allows use of engines to quietly recover from ressource errors.

For owners of BinProlog's C-source licenses, C-functions with similar names and semantics as the predicates described in this section are available to allow embedding of multiple independent Prolog engines in their C-applications.



next up previous
Next: 4.3 Findall/3 with multiple Up: 4 Using BinProlog Previous: 4.1 The interactive toplevel



Paul Tarau
Thu Apr 3 10:26:39 AST 1997