Urban Hafner : Tag concurrency, everything about concurrency http://bettong.net/tag/concurrency.rss en-us 40 CTM:Exercise 4-8 <h3>The problem</h3> <p><em>Dataflow behaviour in a concurrent setting.</em> Consider the function <code>{Filter In F}</code>, which returns the elements of <code>In</code> for which the boolean function <code>F</code> returns <strong>true</strong>. Here is a possible definition of <code>Filter</code>:</p> <div class="typocode"><pre><code class="typocode_default ">fun {Filter In F} case In of X|In2 then if {F X} then X|{Filter In2 F} else {Filter In2 F} end else nil end end</code></pre></div> <p>Executing the following:</p> <div class="typocode"><pre><code class="typocode_default ">{Show {Filter [5 1 2 4 0] fun {$ X} X&gt;2 end}}</code></pre></div> <p>displays</p> <div class="typocode"><pre><code class="typocode_default ">[5 4]</code></pre></div> <p>(We use the procedure <code>Show</code>, which displays the instantaneous value of its argument in Mozart&#8217;s emulator window. Unlike <code>Browse</code>, this output is not updated if the argument is subsequently bound.) So <code>Filter</code> works as expected in the case of a sequential execution when all the input values are available. Let us now explore the dataflow behaviour of <code>Filter</code>.</p> <p><b>a)</b> What happens when we execute the following?: <div class="typocode"><pre><code class="typocode_default ">declare A {Show {Filter [5 1 A 4 0] fun {$ X} X&gt;2 end}}</code></pre></div> One of the list elements is a variable <code>A</code> that is not yet bound to a value. Remember that the <strong>case</strong> and <strong>if</strong> statements will suspend the thread in which they execute, until they can decide which alternative path to take.</p> <p><b>b)</b> What happens when we execute the following?: <div class="typocode"><pre><code class="typocode_default ">declare Out A thread Out={Filter [5 1 A 4 0] fun {$ X} X&gt;2 end} end {Show Out}</code></pre></div> Remember calling <code>Show</code> displays its argument as it exists at the instant of the call. Several possible results can be displayed. Which and why? Is the <code>Filter</code> function deterministic? Why or why not?</p> <p><b>c)</b> What happens when we execute the following?: <div class="typocode"><pre><code class="typocode_default ">declare Out A thread Out={Filter [5 1 A 4 0] fun {$ X} X&gt;2 end} end {Delay 1000} {Show Out}</code></pre></div> Remember that the call <code>{Delay N}</code> suspends its thread for at least <code>N</code> ms. During this time, other ready threads can be executed.</p> <p><b>d)</b> What happens when we execute the following?: <div class="typocode"><pre><code class="typocode_default ">declare Out A thread Out={Filter [5 1 A 4 0] fun {$ X} X&gt;2 end} end thread A=6 end {Delay 1000} {Show Out}</code></pre></div> What is displayed and why?</p> <h3>My solution</h3> <p><b>a)</b> Nothing is displayed because <code>Filter</code> is suspended waiting on <code>A</code> to become bound.</p> <p><b>b)</b> What gets displayed on my box is <code>Out&lt;optimized&gt;</code>. But when I use the Browser I see <code>5|_</code>. Another possible value would <code>_</code> if <code>Show</code> is executed before <code>Filter</code>. But even though there can be different values shown doesn&#8217;t mean that <code>Filter</code> itself is not deterministic. What&#8217;s not deterministic is the way threads are executed and <code>Show</code> is probably not part of the declarative model.</p> <p><b>c)</b> This displays <code>5|_&lt;optimized&gt;</code>. So it seems that the calls to <code>Show</code> in <b>b)</b> that I got were all before the call to <code>Filter</code>.</p> <p><b>d)</b> This of course displays <code>[5 6 4]</code>. <code>Filter</code> can finish because <code>A</code> gets bound, and we are waiting long enough before calling <code>Show</code>.</p> Mon, 07 Nov 2005 14:31:03 +0000 urn:uuid:b64b7534-72ba-4d17-9c31-1724a6ec1278 urban@bettong.net (Urban Hafner) http://bettong.net/2005/11/07/ctm-exercise-4-8#comments CTM exercise book mozart oz dataflow concurrency http://bettong.net/2005/11/07/ctm-exercise-4-8 CTM: Exercise 4-4 <h3>The problem</h3> <p><em>Order-determining concurrency.</em> Explain what happens when executing the following:</p> <div class="typocode"><pre><code class="typocode_default ">declare A B C D in thread B=C+1 end thread C=B+1 end thread A=1 end thread B=A+1 end {Browse D}</code></pre></div> <p>In what order are the threads created? In what order are the additions done? What is the final result? Compare with the following:</p> <div class="typocode"><pre><code class="typocode_default ">declare A B C D in A=1 B=A+1 C=B+1 D=C+1 {Browse D}</code></pre></div> <p>Here there is only one thread. In what order are the additions done? What is the final result? What do you conclude?</p> <h3>My solution</h3> <p>Of course both code snippets produce the same result, namely 4. And of course the threads in the first code example are executed in the same way as in the second. That&#8217;s because the threads have to wait for the variables they are using in the additions to become bound.</p> <p>An what do I conclude? Well, that this kind of concurrency is declarative. At least that&#8217;s what I think I was supposed to answer :)</p> Wed, 02 Nov 2005 16:13:30 +0000 urn:uuid:705cd1fa-27d4-457b-8058-aa6680c62e3a urban@bettong.net (Urban Hafner) http://bettong.net/2005/11/02/ctm-exercise-4-4#comments CTM exercise book mozart oz concurrency dataflow variable http://bettong.net/2005/11/02/ctm-exercise-4-4 CTM: Exercise 4-3 <h3>The problem</h3> <p><em>Concurrent Fibonacci.</em> Consider the following sequential definition of the Fibonacci function:</p> <div class="typocode"><pre><code class="typocode_default ">fun {Fib X} if X=&lt;2 then 1 else {Fib X-1}+{Fib X-2} end end</code></pre></div> <p>and compare it with the concurrent definition given in section 4.2.3. Run both on the Mozart system and compare their performance. How much faster is the sequential definition? How many threads are created by the concurrent call <code>{Fib N}</code> as a function if <code>N</code>?</p> <h3>My solution</h3> <p>For timing I used the following function. Even though it can only count in seconds, it&#8217;s enough for this purpose.</p> <div class="typocode"><pre><code class="typocode_default ">fun {Timer Fun Arg} Before in Before={Time.time} {Fun Arg _} {Time.time}-Before end</code></pre></div> <p>For <code>N=30</code> the normal Fibonacci function took 1 second, the concurrent one 19. For <code>N=40</code> the normal one took 289 seconds and the concurrent one made my computer swap so I stopped it.</p> <p>The number of thread created should be in the same order as the number of function calls done, because every other function call is done in its own thread. As the number of function calls is exponential so is the number of threads. Of course this is quite a lame answer, but neither did I have skills nor the motivation to find an exact solution. Maybe Wikipedia can help?</p> Wed, 02 Nov 2005 16:13:16 +0000 urn:uuid:cbb705d8-f4e0-4e8f-8a17-194cbff85352 urban@bettong.net (Urban Hafner) http://bettong.net/2005/11/02/ctm-exercise-4-3#comments CTM exercise book mozart oz concurrency fibonacci http://bettong.net/2005/11/02/ctm-exercise-4-3