{"id":318,"date":"2015-01-13T12:54:08","date_gmt":"2015-01-13T04:54:08","guid":{"rendered":"http:\/\/planckscale.info\/?p=318"},"modified":"2016-12-19T18:25:03","modified_gmt":"2016-12-19T10:25:03","slug":"cuda-%e8%bd%af%e4%bb%b6%e6%8a%bd%e8%b1%a1%e7%9a%84%e5%b9%bb%e5%bd%b1%e8%83%8c%e5%90%8e-%e4%b9%8b%e4%b8%89","status":"publish","type":"post","link":"https:\/\/mindspectrum.xyz\/en\/2015\/01\/13\/cuda-%e8%bd%af%e4%bb%b6%e6%8a%bd%e8%b1%a1%e7%9a%84%e5%b9%bb%e5%bd%b1%e8%83%8c%e5%90%8e-%e4%b9%8b%e4%b8%89\/","title":{"rendered":"CUDA, \u8f6f\u4ef6\u62bd\u8c61\u7684\u5e7b\u5f71\u80cc\u540e \u4e4b\u4e09"},"content":{"rendered":"<h5><strong>\u7248\u6743\u58f0\u660e\uff1a\u539f\u521b\u4f5c\u54c1\uff0c\u6b22\u8fce\u8f6c\u8f7d\uff0c\u4f46\u8f6c\u8f7d\u8bf7\u4ee5\u8d85\u94fe\u63a5\u5f62\u5f0f\u6ce8\u660e\u6587\u7ae0\u6765\u6e90(<a href=\"https:\/\/mindspectrum.xyz\" target=\"_blank\">planckscale.info<\/a>)\u3001\u4f5c\u8005\u4fe1\u606f\u548c\u672c\u58f0\u660e\uff0c\u5426\u5219\u5c06\u8ffd\u7a76\u6cd5\u5f8b\u8d23\u4efb\u3002<\/strong><\/h5>\n<p>\u4e0a\u4e00\u7bc7\u4e2d\u8c08\u5230\u4e86\u7f16\u7a0b\u6a21\u578b\u4e2d\u7684Block\u7b49\u6982\u5ff5\u5982\u4f55\u6620\u5c04\u5230\u786c\u4ef6\u4e0a\u6267\u884c\uff0c\u4ee5\u53caCUDA\u5982\u4f55\u7528\u5e76\u884c\u6765\u63a9\u76d6\u5ef6\u8fdf\u3002\u8fd9\u4e00\u7bc7\u7ee7\u7eed\u5256\u6790SIMT\uff0c\u8c08\u4e00\u8c08\u63a7\u5236\u6d41\u5206\u53c9\uff0c\u6307\u4ee4\u541e\u5410\u548c\u7ebf\u7a0b\u95f4\u901a\u8baf\u673a\u5236\u3002<br \/>\n\u867d\u7136\u6211\u4eec\u8bf4warp\u4e2d\u7684\u7ebf\u7a0b\u7c7b\u4f3c\u4e8eSIMD,\u4f46\u4e8b\u5b9e\u4e0a\u5b83\u662f\u771f\u6b63\u7684\u7ebf\u7a0b\u3002warp\u4e2d\u7684\u6bcf\u4e00\u4e2athread\u90fd\u6709\u81ea\u5df1\u7684\u6307\u4ee4\u5730\u5740\u5bc4\u5b58\u5668\uff0c\u5141\u8bb8\u5b83\u4eec\u5404\u81ea\u6267\u884c\u4e0d\u540c\u7684\u4efb\u52a1\uff08\u63a7\u5236\u6d41\u5206\u53c9\uff09\u3002\u6700\u7b80\u5355\u7684\uff0c\u6bd4\u5982\u4e00\u4e2a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nif(threadIdx &amp;lt 10)\r\n {}\r\n else\r\n {}\r\n<\/pre>\n<p>\u8bed\u53e5\uff0c\u5c06threadIdx=0&#8230;31\u8fd9\u4e00\u4e2awarp\u5212\u5206\u6210\u4e24\u4e2a\u5206\u652f\uff0c\u5404\u81ea\u505a\u4e0d\u540c\u7684\u4e8b\u60c5\u3002\u8fd9\u4e2a\u7075\u6d3b\u6027\u4ee5\u6027\u80fd\u4e3a\u4ee3\u4ef7\uff0c\u5f53\u4e00\u4e2awarp\u4e2d\u63a7\u5236\u6d41\u51fa\u73b0\u5206\u53c9\u65f6\uff0c\u4e0d\u540c\u5206\u652f\u7684\u7ebf\u7a0b\u4f1a\u88ab\u5206\u7ec4\u76f8\u7ee7\u6267\u884c\uff0c\u76f4\u5230\u5404\u5206\u652f\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u63a7\u5236\u6d41\u91cd\u65b0\u6c47\u805a\u6210\u4e00\u652f\uff08\u4e0a\u4f8b\u4e2d\u5373if\u8bed\u53e5\u7684\u7ed3\u675f\u70b9\uff09\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6267\u884c\u5355\u5143\u7684\u5229\u7528\u7387\u8f83\u4f4e\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u5206\u652f\u6267\u884c\u65f6\u90fd\u9700\u8981\u5173\u95ed\u5176\u4ed6\u5206\u652f\u7684\u7ebf\u7a0b\uff0c\u6240\u4ee5\u8fd9\u65f6\u4e00\u4e9b\u6267\u884c\u5355\u5143\u662f\u7528\u4e0d\u5230\u7684\u3002<br \/>\n\u4e3a\u4e86\u5c3d\u53ef\u80fd\u9ad8\u6548\u7684\u8ba1\u7b97\uff0c\u9700\u8981\u7ea6\u675f\u63a7\u5236\u6d41\u5206\u53c9\u7684\u51fa\u73b0\u3002\u9664\u4e86\u51cf\u5c11\u6d41\u7a0b\u63a7\u5236\u8bed\u53e5\u5916\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\uff0c\u5e76\u4e0d\u662f\u53ea\u8981\u6709\u6d41\u7a0b\u63a7\u5236\u8bed\u53e5\u5c31\u4e00\u5b9a\u4f1a\u5e26\u6765\u63a7\u5236\u6d41\u5206\u53c9\u3002\u5173\u952e\u662f\uff0c\u63a7\u5236\u6d41\u5206\u53c9\u53ea\u662f\u9488\u5bf9\u540c\u4e00warp\u4e2d\u7684\u7ebf\u7a0b\u800c\u8a00\uff0c\u4e0d\u540cwarp\u7684\u7ebf\u7a0b\u539f\u672c\u5c31\u662f\u4e32\u884c\u5316\u6267\u884c\u7684\uff0c\u5206\u53c9\u5bf9\u5176\u65e0\u5f71\u54cd\u3002\u56e0\u6b64\uff0c\u53ea\u6709\u6d41\u7a0b\u63a7\u5236\u8bed\u53e5\u7684\u6761\u4ef6\u5728<br \/>\n\u540c\u4e00warp\u5185\u4e0d\u4e00\u81f4\u65f6\uff0c\u624d\u4f1a\u6709\u63a7\u5236\u6d41\u5206\u53c9\u3002\u8fd9\u6837,\u8bf8\u5982<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nif(threadIdx.x \/ WARPSIZE &amp;lt n)\r\n{...}\r\nelse\r\n{...}\r\n<\/pre>\n<p>\u8fd9\u6837\u7684\u8bed\u53e5\u662f\u4e0d\u4f1a\u6709\u5206\u53c9\u7684\u3002\u5f53\u7136\uff0c\u66f4\u5bbd\u677e\u7684\u6761\u4ef6\u5982<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nif(blockIdx.x &amp;lt n)\r\n{...}\r\nelse\r\n{...}\r\n<\/pre>\n<p>\u4e5f\u4e0d\u4f1a\u6709\u5206\u53c9\u3002\u4f9d\u8d56\u4e8e\u8f93\u5165\u6570\u636e\u7684\u6761\u4ef6\u5982<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nif(globalArray[threadIdx.x] &amp;lt n)\r\n{...}\r\nelse\r\n{...}\r\n<\/pre>\n<p>\u5219\u4f1a\u5e26\u6765\u5206\u53c9\u3002<\/p>\n<p>\u5bf9\u4e8e\u7b80\u5355\u7684\u6307\u4ee4\u598232\u4f4d\u6d6e\u70b9\u6570\u7684\u52a0\u3001\u4e58\uff0c32\u4f4d\u6574\u6570\u7684\u52a0\u51cf\u7b49\uff0c\u901a\u5e38CUDA Core\u5728\u4e00\u4e2a\u65f6\u949f\u5468\u671f\u5185\u53ef\u4ee5\u5b8c\u6210\u4e00\u6b21\u64cd\u4f5c\uff0c\u56e0\u800c\u4e00\u4e2a\u5468\u671f\u5185\u5b8c\u6210\u7684\u64cd\u4f5c\u6570\u76ee\u5c31\u7b49\u4e8eCUDA Cores\u6570\u76ee\uff1b\u800c\u5bf9\u4e8e\u4e00\u4e9b\u8f83\u590d\u6742\u7684\u6307\u4ee4\uff0c\u5982sin\/cos\u7b49\u8d85\u8d8a\u51fd\u6570\uff0c\u6267\u884c\u5355\u5143\u5e76\u4e0d\u80fd\u63d0\u4f9b\u8fd9\u4e48\u9ad8\u7684\u541e\u5410\u7387\u3002<br \/>\n\u6211\u4eec\u53ef\u4ee5\u7528\u5355\u4f4d\u5468\u671f\u5185\u8fdb\u884c\u7684\u64cd\u4f5c\u6570\u76eeN\u9664\u4ee532\u6765\u8ba1\u7b97\u6307\u4ee4\u7684\u541e\u5410\u7387\u3002\u4ee5GM204\u4e3a\u4f8b\uff0c\u5b83\u7684SM\u4e2d\u670932*4 = 128\u4e2aCUDA Cores\uff0c32\u4e2aSFU\uff08\u7279\u6b8a\u51fd\u6570\u5355\u5143\uff09\uff0c\u5728\u8ba1\u7b9732\u4f4d\u6d6e\u70b9\u52a0\u6cd5\u65f6\u5177\u6709\u6700\u9ad8\u541e\u5410\uff0c\u4e00\u4e2a\u5468\u671f\u5185\u5b8c\u6210128\u6b21\u64cd\u4f5c\uff0c\u5355\u4f4d\u5468\u671f\u5185\u6307\u4ee4\u541e\u5410\u4e3a128\/32 = 4\uff1b\u800c\u8ba1\u7b97\u5982sin\/cos\u7b49\u8d85\u8d8a\u51fd\u6570\u65f6\u7ebf\u7a0b\u4e0d\u518d\u4e00\u4e00\u5206\u914d\u5230CUDA Cores\u4e0a\uff0c\u800c\u662f\u8981\u572832\u4e2aSFU\u4e0a\u8ba1\u7b97\uff0c\u5355\u4f4d\u5468\u671f\u5185\u53ea\u80fd\u5b8c\u621032\u6b21\u64cd\u4f5c\uff0c\u6307\u4ee4\u541e\u5410\u4e3a1\u6761\u6307\u4ee4\u6bcf\u5468\u671f.<br \/>\n\u6307\u4ee4\u7684\u541e\u5410\u7387\u6570\u636e\u53ef\u53c2\u8003CUDA C Programming Guide\u4e2d 5.4.1. Arithmetic Instructions\uff0c\u8be5\u5c0f\u8282\u4ee5\u5355\u4f4d\u65f6\u949f\u5468\u671f\u6bcfSM\u4e0a\u80fd\u591f\u8fdb\u884c\u7684\u64cd\u4f5c\u6570\u7684\u5f62\u5f0f\u7ed9\u51fa\u4e86\u5404\u6307\u4ee4\u7684\u541e\u5410\u7387\u3002<br \/>\n\u6307\u4ee4\u541e\u5410\u7387\u662f\u6211\u4eec\u8fdb\u884c\u6027\u80fd\u4f18\u5316\u7684\u6709\u4e00\u4e2a\u91cd\u8981\u6307\u6807\u3002\u901a\u5e38\uff0c\u5f71\u54cd\u6307\u4ee4\u541e\u5410\u7387\u7684\u56e0\u7d20\u9664\u4e86\u6570\u503c\u8ba1\u7b97\u64cd\u4f5c\u7684\u590d\u6742\u5ea6\u3001\u7cbe\u786e\u5ea6\u4e4b\u5916\uff0c\u63a7\u5236\u6d41\u5206\u53c9\u4e5f\u662f\u4e00\u4e2a\u8d21\u732e\u56e0\u5b50\u3002\u8fd9\u91cc\u7684\u539f\u56e0\u4e0d\u96be\u7406\u89e3\uff0c\u63a7\u5236\u6d41\u5206\u53c9\u65f6\u6267\u884c\u5355\u5143\u7684\u5229\u7528\u7387\u4e0b\u964d\uff0c\u4f7f\u5f97\u5355\u4f4d\u5468\u671f\u5185\u6267\u884c\u7684\u64cd\u4f5c\u6570\u76ee\u4e0b\u964d\uff0c\u4ece\u800c\u964d\u4f4e\u4e86\u6307\u4ee4\u541e\u5410\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u786c\u4ef6\u56fe\u666f\u4e0b\u7ebf\u7a0b\u7684\u6267\u884c\u5c31\u57fa\u672c\u8bf4\u5b8c\u4e86\uff0c\u53ea\u5269\u4e0b\u4e00\u4e2a\u7559\u5230\u6700\u540e\u7684\u8bdd\u9898\uff1a\u7ebf\u7a0b\u95f4\u4ea4\u4e92\u3002\u901a\u5e38\uff0c\u4e0d\u5b58\u5728\u4efb\u4f55\u76f8\u4e92\u4f5c\u7528\u7684\u7ebf\u7a0b\uff0c\u5b83\u4eec\u4e4b\u95f4\u624d\u80fd\u591f\u4ee5\u4efb\u610f\u7684\u987a\u5e8f\u6267\u884c\uff0c\u50cfblock\u3002\u4f46\u5bf9\u4e8ewarp\u8fd9\u6837\u7684\u7ebf\u7a0b\u7ec4\uff0c\u662f\u53ef\u80fd\u4e0e\u540c\u4e00block\u4e2d\u5176\u4ed6warp\u901a\u8baf\u6216\u540c\u6b65\u7684\uff0c\u8fd9\u65f6\u6267\u884c\u987a\u5e8f\u5c31\u4e0d\u80fd\u4efb\u610f\u3002\u6240\u5e78\u5373\u4fbf\u5728block\u4e4b\u5185\uff0c\u7ebf\u7a0b\u95f4\u7684\u4ea4\u4e92\u4ecd\u7136\u662f\u8f83\u5f31\u7684\uff0c\u56e0\u800c\u5e95\u5c42\u53ef\u4ee5\u5c06block\u5212\u5206\u6210warp\u6765\u5206\u7ec4\u4e32\u884c\u5316\u6267\u884c\uff0c\u9047\u5230\u4ea4\u4e92\u65f6\u518d\u53e6\u4f5c\u5904\u7406\u3002\u6211\u4eec\u73b0\u5728\u6765\u770b\u770b\u8fd9\u4e9b\u4ea4\u4e92\u673a\u5236\u3002<\/p>\n<p>\u7ebf\u7a0b\u95f4\u4ea4\u4e92\u53ef\u4ee5\u7ec6\u5206\u4e3a\u901a\u8baf\u548c\u540c\u6b65\u4e24\u7c7b\u3002\u901a\u8baf\u4e3b\u8981\u7531\u516c\u5171\u5b58\u50a8\u533a\u57df\u4ea4\u6362\u6570\u636e\u6765\u5b9e\u73b0\uff0c\u4f46\u4e5f\u4e0d\u6392\u9664\u50cfshuffle\u8fd9\u6837\u7684\u7279\u6b8a\u65b9\u5f0f\u5b58\u5728\u3002<br \/>\n\u4ece\u901a\u8baf\u7684\u7c92\u5ea6\u6765\u770b\uff0c\u53ef\u4ee5\u5206\u4e3awarp\u5185\u90e8\u7ebf\u7a0b\u95f4\u901a\u8baf\uff0cblock\u5185\u90e8\u7ebf\u7a0b\u95f4\u901a\u8baf\uff0cblock\u95f4\u901a\u8baf\uff0c\u66f4\u7c97\u7684\u7c92\u5ea6\u8fd9\u91cc\u4e0d\u8003\u8651\u3002<strong>block\u4e4b\u95f4\u7684\u901a\u8baf\u5219\u53ea\u80fd\u57fa\u4e8eglobal\u00a0memory\uff0cblock\u5185\u90e8\u7684\u901a\u8baf\u4e3b\u8981\u57fa\u4e8eshared memory\/global memory\uff0cwarp\u5185\u90e8\u7ebf\u7a0b\u95f4\u9664\u4e86\u53ef\u4ee5\u5229\u7528\u4e0a\u8ff0\u6240\u6709\u65b9\u5f0f\uff0c\u8fd8\u6709\u4e00\u79cd\u7279\u6b8a\u7684shuffle\u673a\u5236.<\/strong>\u4e0b\u9762\u6211\u4eec\u4ee5\u901a\u8baf\u7684\u7c92\u5ea6\u5206\u7c7b\u9648\u8ff0\u5404\u79cd\u901a\u8baf\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<p>block\u95f4\u901a\u8baf\u901a\u5e38\u57fa\u4e8e<strong>\u4e24\u6b21kernel\u53d1\u5c04<\/strong>\uff0c\u4e00\u6b21\u5c06\u901a\u8baf\u6570\u636e\u5199\u5165global memory\uff0c\u53e6\u4e00\u6b21\u53d1\u5c04\u8bfbglobal memory\u8fdb\u884c\u540e\u7eed\u5904\u7406\u3002\u8fd9\u79cd\u901a\u8baf\u5f00\u9500\u8f83\u5927\uff0c\u4e3b\u8981\u6765\u81ea\u4e8eglobal memory\u8bbf\u5b58\u548ckernel\u53d1\u5c04\uff0c\u6240\u4ee5\u5982\u679c\u6709\u53ef\u80fd\uff0c\u5c3d\u91cf\u628a\u4efb\u52a1\u653e\u5728\u4e00\u6b21kernel\u53d1\u5c04\u4e2d\u5b8c\u6210\u3002<br \/>\n\u6216\u8bb8\u6709\u4eba\u4f1a\u95ee\uff0c\u540c\u4e00\u4e2akernel\u53d1\u5c04\u4e2d\u7684\u4e24\u4e2ablock\u5177\u6709\u5171\u540c\u7684<strong>global memory<\/strong>\uff0c\u662f\u4e0d\u662f\u4e5f\u53ef\u4ee5\u5229\u7528\u8fd9\u4e2a\u7279\u70b9\u6765\u6784\u9020\u540c\u4e00kernel\u4e0bblock\u95f4\u7684\u901a\u8baf\u5462\uff1f\u901a\u5e38\u7684\u7b54\u6848\u662fno\uff0c\u56e0\u4e3ablock\u4e4b\u95f4\u6267\u884c\u987a\u5e8f\u4e0d\u5b9a\uff0c\u5f88\u96be\u6784\u9020\u6709\u610f\u4e49\u7684\u901a\u8baf\uff1b\u4f46\u5982\u679c\u8981\u8f83\u771f\uff0c\u7b54\u6848\u662fyes\uff0c\u6211\u4eec\u771f\u7684\u53ef\u4ee5\u6784\u9020\u4e00\u4e9b\u7279\u6b8a\u7684block\u95f4\u901a\u8baf\u65b9\u5f0f\u3002\u4e00\u4e2a\u4f8b\u5b50\u5982\u4e0b\u6240\u793a\uff0c\u8be5\u5b9e\u4f8b\u6765\u81ea\u4e8eCUDA C Programming Guide B.5. Memory Fence Functions\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n__device__ unsigned int count = 0;\r\n__shared__ bool isLastBlockDone;\r\n__global__ void sum(const float* array, unsigned int N,\r\n\tvolatile float* result)\r\n{\r\n\t\/\/ Each block sums a subset of the input array.\r\n\tfloat partialSum = calculatePartialSum(array, N);\r\n\tif (threadIdx.x == 0) {\r\n\t\t\/\/ Thread 0 of each block stores the partial sum\r\n\t\t\/\/ to global memory. The compiler will use\r\n\t\t\/\/ a store operation that bypasses the L1 cache\r\n\t\t\/\/ since the &quot;result&quot; variable is declared as\r\n\t\t\/\/ volatile. This ensures that the threads of\r\n\t\t\/\/ the last block will read the correct partial\r\n\t\t\/\/ sums computed by all other blocks.\r\n\t\tresult[blockIdx.x] = partialSum;\r\n\t\t\/\/ Thread 0 makes sure that the incrementation\r\n\t\t\/\/ of the &quot;count&quot; variable is only performed after\r\n\t\t\/\/ the partial sum has been written to global memory.\r\n\t\t__threadfence();\r\n\t\t\/\/ Thread 0 signals that it is done.\r\n\t\tunsigned int value = atomicInc(&amp; count, gridDim.x);\r\n\t\t\/\/ Thread 0 determines if its block is the last\r\n\t\t\/\/ block to be done.\r\n\t\tisLastBlockDone = (value == (gridDim.x - 1));\r\n\t}\r\n\t\/\/ Synchronize to make sure that each thread reads\r\n\t\/\/ the correct value of isLastBlockDone.\r\n\t__syncthreads();\r\n\tif (isLastBlockDone) {\r\n\t\t\/\/ The last block sums the partial sums\r\n\t\t\/\/ stored in result[0 .. gridDim.x-1]\r\n\t\tfloat totalSum = calculateTotalSum(result);\r\n\t\tif (threadIdx.x == 0) {\r\n\t\t\t\/\/ Thread 0 of last block stores the total sum\r\n\t\t\t\/\/ to global memory and resets the count\r\n\t\t\t\/\/ varialble, so that the next kernel call\r\n\t\t\t\/\/ works properly.\r\n\t\t\tresult[0] = totalSum;\r\n\t\t\tcount = 0;\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>\u4ee3\u7801 1. block\u95f4\u901a\u8baf\u5b9e\u73b0\u6570\u7ec4\u6c42\u548c<br \/>\n\u672c\u4ee3\u7801\u6458\u5f55\u81ea CUDA C Programming Guide B.5. Memory Fence Functions<\/p>\n<p>\u8be5\u4f8b\u5b9e\u73b0\u4e00\u4e2a\u6570\u7ec4\u7684\u6c42\u548c\uff0c\u9996\u5148\u5404\u4e2ablock\u8ba1\u7b97\u90e8\u5206\u548c\uff0c\u7136\u540e\u7531\u6700\u540e\u4e00\u4e2a\u5b8c\u6210\u90e8\u5206\u548c\u8ba1\u7b97\u7684block\u518d\u628a\u6240\u6709\u7684\u90e8\u5206\u548c\u52a0\u548c\u51fa\u6700\u7ec8\u7ed3\u679c\u3002block\u95f4\u901a\u8fc7\u4e00\u4e2a\u4f4d\u4e8eglobal memory\u7684\u53d8\u91cfcount\u901a\u8baf\uff0c\u5b83\u8bb0\u5f55\u4e86\u76ee\u524d\u5df2\u7ecf\u5b8c\u6210\u8ba1\u7b97\u7684\u7ebf\u7a0b\u6570\u3002\u8fd9\u6837\uff0c\u6700\u540e\u4e00\u4e2a\u5b8c\u6210\u90e8\u5206\u548c\u8ba1\u7b97\u7684block\u5c31\u4f1a\u53d1\u73b0count\u7684\u6570\u503c\u4e3a\u6700\u5927\u7ebf<br \/>\n\u7a0bid\uff0c\u56e0\u6b64\u53ef\u4ee5\u5224\u5b9a\u9700\u8981\u7531\u5b83\u81ea\u5df1\u6765\u5b8c\u6210\u6700\u540e\u4ece\u90e8\u5206\u548c\u5411\u603b\u548c\u7684\u8ba1\u7b97\u3002<br \/>\n\u4e0d\u8fc7\uff0c\u4e3a\u4e86\u66f4\u597d\u7684\u8f6f\u4ef6\u7ed3\u6784\uff0c\u6700\u597d\u8fd8\u662f\u907f\u514d\u540c\u4e00kernel\u7684block\u95f4\u4ea7\u751f\u8026\u5408\u3002\u540c\u4e00kernel\u4e2dblock\u7684\u901a\u8baf\u8fd8\u6d89\u53ca\u5230CUDA\u7684weakly-ordered\u5185\u5b58\u6a21\u578b\u95ee\u9898\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u5148\u540e\u4e24\u6b21\u5185\u5b58\u64cd\u4f5c\u5728\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u770b\u6765\u672a\u5fc5\u80fd\u591f\u4fdd\u6301\u539f\u6709\u987a\u5e8f\uff0c\u8fd9\u4ea7\u751f\u4e86\u76f8\u5f53\u5927\u7684\u590d\u6742\u6027\u3002\u6211\u4eec\u5728\u4e0b\u6587\u8fd8\u4f1a\u63d0\u5230\u8fd9\u4e00\u95ee\u9898\u3002<\/p>\n<p>block\u5185\u7684\u7ebf\u7a0b\u901a\u8baf\u673a\u5236\u8f83\u4e3a\u4e30\u5bcc\uff0c\u5c24\u5176\u662f\u7ebf\u7a0b\u540c\u5c5e\u4e00\u4e2awarp\u65f6\u7684<strong>shuffle\u673a\u5236<\/strong>\u3002shuffle\u5728Kepler\u540e\u51fa\u73b0\uff0c\u662f\u4e00\u79cd\u76f8\u5f53\u5feb\u7684\u7ebf\u7a0b\u95f4\u901a\u8baf\u65b9\u5f0f\uff0c\u5b83\u5141\u8bb8\u540c\u5c5e\u4e00\u4e2awarp\u7684\u7ebf\u7a0b\u95f4\u53ef\u4ee5\u4e92\u76f8\u5f15\u7528\u5f7c\u6b64\u7684\u5bc4\u5b58\u5668\uff0c\u6bd4\u5982\u4e0b\u4f8b\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n__global__ void bcast(int arg)\r\n{\r\n\tint laneId = threadIdx.x &amp; 0x1f;\r\n\tint value;\r\n\tif (laneId == 0) \/\/ Note unused variable for\r\n\t\tvalue = arg; \/\/ all threads except lane 0\r\n\tvalue = __shfl(value, 0); \/\/ Get &quot;value&quot; from lane 0\r\n\tif (value != arg)\r\n\t\tprintf(&quot;Thread %d failed.\\n&quot;, threadIdx.x);\r\n}\r\n<\/pre>\n<p>\u4ee3\u7801 2. shuffle\u673a\u5236\u5b9e\u73b0\u4e00\u4e2a\u503c\u5411\u6574\u4e2awarp\u7684\u5e7f\u64ad<br \/>\n\u672c\u4ee3\u7801\u6458\u5f55\u81ea CUDA C Programming Guide B.14. Warp Shuffle Functions<\/p>\n<p>laneId\u662fwarp\u4e2d\u7ebf\u7a0b\u7684\u4e00\u4e2aindex\uff0c\u6709threadIdx\u5bf932\u53d6\u4f59\u5f97\u5230\u3002__shfl(value, 0)\u8bed\u53e5\u4f7f\u5f97\u5404\u7ebf\u7a0b\u80fd\u591f\u8bbf\u95eelaneId==0\u8fd9\u4e00\u7ebf\u7a0b\u4e2dvalue\u7684\u503c\u3002<\/p>\n<p>\u66f4\u5e38\u7528\u7684\u901a\u8baf\u673a\u5236\u81ea\u7136\u662f<strong>shared memory\u548cglobal memory<\/strong>\u4e86\u3002\u5176\u4e2dshared memory\u66f4\u5feb\u901f\uff0c\u5728\u5927\u591a\u6570\u65f6\u5019\u662f\u6784\u5efa\u9ad8\u6027\u80fdCUDA\u7a0b\u5e8f\u7684\u5fc5\u7531\u4e4b\u8def\u3002\u8fd9\u4e9b\u5e38\u8bc6\u4e0d\u518d\u8d58\u8ff0\u3002\u57fa\u4e8eshared\/global memory\u7684\u7ebf\u7a0b\u95f4\u6570\u636e\u4ea4\u6362\uff0c\u4e00\u5b9a\u8981\u6ce8\u610f\u7ebf\u7a0b\u7684\u540c\u6b65\u3002block\u4e2d\u7ebf\u7a0b\u7684\u540c\u6b65\u7531__syncthreads()\u5b9e\u73b0\u3002\u7ebf\u7a0b\u4f1a\u7b49\u5f85\u540cblock\u4e2d\u5176\u4ed6\u7ebf\u7a0b\u90fd\u6267\u884c\u5230\u8fd9\u4e00\u70b9\uff0c\u5e76\u4e14__syncthreads()\u8bed\u53e5\u4e4b\u524d\u7684\u6240\u6709shared\/global memory\u64cd\u4f5c\u90fd\u5c18\u57c3\u843d\u5b9a\uff0c\u4fdd\u8bc1block\u5185\u6240\u6709\u7ebf\u7a0b\u5728__syncthreads()\u4e4b\u540e\u90fd\u80fd\u770b\u5230\u8fd9\u4e9b\u64cd\u4f5c\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u6700\u540e\u8c08\u4e00\u4e0bCUDA\u91c7\u7528\u7684weakly-ordered\u5185\u5b58\u6a21\u578b\u3002\u5b83\u5bfc\u81f4\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u76f8\u7ee7\u6267\u884c\u7684\u4e24\u4e2a\u5b58\u50a8\u5668\u64cd\u4f5c\u5728\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u770b\u6765\u672a\u5fc5\u662f\u4e00\u6837\u7684\u987a\u5e8f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n__device__ int X = 1, Y = 2;\r\n\/\/thread 0\r\n__device__ void writeXY()\r\n{\r\n\tX = 10;\r\n\tY = 20;\r\n}\r\n\r\n\r\n\r\n\/\/thread 1\r\n__device__ void readXY()\r\n{\r\n\tint B = Y;\r\n\tint A = X;\r\n}\r\n<\/pre>\n<p>\u4ee3\u7801 3. weakly-ordered\u5185\u5b58\u6a21\u578b\u793a\u4f8b<br \/>\n\u672c\u4ee3\u7801\u6458\u5f55\u81ea CUDA C Programming Guide B.5. Memory Fence Functions<\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u53ef\u80fd\u4ea7\u751fA=1\uff0cB=20\u8fd9\u6837\u7684\u7ed3\u679c\u3002\u539f\u56e0\u662f\u6709\u591a\u79cd\u53ef\u80fd\u7684\uff0c\u8981\u4e48thread 1\u770b\u5230\u7684X\u3001Y\u7684\u5199\u5165\u987a\u5e8f\u88ab\u98a0\u5012\uff0c\u8981\u4e48thread 1\u4e2d\u8bfb\u53d6\u987a\u5e8f\u88ab\u98a0\u5012\u3002\u8fd9\u79cd\u770b\u4f3c\u76f8\u5f53\u6bc1\u4e09\u89c2\u7684\u4e8b\u60c5\u786e\u786e\u5b9e\u5b9e\u53d1\u751f\u5728\u6211\u4eec\u7684\u4ee3\u7801\u80cc\u540e\u3002\u5728\u4e00\u4e2a\u7ebf\u7a0b\u91cc\u4e24\u4e2a\u76f8\u7ee7\u4f46\u65e0\u4f9d\u8d56\u7684\u5185\u5b58\u64cd\u4f5c\uff0c\u5176\u5b9e\u9645\u5b8c\u6210\u7684\u987a\u5e8f\u53ef\u80fd\u662f\u4e0d\u786e\u5b9a\u7684\u3002\u5728\u8fd9\u4e2a\u7ebf\u7a0b<br \/>\n\u770b\u6765\u8fd9\u5e76\u6ca1\u6709\u5bfc\u81f4\u4ec0\u4e48\u4e0d\u540c\uff0c\u56e0\u4e3a\u4e24\u4e2a\u64cd\u4f5c\u65e0\u4f9d\u8d56\uff0c\u5e76\u4e0d\u4f1a\u7834\u574f\u56e0\u679c\u94fe\uff1b\u4f46\u5728\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u7684\u773c\u91cc\uff0c\u5b83\u5c31\u66b4\u9732\u51fa\u6765\u4e86\u3002<br \/>\n\u5fcd\u4e0d\u4f4f\u63d2\u53e5\u5634\uff0c\u8fd9\u7b80\u76f4\u5c31\u662f\u72ed\u4e49\u76f8\u5bf9\u8bba\u7684\u4e16\u754c\u89c2\u5728\u8ba1\u7b97\u673a\u4e16\u754c\u7684\u7ffb\u7248\uff1a\u4e00\u4e2a\u53c2\u8003\u7cfb\u7684\u89c2\u5bdf\u8005\u6240\u770b\u5230\u4e24\u4e2a\u7c7b\u7a7a\u95f4\u9694\u4e8b\u4ef6(\u53ef\u4ee5\u662f\u76f8\u7ee7\u53d1\u751f\u4f46\u56e0\u8ddd\u79bb\u9065\u8fdc\u800c\u65e0\u56e0\u679c\u5173\u8054\uff09\u5728\u53e6\u4e00\u4e2a\u53c2\u8003\u7cfb\u4e2d\u770b\u6765\u662f\u98a0\u5012\u7684\uff0c\u4f46\u6709\u56e0\u679c\u5173\u8054\u7684\u4e24\u4e8b\u4ef6\u5728\u6240\u6709\u89c2\u5bdf\u8005\u770b\u6765\u65f6\u5e8f\u90fd\u4e0d\u4f1a\u6539\u53d8\u3002\u597d\u73a9\u5427\uff1f<\/p>\n<p>\u6240\u4ee5\uff0c\u8868\u9762\u7684\u79e9\u5e8f\u4e95\u7136\u80cc\u540e\u6709\u7740\u5de8\u5927\u7684\u590d\u6742\u6027\u602a\u517d\uff0c\u4e3a\u4e86\u5173\u7262\u5b83\u7684\u7b3c\u5b50\uff0c\u6211\u4eec\u9700\u8981\u7ea6\u675f\u6211\u4eec\u7684\u4ee3\u7801\uff0c\u7528\u5408\u9002\u7684\u673a\u5236\u6765\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u901a\u8baf\u3002\u8981\u4fdd\u8bc1\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u770b\u8d77\u6765\uff0c\u4e24\u7ec4\u5b58\u50a8\u5668\u64cd\u4f5c\u5177\u6709\u6211\u4eec\u6240\u5e0c\u671b\u7684\u987a\u5e8f\uff0c\u9700\u8981\u7528 Memory Fence Function. \u8fd9\u91cc\u4e0d\u518d\u6d89\u53ca\uff0c\u5bf9\u66f4\u591a\u7ec6\u8282\u611f\u5174\u8da3\u7684\u540c\u5b66\uff0c\u8bf7\u53c2\u8003CUDA C Programming Guide B.5.\u00a0Memory Fence Functions\u7b49\u7ae0\u8282\u3002<br \/>\n\uff08\u672a\u5b8c\u5f85\u7eed\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7248\u6743\u58f0\u660e\uff1a\u539f\u521b\u4f5c\u54c1\uff0c\u6b22\u8fce\u8f6c\u8f7d\uff0c\u4f46\u8f6c\u8f7d\u8bf7\u4ee5\u8d85\u94fe\u63a5\u5f62\u5f0f\u6ce8\u660e\u6587\u7ae0\u6765\u6e90(planckscale.info)\u3001\u4f5c\u8005\u4fe1\u606f\u548c\u672c\u58f0\u660e\uff0c\u5426\u5219\u5c06\u8ffd\u7a76\u6cd5\u5f8b\u8d23\u4efb\u3002 \u4e0a\u4e00\u7bc7\u4e2d\u8c08\u5230\u4e86\u7f16\u7a0b\u6a21\u578b\u4e2d\u7684Block\u7b49\u6982\u5ff5\u5982\u4f55\u6620\u5c04\u5230\u786c\u4ef6\u4e0a\u6267\u884c\uff0c\u4ee5\u53caCUDA\u5982\u4f55\u7528\u5e76\u884c\u6765\u63a9\u76d6\u5ef6\u8fdf\u3002\u8fd9\u4e00\u7bc7\u7ee7\u7eed\u5256\u6790SIMT\uff0c\u8c08\u4e00\u8c08\u63a7\u5236\u6d41\u5206\u53c9\uff0c\u6307\u4ee4\u541e\u5410\u548c\u7ebf\u7a0b\u95f4\u901a\u8baf\u673a\u5236\u3002 \u867d\u7136\u6211\u4eec\u8bf4warp\u4e2d\u7684\u7ebf\u7a0b\u7c7b\u4f3c\u4e8eSIMD,\u4f46\u4e8b\u5b9e\u4e0a\u5b83\u662f\u771f\u6b63\u7684\u7ebf\u7a0b\u3002warp\u4e2d\u7684\u6bcf\u4e00\u4e2athread\u90fd\u6709\u81ea\u5df1\u7684\u6307\u4ee4\u5730\u5740\u5bc4\u5b58\u5668\uff0c\u5141\u8bb8\u5b83\u4eec\u5404\u81ea\u6267\u884c\u4e0d\u540c\u7684\u4efb\u52a1\uff08\u63a7\u5236\u6d41\u5206\u53c9\uff09\u3002\u6700\u7b80\u5355\u7684\uff0c\u6bd4\u5982\u4e00\u4e2a \u8bed\u53e5\uff0c\u5c06threadIdx=0&#8230;31\u8fd9\u4e00\u4e2awarp\u5212\u5206\u6210\u4e24\u4e2a\u5206\u652f\uff0c\u5404\u81ea\u505a\u4e0d\u540c\u7684\u4e8b\u60c5\u3002\u8fd9\u4e2a\u7075\u6d3b\u6027\u4ee5\u6027\u80fd\u4e3a\u4ee3\u4ef7\uff0c\u5f53\u4e00\u4e2awarp\u4e2d\u63a7\u5236\u6d41\u51fa\u73b0\u5206\u53c9\u65f6\uff0c\u4e0d\u540c\u5206\u652f\u7684\u7ebf\u7a0b\u4f1a\u88ab\u5206\u7ec4\u76f8\u7ee7\u6267\u884c\uff0c\u76f4\u5230\u5404\u5206\u652f\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u63a7\u5236\u6d41\u91cd\u65b0\u6c47\u805a\u6210\u4e00\u652f\uff08\u4e0a\u4f8b\u4e2d\u5373if\u8bed\u53e5\u7684\u7ed3\u675f\u70b9\uff09\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6267\u884c\u5355\u5143\u7684\u5229\u7528\u7387\u8f83\u4f4e\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u5206\u652f\u6267\u884c\u65f6\u90fd\u9700\u8981\u5173\u95ed\u5176\u4ed6\u5206\u652f\u7684\u7ebf\u7a0b\uff0c\u6240\u4ee5\u8fd9\u65f6\u4e00\u4e9b\u6267\u884c\u5355\u5143\u662f\u7528\u4e0d\u5230\u7684\u3002 \u4e3a\u4e86\u5c3d\u53ef\u80fd\u9ad8\u6548\u7684\u8ba1\u7b97\uff0c\u9700\u8981\u7ea6\u675f\u63a7\u5236\u6d41\u5206\u53c9\u7684\u51fa\u73b0\u3002\u9664\u4e86\u51cf\u5c11\u6d41\u7a0b\u63a7\u5236\u8bed\u53e5\u5916\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\uff0c\u5e76\u4e0d\u662f\u53ea\u8981\u6709\u6d41\u7a0b\u63a7\u5236\u8bed\u53e5\u5c31\u4e00\u5b9a\u4f1a\u5e26\u6765\u63a7\u5236\u6d41\u5206\u53c9\u3002\u5173\u952e\u662f\uff0c\u63a7\u5236\u6d41\u5206\u53c9\u53ea\u662f\u9488\u5bf9\u540c\u4e00warp\u4e2d\u7684\u7ebf\u7a0b\u800c\u8a00\uff0c\u4e0d\u540cwarp\u7684\u7ebf\u7a0b\u539f\u672c\u5c31\u662f\u4e32\u884c\u5316\u6267\u884c\u7684\uff0c\u5206\u53c9\u5bf9\u5176\u65e0\u5f71\u54cd\u3002\u56e0\u6b64\uff0c\u53ea\u6709\u6d41\u7a0b\u63a7\u5236\u8bed\u53e5\u7684\u6761\u4ef6\u5728 \u540c\u4e00warp\u5185\u4e0d\u4e00\u81f4\u65f6\uff0c\u624d\u4f1a\u6709\u63a7\u5236\u6d41\u5206\u53c9\u3002\u8fd9\u6837,\u8bf8\u5982 \u8fd9\u6837\u7684\u8bed\u53e5\u662f\u4e0d\u4f1a\u6709\u5206\u53c9\u7684\u3002\u5f53\u7136\uff0c\u66f4\u5bbd\u677e\u7684\u6761\u4ef6\u5982 \u4e5f\u4e0d\u4f1a\u6709\u5206\u53c9\u3002\u4f9d\u8d56\u4e8e\u8f93\u5165\u6570\u636e\u7684\u6761\u4ef6\u5982 \u5219\u4f1a\u5e26\u6765\u5206\u53c9\u3002 \u5bf9\u4e8e\u7b80\u5355\u7684\u6307\u4ee4\u598232\u4f4d\u6d6e\u70b9\u6570\u7684\u52a0\u3001\u4e58\uff0c32\u4f4d\u6574\u6570\u7684\u52a0\u51cf\u7b49\uff0c\u901a\u5e38CUDA Core\u5728\u4e00\u4e2a\u65f6\u949f\u5468\u671f\u5185\u53ef\u4ee5\u5b8c\u6210\u4e00\u6b21\u64cd\u4f5c\uff0c\u56e0\u800c\u4e00\u4e2a\u5468\u671f\u5185\u5b8c\u6210\u7684\u64cd\u4f5c\u6570\u76ee\u5c31\u7b49\u4e8eCUDA Cores\u6570\u76ee\uff1b\u800c\u5bf9\u4e8e\u4e00\u4e9b\u8f83\u590d\u6742\u7684\u6307\u4ee4\uff0c\u5982sin\/cos\u7b49\u8d85\u8d8a\u51fd\u6570\uff0c\u6267\u884c\u5355\u5143\u5e76\u4e0d\u80fd\u63d0\u4f9b\u8fd9\u4e48\u9ad8\u7684\u541e\u5410\u7387\u3002 \u6211\u4eec\u53ef\u4ee5\u7528\u5355\u4f4d\u5468\u671f\u5185\u8fdb\u884c\u7684\u64cd\u4f5c\u6570\u76eeN\u9664\u4ee532\u6765\u8ba1\u7b97\u6307\u4ee4\u7684\u541e\u5410\u7387\u3002\u4ee5GM204\u4e3a\u4f8b\uff0c\u5b83\u7684SM\u4e2d\u670932*4 = 128\u4e2aCUDA Cores\uff0c32\u4e2aSFU\uff08\u7279\u6b8a\u51fd\u6570\u5355\u5143\uff09\uff0c\u5728\u8ba1\u7b9732\u4f4d\u6d6e\u70b9\u52a0\u6cd5\u65f6\u5177\u6709\u6700\u9ad8\u541e\u5410\uff0c\u4e00\u4e2a\u5468\u671f\u5185\u5b8c\u6210128\u6b21\u64cd\u4f5c\uff0c\u5355\u4f4d\u5468\u671f\u5185\u6307\u4ee4\u541e\u5410\u4e3a128\/32 = 4\uff1b\u800c\u8ba1\u7b97\u5982sin\/cos\u7b49\u8d85\u8d8a\u51fd\u6570\u65f6\u7ebf\u7a0b\u4e0d\u518d\u4e00\u4e00\u5206\u914d\u5230CUDA Cores\u4e0a\uff0c\u800c\u662f\u8981\u572832\u4e2aSFU\u4e0a\u8ba1\u7b97\uff0c\u5355\u4f4d\u5468\u671f\u5185\u53ea\u80fd\u5b8c\u621032\u6b21\u64cd\u4f5c\uff0c\u6307\u4ee4\u541e\u5410\u4e3a1\u6761\u6307\u4ee4\u6bcf\u5468\u671f. \u6307\u4ee4\u7684\u541e\u5410\u7387\u6570\u636e\u53ef\u53c2\u8003CUDA C Programming Guide\u4e2d 5.4.1. Arithmetic Instructions\uff0c\u8be5\u5c0f\u8282\u4ee5\u5355\u4f4d\u65f6\u949f\u5468\u671f\u6bcfSM\u4e0a\u80fd\u591f\u8fdb\u884c\u7684\u64cd\u4f5c\u6570\u7684\u5f62\u5f0f\u7ed9\u51fa\u4e86\u5404\u6307\u4ee4\u7684\u541e\u5410\u7387\u3002 \u6307\u4ee4\u541e\u5410\u7387\u662f\u6211\u4eec\u8fdb\u884c\u6027\u80fd\u4f18\u5316\u7684\u6709\u4e00\u4e2a\u91cd\u8981\u6307\u6807\u3002\u901a\u5e38\uff0c\u5f71\u54cd\u6307\u4ee4\u541e\u5410\u7387\u7684\u56e0\u7d20\u9664\u4e86\u6570\u503c\u8ba1\u7b97\u64cd\u4f5c\u7684\u590d\u6742\u5ea6\u3001\u7cbe\u786e\u5ea6\u4e4b\u5916\uff0c\u63a7\u5236\u6d41\u5206\u53c9\u4e5f\u662f\u4e00\u4e2a\u8d21\u732e\u56e0\u5b50\u3002\u8fd9\u91cc\u7684\u539f\u56e0\u4e0d\u96be\u7406\u89e3\uff0c\u63a7\u5236\u6d41\u5206\u53c9\u65f6\u6267\u884c\u5355\u5143\u7684\u5229\u7528\u7387\u4e0b\u964d\uff0c\u4f7f\u5f97\u5355\u4f4d\u5468\u671f\u5185\u6267\u884c\u7684\u64cd\u4f5c\u6570\u76ee\u4e0b\u964d\uff0c\u4ece\u800c\u964d\u4f4e\u4e86\u6307\u4ee4\u541e\u5410\u3002 \u5230\u8fd9\u91cc\uff0c\u786c\u4ef6\u56fe\u666f\u4e0b\u7ebf\u7a0b\u7684\u6267\u884c\u5c31\u57fa\u672c\u8bf4\u5b8c\u4e86\uff0c\u53ea\u5269\u4e0b\u4e00\u4e2a\u7559\u5230\u6700\u540e\u7684\u8bdd\u9898\uff1a\u7ebf\u7a0b\u95f4\u4ea4\u4e92\u3002\u901a\u5e38\uff0c\u4e0d\u5b58\u5728\u4efb\u4f55\u76f8\u4e92\u4f5c\u7528\u7684\u7ebf\u7a0b\uff0c\u5b83\u4eec\u4e4b\u95f4\u624d\u80fd\u591f\u4ee5\u4efb\u610f\u7684\u987a\u5e8f\u6267\u884c\uff0c\u50cfblock\u3002\u4f46\u5bf9\u4e8ewarp\u8fd9\u6837\u7684\u7ebf\u7a0b\u7ec4\uff0c\u662f\u53ef\u80fd\u4e0e\u540c\u4e00block\u4e2d\u5176\u4ed6warp\u901a\u8baf\u6216\u540c\u6b65\u7684\uff0c\u8fd9\u65f6\u6267\u884c\u987a\u5e8f\u5c31\u4e0d\u80fd\u4efb\u610f\u3002\u6240\u5e78\u5373\u4fbf\u5728block\u4e4b\u5185\uff0c\u7ebf\u7a0b\u95f4\u7684\u4ea4\u4e92\u4ecd\u7136\u662f\u8f83\u5f31\u7684\uff0c\u56e0\u800c\u5e95\u5c42\u53ef\u4ee5\u5c06block\u5212\u5206\u6210warp\u6765\u5206\u7ec4\u4e32\u884c\u5316\u6267\u884c\uff0c\u9047\u5230\u4ea4\u4e92\u65f6\u518d\u53e6\u4f5c\u5904\u7406\u3002\u6211\u4eec\u73b0\u5728\u6765\u770b\u770b\u8fd9\u4e9b\u4ea4\u4e92\u673a\u5236\u3002 \u7ebf\u7a0b\u95f4\u4ea4\u4e92\u53ef\u4ee5\u7ec6\u5206\u4e3a\u901a\u8baf\u548c\u540c\u6b65\u4e24\u7c7b\u3002\u901a\u8baf\u4e3b\u8981\u7531\u516c\u5171\u5b58\u50a8\u533a\u57df\u4ea4\u6362\u6570\u636e\u6765\u5b9e\u73b0\uff0c\u4f46\u4e5f\u4e0d\u6392\u9664\u50cfshuffle\u8fd9\u6837\u7684\u7279\u6b8a\u65b9\u5f0f\u5b58\u5728\u3002 \u4ece\u901a\u8baf\u7684\u7c92\u5ea6\u6765\u770b\uff0c\u53ef\u4ee5\u5206\u4e3awarp\u5185\u90e8\u7ebf\u7a0b\u95f4\u901a\u8baf\uff0cblock\u5185\u90e8\u7ebf\u7a0b\u95f4\u901a\u8baf\uff0cblock\u95f4\u901a\u8baf\uff0c\u66f4\u7c97\u7684\u7c92\u5ea6\u8fd9\u91cc\u4e0d\u8003\u8651\u3002block\u4e4b\u95f4\u7684\u901a\u8baf\u5219\u53ea\u80fd\u57fa\u4e8eglobal\u00a0memory\uff0cblock\u5185\u90e8\u7684\u901a\u8baf\u4e3b\u8981\u57fa\u4e8eshared memory\/global memory\uff0cwarp\u5185\u90e8\u7ebf\u7a0b\u95f4\u9664\u4e86\u53ef\u4ee5\u5229\u7528\u4e0a\u8ff0\u6240\u6709\u65b9\u5f0f\uff0c\u8fd8\u6709\u4e00\u79cd\u7279\u6b8a\u7684shuffle\u673a\u5236.\u4e0b\u9762\u6211\u4eec\u4ee5\u901a\u8baf\u7684\u7c92\u5ea6\u5206\u7c7b\u9648\u8ff0\u5404\u79cd\u901a\u8baf\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002 block\u95f4\u901a\u8baf\u901a\u5e38\u57fa\u4e8e\u4e24\u6b21kernel\u53d1\u5c04\uff0c\u4e00\u6b21\u5c06\u901a\u8baf\u6570\u636e\u5199\u5165global memory\uff0c\u53e6\u4e00\u6b21\u53d1\u5c04\u8bfbglobal memory\u8fdb\u884c\u540e\u7eed\u5904\u7406\u3002\u8fd9\u79cd\u901a\u8baf\u5f00\u9500\u8f83\u5927\uff0c\u4e3b\u8981\u6765\u81ea\u4e8eglobal memory\u8bbf\u5b58\u548ckernel\u53d1\u5c04\uff0c\u6240\u4ee5\u5982\u679c\u6709\u53ef\u80fd\uff0c\u5c3d\u91cf\u628a\u4efb\u52a1\u653e\u5728\u4e00\u6b21kernel\u53d1\u5c04\u4e2d\u5b8c\u6210\u3002 \u6216\u8bb8\u6709\u4eba\u4f1a\u95ee\uff0c\u540c\u4e00\u4e2akernel\u53d1\u5c04\u4e2d\u7684\u4e24\u4e2ablock\u5177\u6709\u5171\u540c\u7684global memory\uff0c\u662f\u4e0d\u662f\u4e5f\u53ef\u4ee5\u5229\u7528\u8fd9\u4e2a\u7279\u70b9\u6765\u6784\u9020\u540c\u4e00kernel\u4e0bblock\u95f4\u7684\u901a\u8baf\u5462\uff1f\u901a\u5e38\u7684\u7b54\u6848\u662fno\uff0c\u56e0\u4e3ablock\u4e4b\u95f4\u6267\u884c\u987a\u5e8f\u4e0d\u5b9a\uff0c\u5f88\u96be\u6784\u9020\u6709\u610f\u4e49\u7684\u901a\u8baf\uff1b\u4f46\u5982\u679c\u8981\u8f83\u771f\uff0c\u7b54\u6848\u662fyes\uff0c\u6211\u4eec\u771f\u7684\u53ef\u4ee5\u6784\u9020\u4e00\u4e9b\u7279\u6b8a\u7684block\u95f4\u901a\u8baf\u65b9\u5f0f\u3002\u4e00\u4e2a\u4f8b\u5b50\u5982\u4e0b\u6240\u793a\uff0c\u8be5\u5b9e\u4f8b\u6765\u81ea\u4e8eCUDA C Programming Guide B.5. Memory Fence Functions\uff1a \u4ee3\u7801 1. block\u95f4\u901a\u8baf\u5b9e\u73b0\u6570\u7ec4\u6c42\u548c \u672c\u4ee3\u7801\u6458\u5f55\u81ea CUDA C Programming Guide B.5. Memory [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[36],"tags":[7,72,75,73,71,74],"class_list":["post-318","post","type-post","status-publish","format-standard","hentry","category-tech","tag-cuda","tag-divergent","tag-75","tag-73","tag-71","tag-74"],"translation":{"provider":"WPGlobus","version":"2.12.2","language":"en","enabled_languages":["zh","en"],"languages":{"zh":{"title":true,"content":true,"excerpt":false},"en":{"title":false,"content":false,"excerpt":false}}},"_links":{"self":[{"href":"https:\/\/mindspectrum.xyz\/en\/wp-json\/wp\/v2\/posts\/318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mindspectrum.xyz\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindspectrum.xyz\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindspectrum.xyz\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mindspectrum.xyz\/en\/wp-json\/wp\/v2\/comments?post=318"}],"version-history":[{"count":34,"href":"https:\/\/mindspectrum.xyz\/en\/wp-json\/wp\/v2\/posts\/318\/revisions"}],"predecessor-version":[{"id":734,"href":"https:\/\/mindspectrum.xyz\/en\/wp-json\/wp\/v2\/posts\/318\/revisions\/734"}],"wp:attachment":[{"href":"https:\/\/mindspectrum.xyz\/en\/wp-json\/wp\/v2\/media?parent=318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindspectrum.xyz\/en\/wp-json\/wp\/v2\/categories?post=318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindspectrum.xyz\/en\/wp-json\/wp\/v2\/tags?post=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}