{"id":219,"date":"2020-12-12T22:10:05","date_gmt":"2020-12-12T14:10:05","guid":{"rendered":"https:\/\/blog.indeex.club\/?p=219"},"modified":"2021-10-13T09:55:54","modified_gmt":"2021-10-13T01:55:54","slug":"%e5%8f%8c%e8%b0%83%e6%8e%92%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/blog.indeex.club\/index.php\/2020\/12\/12\/%e5%8f%8c%e8%b0%83%e6%8e%92%e5%ba%8f\/","title":{"rendered":"\u53cc\u8c03\u6392\u5e8f"},"content":{"rendered":"<hr \/>\n<h3>\u4ec0\u4e48\u662f\u53cc\u8c03\u6392\u5e8f<\/h3>\n<p>\u767e\u5ea6\u767e\u79d1\uff1a<\/p>\n<p>\u53cc\u8c03\u6392\u5e8f\uff08bitonic sort\uff09\u5c5e\u4e8e\u6392\u5e8f\u7f51\u7edc(Sorting Network\uff09\u7684\u4e00\u79cd\u3002\u76f8\u8f83\u4e8e\u4f20\u7edf\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u6392\u5e8f\u7f51\u7edc\u771f\u6b63\u7684\u7814\u7a76\u4ef7\u503c\u5728\u4e8e\uff0c\u5047\u5982\u6709\u673a\u5668\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u6bd4\u8f83\u5668\uff0c\u6392\u5e8f\u7684\u901f\u5ea6\u5c06\u5927\u5e45\u5ea6\u63d0\u9ad8\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u5b83\u662f\u4e00\u79cd\u53ef\u4ee5\u5e76\u884c\u8ba1\u7b97\u7684\u6392\u5e8f\u7b97\u6cd5\u3002<\/p>\n<p>very good\u7684\u767e\u5ea6\u89e3\u91ca\u3002<\/p>\n<p>\u7b80\u5355\u8bf4\uff1a\u53cc\u8c03\u6392\u5e8f\u662f\u6bd4\u8f83\u987a\u5e8f\u4e0e\u6570\u636e\u65e0\u5173\u7684\u6392\u5e8f\u65b9\u6cd5\uff0c\u7279\u522b\u9002\u5408\u505a\u5e76\u884c\u8ba1\u7b97\uff0c\u4f8b\u5982\u7528GPU\u3001FPGA\u6765\u8ba1\u7b97\u3002\u5f53\u8981\u6392\u5e8f\u7684\u6570\u7684\u4e2a\u6570\u4e0d\u662f2\u7684\u5e42\u65f6\uff0c\u5904\u7406\u65f6\u8f83\u4e3a\u56f0\u96be\u3002<\/p>\n<h3>\u7b97\u6cd5\u5b9e\u73b0<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e2a\u7b80\u5355\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">function kernel(x, p, q) {\n  const d = 1 &lt;&lt; (p - q);\n  for (let i = 0; i &lt; x.length; i++) {\n    const up = ((i &gt;&gt; p) &amp; 2) === 0;\n    if ((i &amp; d) == 0 &amp;&amp; (x[i] &gt; x[i | d]) === up) {\n      const tmp = x[i];\n      x[i] = x[i | d];\n      x[i | d] = tmp;\n    }\n  }\n}\n\nfunction sort(x, n) {\n  for (let i = 0; i &lt; n; i++) {\n    for(let j = 0; j &lt;=i; j++) {\n      kernel(x, i, j);\n    }\n  }\n}\n<\/code><\/pre>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff1a\u5c06\u4e00\u4e2a\u5305\u542b\u4ece 0 \u5230 255 \u7684\u6df7\u5408\u7ec4\u5408\u6570\u5b57\u7684\u6570\u7ec4sort(x, n)\u6392\u5e8f\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">const n = 8;\nconst size = 2**n;\nconst x = Array.from({length: size}, (v, i) =&gt; );\n\nx.forEach((v, i) =&gt; {\n  const j = Math.floor(Math.random() * size);\n  const tmp = x[i];\n  x[i] = x[j];\n  x[j] = tmp;\n});\n\nconsole.log(x);\n\/\/ [75, 16, 59, 28, 110, 199, 188, 144, 10, ...]\n\nsort(x, n);\n\nconsole.log(x);\n\/\/ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...]\n<\/code><\/pre>\n<p>\u8fc7\u7a0b\u5982\u56fe\uff1a<\/p>\n<p><img src=\"https:\/\/hungking.cc\/assets\/imgs\/indeex.cc\/bitonic1.png\" alt=\"\u53cc\u8c03\u6392\u5e8f\" \/><\/p>\n<p>\u56fe\u4e2d\u663e\u793a\u4e86\u6392\u5e8f\u662f\u5982\u4f55\u4ece\u5de6\u5230\u53f3\u8fdb\u884c\u7684\uff0c\u53f3\u7aef\u662f\u6392\u5e8f\u5b8c\u6210\u7684\u72b6\u6001\u3002\u84dd\u8272\u90e8\u5206\u6309\u5347\u5e8f\u6392\u5217\uff0c\u7eff\u8272\u90e8\u5206\u6309\u964d\u5e8f\u6392\u5217\uff0c\u7bad\u5934\u8868\u793a\u6bd4\u8f83\u503c\u7684\u7d22\u5f15\u3002<\/p>\n<p>\u5982\u679c\u4ece\u6c34\u5e73\u65b9\u5411\u770b\u56fe\u50cf\uff0c\u5b83\u5927\u81f4\u5206\u4e3a\u56db\u4e2a\u5757\u3002\u8fd9\u662fsort\u51fd\u6570\u5916\u7684for\u5faa\u73af\u3002\u56fe\u4e2d\u6570\u7ec4\u7684\u957f\u5ea6\u5f88\u957f\uff0c\u5faa\u73af\u4e86\u56db\u6b21\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u67e5\u770b\u6bcf\u4e2a\u5757\u7684\u5185\u90e8\uff0c\u5b83\u4ece\u5de6\u4fa7\u5757\u5f00\u59cb\u6309\u987a\u5e8f\u5305\u542b 1\u30012\u30013\u30014 \u56db \u4e2a\u5b50\u5757\u3002sort\u5bf9\u5e94\u4e8e\u51fd\u6570\u5185\u90e8\u7684 for \u5faa\u73af\u3002\u6362\u53e5\u8bdd\u8bf4\uff0ckernel\u51fd\u6570\u5728\u4e00\u4e2a\u5b50\u5757\u4e2d\u5904\u7406\u3002<\/p>\n<p>kernel\u51fd\u6570\u9996\u5148\u9700\u8981\u6ee1\u8db3\u6761\u4ef6 d = 1 &lt;&lt; (p &#8211; q) \u3002\u8be5\u503c\u662f\u4ece\u6570\u7ec4\u7d22\u5f15\u5230\u8981\u6bd4\u8f83\u7d22\u5f15\u7684\u8ddd\u79bb\u503c\u3002\u6bd4\u5982\uff0c\u5728\u56fe\u50cf\u7684\u7b2c\u56db\u4e2a\u5757\u7684\u7b2c\u4e00\u4e2a\u5b50\u5757\u4e2d\uff0cp = 3, q = 0\u5b83d = 1 &lt;&lt; (3 &#8211; 0) = 8\uff0c\u5e76\u4e14\u6bcf\u4e2a\u7d22\u5f15\u5c06\u7d22\u5f15\u548c\u503c\u76f8\u96948\u3002<\/p>\n<p>kernel\u51fd\u6570\u6bd4\u8f83\u8fc7\u7a0b\u5b9e\u9645\u4e0a\u662f\u5728for\u5faa\u73af\u4e2d\u8fdb\u884c\u7684\uff0c\u5728\u5fc5\u8981\u65f6\u4f1a\u4ea4\u6362\u503c\u3002<\/p>\n<p>\u5bf9\u4e8e\u7b2c\u4e00\u4e2a\u5faa\u73afup = ((i >> p) &amp; 2) <span class=\"text-highlighted-inline\" style=\"background-color: #fffd38;\">= 0\u4e2d\u7684\u7d22\u5f15\u5c06\u8981\u786e\u5b9a\u662f\u5426\u6309\u5347\u5e8f\u6392\u5217\uff0c\u6216\u8005\u6309\u503c\u7684\u964d\u5e8f\u6392\u5217\u3002\u5728\u7b2c\u4e00\u4e2a\u5757\u4e2d\uff0c\u6bcf\u6b21\u7d22\u5f15\u524d\u8fdb2\u65f6\uff0c\u964d\u5e8f\u548c\u5347\u5e8f\u4ea4\u6362\uff0c\u7b2c\u4e8c\u4e2a\u5757\u4ea4\u63624\u4e2a\uff0c\u7b2c\u4e09\u4e2a\u5757\u4e3a8\uff0c\u7b2c\u56db\u4e2a\u4e3a16\uff0c2^(p+1)\u4f9d\u6b64\u7c7b\u63a8\u3002\u56e0\u6b64\u5f53(i >> p) &amp; 2\uff0cp + 1\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u4f4d\u7684\u503c\u6765\u786e\u5b9a\u662f\u6309\u5347\u5e8f\u6392\u5e8f\u8fd8\u662f\u964d\u5e8f\u6392\u5e8f\u7684\u3002<\/span><\/p>\n<p>\u4e0e\u7d22\u5f15\u6bd4\u8f83\u7684\u7d22\u5f15\u7531 d \u5206\u9694\u3002\u56e0\u6b64\uff0c\u5f53p &#8211; q\u65f6\uff0c\u7b2cbit\u4f4d\u7684\u503c\u8fd8\u662f0\uff08\u6700\u53f3\u8fb9\u7684\u4f4d\u7b97\u4f5c\u7b2c0\u4f4d\uff09\u3002\u5982\u679c\u9760\u8fd1\u6570\u7ec4\u5f00\u5934\u7684\u7d22\u5f15\u662f i\uff0cp &#8211; q\u5219\u8981\u6bd4\u8f83\u7684\u7d22\u5f15\u4fbf\u662f i || d <span class=\"text-highlighted-inline\" style=\"background-color: #fffd38;\"> 0 \u3002\u6362\u53e5\u8bdd\u8bf4\uff0c(i &amp; d) <\/span> 0\u6211\u4eec\u6b63\u5728\u68c0\u67e5 if\u8bed\u53e5\u4e2d\u7684\u7d22\u5f15\u662f\u5426\u66f4\u63a5\u8fd1\u6570\u7ec4\u7684\u5f00\u5934\uff0cx[i] > x[i | d]\u5e76\u4e14\u6211\u4eec\u6b63\u5728\u68c0\u67e5\u4e24\u4e2a\u503c\u7684\u5927\u5c0f\u3002<\/p>\n<p>GPU\u662f\u4ea4\u66ff\u5199\u5165\u4e24\u4e2a\u7f13\u51b2\u533a\u7684\uff0c\u8fd9\u4e2a\u6211\u4e5f\u5b9e\u73b0\u4e86kernel\u51fd\u6570\u4e2d\u503c\u7684\u4ea4\u6362\u3002<\/p>\n<pre><code class=\"language-javascript line-numbers\">function kernel(x, p, q) {\n  const y = new Array(x.length);\n  const d = 1 &lt;&lt; (p - q);\n\n  for (let i = 0; i &lt; x.length; i++) {\n    const up = ((i &gt;&gt; p) &amp; 2) === 0;\n\n    if ((i &amp; d) == 0 &amp;&amp; (x[i] &gt; x[i + d]) === up) {\n      y[i] =  x[i + d];\n    } else if ((i &amp; d) == d &amp;&amp; (x[i - d] &gt; x[i]) === up) {\n      y[i] = x[i - d];\n    } else {\n      y[i] = x[i];\n    }\n  }\n  return y;\n}\n\nfunction sort(x, n) {\n  for (let i = 0; i &lt; n; i++) {\n    for(let j = 0; j &lt;=i; j++) {\n      x = kernel(x, i, j);\n    }\n  }\n  return x;\n}\n\nconst n = 8;\nconst size = 2**n;\nconst x = Array.from({length: size}, (v, i) =&gt; i);\n\nx.forEach((v, i) =&gt; {\n  const j = Math.floor(Math.random() * size);\n  const tmp = x[i];\n  x[i] = x[j];\n  x[j] = tmp;\n});\n\nconsole.log(x);\n\nconst y = sort(x, n);\n\nconsole.log(y);\n<\/code><\/pre>\n<p>code enjoy!\ud83d\ude1c\ud83d\ude1c\ud83d\ude1c<\/p>\n<p>\u4f5c\u8005\uff1aindeex<\/p>\n<p>\u94fe\u63a5\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/indeex.club\/\">https:\/\/indeex.club<\/a><\/p>\n<p>\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f\u53cc\u8c03\u6392\u5e8f \u767e\u5ea6\u767e\u79d1\uff1a \u53cc\u8c03\u6392\u5e8f\uff08bitonic sort\uff09\u5c5e\u4e8e\u6392\u5e8f\u7f51\u7edc(Sorting Net<a href=\"https:\/\/blog.indeex.club\/index.php\/2020\/12\/12\/%e5%8f%8c%e8%b0%83%e6%8e%92%e5%ba%8f\/\" class=\"read-more\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[4],"_links":{"self":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/219"}],"collection":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/comments?post=219"}],"version-history":[{"count":1,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/219\/revisions"}],"predecessor-version":[{"id":220,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/219\/revisions\/220"}],"wp:attachment":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/media?parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/categories?post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/tags?post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}