<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Helvetica;
        panose-1:2 11 6 4 2 2 2 2 2 4;}
@font-face
        {font-family:Wingdings;
        panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p
        {mso-style-priority:99;
        mso-margin-top-alt:auto;
        margin-right:0cm;
        mso-margin-bottom-alt:auto;
        margin-left:0cm;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
p.Code, li.Code, div.Code
        {mso-style-name:Code;
        mso-style-link:"Code Char";
        margin-top:0cm;
        margin-right:0cm;
        margin-bottom:0cm;
        margin-left:36.0pt;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Courier New";
        color:#1F497D;}
span.CodeChar
        {mso-style-name:"Code Char";
        mso-style-link:Code;
        font-family:"Courier New";
        color:#1F497D;}
span.EmailStyle20
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
/* List Definitions */
@list l0
        {mso-list-id:430590104;
        mso-list-template-ids:2087123700;}
@list l0:level1
        {mso-level-number-format:bullet;
        mso-level-text:;
        mso-level-tab-stop:36.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Symbol;}
@list l0:level2
        {mso-level-number-format:bullet;
        mso-level-text:o;
        mso-level-tab-stop:72.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:"Courier New";
        mso-bidi-font-family:"Times New Roman";}
@list l0:level3
        {mso-level-number-format:bullet;
        mso-level-text:;
        mso-level-tab-stop:108.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l0:level4
        {mso-level-number-format:bullet;
        mso-level-text:;
        mso-level-tab-stop:144.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l0:level5
        {mso-level-number-format:bullet;
        mso-level-text:;
        mso-level-tab-stop:180.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l0:level6
        {mso-level-number-format:bullet;
        mso-level-text:;
        mso-level-tab-stop:216.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l0:level7
        {mso-level-number-format:bullet;
        mso-level-text:;
        mso-level-tab-stop:252.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l0:level8
        {mso-level-number-format:bullet;
        mso-level-text:;
        mso-level-tab-stop:288.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l0:level9
        {mso-level-number-format:bullet;
        mso-level-text:;
        mso-level-tab-stop:324.0pt;
        mso-level-number-position:left;
        text-indent:-18.0pt;
        mso-ansi-font-size:10.0pt;
        font-family:Wingdings;}
@list l1
        {mso-list-id:1983460147;
        mso-list-template-ids:-266302526;}
ol
        {margin-bottom:0cm;}
ul
        {margin-bottom:0cm;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-GB" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US">Konrad<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><br>
That does sound frustrating.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US">I think your first port of call should be Manuel Chakravarty, the author of accelerate.  The example you give in
<a href="http://stackoverflow.com/questions/27541609/difference-in-performance-of-compiled-accelerate-code-ran-from-ghci-and-shell">
your stackoverflow post</a> can only be some weird systems thing.  After all, you are executing precisely the same code (namely compiled Accelerate code); it’s just that in one case it’s dynamically linked and excecuted from GHCi and in the other it’s linked
 and executed by the shell.  I have no clue what could cause that.  I wonder if you are using a GPU and whether that might somehow behave differently.   Could it be the difference between static linking and dynamic linking (which could plausibly account for
 some startup delay)?  Is it a fixed overhead (eg takes 100ms extra) or does it run a factor of two slower (increase the size of your test case to see)?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US">I’d be happy to have a Skype call with you, but I am rather unlikely to know anything helpful because it doesn’t sound like a core
 Haskell issue at all.   You are executing the very same machine instructions! <o:p>
</o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US">The overheads of the GHC API to compile and run the expression “main” are pretty small.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US">I’m copying ghc-devs in case anyone else has any ideas.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US">Simon<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif"> Konrad Gądek [mailto:kgadek@gmail.com]
<br>
<b>Sent:</b> 14 January 2015 13:59<br>
<b>To:</b> Simon Peyton Jones<br>
<b>Cc:</b> Piotr Młodawski; kgadek@flowbox.io<br>
<b>Subject:</b> Request for assistance from Haskell-oriented startup: GHCi performance<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:12.1pt;margin-left:0cm">
<span style="font-size:10.0pt;font-family:"Helvetica",sans-serif">Dear Mr Jones,</span><span style="font-size:15.0pt;font-family:"Helvetica",sans-serif"><o:p></o:p></span></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:12.1pt;margin-left:0cm">
<span style="font-size:15.0pt;font-family:"Helvetica",sans-serif"><o:p> </o:p></span></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:12.1pt;margin-left:0cm">
<span style="font-size:10.0pt;font-family:"Helvetica",sans-serif">My name is Konrad Gądek and I'm one of the programmers at Flowbox ( <a href="http://flowbox.io">http://flowbox.io</a> ), a startup that is to bring a fresh view on image composition in movie
 industry. We proudly use Haskell in nearly all of our development. I believe you may remember our CEO, Wojciech Daniło, from discussions like in this thread: <a href="https://phabricator.haskell.org/D69">https://phabricator.haskell.org/D69</a> .</span><span style="font-size:15.0pt;font-family:"Helvetica",sans-serif"><o:p></o:p></span></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:12.1pt;margin-left:0cm">
What can be interesting for you is that to achieve our goals as a company, we started developing a new programming language - Luna. Long story short, we believe that Luna could be as beneficial for the Haskell community as Elixir is for Erlang.<o:p></o:p></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:12.1pt;margin-left:0cm">
However, we found some major performance problems with the code that are as critical for us as they are cryptic. We have found difficulties in pinpointing the actual issue, not to mention solving it. We're getting a bit desperate about that, nobody so far has
 been able to help us, and so we would like to ask you for help. We would be really really grateful if you could take a look, maybe your fresh ideas could shed some light on the issue. Details are attached below.<o:p></o:p></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:12.1pt;margin-left:0cm">
Is there any chance we could arrange eg. a Skype call so we could further discuss the matter?<o:p></o:p></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:12.1pt;margin-left:0cm">
<o:p> </o:p></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:12.1pt;margin-left:0cm">
Thank you in advance!<o:p></o:p></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:12.1pt;margin-left:0cm">
<span style="font-size:15.0pt;font-family:"Helvetica",sans-serif"><o:p> </o:p></span></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:12.1pt;margin-left:0cm">
<b><span style="font-size:15.0pt;font-family:"Helvetica",sans-serif">Background</span></b><span style="font-size:15.0pt;font-family:"Helvetica",sans-serif"><o:p></o:p></span></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:9.75pt;margin-left:0cm">
<span style="font-family:"Helvetica",sans-serif">Currently Luna is trans-compiled to Haskell and then compiled to bytecode by GHC. Furthermore, we use ghci to evaluate expressions (the flow graph) interactively. We use accelerate library to perform high-performance
 computations with the help of graphic cards.<o:p></o:p></span></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:12.1pt;margin-left:0cm">
<b><span style="font-size:15.0pt;font-family:"Helvetica",sans-serif">The problem</span></b><span style="font-size:15.0pt;font-family:"Helvetica",sans-serif"><o:p></o:p></span></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:9.75pt;margin-left:0cm">
<span style="font-family:"Helvetica",sans-serif;color:black">Executing some of the functions from libraries compiled with -O2 (especially from accelerate) is much slower than calling it from compiled executable (see <a href="http://stackoverflow.com/questions/27541609/difference-in-performance-of-compiled-accelerate-code-ran-from-ghci-and-shell">http://stackoverflow.com/questions/27541609/difference-in-performance-of-compiled-accelerate-code-ran-from-ghci-and-shell</a> and <a href="https://github.com/AccelerateHS/accelerate/issues/227">https://github.com/AccelerateHS/accelerate/issues/227</a>).</span><span style="font-family:"Helvetica",sans-serif;color:#017BFF"><o:p></o:p></span></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:9.75pt;margin-left:0cm">
<span style="font-family:"Helvetica",sans-serif">Maybe there is some other way to interactively evaluate Haskell code, which is more lightweight/more customizable ie. would not require all ghc-api features which are probably slowing down the whole process?
 Is it possible to just use ghc linker and make function calls simpler and more time efficient?<o:p></o:p></span></p>
<p style="margin:0cm;margin-bottom:.0001pt;min-height: 16px"><span style="font-family:"Helvetica",sans-serif"><o:p> </o:p></span></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:12.1pt;margin-left:0cm">
<b><span style="font-size:15.0pt;font-family:"Helvetica",sans-serif">Details</span></b><span style="font-size:15.0pt;font-family:"Helvetica",sans-serif"><o:p></o:p></span></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:9.75pt;margin-left:0cm">
<span style="font-family:"Helvetica",sans-serif">We feed ghci with statements (using ghc-api) and declarations (using runStmt and runDecls). We can also change imports and language extensions before each call. The overall process is as follows:<o:p></o:p></span></p>
<ul style="margin-top:0cm" type="disc">
<li class="MsoNormal" style="mso-list:l0 level1 lfo1"><span style="font-family:"Helvetica",sans-serif">on init:<o:p></o:p></span></li></ul>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:72.0pt;text-indent:-18.0pt;mso-list:l0 level1 lfo1">
<![if !supportLists]><span style="font-size:10.0pt;font-family:Symbol"><span style="mso-list:Ignore">·<span style="font:7.0pt "Times New Roman"">       
</span></span></span><![endif]><o:p> </o:p></p>
<ul style="margin-top:0cm" type="disc">
<ul style="margin-top:0cm" type="circle">
<li class="MsoNormal" style="mso-list:l0 level2 lfo1"><span style="font-family:"Helvetica",sans-serif">set ghcpath to one with our custom installation of ghc with preinstalled graphic libraries<o:p></o:p></span></li><li class="MsoNormal" style="mso-list:l0 level2 lfo1"><span style="font-family:"Helvetica",sans-serif">set imports to our libraries<o:p></o:p></span></li><li class="MsoNormal" style="mso-list:l0 level2 lfo1"><span style="font-family:"Helvetica",sans-serif">enable/disable appropriate language extensions<o:p></o:p></span></li></ul>
</ul>
<ul style="margin-top:0cm" type="disc">
<li class="MsoNormal" style="mso-list:l0 level1 lfo1"><span style="font-family:"Helvetica",sans-serif">for each run:<o:p></o:p></span></li></ul>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:72.0pt;text-indent:-18.0pt;mso-list:l0 level1 lfo1">
<![if !supportLists]><span style="font-size:10.0pt;font-family:Symbol"><span style="mso-list:Ignore">·<span style="font:7.0pt "Times New Roman"">       
</span></span></span><![endif]><o:p> </o:p></p>
<ul style="margin-top:0cm" type="disc">
<ul style="margin-top:0cm" type="circle">
<li class="MsoNormal" style="mso-list:l0 level2 lfo1"><span style="font-family:"Helvetica",sans-serif">generate haskell code (including datatype declarations, using lenses and TemplateHaskell) and load it to ghci using runDecls<o:p></o:p></span></li><li class="MsoNormal" style="mso-list:l0 level2 lfo1"><span style="font-family:"Helvetica",sans-serif">for each expression:<o:p></o:p></span></li></ul>
</ul>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:108.0pt;text-indent:-18.0pt;mso-list:l0 level2 lfo1">
<![if !supportLists]><span style="font-size:10.0pt;font-family:"Courier New""><span style="mso-list:Ignore">o<span style="font:7.0pt "Times New Roman"">  
</span></span></span><![endif]><o:p> </o:p></p>
<ul style="margin-top:0cm" type="disc">
<ul style="margin-top:0cm" type="circle">
<ul style="margin-top:0cm" type="square">
<li class="MsoNormal" style="mso-list:l0 level3 lfo1"><span style="font-family:"Helvetica",sans-serif">run statements that use freshly generated code<o:p></o:p></span></li><li class="MsoNormal" style="mso-list:l0 level3 lfo1"><span style="font-family:"Helvetica",sans-serif">bind (lazy) results to variables<o:p></o:p></span></li><li class="MsoNormal" style="mso-list:l0 level3 lfo1"><span style="font-family:"Helvetica",sans-serif">evaluate values from bound variables, and get it from GhcMonad to runtime of our interpreter (see
<a href="http://hackage.haskell.org/package/hint%E2%80%930.4.2.1/docs/Language-Haskell-Interpreter.html#v:interpret">
<span style="color:#017BFF">http://hackage.haskell.org/package/hint–0.4.2.1/docs/Language-Haskell-Interpreter.html#v:interpret</span></a>)<o:p></o:p></span></li></ul>
</ul>
</ul>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:9.75pt;margin-left:0cm">
<span style="font-family:"Helvetica",sans-serif">This behaviour was observed when using GHC 7.8.3 (with D69 patch) on Fedora 20 (x86-64), Intel(R) Core(TM) i5-4570 CPU @ 3.20GHz<o:p></o:p></span></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:12.1pt;margin-left:0cm">
<b><span style="font-size:15.0pt;font-family:"Helvetica",sans-serif">Tried so far</span></b><span style="font-size:15.0pt;font-family:"Helvetica",sans-serif"><o:p></o:p></span></p>
<ol style="margin-top:0cm" start="1" type="1">
<li class="MsoNormal" style="mso-list:l1 level1 lfo2"><span style="font-family:"Helvetica",sans-serif">Specializing nearly everything in accelerate library, specializing calls to accelearate methods (no speedup).<o:p></o:p></span></li><li class="MsoNormal" style="mso-list:l1 level1 lfo2"><span style="font-family:"Helvetica",sans-serif">Load precompiled, optimised code to ghci (no speedup).<o:p></o:p></span></li><li class="MsoNormal" style="mso-list:l1 level1 lfo2"><span style="font-family:"Helvetica",sans-serif">Truth to be told, we have no idea what to try next.<o:p></o:p></span></li></ol>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">-- </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">Konrad Gądek</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Helvetica",sans-serif">typechecker team-leader in Flowbox</span><o:p></o:p></p>
</div>
</div>
</div>
</div>
</body>
</html>