 WaitForFirst - Maple Help

Grid

 WaitForFirst
 wait for parallel computation to finish Calling Sequence WaitForFirst() Description

 • The WaitForFirst command stops execution until one remote compute nodes is finished processing.
 • The returned result is the node number of an idle worker node.
 • When more than one compute node finish at the same time (or when nodes are already idle prior to calling WaitForFirst), the lowest node number is returned.
 • The WaitForFirst command is only available in local Grid mode. Examples

This example shows the steps to start two jobs and wait for the first to finish

 > $\mathrm{Grid}:-\mathrm{Run}\left(0,"for i from 1 to 10^6 do od:",'\mathrm{numnodes}'=2\right)$
 > $\mathrm{Grid}:-\mathrm{Run}\left(1,"for i from 1 to 10^5 do od:"\right)$
 > $\mathrm{Grid}:-\mathrm{WaitForFirst}\left(\right)$
 ${1}$ (1)

If you are going to run the next example right away, you need to ensure both of the previous jobs are complete.

 > $\mathrm{Grid}:-\mathrm{Wait}\left(\right)$

The following example demonstrates the implementation of a first-come-first-serve work splitting algorithm. As soon as a node is finished, it will get sent more work to do. Generally, unless f is expensive to compute, it is better to compute a range of things at a time in each node. This example computes only one item at a time. See Grid:-Seq for a similar concept.

 > runInParallel := proc( f, n )   local i, result, freenode;   result := Array(1..n);   for i from 1 to min(Grid:-NumNodes(),n) do       Grid:-Run(i-1,f,[i],assignto='result'[i]);   end do;   for i to n do       freenode := Grid:-WaitForFirst();       Grid:-Run(freenode,f,[i],assignto='result'[i]);   end do;   Grid:-Wait();   return result; end:
 > $\mathrm{runInParallel}\left(\mathrm{ithprime},7\right)$
 $\left[\begin{array}{ccccccc}{2}& {3}& {5}& {7}& {11}& {13}& {17}\end{array}\right]$ (2)
 > $\mathrm{runInParallel}\left(i↦{x}^{i},5\right)$
 $\left[\begin{array}{ccccc}{x}& {{x}}^{{2}}& {{x}}^{{3}}& {{x}}^{{4}}& {{x}}^{{5}}\end{array}\right]$ (3)
 > $\mathrm{runInParallel}\left(i↦\mathrm{combinat}:-\mathrm{unrankperm}\left(i,4\right),\mathrm{combinat}:-\mathrm{numbperm}\left(4\right)\right)$
 $\left[\begin{array}{cccccccccccccccccccccccc}\left[{1}{,}{2}{,}{3}{,}{4}\right]& \left[{1}{,}{2}{,}{4}{,}{3}\right]& \left[{1}{,}{3}{,}{2}{,}{4}\right]& \left[{1}{,}{3}{,}{4}{,}{2}\right]& \left[{1}{,}{4}{,}{2}{,}{3}\right]& \left[{1}{,}{4}{,}{3}{,}{2}\right]& \left[{2}{,}{1}{,}{3}{,}{4}\right]& \left[{2}{,}{1}{,}{4}{,}{3}\right]& \left[{2}{,}{3}{,}{1}{,}{4}\right]& \left[{2}{,}{3}{,}{4}{,}{1}\right]& \left[{2}{,}{4}{,}{1}{,}{3}\right]& \left[{2}{,}{4}{,}{3}{,}{1}\right]& \left[{3}{,}{1}{,}{2}{,}{4}\right]& \left[{3}{,}{1}{,}{4}{,}{2}\right]& \left[{3}{,}{2}{,}{1}{,}{4}\right]& \left[{3}{,}{2}{,}{4}{,}{1}\right]& \left[{3}{,}{4}{,}{1}{,}{2}\right]& \left[{3}{,}{4}{,}{2}{,}{1}\right]& \left[{4}{,}{1}{,}{2}{,}{3}\right]& \left[{4}{,}{1}{,}{3}{,}{2}\right]& \left[{4}{,}{2}{,}{1}{,}{3}\right]& \left[{4}{,}{2}{,}{3}{,}{1}\right]& \left[{4}{,}{3}{,}{1}{,}{2}\right]& \left[{4}{,}{3}{,}{2}{,}{1}\right]\end{array}\right]$ (4)

This example will run four different jobs and wait for the first one to finish; then will terminate the others.  Note the use of Grid:-Set to define the delay procedure on the remote nodes.  Make sure you reset the number of nodes before calling Set, otherwise only the active nodes will receive the definition of the delay procedure.

 > delay := proc( n, c )   local i;   for i from 1 to n do       printf("%c",c);       Threads:-Sleep(1);   od;   printf("\n");   c; end proc:
 > $\mathrm{Grid}:-\mathrm{Setup}\left('\mathrm{numnodes}'=4\right)$
 $\left[{4}{,}{3}{,}{1}{,}{2}\right]$ (5)
 > $\mathrm{Grid}:-\mathrm{Set}\left(\mathrm{delay}\right)$
 > $\mathrm{Grid}:-\mathrm{Run}\left(0,\mathrm{delay},\left[15,"x"\right]\right)$
 > $\mathrm{Grid}:-\mathrm{Run}\left(1,\mathrm{delay},\left[3,"+"\right]\right)$
 > $\mathrm{Grid}:-\mathrm{Run}\left(2,\mathrm{delay},\left[16,"!"\right]\right)$
 > $\mathrm{Grid}:-\mathrm{Run}\left(3,\mathrm{delay},\left[9,"z"\right]\right)$
 > $n≔\mathrm{Grid}:-\mathrm{WaitForFirst}\left(\right)$
 ${n}{≔}{0}$ (6)
 > $\mathrm{Grid}:-\mathrm{Interrupt}\left(\right)$
 > $\mathrm{Grid}:-\mathrm{Wait}\left(\right)$
 > $\mathrm{Grid}:-\mathrm{GetLastResult}\left(n\right)$ Compatibility

 • The Grid[WaitForFirst] command was introduced in Maple 2015.