MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Chain of packets in MathLink: are packets always strictly ordered?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg116619] Re: Chain of packets in MathLink: are packets always strictly ordered?
  • From: John Fultz <jfultz at wolfram.com>
  • Date: Mon, 21 Feb 2011 19:31:05 -0500 (EST)

On Mon, 21 Feb 2011 04:20:41 -0500 (EST), Alexey wrote:
> Hello,
>
> The Documentation does not state clear the order of packets returned
> by slave kernel via MathLink. It is natural to assume that (when
> sending an input expression with head EnterExpressionPacket):
>
> 1) the last packet before the next InputNamePacket is always
> ReturnExpressionPacket
> 2) there may be always only one ReturnExpressionPacket and one
> OutputNamePacket for one EnterExpressionPacket
> 3) ReturnExpressionPacket always goes after OutputNamePacket
> 4) after MessagePacket the next packet is always TextPacket with
> contents of that message.
> 5) when working in standard mode there may be only 7 types of returned
> packets: InputNamePacket, OutputNamePacket, ReturnExpressionPacket,
> DisplayPacket, DisplayEndPacket, MessagePacket, TextPacket.
>
> Are these assumptions true?

No, they are not.  I'll pick them apart one by one...

1) If the output is suppressed by a semicolon, then you won't get any
output-related packets.  Also, you might get a ReturnTextPacket if the output is
requested in a textual format such as OutputForm or InputForm.

2) False and false, for the same reason as above.

3) *If* you get an OutputNamePacket, it will be followed by *either* a
ReturnExpressionPacket or a ReturnTextPacket.  If an input produces multiple
outputs, then each OutputNamePacket is followed immediately by its corresponding
return packet.

4) True.

5) Also, ExpressionPacket.  And if you accept arbitrary Mathematica input, you
really should be prepared to accept a ReturnTextPacket as well.  There's also
the PostScript-related packets, but you might be able to safely assume no legacy
behavior and ignore those.


Perhaps answering your specific assumptions isn't a very good way of summarizing
the rules, so let me summarize them.  These are the rules of the state machine
for a front end to the kernel:

* Main loop evaluations (ones that assign In/Out) may begin with an
EnterExpressionPacket or an EnterTextPacket.

* Main loop evaluations are always terminated by exactly one InputNamePacket,
regardless of how many individual inputs the enter packet contained.

* If the main loop evaluation returns output(s), it(they) will be in the form of
an OutputNamePacket[] followed by a ReturnTextPacket or ReturnExpressionPacket.

* Messages are TextPackets (and theoretically could be ExpressionPackets,
although that doesn't happen now) which are identified by being preceded by a
MessagePacket.

* All unidentified Text/ExpressionPackets are considered Print cells.

* You can send a non-main-loop evaluation using EvaluatePacket.  The kernel
always responds to an EvaluatePacket with a ReturnPacket (this rule is slightly
more complex for *the* Mathematica front end, but it suffices for custom front
ends).  Such evaluations should not be sent while you're waiting for an
InputNamePacket.

All of these rules can be observed without too much effort by launching a kernel
from a kernel using, e.g.,

lnk=LinkLaunch["mathkernel -mathlink"]

and then using LinkRead/LinkWrite to communicate with the link.  Although one
needs to be careful with EvaluatePackets that their contents evaluate where you
intended them to.  I sometimes will do SetAttributes[EvaluatePacket, {HoldAll}]
when I'm doing some sort of demonstration or experiment to make sure that
LinkWrite isn't evaluating the expression before it sends it.

Sincerely,

John Fultz
jfultz at wolfram.com
User Interface Group
Wolfram Research, Inc.


  • Prev by Date: Re: NDSolve Unable to find initial conditions that
  • Next by Date: Re: Vector Runge-Kutta ODE solver with compilation?
  • Previous by thread: Chain of packets in MathLink: are packets always strictly ordered?
  • Next by thread: Re: Chain of packets in MathLink: are packets always strictly ordered?