|
|
Line 1: |
Line 1: |
| {{for|physical models of polyhedra|Polyhedron model}}
| | The author is known by the name of Numbers Lint. She is [http://xrambo.com/user/RKDBG home std test] kit [http://www.dynast.co.kr/xe/?document_srl=150618 at home std testing] home std test a librarian but she's usually needed her personal company. One of the very best things in the globe for me is to do aerobics and I've been [http://www.Itsyoursexlife.com/gyt/std-and-testing-faqs/ performing] it for fairly a while. Minnesota has always been his house but his spouse wants them to move.<br><br>Also visit my site :: [https://www.machlitim.org.il/subdomain/megila/end/node/9243 https://www.machlitim.org.il/subdomain/megila/end/node/9243] |
| The '''polyhedral model''' (also called the '''polytope method''') is a mathematical framework for [[loop nest optimization]] in [[program optimization]]. The polytope method treats each loop iteration within nested loops as lattice points inside mathematical objects called [[polytope]]s, performs [[affine transformation]]s or more general non-affine transformations such as tiling on the polytopes, and then converts the transformed polytopes into equivalent, but optimized (depending on targeted optimization goal), loop nests through polyhedra scanning. | |
| | |
| == Detailed example ==
| |
| | |
| | |
| [[Image:Polytope model unskewed.svg|thumb|right|The dependencies of <code>src</code>, before [[Loop_optimization#Common_loop_transformations|loop skewing]]. The red dot corresponds to <code>src[1][0]</code>; the purple dot corresponds to <code>src[2][2]</code>.]] | |
| The following [[C (programming language)|C]] code implements a form of error-distribution [[dither]]ing similar to [[Floyd–Steinberg dithering]], but modified for pedagogical reasons. The two-dimensional array <code>src</code> contains <code>h</code> rows of <code>w</code> pixels, each pixel having a [[grayscale]] value between 0 and 255 inclusive. After the routine has finished, the output array <code>dst</code> will contain only pixels with value 0 or value 255. During the computation, each pixel's dithering error is collected by adding it back into the <code>src</code> array. (Notice that <code>src</code> and <code>dst</code> are both read and written during the computation; <code>src</code> is not read-only, and <code>dst</code> is not write-only.)
| |
| | |
| Each iteration of the [[inner loop]] modifies the values in <code>src[i][j]</code> based on the values of <code>src[i-1][j]</code>, <code>src[i][j-1]</code>, and <code>src[i+1][j-1]</code>. (The same dependencies apply to <code>dst[i][j]</code>. For the purposes of [[Loop_optimization#Common_loop_transformations|loop skewing]], we can think of <code>src[i][j]</code> and <code>dst[i][j]</code> as the same element.) We can illustrate the dependencies of <code>src[i][j]</code> graphically, as in the diagram on the right.
| |
| | |
| <!-- This table nonsense makes the page look slightly better in Firefox on Windows XP. Is there a more portable and self-explanatory way to get the code and images not to overlap with each other? --~~~~ -->
| |
| <table><tr><td width="50%">
| |
| <source lang=C>
| |
| #define ERR(x,y) (dst[x][y] - src[x][y])
| |
|
| |
| void dither(unsigned char **src, unsigned char **dst, int w, int h)
| |
| {
| |
| int i, j;
| |
| for (j = 0; j < h; ++j) {
| |
| for (i = 0; i < w; ++i) {
| |
| int v = src[i][j];
| |
| if (i > 0)
| |
| v -= ERR(i - 1, j) /2;
| |
| if (j > 0)
| |
| v -= ERR(i, j - 1) / 4;
| |
| if (j > 0 && i < w - 1)
| |
| v -= ERR(i + 1, j - 1) / 4;
| |
| dst[i][j] = (v < 128) ? 0 : 255;
| |
| src[i][j] = (v < 0) ? 0 : (v < 255) ? v : 255;
| |
| }
| |
| }
| |
| }
| |
| </source>
| |
| </td><td></td></tr></table>
| |
| | |
| [[Image:Polytope model skewed.svg|thumb|right|The dependencies of <code>src</code>, after loop skewing. The array elements will be processed in the order ''gray, red, green, blue, yellow...'']]
| |
| | |
| Performing the affine transformation <math>(p,\, t) = (i,\, 2j+i)</math> on the original dependency diagram gives us a new diagram, which is shown in the next image. We can then rewrite the code to loop on <code>p</code> and <code>t</code> instead of <code>i</code> and <code>j</code>, obtaining the following "skewed" routine.
| |
| | |
| <!-- Please don't break this code. Test before committing! -->
| |
| <table><tr><td width="50%"> | |
| <source lang=C>
| |
| void dither_skewed(unsigned char **src, unsigned char **dst, int w, int h)
| |
| {
| |
| int t, p;
| |
| for (t = 0; t < (w + (2 * h)); ++t) {
| |
| int pmin = max(t % 2, t - (2 * h) + 2);
| |
| int pmax = min(t, w - 1);
| |
| for (p = pmin; p <= pmax; p += 2) {
| |
| int i = p;
| |
| int j = (t - p) / 2;
| |
| int v = src[i][j];
| |
| if (i > 0)
| |
| v -= ERR(i - 1, j) / 2;
| |
| if (j > 0)
| |
| v -= ERR(i, j - 1) / 4;
| |
| if (j > 0 && i < w - 1)
| |
| v -= ERR(i + 1, j - 1) / 4;
| |
| dst[i][j] = (v < 128) ? 0 : 255;
| |
| src[i][j] = (v < 0) ? 0 : (v < 255) ? v : 255;
| |
| }
| |
| }
| |
| }
| |
| </source>
| |
| </td><td></td></tr></table>
| |
| | |
| ==See also==
| |
| *[[Frameworks supporting the polyhedral model]]
| |
| *[[Loop nest optimization]]
| |
| *[[Loop unrolling]]
| |
| *[[Loop reversal]]
| |
| *[[Loop tiling]]
| |
| | |
| ==External links and references==
| |
| *[http://www.infosun.fmi.uni-passau.de/cl/loopo/doc/loopo_doc/node3.html "The basic polytope method"], tutorial by Martin Griebl containing diagrams of the pseudocode example above
| |
| *[[Frameworks supporting the polyhedral model#Transitive closure operation|"Framework for polyhedral model"]]
| |
| *[http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.30.5675 "Code Generation in the Polytope Model"] (1998). Martin Griebl, Christian Lengauer, and Sabine Wetzel
| |
| *[http://www.cloog.org/ "The CLooG Polyhedral Code Generator"]
| |
| *[http://www.chunchen.info/omega "CodeGen+: Z-polyhedra scanning"]
| |
| | |
| [[Category:Compiler optimizations]]
| |
| [[Category:Articles with example pseudocode]]
| |
| [[Category:Articles with example C code]]
| |
The author is known by the name of Numbers Lint. She is home std test kit at home std testing home std test a librarian but she's usually needed her personal company. One of the very best things in the globe for me is to do aerobics and I've been performing it for fairly a while. Minnesota has always been his house but his spouse wants them to move.
Also visit my site :: https://www.machlitim.org.il/subdomain/megila/end/node/9243