This example shows the steps to start two jobs and wait for the first to finish
>
|
|
>
|
|
If you are going to run the next example right away, you need to ensure both of the previous jobs are complete.
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:
|
>
|
|
>
|
|
>
|
|
| (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:
|
| (5) |
>
|
|
>
|
|
>
|
|
>
|
|