{"id":1864,"date":"2015-06-29T08:38:27","date_gmt":"2015-06-29T08:38:27","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=1864"},"modified":"2015-06-29T08:39:34","modified_gmt":"2015-06-29T08:39:34","slug":"in-praise-of-magic-functions-part-two","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2015\/06\/in-praise-of-magic-functions-part-two\/","title":{"rendered":"In Praise of Magic Functions: Part II"},"content":{"rendered":"<p><em><a title=\"In Praise of Magic Functions: Part I\" href=\"http:\/\/www.dyalog.com\/blog\/2015\/06\/in-praise-of-magic-functions-part-one\/\">Part I<\/a> of this post was concerned with the development speed and execution speed of magic functions and should be read before this post.<\/em><\/p>\n<h2 id=\"4\">Benefitting from Future and Past Improvements<\/h2>\n<p>Looking at the magic function for <em>key<\/em> in <a title=\"In Praise of Magic Functions: Part I\" href=\"http:\/\/www.dyalog.com\/blog\/2015\/06\/in-praise-of-magic-functions-part-one\/#2\">Part I<\/a>, we see that its performance depends on the following APL primitives, listed with information on when they were last improved (or when they will be improved):<\/p>\n<table style=\"width: 350px;\">\n<tbody>\n<tr>\n<td><strong>expression<\/strong><\/td>\n<td><strong>factor<\/strong><\/td>\n<td><strong>version<\/strong><\/td>\n<\/tr>\n<tr>\n<td><code class=\"language-apl\">\u234bi<\/code><\/td>\n<td>2-18<\/td>\n<td>12.1<\/td>\n<\/tr>\n<tr>\n<td><code class=\"language-apl\">\u2395DR<\/code><\/td>\n<td>1-3.8<\/td>\n<td>14.1<\/td>\n<\/tr>\n<tr>\n<td><code class=\"language-apl\">\u2373\u2368x<\/code><\/td>\n<td>2-4<\/td>\n<td>14.1<\/td>\n<\/tr>\n<tr>\n<td><code class=\"language-apl\">\u2373\u2368i<\/code><\/td>\n<td>3-6<\/td>\n<td>14.1<\/td>\n<\/tr>\n<tr>\n<td><code class=\"language-apl\">\u2191x<\/code><\/td>\n<td>5-12<\/td>\n<td>15.0<\/td>\n<\/tr>\n<tr>\n<td><code class=\"language-apl\">\u2282[a]x<\/code><\/td>\n<td>1-1.75<\/td>\n<td>15.0<\/td>\n<\/tr>\n<tr>\n<td><code class=\"language-apl\">b\u2282[\u2395IO]x<\/code><\/td>\n<td>1.3-42<\/td>\n<td>14.1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><em>Key<\/em> was introduced in version 14.0 and <code class=\"language-apl\">x{\u237a,f\u233f\u2375}\u2338y<\/code> is currently computed by the general case. Comparing the speed in version 14.0 to 15.0 as of June 2015:<\/p>\n<pre><code class=\"language-apl\">   x\u2190?1e6\u23745e4\r\n   y\u2190?1e6\u23740\r\n\r\n   1 1 cmpx 'x{\u237a,+\/\u2375}\u2338y'    \u235d 14.0\r\n0.1735\r\n\r\n   1 1 cmpx 'x{\u237a,+\/\u2375}\u2338y'    \u235d 15.0\r\n0.12855\r\n\r\n   0.1735 \u00f7 0.12855\r\n1.34967\r\n<\/code><\/pre>\n<p>a factor of 1.3 improvement without touching the <em>key<\/em> code. Special code <em>had<\/em> been planned for <code class=\"language-apl\">{\u237a,f\/\u2375}\u2338<\/code>, and we can get an idea of the amount of speed up:<\/p>\n<pre><code class=\"language-apl\">   cmpx 'x{\u237a,+\/\u2375}\u2338y' '(\u222ax),\u23640\u22a2x{+\/\u2375}\u2338y'    \u235d 15.0\r\n x{\u237a,+\/\u2375}\u2338y       \u2192 1.25E\u00af1 |   0% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\r\n (\u222ax),\u23640\u22a2x{+\/\u2375}\u2338y \u2192 1.16E\u00af2 | -91% \u2395\u2395\u2395\r\n<\/code><\/pre>\n<p>Perhaps the special code for <code class=\"language-apl\">{\u237a,f\/\u2375}\u2338<\/code> can be a magic function?<\/p>\n<p>Magic functions also benefit from past improvements. The primitive <code class=\"language-apl\">\u2355b<\/code> (in the <a href=\"#5.2\">Suggestivity<\/a> subsection below) has a magic function implementation: <code class=\"language-apl\">MAGIC(\"(\u00af1+2\u00d7\u2262\u2375)\u2374(2 2\u2374'0 1 ')[\u2375;]\u22a3\u2395io\u21900\")<\/code>. Then I thought, why not get an extra level of performance by doing it in native C? Benchmarks done after that work showed that the native C implementation was faster by a factor of 25 but the magic function was faster by a factor of 40. What happened? On reflection, the speed of the code depends crucially on the speed of <code class=\"language-apl\">x[b;\u2026;]<\/code> and it turned out that <code class=\"language-apl\">x[b;\u2026;]<\/code> was previously sped up (version 13.2). Apparently the <code class=\"language-apl\">x[b;\u2026;]<\/code> code is faster than a casually-written C program.<\/p>\n<p>Finally, I expect magic functions are prime candidates for being fed to the APL compiler when it becomes possible to do so.<\/p>\n<h2 id=\"5\">APL as a Tool of Implementation<\/h2>\n<p>Being APL code, magic functions have the characteristic terseness of APL and all that that entails. K.E. Iverson&#8217;s 1979 Turing Lecture <em><a href=\"http:\/\/www.jsoftware.com\/papers\/tot.htm\" target=\"_blank\">Notation as a Tool of Thought<\/a><\/em> lists five important characteristics of notation. We examine how they play out in this context.<\/p>\n<p><strong id=\"5.1\">Ease of Expression<\/strong><br \/>\n<em>Notation as a Tool of Thought<\/em> entitles this characteristic as &#8220;ease of expressing constructs arising in problems&#8221;, explicated as follows (\u00a71.1):<\/p>\n<div style=\"margin: 15px 30px 15px 30px;\"><em>If it is to be effective as a tool of thought, a notation must allow convenient expression not only of notions arising directly from a problem, but also of those arising in subsequent analysis, generalization, and specialization.<\/em><\/div>\n<p>For magic functions, the analogous concept would be facility in program development, debugging, tracing, profiling, benchmarking, and other activities required in implementation. Dyalog APL has these in spades.<\/p>\n<p><strong id=\"5.2\">Suggestivity<\/strong><br \/>\nIt was first identified that <code class=\"language-apl\">\u2355b<\/code> can be sped up by doing <code class=\"language-apl\">(\u00af1+2\u00d7\u2262\u2375)\u2374(2 2\u2374'0 1 ')[\u2375;]\u22a3\u2395io\u21900<\/code>, the idea being that if <code class=\"language-apl\">f<\/code> is an expensive function and <code class=\"language-apl\">x<\/code> is from a small domain <code class=\"language-apl\">D<\/code>, then <code class=\"language-apl\">f x<\/code> can be computed by <code class=\"language-apl\">(f D)[\u2375;]<\/code>. (A similar idea is described in \u00a74.3 of <em><a href=\"http:\/\/www.jsoftware.com\/papers\/tot.htm\" target=\"_blank\">Notation as a Tool of Thought<\/a><\/em>.) For the idea to be effective, <code class=\"language-apl\">f<\/code> does not have to be <em>very<\/em> expensive, just more expensive than indexing, and <code class=\"language-apl\">x<\/code> is sufficiently large. A few speed-ups in the pattern have been identified:<\/p>\n<div style=\"margin: -20px 0px 20px 30px;\"><code class=\"language-apl\">\u2355b<\/code><br \/>\n<code class=\"language-apl\">x\u2355b<\/code><br \/>\n<code class=\"language-apl\">float \u2373 b<\/code><br \/>\n<code class=\"language-apl\">float \u2373 int8<\/code><br \/>\n<code class=\"language-apl\">scalar\u00d7b<\/code> and <code class=\"language-apl\">b\u00d7scalar<\/code><br \/>\n<code class=\"language-apl\">scalar*b<\/code><\/div>\n<p>All of these can be implemented by magic functions. As well, the utility of the pattern <code class=\"language-apl\">(f D)[i;]<\/code> motivates extra attention on <code class=\"language-apl\">x[i;\u2026;]<\/code>. Fortuitously, <code class=\"language-apl\">x[b;\u2026;]<\/code> has previously been made fast.<\/p>\n<p><strong id=\"5.3\">Subordination of Detail<\/strong><br \/>\nWriting in APL means not having to deal with many details involved in writing in C in the interpreter, including:<\/p>\n<ul>\n<li>workspace compaction<\/li>\n<li>arrays changing their internal datatype<\/li>\n<li>call by name<\/li>\n<li>allocating and releasing memory<\/li>\n<li>index errors AKA memory read and write exceptions<\/li>\n<li>different numeric types<\/li>\n<li>different character types<\/li>\n<li>loops and nested loops<\/li>\n<li>declarations<\/li>\n<li>etc.<\/li>\n<\/ul>\n<p><strong id=\"5.4\">Economy<\/strong><br \/>\nEconomy refers to the size of the vocabulary and the complexity of the grammar. Here, the comparison is not just between APL and C (in which comparison APL wins), but between APL and the collection of programs, utilities, macros, data structures, <code class=\"language-apl\">typedef<\/code>s, calling conventions, &#8230;, accumulated by the C source code over the years. These have not been as rigorously defined and executed as APL.<\/p>\n<p><strong id=\"5.5\">Amenability to Formal Manipulation<\/strong><br \/>\nThe opening paragraph of the present text had a different magic function for <code class=\"language-apl\">\u2227.=<\/code> in a previous version. In repeated readings of the MSS and in meditation on the ideas I realized that the line of code can be made simpler:<\/p>\n<pre><code class=\"language-apl\">MAGIC(\"((\u2262\u237a)\u2191i)\u2218.=(\u2262\u237a)\u2193i\u2190\u237a\u2373\u237a\u236a\u2349\u2375\");    \u235d old version\r\nMAGIC(\"(\u2262\u237a)(\u2191\u2218.=\u2193)\u237a\u2373\u237a\u236a\u2349\u2375\");           \u235d current version\r\n<\/code><\/pre>\n<p>I daresay that such simplification would be harder to conceive with a more verbose statement of the algorithm.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Part I of this post was concerned with the development speed and execution speed of magic functions and should be read before this post. Benefitting from Future and Past Improvements Looking at the magic function for key in Part I, &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2015\/06\/in-praise-of-magic-functions-part-two\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,8],"tags":[],"class_list":["post-1864","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-apl-language"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=1864"}],"version-history":[{"count":11,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1864\/revisions"}],"predecessor-version":[{"id":3083,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1864\/revisions\/3083"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=1864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=1864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=1864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}