MathLink

Controlling Kernels

Start a local kernel (the command to do so may depend on the operating system):

[Graphics:../Images/MSRI98_gr_3.gif]
[Graphics:../Images/MSRI98_gr_4.gif]

Start a remote kernel with rsh or ssh:

remote=LinkLaunch["ssh urania math -MathLink -noinit"]
[Graphics:../Images/MSRI98_gr_5.gif]

You can also start the remote kernel manually with math -MathLink -linkcreate and then connect to the resulting port using LinkConnect.

Swallowing initial line-number packet.

[Graphics:../Images/MSRI98_gr_6.gif]
[Graphics:../Images/MSRI98_gr_7.gif]
[Graphics:../Images/MSRI98_gr_8.gif]
[Graphics:../Images/MSRI98_gr_9.gif]

Avoid blocking:

[Graphics:../Images/MSRI98_gr_10.gif]
[Graphics:../Images/MSRI98_gr_11.gif]

Sending expressions for evaluation:

[Graphics:../Images/MSRI98_gr_12.gif]

Polling for result

[Graphics:../Images/MSRI98_gr_13.gif]
[Graphics:../Images/MSRI98_gr_14.gif]

Dealing with other packets

[Graphics:../Images/MSRI98_gr_15.gif]
[Graphics:../Images/MSRI98_gr_16.gif]
[Graphics:../Images/MSRI98_gr_17.gif]
[Graphics:../Images/MSRI98_gr_18.gif]
[Graphics:../Images/MSRI98_gr_19.gif]
[Graphics:../Images/MSRI98_gr_20.gif]
[Graphics:../Images/MSRI98_gr_21.gif]

Ending session

[Graphics:../Images/MSRI98_gr_22.gif]
[Graphics:../Images/MSRI98_gr_23.gif]

Lining up Workers

Higher-level routines for starting/closing MathLink connections (OS dependent).

LaunchSlave["host", "cmdTemplate", options...]
ConnectSlave["port@host", options...]

cmdTemplate is a string used in StringForm, `1` will be replaced by the hostname. The second form connects to a waiting kernel; this form can be used if security prevents you from executing a remote command directly.

Start local kernel:

LaunchSlave["localhost", $mathkernel]

List of all available remote kernels

[Graphics:../Images/MSRI98_gr_24.gif]

Sending and Receiving Expressions

Basic remote evaluation (blocking):

SetAttributes[RemoteEvaluate, HoldRest]
RemoteEvaluate[expr_, link_LinkObject]:=
    Module[{},
        Send[link, expr];
        Receive[link]

    ]

RemoteEvaluate[cmd] sends cmd to all kernels in parallel, gives list of all results.

Here we gather information about the remote kernels currently running.

[Graphics:../Images/MSRI98_gr_25.gif]
"" "host" "system" "process" "Mathematica Version"
1 "prokyon" "Solaris" 4409 "Solaris 3.0 (April 26, 1997)"
2 "prokyon" "Solaris" 4410 "Solaris 3.0 (April 26, 1997)"
3 "urania" "Linux" 3392 "Linux 3.0 (April 25, 1997)"

Document converted by Mathematica of Wolfram Research