Debian Codegraph @

doc/hpc/chap1.html

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
5
6 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
7 <head>
8 <title>GAP (hpc) - Chapter 1: Tasks</title>
9 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
10 <meta name="generator" content="GAPDoc2HTML" />
11 <link rel="stylesheet" type="text/css" href="manual.css" />
12 <script src="manual.js" type="text/javascript"></script>
13 <script type="text/javascript">overwriteStyle();</script>
14 </head>
15 <body class="chap1" onload="jscontent()">
16
17
18 <div class="chlinktop"><span class="chlink1">Goto Chapter: </span><a href="chap0.html">Top</a> <a href="chap1.html">1</a> <a href="chap2.html">2</a> <a href="chap3.html">3</a> <a href="chap4.html">4</a> <a href="chap5.html">5</a> <a href="chap6.html">6</a> <a href="chap7.html">7</a> <a href="chap8.html">8</a> <a href="chap9.html">9</a> <a href="chap10.html">10</a> <a href="chap11.html">11</a> <a href="chapInd.html">Ind</a> </div>
19
20 <div class="chlinkprevnexttop">&nbsp;<a href="chap0.html">[Top of Book]</a>&nbsp; <a href="chap0.html#contents">[Contents]</a>&nbsp; &nbsp;<a href="chap0.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap2.html">[Next Chapter]</a>&nbsp; </div>
21
22 <p id="mathjaxlink" class="pcenter"><a href="chap1_mj.html">[MathJax on]</a></p>
23 <p><a id="X7EA285468533E153" name="X7EA285468533E153"></a></p>
24 <div class="ChapSects"><a href="chap1.html#X7EA285468533E153">1 <span class="Heading">Tasks</span></a>
25 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap1.html#X8389AD927B74BA4A">1.1 <span class="Heading">Overview</span></a>
26 </span>
27 </div>
28 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap1.html#X7C9F2EF384645B8F">1.2 <span class="Heading">Running tasks</span></a>
29 </span>
30 <div class="ContSSBlock">
31 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X7DEC1649841E3813">1.2-1 RunTask</a></span>
32 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X7A86AE967D2A9E94">1.2-2 ScheduleTask</a></span>
33 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X85BC2BBC7EF5B22E">1.2-3 DelayTask</a></span>
34 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X856288C27C62B14A">1.2-4 RunAsyncTask</a></span>
35 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X7EAC7A4B86F5F74C">1.2-5 ScheduleAsyncTask</a></span>
36 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X791CA0E07B16E7F9">1.2-6 MakeTaskAsync</a></span>
37 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X84AFD0FA7A4E3633">1.2-7 ImmediateTask</a></span>
38 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X83D2E32C8759FB37">1.2-8 ExecuteTask</a></span>
39 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X861BC1488345C424">1.2-9 WaitTask</a></span>
40 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X81489CD684EA11B6">1.2-10 WaitAnyTask</a></span>
41 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X7A86C732823E04B6">1.2-11 TaskResult</a></span>
42 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X830A82A7847993FF">1.2-12 CullIdleTasks</a></span>
43 </div></div>
44 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap1.html#X86F8419B7968345B">1.3 <span class="Heading">Information about tasks</span></a>
45 </span>
46 <div class="ContSSBlock">
47 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X87BB5EF38415B923">1.3-1 TaskSuccess</a></span>
48 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X85F42F5E7E04F3C2">1.3-2 TaskError</a></span>
49 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X7C0C66E37DB9ACC0">1.3-3 CurrentTask</a></span>
50 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X7DA4E06B792002C7">1.3-4 RunningTasks</a></span>
51 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X7A023A657BB4F17D">1.3-5 TaskStarted</a></span>
52 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X7E7500E17EC1067D">1.3-6 TaskFinished</a></span>
53 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X7B37CED386024A1F">1.3-7 TaskIsAsync</a></span>
54 </div></div>
55 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap1.html#X7B286945821A3BBA">1.4 <span class="Heading">Cancelling tasks</span></a>
56 </span>
57 <div class="ContSSBlock">
58 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X86C26E3081C3C9AA">1.4-1 CancelTask</a></span>
59 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X8431B91487A8A3F1">1.4-2 TaskCancellationRequested</a></span>
60 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X7D61F9C487232DE7">1.4-3 OnTaskCancellation</a></span>
61 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X82D1773F8058CA54">1.4-4 OnTaskCancellationReturn</a></span>
62 </div></div>
63 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap1.html#X802FC4C284F68696">1.5 <span class="Heading">Conditions</span></a>
64 </span>
65 </div>
66 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap1.html#X7EE6DC40842CD74B">1.6 <span class="Heading">Milestones</span></a>
67 </span>
68 <div class="ContSSBlock">
69 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X80CB3247854A46E4">1.6-1 NewMilestone</a></span>
70 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X800AE2737CB715C5">1.6-2 ContributeToMilestone</a></span>
71 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X7ACB26D38096CD56">1.6-3 AchieveMilestone</a></span>
72 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap1.html#X83E7AF007E040FB9">1.6-4 IsMilestoneAchieved</a></span>
73 </div></div>
74 </div>
75
76 <h3>1 <span class="Heading">Tasks</span></h3>
77
78 <p><a id="X8389AD927B74BA4A" name="X8389AD927B74BA4A"></a></p>
79
80 <h4>1.1 <span class="Heading">Overview</span></h4>
81
82 <p>Tasks provide mid- to high-level functionality for programmers to describe asynchronous workflows. A task is an asynchronously or synchronously executing job; functions exist to create tasks that are executed concurrently, on demand, or in the current thread; to wait for their completion, check their status, and retrieve any results.</p>
83
84 <p>Here is a simple example of sorting a list in the background:</p>
85
86
87 <div class="example"><pre>
88 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">task := RunTask(x -&gt; SortedList(x), [3,2,1]);;</span>
89 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">WaitTask(task);</span>
90 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">TaskResult(task);</span>
91 [ 1, 2, 3 ]
92 </pre></div>
93
94 <p><code class="func">RunTask</code> (<a href="chap1.html#X7DEC1649841E3813"><span class="RefLink">1.2-1</span></a>) dispatches a task to run in the background; a task is described by a function and zero or more arguments that are passed to <code class="func">RunTask</code> (<a href="chap1.html#X7DEC1649841E3813"><span class="RefLink">1.2-1</span></a>). <code class="func">WaitTask</code> (<a href="chap1.html#X861BC1488345C424"><span class="RefLink">1.2-9</span></a>) waits for the task to complete; and <code class="code">TaskResult</code> returns the result of the task.</p>
95
96 <p><code class="func">TaskResult</code> (<a href="chap1.html#X7A86C732823E04B6"><span class="RefLink">1.2-11</span></a>) does an implicit <code class="func">WaitTask</code> (<a href="chap1.html#X861BC1488345C424"><span class="RefLink">1.2-9</span></a>), so the second line above can actually be omitted:</p>
97
98
99 <div class="example"><pre>
100 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">task := RunTask(x -&gt; SortedList(x), [3,2,1]);;</span>
101 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">TaskResult(task);</span>
102 [ 1, 2, 3 ]
103 </pre></div>
104
105 <p>It is simple to run two tasks in parallel. Let's compute the factorial of 10000 by splitting the work between two tasks:</p>
106
107
108 <div class="example"><pre>
109 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">task1 := RunTask(Product, [1..5000]);;</span>
110 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">task2 := RunTask(Product, [5001..10000]);;</span>
111 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">TaskResult(task1) * TaskResult(task2) = Factorial(10000);</span>
112 true
113 </pre></div>
114
115 <p>You can use <code class="func">DelayTask</code> (<a href="chap1.html#X85BC2BBC7EF5B22E"><span class="RefLink">1.2-3</span></a>) to delay executing the task until its result is actually needed.</p>
116
117
118 <div class="example"><pre>
119 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">task1 := DelayTask(Product, [1..5000]);;</span>
120 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">task2 := DelayTask(Product, [5001..10000]);;</span>
121 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">WaitTask(task1, task2);</span>
122 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">TaskResult(task1) * TaskResult(task2) = Factorial(10000);</span>
123 true
124 </pre></div>
125
126 <p>Note that <code class="func">WaitTask</code> (<a href="chap1.html#X861BC1488345C424"><span class="RefLink">1.2-9</span></a>) is used here to start execution of both tasks; otherwise, <code class="code">task2</code> would not be started until <code class="code">TaskResult(task1)</code> has been evaluated.</p>
127
128 <p>To start execution of a delayed task, you can also use <code class="code">ExecuteTask</code>. This has no effect if a task has already been running.</p>
129
130 <p>For convenience, you can also use <code class="func">ImmediateTask</code> (<a href="chap1.html#X84AFD0FA7A4E3633"><span class="RefLink">1.2-7</span></a>) to execute a task synchronously (i.e., the task is started immediately and the call does not return until the task has completed).</p>
131
132
133 <div class="example"><pre>
134 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">task := ImmediateTask(x -&gt; SortedList(x), [3,2,1]);;</span>
135 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">TaskResult(task);</span>
136 [ 1, 2, 3 ]
137 </pre></div>
138
139 <p>This is indistinguishable from calling the function directly, but provides the same interface as normal tasks.</p>
140
141 <p>If e.g. you want to call a function only for its side-effects, it can be useful to ignore the result of a task. <code class="func">RunAsyncTask</code> (<a href="chap1.html#X856288C27C62B14A"><span class="RefLink">1.2-4</span></a>) provides the necessary functionality. Such a task cannot be waited for and its result (if any) is ignored.</p>
142
143
144 <div class="example"><pre>
145 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RunAsyncTask(function() Print("Hello, world!\n"); end);;</span>
146 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">!list</span>
147 --- Thread 0 [0]
148 --- Thread 5 [5] (pending output)
149 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">!5</span>
150 --- Switching to thread 5
151 [5] Hello, world!
152 !0
153 --- Switching to thread 0
154 gap&gt;
155 </pre></div>
156
157 <p>For more information on the multi-threaded user interface, see Chapter <a href="chap4.html#X7AFF436381C319CD"><span class="RefLink">4</span></a>.</p>
158
159 <p>Task arguments are generally copied so that both the task that created them and the task that uses them can access the data concurrently without fear of race conditions. To avoid copying, arguments should be made shared or public (see the relevant parts of section <a href="chap3.html#X87315FA584A37637"><span class="RefLink">3.6</span></a> on migrating objects between regions); shared and public arguments will not be copied.</p>
160
161 <p>HPC-GAP currently has multiple implementations of the task API. To use an alternative implementation to the one documented here, set the environment variable <code class="code">GAP_WORKSTEALING</code> to a non-empty value before starting GAP.</p>
162
163 <p><a id="X7C9F2EF384645B8F" name="X7C9F2EF384645B8F"></a></p>
164
165 <h4>1.2 <span class="Heading">Running tasks</span></h4>
166
167 <p><a id="X7DEC1649841E3813" name="X7DEC1649841E3813"></a></p>
168
169 <h5>1.2-1 RunTask</h5>
170
171 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; RunTask</code>( <var class="Arg">func</var>[, <var class="Arg">arg1</var>, <var class="Arg">...</var>, <var class="Arg">argn</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
172 <p><code class="func">RunTask</code> prepares a task for execution and starts it. The task will call the function <var class="Arg">func</var> with arguments <var class="Arg">arg1</var> through <var class="Arg">argn</var> (if provided). The return value of <var class="Arg">func</var> is the result of the task. The <code class="func">RunTask</code> call itself returns a task object that can be used by functions that expect a task argument.</p>
173
174 <p><a id="X7A86AE967D2A9E94" name="X7A86AE967D2A9E94"></a></p>
175
176 <h5>1.2-2 ScheduleTask</h5>
177
178 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ScheduleTask</code>( <var class="Arg">condition</var>, <var class="Arg">func</var>[, <var class="Arg">arg1</var>, <var class="Arg">...</var>, <var class="Arg">argn</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
179 <p><code class="func">ScheduleTask</code> prepares a task for execution, but, unlike <code class="func">RunTask</code> (<a href="chap1.html#X7DEC1649841E3813"><span class="RefLink">1.2-1</span></a>) does not start it until <var class="Arg">condition</var> is met. See on how to construct conditions. Simple examples of conditions are individual tasks, where execution occurs after the task completes, or lists of tasks, where execution occurs after all tasks in the list complete.</p>
180
181
182 <div class="example"><pre>
183 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">t1 := RunTask(x-&gt;x*x, 3);;</span>
184 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">t2 := RunTask(x-&gt;x*x, 4);;</span>
185 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">t := ScheduleTask([t1, t2], function()</span>
186 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> return TaskResult(t1) + TaskResult(t2);</span>
187 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> end);;</span>
188 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">TaskResult(t);</span>
189 25
190 </pre></div>
191
192 <p>While the above example could also be achieved with <code class="func">RunTask</code> (<a href="chap1.html#X7DEC1649841E3813"><span class="RefLink">1.2-1</span></a>) in lieu of <code class="func">ScheduleTask</code>, since <code class="func">TaskResult</code> (<a href="chap1.html#X7A86C732823E04B6"><span class="RefLink">1.2-11</span></a>) would wait for <var class="Arg">t1</var> and <var class="Arg">t2</var> to complete, the above implementation does not actually start the final task until the others are complete, making it more efficient, since no additional worker thread needs to be occupied.</p>
193
194 <p><a id="X85BC2BBC7EF5B22E" name="X85BC2BBC7EF5B22E"></a></p>
195
196 <h5>1.2-3 DelayTask</h5>
197
198 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; DelayTask</code>( <var class="Arg">func</var>[, <var class="Arg">arg1</var>, <var class="Arg">...</var>, <var class="Arg">argn</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
199 <p><code class="func">DelayTask</code> works as <code class="func">RunTask</code> (<a href="chap1.html#X7DEC1649841E3813"><span class="RefLink">1.2-1</span></a>), but its start is delayed until it is being waited for (including implicitly by calling <code class="func">TaskResult</code> (<a href="chap1.html#X7A86C732823E04B6"><span class="RefLink">1.2-11</span></a>)).</p>
200
201 <p><a id="X856288C27C62B14A" name="X856288C27C62B14A"></a></p>
202
203 <h5>1.2-4 RunAsyncTask</h5>
204
205 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; RunAsyncTask</code>( <var class="Arg">func</var>[, <var class="Arg">arg1</var>, <var class="Arg">...</var>, <var class="Arg">argn</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
206 <p><code class="func">RunAsyncTask</code> creates an asynchronous task. It works like <code class="func">RunTask</code> (<a href="chap1.html#X7DEC1649841E3813"><span class="RefLink">1.2-1</span></a>), except that its result will be ignored.</p>
207
208 <p><a id="X7EAC7A4B86F5F74C" name="X7EAC7A4B86F5F74C"></a></p>
209
210 <h5>1.2-5 ScheduleAsyncTask</h5>
211
212 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ScheduleAsyncTask</code>( <var class="Arg">condition</var>, <var class="Arg">func</var>[, <var class="Arg">arg1</var>, <var class="Arg">...</var>, <var class="Arg">argn</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
213 <p><code class="func">ScheduleAsyncTask</code> creates an asynchronous task. It works like <code class="func">ScheduleTask</code> (<a href="chap1.html#X7A86AE967D2A9E94"><span class="RefLink">1.2-2</span></a>), except that its result will be ignored.</p>
214
215 <p><a id="X791CA0E07B16E7F9" name="X791CA0E07B16E7F9"></a></p>
216
217 <h5>1.2-6 MakeTaskAsync</h5>
218
219 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; MakeTaskAsync</code>( <var class="Arg">task</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
220 <p><code class="func">MakeTaskAsync</code> turns a synchronous task into an asynchronous task that cannot be waited for and whose result will be ignored.</p>
221
222 <p><a id="X84AFD0FA7A4E3633" name="X84AFD0FA7A4E3633"></a></p>
223
224 <h5>1.2-7 ImmediateTask</h5>
225
226 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ImmediateTask</code>( <var class="Arg">func</var>[, <var class="Arg">arg1</var>, <var class="Arg">...</var>, <var class="Arg">argn</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
227 <p><code class="func">ImmediateTask</code> executes the task specified by its arguments synchronously, usually within the current thread.</p>
228
229 <p><a id="X83D2E32C8759FB37" name="X83D2E32C8759FB37"></a></p>
230
231 <h5>1.2-8 ExecuteTask</h5>
232
233 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ExecuteTask</code>( <var class="Arg">task</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
234 <p><code class="func">ExecuteTask</code> starts <var class="Arg">task</var> if it is not already running. It has only an effect if its argument is a task returned by <code class="func">DelayTask</code> (<a href="chap1.html#X85BC2BBC7EF5B22E"><span class="RefLink">1.2-3</span></a>); otherwise, it is a no-op.</p>
235
236 <p><a id="X861BC1488345C424" name="X861BC1488345C424"></a></p>
237
238 <h5>1.2-9 WaitTask</h5>
239
240 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; WaitTask</code>( <var class="Arg">task1</var>, <var class="Arg">...</var>, <var class="Arg">taskn</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
241 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; WaitTask</code>( <var class="Arg">condition</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
242 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; WaitTasks</code>( <var class="Arg">task1</var>, <var class="Arg">...</var>, <var class="Arg">taskn</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
243 <p><code class="func">WaitTask</code> waits until <var class="Arg">task1</var> through <var class="Arg">taskn</var> have completed; after that, it returns. Alternatively, a condition can be passed to <code class="func">WaitTask</code> in order to wait until a condition is met. See on how to construct conditions. <code class="func">WaitTasks</code> is an alias for <code class="func">WaitTask</code>.</p>
244
245 <p><a id="X81489CD684EA11B6" name="X81489CD684EA11B6"></a></p>
246
247 <h5>1.2-10 WaitAnyTask</h5>
248
249 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; WaitAnyTask</code>( <var class="Arg">task1</var>, <var class="Arg">...</var>, <var class="Arg">taskn</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
250 <p>The <code class="func">WaitAnyTask</code> function waits for any of its arguments to finish, then returns the number of that task.</p>
251
252
253 <div class="example"><pre>
254 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">task1 := DelayTask(x-&gt;SortedList(x), [3,2,1]);;</span>
255 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">task2 := DelayTask(x-&gt;SortedList(x), [6,5,4]);;</span>
256 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">which := WaitAnyTask(task1, task2);</span>
257 2
258 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">if which = 1 then</span>
259 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> Display(TaskResult(task1));Display(TaskResult(task2));</span>
260 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> else</span>
261 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> Display(TaskResult(task2));Display(TaskResult(task1));</span>
262 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> fi;</span>
263 [ 4, 5, 6 ]
264 [ 1, 2, 3 ]
265 </pre></div>
266
267 <p>One can pass a list of tasks to <code class="code">WaitAnyTask</code> as an argument; <code class="code">WaitAnyTask([task1, ..., taskn])</code> behaves identically to <code class="code">WaitAnyTask(task1, ..., taskn)</code>.</p>
268
269 <p><a id="X7A86C732823E04B6" name="X7A86C732823E04B6"></a></p>
270
271 <h5>1.2-11 TaskResult</h5>
272
273 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; TaskResult</code>( <var class="Arg">task</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
274 <p>The <code class="func">TaskResult</code> function returns the result of a task. It implicitly calls <code class="func">WaitTask</code> (<a href="chap1.html#X861BC1488345C424"><span class="RefLink">1.2-9</span></a>) if that is necessary. Multiple invocations of <code class="func">TaskResult</code> with the same task argument will not do repeated waits and always return the same value.</p>
275
276 <p>If the function executed by <var class="Arg">task</var> encounters an error, <code class="func">TaskResult</code> returns <code class="keyw">fail</code>. Whether <var class="Arg">task</var> encountered an error can be checked via <code class="func">TaskSuccess</code> (<a href="chap1.html#X87BB5EF38415B923"><span class="RefLink">1.3-1</span></a>). In case of an error, the error message can be retrieved via <code class="func">TaskError</code> (<a href="chap1.html#X85F42F5E7E04F3C2"><span class="RefLink">1.3-2</span></a>).</p>
277
278 <p><a id="X830A82A7847993FF" name="X830A82A7847993FF"></a></p>
279
280 <h5>1.2-12 CullIdleTasks</h5>
281
282 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; CullIdleTasks</code>( )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
283 <p>This function terminates unused worker threads.</p>
284
285 <p><a id="X86F8419B7968345B" name="X86F8419B7968345B"></a></p>
286
287 <h4>1.3 <span class="Heading">Information about tasks</span></h4>
288
289 <p><a id="X87BB5EF38415B923" name="X87BB5EF38415B923"></a></p>
290
291 <h5>1.3-1 TaskSuccess</h5>
292
293 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; TaskSuccess</code>( <var class="Arg">task</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
294 <p><code class="func">TaskSuccess</code> waits for <var class="Arg">task</var> and returns <code class="keyw">true</code> if the it finished without encountering an error. Otherwise the function returns <code class="keyw">false</code>.</p>
295
296 <p><a id="X85F42F5E7E04F3C2" name="X85F42F5E7E04F3C2"></a></p>
297
298 <h5>1.3-2 TaskError</h5>
299
300 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; TaskError</code>( <var class="Arg">task</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
301 <p><code class="func">TaskError</code> waits for <var class="Arg">task</var> and returns its error message, if it encountered an error. If it did not encounter an error, the function returns <code class="keyw">fail</code>.</p>
302
303 <p><a id="X7C0C66E37DB9ACC0" name="X7C0C66E37DB9ACC0"></a></p>
304
305 <h5>1.3-3 CurrentTask</h5>
306
307 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; CurrentTask</code>( )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
308 <p>The <code class="func">CurrentTask</code> returns the currently running task.</p>
309
310 <p><a id="X7DA4E06B792002C7" name="X7DA4E06B792002C7"></a></p>
311
312 <h5>1.3-4 RunningTasks</h5>
313
314 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; RunningTasks</code>( )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
315 <p>This function returns the number of currently running tasks. Note that it is only an approximation and can change as new tasks are being started by other threads.</p>
316
317 <p><a id="X7A023A657BB4F17D" name="X7A023A657BB4F17D"></a></p>
318
319 <h5>1.3-5 TaskStarted</h5>
320
321 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; TaskStarted</code>( <var class="Arg">task</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
322 <p>This function returns true if the task has started executing (i.e., for any non-delayed task), false otherwise.</p>
323
324 <p><a id="X7E7500E17EC1067D" name="X7E7500E17EC1067D"></a></p>
325
326 <h5>1.3-6 TaskFinished</h5>
327
328 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; TaskFinished</code>( <var class="Arg">task</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
329 <p>This function returns true if the task has finished executing and its result is available, false otherwise.</p>
330
331 <p><a id="X7B37CED386024A1F" name="X7B37CED386024A1F"></a></p>
332
333 <h5>1.3-7 TaskIsAsync</h5>
334
335 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; TaskIsAsync</code>( <var class="Arg">task</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
336 <p>This function returns true if the task is asynchronous, true otherwise.</p>
337
338 <p><a id="X7B286945821A3BBA" name="X7B286945821A3BBA"></a></p>
339
340 <h4>1.4 <span class="Heading">Cancelling tasks</span></h4>
341
342 <p>HPC-GAP uses a cooperative model for task cancellation. A programmer can request the cancellation of another task, but it is up to that other task to actually terminate itself. The tasks library has functions to request cancellation, to test for the cancellation state of a task, and to perform actions in response to cancellation requests.</p>
343
344 <p><a id="X86C26E3081C3C9AA" name="X86C26E3081C3C9AA"></a></p>
345
346 <h5>1.4-1 CancelTask</h5>
347
348 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; CancelTask</code>( <var class="Arg">task</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
349 <p><code class="func">CancelTask</code> submits a request that <var class="Arg">task</var> is to be cancelled.</p>
350
351 <p><a id="X8431B91487A8A3F1" name="X8431B91487A8A3F1"></a></p>
352
353 <h5>1.4-2 TaskCancellationRequested</h5>
354
355 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; TaskCancellationRequested</code>( <var class="Arg">task</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
356 <p><code class="func">TaskCancellationRequested</code> returns true if <code class="func">CancelTask</code> (<a href="chap1.html#X86C26E3081C3C9AA"><span class="RefLink">1.4-1</span></a>) has been called for <var class="Arg">task</var>, false otherwise.</p>
357
358 <p><a id="X7D61F9C487232DE7" name="X7D61F9C487232DE7"></a></p>
359
360 <h5>1.4-3 OnTaskCancellation</h5>
361
362 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; OnTaskCancellation</code>( <var class="Arg">exit_func</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
363 <p><code class="func">OnTaskCancellation</code> tests if cancellation for the current task has been requested. If so, then <var class="Arg">exit_func</var> will be called (as a parameterless function) and the current task will be aborted. The result of the current task will be the value of <var class="Arg">exit_func()</var>.</p>
364
365
366 <div class="example"><pre>
367 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">task := RunTask(function()</span>
368 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> while true do</span>
369 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> OnTaskCancellation(function() return 314; end);</span>
370 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> od;</span>
371 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> end);;</span>
372 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">CancelTask(task);</span>
373 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">TaskResult(task);</span>
374 314
375 </pre></div>
376
377 <p><a id="X82D1773F8058CA54" name="X82D1773F8058CA54"></a></p>
378
379 <h5>1.4-4 OnTaskCancellationReturn</h5>
380
381 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; OnTaskCancellationReturn</code>( <var class="Arg">value</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
382 <p><code class="func">OnTaskCancellationReturn</code> is a convenience function that does the same as: <code class="code">OnTaskCancellation(function() return value; end);</code></p>
383
384 <p><a id="X802FC4C284F68696" name="X802FC4C284F68696"></a></p>
385
386 <h4>1.5 <span class="Heading">Conditions</span></h4>
387
388 <p><code class="func">ScheduleTask</code> (<a href="chap1.html#X7A86AE967D2A9E94"><span class="RefLink">1.2-2</span></a>) and <code class="func">WaitTask</code> (<a href="chap1.html#X861BC1488345C424"><span class="RefLink">1.2-9</span></a>) can be made to wait on more complex conditions than just tasks. A condition is either a milestone, a task, or a list of milestones and tasks. <code class="func">ScheduleTask</code> (<a href="chap1.html#X7A86AE967D2A9E94"><span class="RefLink">1.2-2</span></a>) starts its task and <code class="func">WaitTask</code> (<a href="chap1.html#X861BC1488345C424"><span class="RefLink">1.2-9</span></a>) returns when the condition has been met. A condition represented by a task is met when the task has completed. A condition represented by a milestone is met when the milestone has been achieved (see below). A condition represented by a list is met when all conditions in the list have been met.</p>
389
390 <p><a id="X7EE6DC40842CD74B" name="X7EE6DC40842CD74B"></a></p>
391
392 <h4>1.6 <span class="Heading">Milestones</span></h4>
393
394 <p>Milestones are a way to represent abstract conditions to which multiple tasks can contribute.</p>
395
396 <p><a id="X80CB3247854A46E4" name="X80CB3247854A46E4"></a></p>
397
398 <h5>1.6-1 NewMilestone</h5>
399
400 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; NewMilestone</code>( [<var class="Arg">list</var>] )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
401 <p>The <code class="func">NewMilestone</code> function creates a new milestone. Its argument is a list of targets, which must be a list of integers and/or strings. If omitted, the list defaults to <code class="code">[0]</code>.</p>
402
403 <p><a id="X800AE2737CB715C5" name="X800AE2737CB715C5"></a></p>
404
405 <h5>1.6-2 ContributeToMilestone</h5>
406
407 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ContributeToMilestone</code>( <var class="Arg">milestone</var>, <var class="Arg">target</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
408 <p>The <code class="func">ContributeToMilestone</code> milestone function contributes the specified target to the milestone. Once all targets have been contributed to a milestone, it has been achieved.</p>
409
410 <p><a id="X7ACB26D38096CD56" name="X7ACB26D38096CD56"></a></p>
411
412 <h5>1.6-3 AchieveMilestone</h5>
413
414 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; AchieveMilestone</code>( <var class="Arg">milestone</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
415 <p>The <code class="func">AchieveMilestone</code> function allows a program to achieve a milestone in a single step without adding individual targets to it. This is most useful in conjunction with the default value for <code class="func">NewMilestone</code> (<a href="chap1.html#X80CB3247854A46E4"><span class="RefLink">1.6-1</span></a>), e.g.</p>
416
417
418 <div class="example"><pre>
419 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">m := NewMilestone();;</span>
420 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">AchieveMilestone(m);</span>
421 </pre></div>
422
423 <p>&gt;</p>
424
425 <p><a id="X83E7AF007E040FB9" name="X83E7AF007E040FB9"></a></p>
426
427 <h5>1.6-4 IsMilestoneAchieved</h5>
428
429 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsMilestoneAchieved</code>( <var class="Arg">milestone</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
430 <p><code class="func">IsMilestoneAchieved</code> tests explicitly if a milestone has been achieved. It returns <code class="keyw">true</code> on success, <code class="keyw">false</code> otherwise.</p>
431
432
433 <div class="example"><pre>
434 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">m := NewMilestone([1,2]);;</span>
435 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ContributeToMilestone(m, 1);</span>
436 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsMilestoneAchieved(m);</span>
437 false
438 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ContributeToMilestone(m, 2);</span>
439 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsMilestoneAchieved(m);</span>
440 true
441 </pre></div>
442
443
444 <div class="chlinkprevnextbot">&nbsp;<a href="chap0.html">[Top of Book]</a>&nbsp; <a href="chap0.html#contents">[Contents]</a>&nbsp; &nbsp;<a href="chap0.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap2.html">[Next Chapter]</a>&nbsp; </div>
445
446
447 <div class="chlinkbot"><span class="chlink1">Goto Chapter: </span><a href="chap0.html">Top</a> <a href="chap1.html">1</a> <a href="chap2.html">2</a> <a href="chap3.html">3</a> <a href="chap4.html">4</a> <a href="chap5.html">5</a> <a href="chap6.html">6</a> <a href="chap7.html">7</a> <a href="chap8.html">8</a> <a href="chap9.html">9</a> <a href="chap10.html">10</a> <a href="chap11.html">11</a> <a href="chapInd.html">Ind</a> </div>
448
449 <hr />
450 <p class="foot">generated by <a href="https://www.math.rwth-aachen.de/~Frank.Luebeck/GAPDoc">GAPDoc2HTML</a></p>
451 </body>
452 </html>