{"id":606,"date":"2023-02-17T13:00:33","date_gmt":"2023-02-17T13:00:33","guid":{"rendered":"https:\/\/blog.liguanxin.cn\/?p=606"},"modified":"2023-02-18T04:56:41","modified_gmt":"2023-02-18T04:56:41","slug":"cuda%e4%bc%98%e5%8c%96%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/blog.liguanxin.cn\/index.php\/2023\/02\/17\/cuda%e4%bc%98%e5%8c%96%e6%8c%87%e5%8d%97\/","title":{"rendered":"CUDA\u4f18\u5316\u6307\u5357"},"content":{"rendered":"<p><strong>CPU\u548cGPU\u7684\u533a\u522b<\/strong>:CPU\u8ba1\u7b97\u6027\u80fd\u5f3a\uff0c\u4f46\u662f\u4e00\u6b21\u53ea\u80fd\u5904\u7406\u51e0\u4ef6\u4e8b\u60c5\u3002GPU\u8ba1\u7b97\u6027\u80fd\u5f31\uff0c\u4f46\u662f\u80fd\u540c\u65f6\u5904\u7406\u5927\u91cf\u7684\u8ba1\u7b97\u3002<\/p>\n<p><img src=\"https:\/\/blog.liguanxin.cn\/wp-content\/uploads\/2023\/02\/v2-a0895b2dcd1983d04f5a96a55ed04b32_r.jpg\" alt=\"\" \/><br \/>\n(ALU: Arithmetic and Logic Unit)<\/p>\n<h2>\u5c40\u90e8\u6027\u539f\u7406<\/h2>\n<p><strong>\u65f6\u95f4\u5c40\u90e8\u6027\uff1a\u7a0b\u5e8f\u6700\u8fd1\u8bbf\u95ee\u7684\u5730\u5740\u5728\u4e0d\u4e45\u7684\u5c06\u6765\u5f88\u53ef\u80fd\u518d\u6b21\u88ab\u8bbf\u95ee\u7684\u7279\u6027\u3002<\/strong><\/p>\n<p>cache\u5c31\u662f\u4e00\u4e2a\u5145\u5206\u5229\u7528\u65f6\u95f4\u5c40\u90e8\u6027\u7684\u5178\u8303\u3002\u5728\u6bcf\u4e2aGPU block\u4e2d\u90fd\u5b58\u5728\u5f53\u524dblock\u7ebf\u7a0b\u5171\u4eab\u7684cache\u5185\u5b58\uff0c\u9891\u7e41\u8bbf\u95ee\u7684\u6570\u636e\u6216\u8005\u53d8\u91cf\u53ef\u4ee5\u5b58\u5728\u6b64\u5904\u3002<\/p>\n<p><strong>\u7a7a\u95f4\u5c40\u90e8\u6027\uff1a\u5f53\u7a0b\u5e8f\u8bbf\u95ee\u67d0\u5b58\u50a8\u5668\u5730\u5740\u540e\uff0c\u5f88\u53ef\u80fd\u9a6c\u4e0a\u8bbf\u95ee\u5176\u90bb\u8fd1\u5730\u5740\u7684\u7279\u6027\u3002<\/strong><\/p>\n<p>\u6839\u636e\u8fd9\u4e2a\u7279\u6027\uff0c\u5f53\u8f93\u5165tensor\u8fc7\u5927\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u9700\u8981\u5b8c\u6210\u591a\u4e2a\u5de5\u4f5c\u65f6\uff0c\u53ef\u4ee5\u4f18\u5148\u8ba9\u7ebf\u7a0b\u5b8c\u6210\u76f8\u90bb\u7a7a\u95f4\u7684\u5de5\u4f5c\uff0c\u800c\u975e\u4f20\u7edf\u7684:<\/p>\n<pre><code class=\"language-c++\">  for (size_t pos = blockIdx.x * blockDim.x + threadIdx.x; pos &lt; out_size; pos += blockDim.x * gridDim.x) {\n    ...\n  }<\/code><\/pre>\n<p>\u5728\u7b97\u5b50<strong>ExtractVolumePatches<\/strong>\u4e2d\uff0c\u6309\u7167\u8f93\u51fa\u7684\u4e2a\u6570\u5b9a\u7ebf\u7a0b\u6570\uff08\u7ebf\u7a0b\u6570\u4e0d\u591f\u5219\u4e00\u4e2a\u7ebf\u7a0b\u5904\u7406\u591a\u4e2a\u4f4d\u7f6e\uff09\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u9700\u8981\u627e\u5230\u5bf9\u5e94\u7684\u8f93\u5165\u4f4d\u7f6e\u7684\u5143\u7d20\u3002\u6ce8\u610f\u5230\u6bcf\u4e2a\u7a97\u53e3\u4e2d\u5b58\u5728\u76f8\u90bb\u7684\u5143\u7d20\uff0c\u5982\u4e0b\u56fe\u7684\u7ea2\u6846\u90e8\u5206<\/p>\n<p><img src=\"https:\/\/blog.liguanxin.cn\/wp-content\/uploads\/2023\/02\/\u672a\u547d\u540d\u7ed8\u56fe.png\" alt=\"\" \/><\/p>\n<p>\u4e8e\u662f\u5f53\u7ebf\u7a0b\u6570\u4e0d\u8db3\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u8ba9\u4e00\u4e2a\u7ebf\u7a0b\u505a\u4e00\u4e2a\u7a97\u53e3\u4e2d\u76f8\u90bb\u7a7a\u95f4\u7684\u5de5\u4f5c\uff08\u5982\u4e0a\u56fe\u4e2d\u84dd\u8272\uff0c\u7d2b\u8272\uff0c\u7eff\u8272\uff09\u3002\u800c\u4e14\u6bcf\u4e2a\u7ebf\u7a0b\u7684\u5de5\u4f5c\u53ea\u662f\u7ed9\u8f93\u51fa\u4f4d\u7f6e\u8d4b\u503c\u4e00\u6b21\uff0c\u4e0d\u4f1a\u56e0\u4e3a\u4e89\u62a2\u9501\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\u3002\u51cf\u5c11\u4e86\u5927\u91cf\u7684\u4f4d\u7f6e\u8ba1\u7b97\u3002<\/p>\n<h2>\u5faa\u73af\u5c55\u5f00(#pragma unroll)<\/h2>\n<p>\u6267\u884c\u6838\u51fd\u6570\u65f6\uff0c\u901a\u5e38\u4ee5warp\u4e3a\u5355\u4f4d\u53bb\u6267\u884c\u6307\u4ee4\u7684\u3002\u5f53warp\u53bb\u6267\u884c\u5faa\u73af\u65f6\uff08\u7ebf\u7a0bID\u53bb\u505afor\u7684\u5224\u65ad\u6761\u4ef6\uff0c\u6216\u8005for\u91cc\u6709\u7ebf\u7a0bID\u7684if\u6761\u4ef6\uff09\uff0c\u4f1a\u4ea7\u751f\u5206\u652f\u51b2\u7a81\uff0c\u589e\u52a0\u6307\u4ee4\u6570\u3002<br \/>\n\u6240\u4ee5\u5faa\u73af\u5c55\u5f00\u53ef\u4ee5\u6709\u6548\u907f\u514d\u5206\u652f\u51b2\u7a81\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<h2>\u7ebf\u7a0b\u540c\u6b65(__syncthreads();)<\/h2>\n<p>__syncthreads()\uff1ablock\u5185\u7684\u7ebf\u7a0b\u540c\u6b65\u3002<br \/>\n\u7ebf\u7a0b\u540c\u6b65\u65f6\u9700\u8981\u6ce8\u610f\uff0c\u662f\u5426\u6709\u5206\u652f\u4f1a\u5bfc\u81f4\u4e2a\u522b\u7ebf\u7a0b\u82b1\u8d39\u5927\u91cf\u65f6\u95f4\uff0c\u5c3d\u91cf\u8ba9\u6240\u6709\u7ebf\u7a0b\u90fd\u5b8c\u6210\u76f8\u540c\u5de5\u4f5c\u3002<\/p>\n<h2>\u89c4\u7ea6\u601d\u60f3<\/h2>\n<p><strong>\u89c4\u7ea6\u52a0\uff1a<\/strong>\u5f53\u9700\u8981\u628a\u591a\u4e2a\u6570\u7d2f\u52a0\u5230\u4e00\u4e2a\u4f4d\u7f6e\u65f6\uff0c\u7531\u4e8e\u7ebf\u7a0b\u4e4b\u95f4\u8d44\u6e90\u62a2\u5360\uff0c\u5bfc\u81f4\u7ebf\u7a0b\u82b1\u8d39\u4e86\u5927\u91cf\u65f6\u95f4\u5728\u7b49\u5f85\u8d44\u6e90\u4e0a\u3002\u4e8e\u662f\u53ef\u4ee5\u7528\u89c4\u7ea6\u7684\u601d\u60f3\uff0c\u51cf\u5c11\u7ebf\u7a0b\u7b49\u5f85\u9501\u7684\u65f6\u95f4\uff0c\u5982\u4e0b\u56fe\u6240\u793a\u3002<\/p>\n<p><img src=\"https:\/\/blog.liguanxin.cn\/wp-content\/uploads\/2023\/02\/\u89c4\u7ea6\u52a0.png\" alt=\"\" \/><\/p>\n<p>\u5176\u4e2d\u9ec4\u8272\u65b9\u5757\u7533\u8bf7\u7684\u662fblock\u5185\u7684\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<h2>\u53cc\u8c03\u6392\u5e8f(Bitonic sorter)\u4ee3\u7801\u89e3\u6790<\/h2>\n<p>\u53cc\u8c03\u6392\u5e8f\u662f\u6392\u5e8f\u7f51\u7edc\u4e2d\u6700\u5feb\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5c5e\u4e8e\u591a\u8def\u5f52\u5e76\u7684\u6392\u5e8f\u7b97\u6cd5\u3002\u5f53\u673a\u5668\u53ef\u4ee5\u540c\u65f6\u5904\u7406\u591a\u4e2a\u6bd4\u8f83\u5668\u65f6\uff0c\u6392\u5e8f\u7684\u901f\u5ea6\u4f1a\u5927\u5927\u63d0\u5347\u3002\u8be5\u7b97\u6cd5\u7531Ken Batcher\u8bbe\u8ba1\uff0c\u4e32\u884c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a<span class=\"katex-eq\" data-katex-display=\"false\">O(nlog^2n)<\/span>\uff0c\u540c\u65f6\u4e5f\u662f\u6bd4\u8f83\u6b21\u6570\u3002\u800c\u5e76\u884c\u65f6\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u8ba4\u4e3a\u662f<span class=\"katex-eq\" data-katex-display=\"false\">O(log^2n)<\/span><\/p>\n<p>\u9996\u5148\u6211\u4eec\u9700\u8981\u77e5\u9053\u4ec0\u4e48\u662f\u53cc\u8c03\u5e8f\u5217\uff0c\u53cc\u8c03\u5e8f\u5217\u662f\u5176\u4e2d\u5143\u7d20\u9996\u5148\u6309\u9012\u589e\u987a\u5e8f\u7136\u540e\u5728\u67d0\u4e2a\u7279\u5b9a\u7d22\u5f15\u4e4b\u540e\u5f00\u59cb\u9012\u51cf\u7684\u5e8f\u5217\u6216\u8fd9\u79cd\u5e8f\u5217\u7684\u5faa\u73af\u79fb\u4f4d\u3002\u59821, 2, 3, 4, 5, 3, 0\u3002<\/p>\n<p>\u601d\u60f3\uff1a<br \/>\n<img src=\"https:\/\/blog.liguanxin.cn\/wp-content\/uploads\/2023\/02\/843px-BitonicSort1.svg_.png\" alt=\"\" \/><\/p>\n<p>\u5176\u4e2d\u7bad\u5934\u662f\u6bd4\u8f83\u5668\uff0c\u6bd4\u8f83\u4ee5\u786e\u4fdd\u7bad\u5934\u6307\u5411\u8f83\u5927\u7684\u6570\u5b57\u3002<\/p>\n<p><strong>\u66ff\u4ee3\u8868\u793a<\/strong><br \/>\n\u4e0a\u56fe\u4e2d\uff0c\u6bcf\u4e2a\u7eff\u8272\u6846\u6267\u884c\u4e0e\u84dd\u8272\u6846\u76f8\u540c\u7684\u64cd\u4f5c\uff0c\u4f46\u6392\u5e8f\u65b9\u5411\u76f8\u53cd\u3002\u56e0\u6b64\uff0c\u6bcf\u4e2a\u7eff\u8272\u76d2\u5b50\u90fd\u53ef\u4ee5\u88ab\u4e00\u4e2a\u84dd\u8272\u76d2\u5b50\u53d6\u4ee3\uff0c\u4e8e\u662f\u53cc\u8c03\u5e8f\u5217\u53d8\u6210\u5355\u8c03\u5e8f\u5217\uff0c\u4f46\u662f\u6211\u4eec\u53ea\u8981\u4f9d\u7136\u6309\u7167\u53cc\u8c03\u7684\u539f\u7406\u6765\u6bd4\u8f83\uff0c\u4fbf\u53ef\u4ee5\u5f97\u5230\u4e00\u79cd\u7b80\u4fbf\u8868\u793a\u3002<br \/>\n<img src=\"https:\/\/blog.liguanxin.cn\/wp-content\/uploads\/2023\/02\/1920px-BitonicSort.svg_.png\" alt=\"\" \/><\/p>\n<p>\u4e0d\u9700\u8981\u7ed8\u5236\u7bad\u5934\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u6bd4\u8f83\u5668\u90fd\u6cbf\u540c\u4e00\u65b9\u5411\u6392\u5e8f\u3002\u84dd\u8272\u548c\u7ea2\u8272\u5757\u6267\u884c\u4e0e\u4ee5\u524d\u76f8\u540c\u7684\u64cd\u4f5c\u3002\u6a59\u8272\u5757\u7b49\u6548\u4e8e\u7ea2\u8272\u5757\uff0c\u5176\u4e2d\u5176\u8f93\u5165\u7684\u4e0b\u534a\u90e8\u5206\u548c\u8f93\u51fa\u7684\u4e0b\u534a\u90e8\u5206\u7684\u5e8f\u5217\u987a\u5e8f\u76f8\u53cd\u3002\u8fd9\u662f\u53cc\u8c03\u6392\u5e8f\u7f51\u7edc\u6700\u5e38\u89c1\u7684\u8868\u793a\u5f62\u5f0f\u3002<\/p>\n<p><strong>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u53cc\u8c03\u6392\u5e8f\u8981\u6c42\u8f93\u5165\u4e2a\u6570\u5fc5\u987b\u662f2\u7684\u6b21\u65b9\u6570\uff0c\u5982\u679c\u4e0d\u6ee1\u8db3\uff0c\u5219\u53ef\u4ee5\u7ed9\u8f93\u5165\u8865\u5145\u5f53\u524d\u7c7b\u578b\u7684\u6700\u5927\u503c\uff0c\u76f4\u5230\u8f93\u5165\u4e2a\u6570\u7b49\u4e8e2\u7684\u6b21\u65b9\u6570<\/strong><\/p>\n<p>\u8ba1\u7b97\u5927\u4e8e\u5f53\u524dv\u7684\u6700\u5c0f2\u7684\u6b21\u65b9\u6570(\u7c7b\u578b\u4e3aint)\uff1a<\/p>\n<pre><code class=\"language-c++\">int RoundUpPower2(int v) {\n  v--;\n  v |= v &gt;&gt; 1;\n  v |= v &gt;&gt; 2;\n  v |= v &gt;&gt; 4;\n  v |= v &gt;&gt; 8;\n  v |= v &gt;&gt; 16;\n  v++;\n  return v;\n}<\/code><\/pre>\n<p><strong>\u53cc\u8c03\u6392\u5e8fCPU\u5b9e\u73b0\uff1a<\/strong><\/p>\n<pre><code class=\"language-c++\">    \/\/ given an array arr of length n, this code sorts it in place\n    \/\/ all indices run from 0 to n-1\n    for (k = 2; k &lt;= n; k *= 2) \/\/ k is doubled every iteration\n        for (j = k\/2; j &gt; 0; j \/= 2) \/\/ j is halved at every iteration, with truncation of fractional parts\n            for (i = 0; i &lt; n; i++)\n                l = bitwiseXOR (i, j); \/\/ in C-like languages this is &quot;i ^ j&quot;\n                if (l &gt; i)\n                    if (  (bitwiseAND (i, k) == 0) AND (arr[i] &gt; arr[l])\n                       OR (bitwiseAND (i, k) != 0) AND (arr[i] &lt; arr[l]) )\n                          swap the elements arr[i] and arr[l]<\/code><\/pre>\n<p><strong>\u53cc\u8c03\u6392\u5e8fCUDA\u5b9e\u73b0\uff1a<\/strong><\/p>\n<pre><code class=\"language-c++\">template &lt;typename T&gt;\n__global__ void BitonicSort(const int ceil_power2, T *input) {\n  for (size_t i = 2; i &lt;= ceil_power2; i &lt;&lt;= 1) {\n    for (size_t j = (i &gt;&gt; 1); j &gt; 0; j &gt;&gt;= 1) {\n      for (size_t tid = threadIdx.x; tid &lt; ceil_power2; tid += blockDim.x) {\n        size_t tid_comp = tid ^ j;\n        if (tid_comp &gt; tid) {\n          if ((tid &amp; i) == 0) {\n            if (input[tid] &gt; input[tid_comp]) {\n              Swap(&amp;input[tid], &amp;input[tid_comp]);\n            }\n          } else {\n            if (input[tid] &lt; input[tid_comp]) {\n              Swap(&amp;input[tid], &amp;input[tid_comp]);\n            }\n          }\n        }\n      }\n      __syncthreads();\n    }\n  }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>CPU\u548cGPU\u7684\u533a\u522b:CPU\u8ba1\u7b97\u6027\u80fd\u5f3a\uff0c\u4f46\u662f\u4e00\u6b21\u53ea\u80fd\u5904\u7406\u51e0\u4ef6\u4e8b\u60c5\u3002GPU\u8ba1\u7b97\u6027\u80fd\u5f31\uff0c\u4f46\u662f\u80fd\u540c\u65f6\u5904\u7406\u5927\u91cf\u7684\u8ba1\u7b97\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29],"tags":[34,30],"_links":{"self":[{"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/posts\/606"}],"collection":[{"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/comments?post=606"}],"version-history":[{"count":0,"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/posts\/606\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/media?parent=606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/categories?post=606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.liguanxin.cn\/index.php\/wp-json\/wp\/v2\/tags?post=606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}