<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>geek thoughts &#187; ghc</title>
	<atom:link href="http://www.mlesniak.com/category/ghc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mlesniak.com</link>
	<description></description>
	<lastBuildDate>Wed, 12 May 2010 09:04:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Ubuntu 64 Bit and GHCs Garbage Collector</title>
		<link>http://www.mlesniak.com/2010/03/02/ubuntu-64-bit-and-ghcs-garbage-collector/</link>
		<comments>http://www.mlesniak.com/2010/03/02/ubuntu-64-bit-and-ghcs-garbage-collector/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 07:38:19 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[ghc]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[solutions]]></category>

		<guid isPermaLink="false">http://www.mlesniak.com/?p=646</guid>
		<description><![CDATA[Hi, Update: I tested a kernel 2.6.31-12 (from kernel.org) and had the same (if not even worse) behaviour as with 2.6.31-19. So, I apologize. It has nothing to do with Ubuntu per se, but probably with the linux-kernel; that makes me wonder if I haven&#8217;t done something faulty: that behaviour should have attracted the attention [...]]]></description>
			<content:encoded><![CDATA[<p>Hi,</p>
<p><em><strong>Update</strong>: I tested a kernel 2.6.31-12 (from kernel.org) and had the same (if not even worse) behaviour as with 2.6.31-19. So, I apologize. It has nothing to do with Ubuntu per se, but probably with the linux-kernel; that makes me wonder if I haven&#8217;t done something faulty: that behaviour should have attracted the attention of some other Haskell developers in the meantime, but I couldn&#8217;t find any information on it. So, take in mind that the following could be incorrect&#8230;</em></p>
<p>finding this strange behaviour had cost me a lot of hours (see also my <a href="http://www.haskell.org/pipermail/haskell-cafe/2010-March/073938.html">post</a> on the haskell-cafe mailing list), because honestly, who do you blame if your parallel programm behaves strangely:</p>
<ol>
<li>yourself</li>
<li>the compiler</li>
<li>the special patches of your OS vendor to your linux kernel</li>
</ol>
<p>Since the probabilities of an error in 2. are quite low (at lot of people use GHC and its parallelization techniques) and &#8212; at least I thought that &#8212; those in 3. are low, too (some of those people from 1. should be using my OS with GHC for parallel development), I chose 1. Hence, I spent a lot of time trying to find synchronisation and performance problems in my code, which in fact, weren&#8217;t there!</p>
<p>The current setup of a standard Ubuntu 9.10 (64 bit) is a vanilla kernel (2.6.31-19-generic) , used with GHC 6.12.1. Look at this small and seemingly innocent program</p>
<pre lang="haskell'">-- File Pi.hs
-- you need the numbers package from hackage.
module Main where
import Data.Number.CReal
import System.Environment
import GHC.Conc

main = do
    digits &lt;- (read . head) `fmap` getArgs :: IO Int
    showCReal (fromEnum digits) pi `pseq` return ()
</pre>
<p>that you can compile with</p>
<pre>ghc -- make -O2 -threaded Pi.hs -o pi
</pre>
<p>Benchmarking with</p>
<pre>for i in `seq 1 5`;do time pi 5000 +RTS -N1;done
for i in `seq 1 5`;do time pi 5000 +RTS -N2;done</pre>
<p>I received the following exemplary runtimes for the vanilla ubuntu kernel.</p>
<p><br class="spacer_" /></p>
<p><a href="http://www.mlesniak.com/wp-content/uploads/2010/03/bad.png"><img class="aligncenter size-full wp-image-648" title="bad" src="http://www.mlesniak.com/wp-content/uploads/2010/03/bad.png" alt="" width="354" height="310" /></a></p>
<p>And this is the threadscope profile of a particular bad run:</p>
<p><img class="aligncenter size-full wp-image-650" title="thread" src="http://www.mlesniak.com/wp-content/uploads/2010/03/thread.png" alt="" width="583" height="247" /><br />
 As you can see, nearly all time is spend in the Garbage Collector, doing&#8230;things, GCs do&#8230;</p>
<p>I then compiled my own kernel2.633 from kernel.org and received these results:</p>
<p><a href="http://www.mlesniak.com/wp-content/uploads/2010/03/good.png"><img class="aligncenter size-full wp-image-649" title="good" src="http://www.mlesniak.com/wp-content/uploads/2010/03/good.png" alt="" width="354" height="310" /></a></p>
<p><strong>Conclusion:</strong> At least for me and my parallel programs (and even some innocent toy programs like Pi.hs) does the standard Ubuntu 64bit kernel harm parallel performance (especially that of the GC) a lot.</p>
<p>I find this highly surprising:</p>
<ul>
<li>Ubuntu is a widely used distribution</li>
<li> 64 bit versions are also becoming quite common</li>
<li>in my perception Haskell developers use parallelism a lot</li>
</ul>
<p><strong>Question. </strong>Either the intersection of these properties is quite small or my measurements have some serious flaw. I&#8217;d be glad if someone could disprove my results (and give me the opportunity to learn a bit) or support my measurements.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mlesniak.com/2010/03/02/ubuntu-64-bit-and-ghcs-garbage-collector/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Idea for capabilities</title>
		<link>http://www.mlesniak.com/2010/01/03/idea-for-capabilities/</link>
		<comments>http://www.mlesniak.com/2010/01/03/idea-for-capabilities/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 17:50:16 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[capabilities]]></category>
		<category><![CDATA[ghc]]></category>

		<guid isPermaLink="false">http://www.mlesniak.com/?p=636</guid>
		<description><![CDATA[After looking at the source code in Capabilities.c I think a rudimentary implementation to add dynamic capabilities can work like this: Wait, until it is safe to change the capabilities structure. Allocate memory for number_of_capabilities + 1 Copy old capabilities structure to new allocated one Relink to new memory location To be honest, I don&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>After looking at the source code in Capabilities.c I think a rudimentary implementation to add dynamic capabilities can work like this:</p>
<ol>
<li>Wait, until it is safe to change the capabilities structure.</li>
<li>Allocate memory for number_of_capabilities + 1</li>
<li>Copy old capabilities structure to new allocated one</li>
<li>Relink to new memory location</li>
</ol>
<p>To be honest, I don&#8217;t think it&#8217;s so easy; but at least this is a starting point I can use. Currently, I have to things to think about and which have to be solved before I can hack around in the code:</p>
<ol>
<li>How can I call C code in the RTS from Haskell?</li>
<li>When is it safe to modify the capabilities structure?</li>
</ol>
<p>Happy for any suggestions,<br />
 Michael</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mlesniak.com/2010/01/03/idea-for-capabilities/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Where am I?</title>
		<link>http://www.mlesniak.com/2009/12/12/where-am-i/</link>
		<comments>http://www.mlesniak.com/2009/12/12/where-am-i/#comments</comments>
		<pubDate>Sat, 12 Dec 2009 13:19:14 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[capabilities]]></category>
		<category><![CDATA[ghc]]></category>

		<guid isPermaLink="false">http://www.mlesniak.com/?p=618</guid>
		<description><![CDATA[And what am I doing here? I currently read (more or less randomized) remarks about GHC&#8217;s source code; the codebase is quite huge (and probably the biggest code base that I&#8217;ve ever worked with): Extension Lines .lhs 300482 .hs 473479 .c 184039 .h 448394 combined 1406394 Found using wc -l `find . -name \*.` Thankfully [...]]]></description>
			<content:encoded><![CDATA[<p>And what am I doing here? I currently read (more or less randomized) remarks about GHC&#8217;s source code; the codebase is quite huge (and probably the biggest code base that I&#8217;ve ever worked with):</p>
<pre style="padding-left: 30px;" lang="c">Extension  Lines
.lhs       300482
.hs        473479
.c         184039
.h         448394

combined  1406394

Found using wc -l `find . -name \*.`
</pre>
<p>Thankfully I only need a tiny fraction of it to add the dynamic-capabilities-feature: according to the <a href="http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts">commentary</a> on the runtime system it consist of only about 50000 lines, and again, I probably need to modify / extend just a fraction of it. The code of the runtime system has an <a href="http://hackage.haskell.org/trac/ghc/wiki/Commentary/SourceTree/Rts">extra page</a> on the wiki; it describes</p>
<ul>
<li>the purpose of the single files. Unfortunately these are currently only one liners and links to the particular files. Maybe I will add a much more extensive description for the Capabilities-files.</li>
<li>the<a href="http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/Conventions"> coding conventions</a> for this subsystem. They are quite extensive but (in my humble opinion) very good and describe not only syntactical conventions but also some general remarks, debugging and robustness tricks. </li>
</ul>
<p>For seeing how the capabilites are initialized I think that the file RtsFlag.c is helpful:</p>
<ul>
<li>RtsFlag.c defines parsing for -N&lt;&#8230;&gt;, hence I see where the capabilites are set and how they fit in the big puzzle of the RTS. </li>
<li>RtsFlag.c uses and sets RtsFlags.ParFlags.nNodes (line 958). The structure is included in includes/Rts.h which includes include/rts/Flags.h (line 189 defines the important parts). </li>
<li>RtsMessage.c would probably help me in the future for printing some debugging messages.</li>
</ul>
<p>I&#8217;m curious if I&#8217;m on the right track. By the way, take all the informations with a grain of salt, since I&#8217;m currently in the beginning of understanding the code of the runtime system.</p>
<p><strong>Minor update</strong>: After a look at the <a href="http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts">commentary</a> I&#8217;m pretty sure I need to understand much of the <a href="http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/Scheduler">scheduling subsystem</a> to extend the capabilities-system. Thankfully the wiki seems to document it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mlesniak.com/2009/12/12/where-am-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GHC 6.12 RC1 and Ubuntu 9.10 (Karmic Koala)</title>
		<link>http://www.mlesniak.com/2009/12/01/ghc-6-12-rc1-and-ubuntu-9-10-karmic-koala/</link>
		<comments>http://www.mlesniak.com/2009/12/01/ghc-6-12-rc1-and-ubuntu-9-10-karmic-koala/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 09:53:57 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[ghc]]></category>
		<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://www.mlesniak.com/?p=605</guid>
		<description><![CDATA[I&#8217;m excited about GHC 6.12 and hope it is released soon. Currently I have some problems with Ubuntu 9.10 and its Linux kernel 2.6.31-14. It seems that GHC 6.12 will solve (some) of my problems. For the examplary unoptimized code -- Compile with ghc-6.10.4 -O2 -threaded --make Example.hs -o example module Main where import GHC.Conc [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m excited about GHC 6.12 and hope it is released soon. Currently I have some <a href="http://www.mail-archive.com/haskell-cafe@haskell.org/msg67148.html">problems</a> with Ubuntu 9.10 and its Linux kernel 2.6.31-14. It seems that GHC 6.12 will solve (some) of my problems.</p>
<p>For the examplary unoptimized code</p>

<div class="wp_codebox"><table><tr id="p6052"><td class="code" id="p605code2"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">-- Compile with ghc-6.10.4 -O2 -threaded --make Example.hs -o example</span>
<span style="color: #06c; font-weight: bold;">module</span> Main <span style="color: #06c; font-weight: bold;">where</span>
<span style="color: #06c; font-weight: bold;">import</span> GHC<span style="color: #339933; font-weight: bold;">.</span>Conc
<span style="color: #06c; font-weight: bold;">import</span> Control<span style="color: #339933; font-weight: bold;">.</span>Concurrent
<span style="color: #06c; font-weight: bold;">import</span> Control<span style="color: #339933; font-weight: bold;">.</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Monad"><span style="color: #cccc00; font-weight: bold;">Monad</span></a>
<span style="color: #06c; font-weight: bold;">import</span> System<span style="color: #339933; font-weight: bold;">.</span>Environment
<span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span>List
<span style="color: #5d478b; font-style: italic;">------------------------------------------------------------</span>
main <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #cccc00; font-weight: bold;">IO</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
main <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
    args <span style="color: #339933; font-weight: bold;">&lt;-</span> getArgs
    <span style="color: #06c; font-weight: bold;">let</span> threads <span style="color: #339933; font-weight: bold;">=</span> numCapabilities    <span style="color: #5d478b; font-style: italic;">-- threads determined by -N...</span>
        taskDur <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">1000000</span>            <span style="color: #5d478b; font-style: italic;">-- big enough magic number</span>
        taskNum <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:read"><span style="font-weight: bold;">read</span></a> <span style="color: #339933; font-weight: bold;">.</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:head"><span style="font-weight: bold;">head</span></a><span style="color: green;">&#41;</span> args <span style="color: #5d478b; font-style: italic;">-- Number of tasks is 1st parameter</span>
&nbsp;
    queue    <span style="color: #339933; font-weight: bold;">&lt;-</span> newChan
    finished <span style="color: #339933; font-weight: bold;">&lt;-</span> newChan
    writeList2Chan queue <span style="color: green;">&#40;</span>replicate taskNum taskDur<span style="color: green;">&#41;</span>
    replicateM<span style="color: #339933; font-weight: bold;">_</span> threads <span style="color: green;">&#40;</span>forkIO <span style="color: green;">&#40;</span>thread queue finished<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
    replicateM<span style="color: #339933; font-weight: bold;">_</span> taskNum <span style="color: green;">&#40;</span>readChan finished<span style="color: green;">&#41;</span>
<span style="color: #5d478b; font-style: italic;">------------------------------------------------------------</span>
thread <span style="color: #339933; font-weight: bold;">::</span> Chan <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a> <span style="color: #339933; font-weight: bold;">-</span>&amp;gt; Chan <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a> <span style="color: #339933; font-weight: bold;">-</span>&amp;gt; <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #cccc00; font-weight: bold;">IO</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span>
thread queue finished <span style="color: #339933; font-weight: bold;">=</span>
    forever <span style="color: #339933; font-weight: bold;">$</span> <span style="color: #06c; font-weight: bold;">do</span>
        task <span style="color: #339933; font-weight: bold;">&lt;-</span> readChan queue
        workFor task
        writeChan finished <span style="color: red;">1</span>
  <span style="color: #06c; font-weight: bold;">where</span> workFor n <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:foldl"><span style="font-weight: bold;">foldl</span></a>' <span style="color: green;">&#40;</span>\a b <span style="color: #339933; font-weight: bold;">-&gt;</span> a <span style="color: #339933; font-weight: bold;">+</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a> <span style="color: green;">&#40;</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">^</span>b<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: red;">1</span> <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">..</span>n<span style="color: green;">&#93;</span> `pseq`
           <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span></pre></td></tr></table></div>

<p>we get the following runtimes on my Dualcore Notebook:</p>
<pre>$ time e-6.12 +RTS -N1 -RTS 16

real	0m43.994s
user	0m43.630s
sys	0m0.040s
$ time e-6.12 +RTS -N2 -RTS 16

real	0m25.987s
user	0m48.790s
sys	0m0.230s
$ time e-6.10 +RTS -N1 -RTS 16

real	0m45.883s
user	0m44.500s
sys	0m0.400s
$ time e-6.10 +RTS -N2 -RTS 16

real	0m38.930s
user	0m46.040s
sys	0m0.490s
</pre>
<p>As you can see, e-6.12 (the version compiled with ghc-6.12-20091010) delivers a good speedup whereas ghc-6.10 does not use my two cores very much. This makes testing, benchmarking and developing my parallel Haskell code currently quite difficult.</p>
<p><br class="spacer_" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mlesniak.com/2009/12/01/ghc-6-12-rc1-and-ubuntu-9-10-karmic-koala/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
