{"id":6016,"date":"2024-08-22T17:01:01","date_gmt":"2024-08-22T09:01:01","guid":{"rendered":""},"modified":"2024-08-22T17:01:01","modified_gmt":"2024-08-22T09:01:01","slug":"cuda\u7f16\u7a0b\u5b9e\u4f8b_C\u8bed\u8a00\u7f16\u7a0b\u9898","status":"publish","type":"post","link":"https:\/\/mushiming.com\/6016.html","title":{"rendered":"cuda\u7f16\u7a0b\u5b9e\u4f8b_C\u8bed\u8a00\u7f16\u7a0b\u9898"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u2003\u2003cuFFT\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4f18\u5316\u7684\u4e14\u57fa\u4e8eCUDA\u5b9e\u73b0\u7684\u5feb\u901f\u5085\u91cc\u53f6\u53d8\u6362(FFT)\u3002FFT\u5728\u4fe1\u53f7\u5904\u7406\u4e2d\u53ef\u4ee5\u5c06\u4fe1\u53f7\u4ece\u65f6\u57df\u8f6c\u6362\u5230\u9891\u57df\uff0c\u9006FFT\u8fc7\u7a0b\u5219\u76f8\u53cd\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u4e00\u4e2aFFT\u4ee5\u89c4\u5219\u7684\u65f6\u95f4\u95f4\u9694\u63a5\u53d7\u4fe1\u53f7\u4e2d\u7684\u5e8f\u5217\u6837\u672c\u5e76\u4f5c\u4e3a\u8f93\u5165\u3002\u7136\u540e\u4f7f\u7528\u8fd9\u4e9b\u6837\u672c\u751f\u6210\u4e00\u7ec4\u53e0\u52a0\u7684\u5206\u91cf\u9891\u7387\uff0c\u6309\u9891\u7387\u62bd\u53d6\u4f5c\u4e3a\u8f93\u5165\u6837\u672c\u7684\u4fe1\u53f7\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u4e24\u4e2a\u4fe1\u53f7\u53e0\u52a0\u751f\u6210\u4fe1\u53f7cos(x)+cos(2x)\uff0c\u5e76\u901a\u8fc7FFT\u5c06\u4e24\u4e2a\u4fe1\u53f7\u7684\u5206\u91cf\u8f6c\u4e3a\u9891\u73871.0\u548c2.0\u3002\u81f3\u4e8eFFT\u5176\u4ed6\u7684\u5185\u5bb9\uff0c\u8fd9\u91cc\u4e0d\u505a\u4ecb\u7ecd\u3002
\"cuda\u7f16\u7a0b\u5b9e\u4f8b_C\u8bed\u8a00\u7f16\u7a0b\u9898<\/font><\/p>\n

\u4f7f \u7528 cuFFT API<\/strong>
\u2003\u2003cuFFT\u901a\u5e38\u6307\u4e24\u4e2a\u72ec\u7acb\u7684\u5e93\uff1a\u6838\u5fc3\u9ad8\u6027\u80fd\u7684cuFFT\u5e93\u548c\u53ef\u79fb\u690d\u7684cuFFTW\u5e93\u3002cuFFT\u5e93\u662f\u5728CUDA\u4e2d\u80fd\u63d0\u4f9b\u81ea\u8eabAPI\u7684FFT\u5b9e\u73b0\u3002\u53e6\u4e00\u65b9\u9762\uff0ccuFFTW\u4e0e\u6807\u51c6\u7684FFTW(\u5feb\u901f\u5085\u91cc\u53f6\u53d8\u6362\u7684\u6807\u51c6C\u8bed\u8a00\u7a0b\u5e8f\u96c6)\u4e3b\u673a\u7aefFFT\u5e93\u6709\u76f8\u540c\u7684API\u3002\u548ccuBLAS\u4e0e\u4f20\u7edf\u7684BLAS\u5e93\u5171\u4eab\u5927\u90e8\u5206\u7684API\u7684\u60c5\u51b5\u7c7b\u4f3c\uff0ccuFFW\u5219\u662f\u7528\u6765\u6700\u5927\u9650\u5ea6\u5730\u63d0\u9ad8\u4f7f\u7528FFTW\u73b0\u6709\u4ee3\u7801\u7684\u53ef\u79fb\u690d\u6027\u3002FFTW\u5e93\u7684\u5f88\u591a\u51fd\u6570\u5728cuFFTW\u4e2d\u540c\u6837\u9002\u7528\u3002\u6b64\u5916\uff0ccuFFTW\u5e93\u5047\u8bbe\u6240\u6709\u8981\u4f20\u8f93\u7684\u8f93\u5165\u6570\u636e\u90fd\u5b58\u50a8\u5728\u4e3b\u673a\u5185\u5b58\u4e2d\uff0c\u5e76\u4e3a\u7528\u6237\u5904\u7406\u6240\u6709\u7684\u5185\u5b58\u5206\u5e03(cudaMalloc)\u548c\u5185\u5b58\u62f7\u8d1d(cudaMemcpy)\u3002\u867d\u7136\u8fd9\u53ef\u80fd\u5bf9\u6027\u80fd\u6709\u6240\u5f71\u54cd\uff0c\u4f46\u5b83\u5927\u5927\u52a0\u5feb\u4e86\u7a0b\u5e8f\u79fb\u690d\u7684\u8fc7\u7a0b\u3002\u81f3\u4e8ecuFFTW\u548ccuFFT\u652f\u6301\u7684\u64cd\u4f5c\uff0c\u8bf7\u53c2\u9605cuFFT\u7528\u6237\u6307\u5357\u3002
\u2003\u2003cuFFT\u5e93\u7684\u914d\u7f6e\u662f\u7528FFT plan\u5b8c\u6210\u7684\u3002\u5373cuFFT\u662f\u7528\u6765\u6307\u4ee3\u5b83\u7684\u64cd\u4f5c\u672f\u8bed\u3002\u4e00\u4e2aplan\u5b9a\u4e49\u4e86\u4e00\u4e2a\u8981\u8fdb\u884c\u7684\u5355\u4e00\u53d8\u6362\u64cd\u4f5c\u3002cuFFT\u4f7f\u7528plan\u6765\u83b7\u53d6\u5185\u5b58\u5206\u914d\u3001\u5185\u5b58\u8f6c\u79fb\u4ee5\u53ca\u5185\u5b58\u542f\u52a8\u6765\u6267\u884c\u53d8\u6362\u8bf7\u6c42\u3002\u4e0d\u540c\u7684plan\u521b\u5efa\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u751f\u6210\u589e\u5927\u590d\u6742\u6027\u548c\u7ef4\u6570\u7684plan\uff1a<\/font><\/font><\/font><\/p>\n

cufftResult cufftPlan1d<\/span>(<\/span>cufftHandle *<\/span>plan,<\/span>int<\/span> nx,<\/span>cufftType type,<\/span>int<\/span> batch)<\/span>;<\/span> cufftResult cufftPlan2d<\/span>(<\/span>cufftHandle *<\/span>plan,<\/span>int<\/span> nx,<\/span>int<\/span> ny,<\/span>cufftType type)<\/span>;<\/span> cufftResult cufftPlan3d<\/span>(<\/span>cufftHandle *<\/span>plan,<\/span>int<\/span> nx,<\/span>int<\/span> ny,<\/span>int<\/span> nz,<\/span>cufftType type)<\/span>;<\/span> <\/code><\/pre>\n

\u2003\u2003cuFFT\u8fd8\u652f\u6301\u591a\u79cd\u8f93\u5165\u548c\u8f93\u51fa\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\uff1a\u590d\u6570\u5230\u590d\u6570\u3001\u5b9e\u6570\u5230\u590d\u6570\u3001\u590d\u6570\u5230\u5b9e\u6570\u3002\u5f53\u7136\uff0c\u5bf9\u4e8e\u8bb8\u591a\u5b9e\u9645\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u6700\u5b9e\u7528\u7684\u662f\u5b9e\u6570\u5230\u590d\u6570\u8fd9\u79cd\u7c7b\u578b\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u4ece\u5b9e\u9645\u7cfb\u7edf\u8f93\u5165\u5b9e\u9645\u7684\u6d4b\u91cf\u7ed3\u679c\u5230cuFFT\u4e2d\u3002
\u2003\u2003\u4e00\u65e6\u914d\u7f6e\u597d\u4e00\u4e2acuFFT plan\uff0c\u4f7f\u7528cufftExec* \u51fd\u6570\u6765\u5bf9\u5b83\u8fdb\u884c\u8c03\u7528\u6267\u884c(\u4f8b\u5982\uff0ccufftExecC2C)\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u65e0\u8bba\u8be5\u53d8\u6362\u662f\u4e00\u79cd\u6b63\u5411FFT(\u65f6\u57df\u5230\u9891\u57df)\u8fd8\u662f\u6012\u5411FFT(\u9891\u57df\u5230\u65f6\u57df)\uff0c\u51fd\u6570\u8c03\u7528\u90fd\u53ef\u4ee5\u5c06plan\u3001\u8f93\u5165\u6570\u636e\u7684\u5b58\u50a8\u4f4d\u7f6e\u3001\u8f93\u51fa\u6570\u636e\u7684\u5b58\u653e\u4f4d\u7f6e\u4f5c\u4e3a\u8f93\u5165\u3002
cuFFT \u529f \u80fd \u793a \u8303<\/strong>
\u2003\u2003\u4e00\u4e2acuFFT\u5e94\u7528\u7a0b\u5e8f\u7684\u5de5\u4f5c\u6d41\u7684\u4e0d\u540c\u53d6\u51b3\u4e8e\u53d8\u6362\u7684\u590d\u6742\u6027\u3002\u4e00\u4e2acuFFT\u5e94\u7528\u7a0b\u5e8f\u7684\u5de5\u4f5c\u6d41\u4e00\u822c\u5e94\u5305\u62ec\uff1a
\u2003\u20031.\u521b\u5efa\u5e76\u914d\u7f6e\u4e00\u4e2acuFFT plan\uff1b
\u2003\u20032.\u7528cudamalloc\u51fd\u6570\u5206\u914d\u8bbe\u5907\u5185\u5b58\u6765\u5b58\u50a8\u8f93\u5165\u6837\u672c\u548c\u8f93\u51fa\u9891\u7387\u3002\u6ce8\u610f\uff0c\u6240\u5206\u914d\u7684\u5185\u5b58\u5fc5\u987b\u652f\u6301\u5bf9\u5e94\u6267\u884c\u7684\u53d8\u6362\u7c7b\u578b(\u4f8b\u5982\uff0c\u590d\u6570\u5230\u590d\u6570\u3001\u5b9e\u6570\u5230\u590d\u6570\u3001\u590d\u6570\u5230\u5b9e\u6570)\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u8bbe\u5907\u5185\u5b58\u5bf9\u8f93\u5165\u548c\u8f93\u51fa\u76f4\u63a5\u8fdb\u884c\u53d8\u6362\uff1b
\u2003\u20033.\u7528cudaMemcpy\u7528\u8f93\u5165\u6837\u672c\u4f20\u9001\u8bbe\u5907\u5185\u5b58\uff1b
\u2003\u2003\u4f7f\u7528cudaExec* \u51fd\u6570\u6267\u884cplan\uff1b
\u2003\u2003\u7528cudaMemcpy\u53d6\u56de\u8bbe\u5907\u5185\u5b58\u4e2d\u7684\u7ed3\u679c\uff1b
\u2003\u2003\u7528cudaFree\u548ccufftDestroy\u91ca\u653eCUDA\u548ccuFFT\u8d44\u6e90\uff1b
\u2003\u2003\u63a5\u4e0b\u6765\u7684\u793a\u4f8b\u4ee3\u7801\u4ece\u51fd\u6570cos(x)\u4e2d\u751f\u6210\u4e00\u4e2a\u8f93\u5165\u6837\u672c\u5e8f\u5217\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u590d\u6570\u540e\u4f20\u7ed9GPU\uff0c\u5728\u5c06\u7ed3\u679c\u62f7\u8d1d\u56de\u4e3b\u673a\u7aef\u4e4b\u524d\u6267\u884c\u590d\u6570\u5230\u590d\u6570\u7684\u4e00\u7ef4plan\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5bf9\u4e8e\u8f93\u5165\u548c\u8f93\u51fa\u53c2\u6570\uff0c\u56e0\u4e3a\u53ef\u4ee5\u5c06\u76f8\u540c\u7684\u5185\u5b58\u4f4d\u7f6edComplexSamples\u4f20\u7ed9cufftExecC2C,\u6240\u4ee5\u8fd9\u662f\u4e00\u4e2a\u5c31\u5730FFT\u8fd0\u7b97\u3002\u53ef\u4ee5\u9884\u5148\u5206\u914d\u4e00\u4e2a\u72ec\u7acb\u7684\u8f93\u51fa\u7f13\u5b58\u533a\uff0c\u5e76\u7528\u6765\u5b58\u50a8\u8f93\u51fa\u7ed3\u679c\u3002<\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/font><\/p>\n

\/\/ Setup the cuFFT plan<\/span> cufftPlan1d<\/span>(<\/span>&<\/span>plan,<\/span> N,<\/span> CUFFT_C2C,<\/span> 1<\/span>)<\/span>;<\/span> \/\/ Allocate device memory<\/span> cudaMalloc<\/span>(<\/span>(<\/span>void<\/span> *<\/span>*<\/span>)<\/span>&<\/span>dComplexSamples,<\/span> sizeof<\/span>(<\/span>cufftComplex)<\/span> *<\/span> N)<\/span>;<\/span> \/\/ Transfer inputs into device memory<\/span> cudaMemcpy<\/span>(<\/span>dComplexSamples,<\/span> complexSamples,<\/span> sizeof<\/span>(<\/span>cufftComplex)<\/span> *<\/span> N,<\/span> cudaMemcpyHostToDevice)<\/span>;<\/span> \/\/ Execute a complex-to-complex 1D FFT<\/span> cufftExecC2C<\/span>(<\/span>plan,<\/span> dComplexSamples,<\/span> dComplexSamples,<\/span> CUFFT_FORWARD)<\/span>;<\/span> \/\/ Retrieve the results into host memory<\/span> cudaMemcpy<\/span>(<\/span>complexFreq,<\/span> dComplexSamples,<\/span> sizeof<\/span>(<\/span>cufftComplex)<\/span> *<\/span> N,<\/span> cudaMemcpyDeviceToHost)<\/span>;<\/span> <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"cuda\u7f16\u7a0b\u5b9e\u4f8b_C\u8bed\u8a00\u7f16\u7a0b\u9898\u2003\u2003\u4f7f\u7528cuFFTAPI\u2003\u2003cuFFT\u529f\u80fd\u793a\u8303\u2003\u2003_cufft","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6016"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=6016"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6016\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=6016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=6016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=6016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}