{"id":6451,"date":"2020-06-05T12:19:03","date_gmt":"2020-06-05T12:19:03","guid":{"rendered":"https:\/\/www.dyalog.com\/blog\/?p=6451"},"modified":"2020-06-05T12:19:03","modified_gmt":"2020-06-05T12:19:03","slug":"towards-improvements-to-stencil","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2020\/06\/towards-improvements-to-stencil\/","title":{"rendered":"Towards Improvements to <em>Stencil<\/em>"},"content":{"rendered":"<style>\n.language-apl{padding:0!important;}\n<\/style>\n<h2>Background<\/h2>\n<p>The <em>stencil<\/em> operator <code class=\"language-apl\">\u233a<\/code> was introduced in Dyalog version 16.0 in 2017. Recently we received some feedback (OK, complaints) that (a) <em>stencil<\/em> does padding which is unwanted sometimes and needs to be removed from the result and (b) <em>stencil<\/em> is too slow when it is not supported by special code.<\/p>\n<p>First, <em>stencil<\/em> in cases supported by special code is much faster than when it is not. The special cases are as follows, from <a href=\"https:\/\/www.dyalog.com\/uploads\/conference\/dyalog17\/workshops\/SA3_Performance_Tuning.zip\">Dyalog &rsquo;17 Workshop SA3<\/a>.<\/p>\n<pre class=\"language-apl\">\r\n   {\u2375}      {\u22a2\u2375}      {,\u2375}      {\u2282\u2375}\r\n{+\/,\u2375}    {\u2227\/,\u2375}    {\u2228\/,\u2375}    {=\/,\u2375}    {\u2260\/,\u2375}  \r\n    \r\n{  +\/,A\u00d7\u2375}    {  +\/\u236aA\u00d7\u23642\u22a2\u2375}\r\n{C<+\/,A\u00d7\u2375}    {C<+\/\u236aA\u00d7\u23642\u22a2\u2375}\r\n<\/pre>\n<div style=\"position:relative;left:3em;line-height:1.6em;\">\n  <code class=\"language=apl\">C:   &nbsp;<\/code>a single number or variable whose value is a single number<br \/>\n  <code class=\"language=apl\">A:   &nbsp;<\/code>a variable whose value is a rank-2 or 3 array<br \/>\n  The comparison can be<code class=\"language-apl\"> < \u2264 \u2265 > = \u2260 <\/code><br \/>\n  odd window size; movement 1; matrix argument<br \/>\n&nbsp;\n<\/div>\n<p>You can test whether a particular case is supported by using a circumlocution to defeat the special case recognizer.<\/p>\n<pre>\r\n   )copy dfns cmpx\r\n\r\n   cmpx '{\u2375}\u233a3 5\u22a2y' '{\u22a2\u22a2\u2375}\u233a3 5\u22a2y' \u22a3 y\u2190?100 200\u23740\r\n  {\u2375}\u233a3 5\u22a2x   \u2192 4.22E\u00af4 |      0%                               \r\n  {\u22a2\u22a2\u2375}\u233a3 5\u22a2x \u2192 5.31E\u00af2 | +12477% \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\u2395\r\n\r\n   cmpx '{\u233d\u2375}\u233a3 5\u22a2y' '{\u22a2\u22a2\u233d\u2375}\u233a3 5\u22a2y'\r\n  {\u233d\u2375}\u233a3 5\u22a2y   \u2192 2.17E\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\u2395\r\n  {\u22a2\u22a2\u233d\u2375}\u233a3 5\u22a2y \u2192 2.21E\u00af1 | +1% \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\u2395\r\n<\/pre>\n<p>If the timings are the same then there is no special code.<\/p>\n<p>Padding and performance improvements will take a lot of work. For example, for padding (i.e. the treatment of cells at the edge of the universe) multiple options are possible: no padding, padding, wrap from opposite edge, etc. While working on these improvements I hit upon the idea of writing a stencil <em>function<\/em> which produces the stencil cells. It only works with no padding and only for movements of 1 (which I understand are common cases), but turns out to be an interesting study.<\/p>\n<h2>A Stencil Function<\/h2>\n<p><code class=\"language-apl\">\u237a stencell \u2375<\/code> produces the stencil cells of size <code class=\"language-apl\">\u237a<\/code> from <code class=\"language-apl\">\u2375<\/code>&nbsp;, and is equivalent to <code class=\"language-apl\">{\u2375}\u233a\u237a\u22a2\u2375<\/code> after the padded cells are removed.<\/p>\n<pre>\r\nstencell\u2190{\r\n  \u2395io\u21900                 \u235d \u2395io delenda est!\r\n  s\u2190(\u2262\u237a)\u2191\u2374\u2375\r\n  f\u21901+s-\u237a               \u235d frame AKA outer shape\r\n  m\u2190\u2296\u00d7\u2340\u22961\u2193s,1           \u235d multiplier for each axis\r\n  i\u2190\u2283\u2218.+\u233f(m,m)\u00d7\u2373\u00a8f,\u237a    \u235d indices\r\n  (\u2282i) \u2337 \u2375 \u2374\u2368 (\u00d7\u233f(\u2262\u237a)\u2191\u2374\u2375),(\u2262\u237a)\u2193\u2374\u2375\r\n}\r\n<\/pre>\n<p>For example, <code class=\"language-apl\">stencell<\/code> is applied to <code class=\"language-apl\">x<\/code> with cell shape <code class=\"language-apl\">3&nbsp;5<\/code>&nbsp;.<\/p>\n<pre>\r\n   \u22a2 x\u21906 10\u2374\u237360                    \u235d (a)\r\n 0  1  2  3  4  5  6  7  8  9\r\n10 11 12 13 14 15 16 17 18 19\r\n20 21 22 23 24 25 26 27 28 29\r\n30 31 32 33 34 35 36 37 38 39\r\n40 41 42 43 44 45 46 47 48 49\r\n50 51 52 53 54 55 56 57 58 59\r\n\r\n   c\u21903 5 stencell x                \u235d (b)\r\n   \u2374c\r\n4 6 3 5\r\n\r\n   c \u2261 1 2 \u2193 \u00af1 \u00af2 \u2193 {\u2375}\u233a3 5 \u22a2x    \u235d (c)\r\n1\r\n\r\n   \u22a2 e\u2190\u2282\u23642 \u22a2c                      \u235d (d)<font size=-2>\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u2502 0  1  2  3  4\u2502 1  2  3  4  5\u2502 2  3  4  5  6\u2502 3  4  5  6  7\u2502 4  5  6  7  8\u2502 5  6  7  8  9\u2502\r\n\u250210 11 12 13 14\u250211 12 13 14 15\u250212 13 14 15 16\u250213 14 15 16 17\u250214 15 16 17 18\u250215 16 17 18 19\u2502\r\n\u250220 21 22 23 24\u250221 22 23 24 25\u250222 23 24 25 26\u250223 24 25 26 27\u250224 25 26 27 28\u250225 26 27 28 29\u2502\r\n\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u250210 11 12 13 14\u250211 12 13 14 15\u250212 13 14 15 16\u250213 14 15 16 17\u250214 15 16 17 18\u250215 16 17 18 19\u2502\r\n\u250220 21 22 23 24\u250221 22 23 24 25\u250222 23 24 25 26\u250223 24 25 26 27\u250224 25 26 27 28\u250225 26 27 28 29\u2502\r\n\u250230 31 32 33 34\u250231 32 33 34 35\u250232 33 34 35 36\u250233 34 35 36 37\u250234 35 36 37 38\u250235 36 37 38 39\u2502\r\n\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u250220 21 22 23 24\u250221 22 23 24 25\u250222 23 24 25 26\u250223 24 25 26 27\u250224 25 26 27 28\u250225 26 27 28 29\u2502\r\n\u250230 31 32 33 34\u250231 32 33 34 35\u250232 33 34 35 36\u250233 34 35 36 37\u250234 35 36 37 38\u250235 36 37 38 39\u2502\r\n\u250240 41 42 43 44\u250241 42 43 44 45\u250242 43 44 45 46\u250243 44 45 46 47\u250244 45 46 47 48\u250245 46 47 48 49\u2502\r\n\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u250230 31 32 33 34\u250231 32 33 34 35\u250232 33 34 35 36\u250233 34 35 36 37\u250234 35 36 37 38\u250235 36 37 38 39\u2502\r\n\u250240 41 42 43 44\u250241 42 43 44 45\u250242 43 44 45 46\u250243 44 45 46 47\u250244 45 46 47 48\u250245 46 47 48 49\u2502\r\n\u250250 51 52 53 54\u250251 52 53 54 55\u250252 53 54 55 56\u250253 54 55 56 57\u250254 55 56 57 58\u250255 56 57 58 59\u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/font>\r\n\r\n    \u222a\u00a8 ,\u00a8 e-\u236c\u2374e                    \u235d (e)<font size=-2>\r\n\u250c\u2500\u2500\u252c\u2500\u2500\u252c\u2500\u2500\u252c\u2500\u2500\u252c\u2500\u2500\u252c\u2500\u2500\u2510\r\n\u25020 \u25021 \u25022 \u25023 \u25024 \u25025 \u2502\r\n\u251c\u2500\u2500\u253c\u2500\u2500\u253c\u2500\u2500\u253c\u2500\u2500\u253c\u2500\u2500\u253c\u2500\u2500\u2524\r\n\u250210\u250211\u250212\u250213\u250214\u250215\u2502\r\n\u251c\u2500\u2500\u253c\u2500\u2500\u253c\u2500\u2500\u253c\u2500\u2500\u253c\u2500\u2500\u253c\u2500\u2500\u2524\r\n\u250220\u250221\u250222\u250223\u250224\u250225\u2502\r\n\u251c\u2500\u2500\u253c\u2500\u2500\u253c\u2500\u2500\u253c\u2500\u2500\u253c\u2500\u2500\u253c\u2500\u2500\u2524\r\n\u250230\u250231\u250232\u250233\u250234\u250235\u2502\r\n\u2514\u2500\u2500\u2534\u2500\u2500\u2534\u2500\u2500\u2534\u2500\u2500\u2534\u2500\u2500\u2534\u2500\u2500\u2518<\/font>\r\n<\/pre>\n<style>\n#stencellTable{position:relative;left:3em;border-bottom:none;}\n#stencellTable > tbody > tr > td{\n  border-top:none;\n  text-align: left;\n}\n<\/style>\n<table id=\"stencellTable\">\n<tr>\n<td valign=top nowrap>(a)&nbsp;<\/td>\n<td>\nThe matrix <code class=\"language-apl\">x<\/code> is chosen to make stencil results easier to understand.\n <\/td>\n<\/tr>\n<tr>\n<td valign=top>(b)<\/td>\n<td>\n<code class=\"language-apl\">stencell<\/code> is applied to <code class=\"language-apl\">x<\/code> with cell shape <code class=\"language-apl\">3&nbsp;5<\/code>&nbsp;.\n <\/td>\n<\/tr>\n<tr>\n<td valign=top>(c)<\/td>\n<td>\nThe result of <code class=\"language-apl\">stencell<\/code> is the same as that for <code class=\"language-apl\">{\u2375}\u233a<\/code> after cells with padding are dropped.\n <\/td>\n<\/tr>\n<tr>\n<td valign=top>(d)<\/td>\n<td>\nEnclose the matrices in <code class=\"language-apl\">c<\/code> (the cells) to make the display more compact and easier to understand.\n <\/td>\n<\/tr>\n<tr>\n<td valign=top>(e)<\/td>\n<td>\nSubsequent discussion is based on the observation that each cell is some scalar integer added to the first cell.\n <\/td>\n<\/tr>\n<\/table>\n<h2>Indices<\/h2>\n<p>The key expression in the computation is<\/p>\n<pre>   \u2283\u2218.+\u233f(m,m)\u00d7\u2373\u00a8f,\u237a <\/code><\/pre>\n<p style=\"margin-bottom:0;\">where<\/p>\n<div style=\"position:relative;left:3em;line-height:1.6em;\">\n  <code class=\"language-apl\">&nbsp; &nbsp;m:&nbsp; 10 1;&nbsp;<\/code> multiplier for each axis<br \/>\n  <code class=\"language-apl\">&nbsp; &nbsp;f:&nbsp;  4 6;&nbsp;<\/code> multiplier for each axis<br \/>\n  <code class=\"language-apl\">&nbsp; &nbsp;\u237a:&nbsp;  3 5;&nbsp;<\/code> multiplier for each axis<br \/>\n&nbsp;\n<\/div>\n<p>We discuss a more verbose but equivalent version of this expression, <\/p>\n<pre>   (\u2283\u2218.+\u233fm\u00d7\u2373\u00a8f)\u2218.+(\u2283\u2218.+\u233fm\u00d7\u2373\u00a8\u237a)<\/pre>\n<p> and in particular the right half,<code class=\"language-apl\"> \u2283\u2218.+\u233fm\u00d7\u2373\u00a8\u237a<\/code>&nbsp;,<code class=\"language-apl\"><\/code> which produces the first cell.<\/p>\n<pre>\r\n   \u2373\u237a               \u235d \u23733 5\r\n\u250c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2510\r\n\u25020 0\u25020 1\u25020 2\u25020 3\u25020 4\u2502\r\n\u251c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2524\r\n\u25021 0\u25021 1\u25021 2\u25021 3\u25021 4\u2502\r\n\u251c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2524\r\n\u25022 0\u25022 1\u25022 2\u25022 3\u25022 4\u2502\r\n\u2514\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2518\r\n   (\u2374\u2375)\u2218\u22a5\u00a8\u2373\u237a        \u235d 6 10\u2218\u22a5\u00a8 \u23733 5\r\n 0  1  2  3  4\r\n10 11 12 13 14\r\n20 21 22 23 24\r\n<\/pre>\n<p>Alternatively, this last result obtains by multiplying by <code class=\"language-apl\">m<\/code> the corresponding indices for each axis, where an element of <code class=\"language-apl\">m<\/code> is the increment for a unit in an axis. That is, <code class=\"language-apl\">m\u2190\u2296\u00d7\u2340\u22961\u2193s,1<\/code> where <code class=\"language-apl\">s\u2190(\u2262\u237a)\u2191\u2374\u2375<\/code> is a prefix of the shape of <code class=\"language-apl\">\u2375<\/code>&nbsp;. The multipliers are with respect to the argument <code class=\"language-apl\">\u2375<\/code> because the indices are required to be with respect to the argument <code class=\"language-apl\">\u2375<\/code>&nbsp;.<\/p>\n<pre>\r\n   \u2373\u00a8\u237a              \u235d \u2373\u00a83 5\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u25020 1 2\u25020 1 2 3 4\u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n   m\u00d7\u2373\u00a8\u237a            \u235d 10 1\u00d7\u2373\u00a83 5\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u25020 10 20\u25020 1 2 3 4\u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n   \u2218.+\u233f m\u00d7\u2373\u00a8\u237a       \u235d \u2218.+\u233f 10 1\u00d7\u2373\u00a83 5\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u2502 0  1  2  3  4\u2502\r\n\u250210 11 12 13 14\u2502\r\n\u250220 21 22 23 24\u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n   ((\u2374\u2375)\u2218\u22a5\u00a8\u2373\u237a) \u2261 \u2283\u2218.+\u233fm\u00d7\u2373\u00a8\u237a\r\n1\r\n<\/pre>\n<p>This alternative computation is more efficient because it avoids creating and working on lots of small nested vectors and because the intermediate results for <code class=\"language-apl\">\u2218.+\u233f<\/code> grows fast from one to the next (i.e., <code class=\"language-apl\">O(\u235fn)<\/code> iterations in the main loop).<\/p>\n<p>The left half, <code class=\"language-apl\">\u2283\u2218.+\u233fm\u00d7\u2373\u00a8f<\/code>&nbsp;, is similar, and computes the necessary scalar integers to be added to the result of the right half.<\/p>\n<p><pre>\r\n   \u2283 \u2218.+\u233f m\u00d7\u2373\u00a8f     \u235d \u2283 \u2218.+\u233f 10 1\u00d7\u2373\u00a84 6\r\n 0  1  2  3  4  5\r\n10 11 12 13 14 15\r\n20 21 22 23 24 25\r\n30 31 32 33 34 35\r\n<\/pre>\n<p>The shorter expression derives from the more verbose one by some simple algebra.<\/p>\n<pre>\r\n(\u2283\u2218.+\u233fm\u00d7\u2373\u00a8f)\u2218.+(\u2283\u2218.+\u233fm\u00d7\u2373\u00a8\u237a)    \u235d verbose version\r\n\u2283\u2218.+\u233f(m\u00d7\u2373\u00a8f),m\u00d7\u2373\u00a8\u237a             \u235d \u2218.+ is associative\r\n\u2283\u2218.+\u233f(m,m)\u00d7(\u2373\u00a8f),\u2373\u00a8\u237a           \u235d m\u00d7 distributes over ,\r\n\u2283\u2218.+\u233f(m,m)\u00d7\u2373\u00a8f,\u237a               \u235d \u2373\u00a8 distributes over ,\r\n<\/pre>\n<p>I am actually disappointed that the shorter expression was found &#9786;; it would have been amusing to have a non-contrived and short expression with three uses of<nobr><code class=\"language-apl\"> \u2218.+<\/code>&nbsp;.<\/nobr><\/p>\n<h2>Cells<\/h2>\n<p>Having the indices <code class=\"language-apl\">i<\/code> in hand, the stencil cells obtain by indexing into an appropriate reshape or ravel of the right argument <code class=\"language-apl\">\u2375<\/code>&nbsp;. In general, the expression is<\/p>\n<pre>\r\n   (\u2282i) \u2337 \u2375 \u2374\u2368 (\u00d7\/(\u2262\u237a)\u2191\u2374\u2375),(\u2262\u237a)\u2193\u2374\u2375\r\n<\/pre>\n<p><code class=\"language-apl\">\u237a<\/code> specifies the cell shape. If <code class=\"language-apl\">(\u2262\u237a)=\u2262\u2374\u2375<\/code>&nbsp;, that is, if a length is specified for each axis of <code class=\"language-apl\">\u2375<\/code>&nbsp;, the expression is equivalent to <code class=\"language-apl\">(\u2282i)\u2337,\u2375<\/code> or <code class=\"language-apl\">(,\u2375)[i]<\/code>&nbsp;; if <code class=\"language-apl\">(\u2262\u237a)<\u2262\u2374\u2375<\/code>&nbsp;, that is, if there are some trailing unstencilled axes, the expression is equivalent to <code class=\"language-apl\">(,[\u2373\u2262\u237a]\u2375)[i;&#8230;;]<\/code> (the leading <code class=\"language-apl\">\u2262\u237a<\/code> axes are ravelled) or <code class=\"language-apl\">\u2191(,\u2282\u2364((\u2262\u2374\u2375)-\u2262\u237a)\u22a2\u2375)[i]<\/code> (as if the trailing axes were shielded from indexing). The general expression covers both cases.<\/p>\n<p><strong>Application<\/strong><\/p>\n<p><code class=\"language-apl\">stencell<\/code> makes it possible to workaround current shortcomings in <code class=\"language-apl\">\u233a<\/code> . The alternative approach is to use <code class=\"language-apl\">stencell<\/code> to get all the stencil cells, all at once, and then work on the cells using <code class=\"language-apl\">\u2364<\/code>&nbsp;, <code class=\"language-apl\">+.\u00d7<\/code>&nbsp;, and<br \/>\nother efficient primitives.<\/p>\n<p>The following example is from Aaron Hsu. In the original problem the size of <code class=\"language-apl\">x<\/code> is<nobr><code class=\"language-apl\"> 512 512 64<\/code>&nbsp;.<\/nobr><\/p>\n<pre>\r\n   K\u2190?64 3 3 64\u23740\r\n   x\u2190?256 256 64\u23740\r\n\r\n   t\u21901 1\u2193\u00af1 \u00af1\u2193{+\/\u236aK\u00d7\u23643\u22a2\u2375}\u233a3 3\u22a2x\r\n   \u2374t\r\n256 256 64\r\n\r\n   cmpx '1 1\u2193\u00af1 \u00af1\u2193{+\/\u236aK\u00d7\u23643\u22a2\u2375}\u233a3 3\u22a2x'\r\n6.76E0 \r\n<\/pre>\n<p>The computation is slow because the cells are rank-3, not supported by special code. Aaron then devised a significant speed-up using a simpler left operand to create the ravels of the cells (but still no special code):\n<\/p>\n<pre>\r\n   t \u2261 (1 1\u2193\u00af1 \u00af1\u2193{,\u2375}\u233a3 3\u22a2x)+.\u00d7\u2349\u236aK\r\n1\r\n   cmpx '(1 1\u2193\u00af1 \u00af1\u2193{,\u2375}\u233a3 3\u22a2x)+.\u00d7\u2349\u236aK'\r\n1.67E0 \r\n<\/pre>\n<p>Use of <code class=\"language-apl\">stencell<\/code> would improve the performance a bit further:<\/p>\n<pre>\r\n   t \u2261 (,\u23643 \u22a23 3 stencell x)+.\u00d7\u2349\u236aK\r\n1\r\n   cmpx '(,\u23643 \u22a23 3 stencell x)+.\u00d7\u2349\u236aK'\r\n1.09E0 \r\n\r\n   cmpx '3 3 stencell x'\r\n6.10E\u00af2\r\n<\/pre>\n<p>The last timing shows that the <code class=\"language-apl\">stencell<\/code> computation is 6% (<code class=\"language-apl\">6.10e\u00af2\u00f71.09e0<\/code>) of the total time.<\/p>\n<p>Materializing all the cells does take more space than if the computation is incorporated in the left operand of <code class=\"language-apl\">\u233a<\/code>&nbsp;, and is practicable only if the workspace sufficiently large.<\/p>\n<pre>\r\n   )copy dfns wsreq\r\n\r\n   wsreq '1 1\u2193\u00af1 \u00af1\u2193{+\/\u236aK\u00d7\u23643\u22a2\u2375}\u233a3 3\u22a2x'\r\n110649900\r\n   wsreq '(1 1\u2193\u00af1 \u00af1\u2193{,\u2375}\u233a3 3\u22a2x)+.\u00d7\u2349\u236aK'\r\n647815900\r\n   wsreq '(,\u23643 \u22a23 3 stencell x)+.\u00d7\u2349\u236aK'\r\n333462260\r\n<\/pre>\n<h2>Performance<\/h2>\n<p><code class=\"language-apl\">stencell<\/code> is competitive with <code class=\"language-apl\">{\u2375}\u233a<\/code> on matrices, where it is supported by special code written in C, and is faster when there is no special code. The benchmarks are done on a larger argument to reduce the effects of padding\/unpadding done in <code class=\"language-apl\">{\u2375}\u233a<\/code>&nbsp;.<\/p>\n<pre>\r\n   y2\u2190?200 300\u23740\r\n          \r\n   cmpx '3 5 stencell y2' '1 2\u2193\u00af1 \u00af2\u2193{\u2375}\u233a3 5\u22a2y2' \r\n  3 5 stencell y      \u2192 1.85E\u00af3 |   0% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395           \r\n  1 2\u2193\u00af1 \u00af2\u2193{\u2375}\u233a3 5\u22a2y \u2192 2.91E\u00af3 | +57% \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\u2395\r\n\r\n   cmpx '3 5 stencell y' '{\u2375}\u233a3 5\u22a2y' \r\n  3 5 stencell y \u2192 1.85E\u00af3 |   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\u2395\r\n* {\u2375}\u233a3 5\u22a2y      \u2192 1.04E\u00af3 | -45% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395             \r\n\r\n   y3\u2190?200 300 64\u23740\r\n\r\n   cmpx '3 5 stencell y3' '1 2\u2193\u00af1 \u00af2\u2193{\u2375}\u233a3 5\u22a2y3' \r\n  3 5 stencell y3      \u2192 8.90E\u00af2 |    0% \u2395\u2395\u2395                           \r\n  1 2\u2193\u00af1 \u00af2\u2193{\u2375}\u233a3 5\u22a2y3 \u2192 7.78E\u00af1 | +773% \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\u2395\r\n\r\n   cmpx '3 5 stencell y3' '{\u2375}\u233a3 5\u22a2y3' \r\n  3 5 stencell y3 \u2192 9.38E\u00af2 |    0% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395                      \r\n* {\u2375}\u233a3 5\u22a2y3      \u2192 3.34E\u00af1 | +256% \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\u2395\r\n<\/pre>\n<p>There is an interesting question of whether the shorter version of the key computation (in the <strong>Indices<\/strong> section above) is faster than the more verbose version.<\/p>\n<pre>\r\n   m\u219010 1 \u22c4 f\u21904 6 \u22c4 a\u21903 5\r\n\r\n   cmpx '\u2283\u2218.+\u233f(m,m)\u00d7\u2373\u00a8f,a' '(\u2283\u2218.+\u233fm\u00d7\u2373\u00a8f)\u2218.+(\u2283\u2218.+\u233fm\u00d7\u2373\u00a8a)'\r\n  \u2283\u2218.+\u233f(m,m)\u00d7\u2373\u00a8f,a            \u2192 3.75E\u00af6 |   0% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\r\n  (\u2283\u2218.+\u233fm\u00d7\u2373\u00a8f)\u2218.+(\u2283\u2218.+\u233fm\u00d7\u2373\u00a8a) \u2192 5.20E\u00af6 | +38% \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<\/pre>\n<p>In this case, it is faster, and I expect it will be faster for cases which arise in stencil calculations, where the argument size is larger than the cell size. But it is easy to think of arguments where <code class=\"language-apl\">\u2218.+\u233f<\/code> is slower than <code class=\"language-apl\">\u2218.+<\/code> done with a different grouping:<\/p>\n<pre>\r\n   cmpx '((\u23730)\u2218.+\u2373100)\u2218.+\u2373200' '(\u23730)\u2218.+((\u2373100)\u2218.+\u2373200)' '\u2283\u2218.+\/\u2373\u00a80 100 200'\r\n  ((\u23730)\u2218.+\u2373100)\u2218.+\u2373200   \u2192 7.86E\u00af7 |     0% \u2395\u2395                            \r\n  (\u23730)\u2218.+((\u2373100)\u2218.+\u2373200) \u2192 1.05E\u00af5 | +1234% \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  \u2283\u2218.+\/\u2373\u00a80 100 200       \u2192 1.11E\u00af5 | +1310% \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\u2395\r\n<\/pre>\n<p>This question will be explored further in a later post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Background The stencil operator \u233a was introduced in Dyalog version 16.0 in 2017. Recently we received some feedback (OK, complaints) that (a) stencil does padding which is unwanted sometimes and needs to be removed from the result and (b) stencil &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2020\/06\/towards-improvements-to-stencil\/\">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,33,18],"tags":[],"class_list":["post-6451","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-apl-language","category-implementation","category-tips-tricks"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/6451","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=6451"}],"version-history":[{"count":8,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/6451\/revisions"}],"predecessor-version":[{"id":6454,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/6451\/revisions\/6454"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=6451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=6451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=6451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}