Queue jobs, wait for (some of) them to finish, get results.
Queue[cmd]
Enters the given expression into queue, returns a process id (cookie).
WaitOne[pid_list]
Waits for one of the given processes to finish and returns {result,pid,remainingIds}.
Wait[pid_list], Wait[pid], Wait[expr]
Wait for all processes or a single process, return list of results and single result, respectively. The argument of Wait can in fact be any expression containing pids.
Anassociative array keeps track of the state of all processes in the various states; first, a process is queued, then it is running on a certain link, then it is finished and waits for collection of result.
queued, link, finished[result]
Enter a job into queue
SetAttributes[Queue, HoldAll]
Queue[cmd_] :=
Module[{pid},
AbortProtect[
AppendTo[$Queue, Hold[cmd, pid]];
qstate[pid] = queued ];
trace[1, pid, " queued (", Length[$Queue], ")"];
QueueRun[]
pid
]
When link becomes available, start a queued process.
When result is waiting, store it and put link back on pool of idle links.
Create two slaves on local host if remote ones are not available.
![[Graphics:../Images/MSRI98_gr_26.gif]](../Images/MSRI98_gr_26.gif)
Queue a job and wait for it to finish.
![[Graphics:../Images/MSRI98_gr_28.gif]](../Images/MSRI98_gr_28.gif)
![[Graphics:../Images/MSRI98_gr_30.gif]](../Images/MSRI98_gr_30.gif)
Queue several jobs
![[Graphics:../Images/MSRI98_gr_32.gif]](../Images/MSRI98_gr_32.gif)
Let them loose.
![[Graphics:../Images/MSRI98_gr_34.gif]](../Images/MSRI98_gr_34.gif)
Nondeterministic waiting for a job and a convenient use of the return value to drain queue:
![[Graphics:../Images/MSRI98_gr_36.gif]](../Images/MSRI98_gr_36.gif)
![[Graphics:../Images/MSRI98_gr_38.gif]](../Images/MSRI98_gr_38.gif)
Cleanup:
![[Graphics:../Images/MSRI98_gr_40.gif]](../Images/MSRI98_gr_40.gif)
Latency: communication overhead, the wait between end of servicing a request and start of servicing a new one.
MathLink is a buffered stream. Can send additional requests before receiving all outstanding results.
Keep buffers full at all times: no latency, each remote processor will always be busy.
To turn on latency hiding, set $LoadFactor to a value larger than 1, such as 2 through 5.
One job per processor...
![[Graphics:../Images/MSRI98_gr_42.gif]](../Images/MSRI98_gr_42.gif)
Tell them their processor #.
![[Graphics:../Images/MSRI98_gr_43.gif]](../Images/MSRI98_gr_43.gif)
Document converted by Mathematica of Wolfram Research