{"id":2083,"date":"2013-03-26T14:13:51","date_gmt":"2013-03-26T05:13:51","guid":{"rendered":"http:\/\/peta.okechan.net\/blog\/?p=2083"},"modified":"2013-03-26T15:02:09","modified_gmt":"2013-03-26T06:02:09","slug":"%e3%81%84%e3%81%bexcode%e3%81%a7cuda%e3%82%92%e3%81%af%e3%81%98%e3%82%81%e3%82%8b%e5%a4%9a%e5%88%86%e3%81%84%e3%81%a1%e3%81%b0%e3%82%93%e7%b0%a1%e5%8d%98%e3%81%8b%e3%82%82%e3%81%97%e3%82%8c%e3%81%aa","status":"publish","type":"post","link":"https:\/\/peta.okechan.net\/blog\/archives\/2083","title":{"rendered":"\u3044\u307eXcode\u3067CUDA\u3092\u306f\u3058\u3081\u308b\u591a\u5206\u3044\u3061\u3070\u3093\u7c21\u5358\u304b\u3082\u3057\u308c\u306a\u3044\u65b9\u6cd5"},"content":{"rendered":"<p>\u672c\u683c\u7684\u306bGPGPU\u3092\u306f\u3058\u3081\u3056\u308b\u3092\u5f97\u306a\u3044\u53ef\u80fd\u6027\u304c\u500b\u4eba\u7684\u306b\u9ad8\u307e\u3063\u3066\u304d\u305f\u306e\u3067\u30e1\u30e2\u3002<br \/>\n\u3044\u3061\u3070\u3093\u7c21\u5358\u306a\u3093\u3066\u30bf\u30a4\u30c8\u30eb\u4ed8\u3051\u3066\u308b\u3051\u3069\u5b9f\u306f\u4ed6\u306e\u3084\u308a\u65b9\u3092\u3042\u307e\u308a\u8abf\u3079\u3066\u306a\u3044\u306e\u3067\u3001\u3082\u3063\u3068\u3044\u3044\u65b9\u6cd5\u304c\u3042\u308c\u3070\u6559\u3048\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h3>0. \u3072\u3068\u304f\u3061\u306bCUDA\u3068\u8a00\u3063\u3066\u3082\u2026<\/h3>\n<p>CUDA\u306b\u306f\u5927\u304d\u304f\u5206\u3051\u3066CUDA Runtime API\u3092\u4f7f\u3046\u65b9\u6cd5\u3068\u3001CUDA Driver API\u3092\u4f7f\u3046\u65b9\u6cd5\u304c\u3042\u308b\u3002<br \/>\n\u3069\u3061\u3089\u3082\u30c7\u30d0\u30a4\u30b9\u30b3\u30fc\u30c9\u306e\u66f8\u304d\u65b9\u306f\u540c\u3058\u3060\u304c\u3001\u30db\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u66f8\u304d\u65b9\u304c\u9055\u3046\u3002<br \/>\nRuntime API\u304c\u4e0a\u4f4d\u3001Driver API\u304c\u4e0b\u4f4d\u306a\u306e\u3067\u3001\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u306e\u306f\u524d\u8005\u306e\u65b9\u304c\u77ed\u304f\u3066\u6e08\u3080\u3002<br \/>\n\u3057\u304b\u3057\u4eca\u56de\u306fDriver API\u3092\u4f7f\u3046\u65b9\u6cd5\u3092\u66f8\u3044\u3066\u3044\u304f\u3002<br \/>\nDriver API\u306e\u307b\u3046\u304c\u304b\u3086\u3044\u6240\u306b\u624b\u304c\u5c4a\u304f\u3057\u3001\u30c7\u30d0\u30a4\u30b9\u30b3\u30fc\u30c9\u3068\u30db\u30b9\u30c8\u30b3\u30fc\u30c9\u3092\u5206\u96e2\u51fa\u6765\u308b\u3057\u3001\u591a\u5206\u5b9f\u969b\u306e\u30a2\u30d7\u30ea\u3067\u306fDriver API\u3092\u4f7f\u3046\u4eba\u304c\u904e\u534a\u6570\u306a\u306e\u3067\u306f\u306a\u3044\u304b\u3068\u601d\u3046\u3002<br \/>\n\u305d\u308c\u306b\u4f55\u3088\u308aDriver API\u3060\u3068\u666e\u901a\u306e\u30b3\u30f3\u30d1\u30a4\u30e9\u3067\u30db\u30b9\u30c8\u30b3\u30fc\u30c9\u304c\u30b3\u30f3\u30d1\u30a4\u30eb\u53ef\u80fd\u3060\u304c\u3001Runtime API\u3060\u3068\u30db\u30b9\u30c8\u30b3\u30fc\u30c9\u3082nvcc(CUDA \u30b3\u30f3\u30d1\u30a4\u30e9)\u3092\u901a\u3055\u306a\u304d\u3083\u884c\u3051\u306a\u304f\u3066\u3001\u3053\u308c\u304cXcode\u3068\u76f8\u6027\u304c\u60aa\u304f\u8a2d\u5b9a\u304c\u9762\u5012\u306a\u306e\u3060\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u3001\u30db\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u66f8\u304d\u65b9\u3092\u30b0\u30b0\u3063\u305f\u3068\u304d\u3001\u305d\u308c\u304cRuntime API\u306e\u3082\u306e\u304bDriver API\u306e\u3082\u306e\u304b\u660e\u8a18\u3055\u308c\u3066\u306a\u3044\u5834\u5408\u304c\u591a\u3044\u304c\u3001\u95a2\u6570\u306e\u63a5\u982d\u8f9e\u304cRuntime API\u306e\u5834\u5408\u306fcuda\u3001Driver API\u306e\u5834\u5408\u306fcu\u306b\u306a\u3063\u3066\u308b\u306e\u3067\u3059\u3050\u898b\u5206\u3051\u304c\u3064\u304f\u3002<\/p>\n<h3>1. CUDA 5.0\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<p><a href=\"https:\/\/developer.nvidia.com\/cuda-downloads\" target=\"_blank\">https:\/\/developer.nvidia.com\/cuda-downloads<\/a><br \/>\n\u4eca\u56de\u306f\u3001 cuda_5.0.36_macos-2.pkg \u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30d5\u30eb\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u305f\u3002<br \/>\n\u53e4\u3044\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u3059\u3067\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3066\u3082\u81ea\u52d5\u7684\u306b\u3044\u3044\u611f\u3058\u306b\u3057\u3066\u304f\u308c\u308b\u307f\u305f\u3044\u3002<\/p>\n<p>\u591a\u5206\u4eca\u56de\u306f\u5fc5\u9808\u3058\u3083\u306a\u3044\u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u7d42\u308f\u3063\u305f\u3089~\/.bash_profile\u3068\u304b\u3067CUDA\u7528\u306e\u74b0\u5883\u5909\u6570\u3092\u8a2d\u5b9a\u3057\u3068\u304f\u3068\u5f8c\u3005\u697d\u304b\u3082\u3057\u308c\u306a\u3044\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">export PATH=\/Developer\/NVIDIA\/CUDA-5.0\/bin:$PATH\r\nexport DYLD_LIBRARY_PATH=\/Developer\/NVIDIA\/CUDA-5.0\/lib:$DYLD_LIBRARY_PATH<\/pre>\n<p>\u8a73\u3057\u3044\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u65b9\u6cd5\u306f\u3053\u3061\u3089\u3092\u53c2\u7167\u3002<br \/>\n<a href=\"http:\/\/docs.nvidia.com\/cuda\/cuda-getting-started-guide-for-mac-os-x\/index.html\" target=\"_blank\">http:\/\/docs.nvidia.com\/cuda\/cuda-getting-started-guide-for-mac-os-x\/index.html<\/a><\/p>\n<h3>2. Xcode\u3067\u65b0\u898f\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210<\/h3>\n<p>OS X \u2192 Application \u2192 Command Line Tool \u3092\u9078\u629e\u3002<br \/>\nDriver API\u3092\u4f7f\u3046\u306e\u3067\u591a\u5206\u8a00\u8a9e\u306fC\u3067\u3082C++\u3067\u3082Objective-C+Cocoa\u3067\u3082\u3044\u3044\u3068\u601d\u3046\u304c\u4eca\u56de\u306fC\u306b\u3057\u305f\u3002<br \/>\n\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u540d\u306fCUDATest\u3068\u3057\u305f\u3002<\/p>\n<p>\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u304c \/Library\/Frameworks\/CUDA.framework \u306b\u3042\u308b\u306e\u3067 TERGETS \u2192 Build Phases \u2192 Link Binary With Libraries \u306b\u3066\u8ffd\u52a0\u3002<br \/>\n\u3042\u3068\u3001 PROJECT \u2192 Build Settings \u2192 Search Paths \u2192 Framework Search Paths \u306b \/Library\/Frameworks \u3092\u30bb\u30c3\u30c8\u3002<\/p>\n<h3>3. \u30c7\u30d0\u30a4\u30b9\u30b3\u30fc\u30c9\u3092\u66f8\u304f<\/h3>\n<p>kernel.cu \u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u3092\u65b0\u898f\u8ffd\u52a0\u3057\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3002<br \/>\n\u4eca\u56de\u306f\u30c6\u30b9\u30c8\u3068\u3044\u3046\u3053\u3068\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30c7\u30fc\u30bf\u3092\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3059\u308b\u3060\u3051\u306e\u7c21\u5358\u306a\u30ab\u30fc\u30cd\u30eb\u3092\u66f8\u3044\u305f\u3002<br \/>\n\uff08\u305f\u3060\u3057\u30a4\u30f3\u30c7\u30af\u30b7\u30f3\u30b0\u3060\u3051\u306f\u30b0\u30ea\u30c3\u30c9\u3001\u30d6\u30ed\u30c3\u30af\u306e\u6700\u591a\u6b21\u5143\u5bfe\u5fdc\u3068\u3044\u3046\uff09<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">extern &quot;C&quot;\r\n{\r\n    __global__ void addone(float *v, int n)\r\n    {\r\n        int blockIndex = gridDim.x * blockIdx.y + blockIdx.x;\r\n        int threadPerBlock = blockDim.x * blockDim.y * blockDim.z;\r\n        int threadIndex = blockIndex * threadPerBlock\r\n                        + blockDim.x * blockDim.y * threadIdx.z\r\n                        + blockDim.x * threadIdx.y\r\n                        + threadIdx.x; \r\n        if (threadIndex &lt; n) {\r\n            v&#x5B;threadIndex] = v&#x5B;threadIndex] + 1.0f;\r\n        }\r\n    }\r\n}<\/pre>\n<p>extern &#8220;C&#8221; {}\u3067\u56f2\u307e\u306a\u3044\u5834\u5408\u3001\u3053\u306e\u30b3\u30fc\u30c9\u3092\u30b3\u30f3\u30d1\u30a4\u30eb\u3059\u308b\u3068\u95a2\u6570\u540d\uff08\u30a8\u30f3\u30c8\u30ea\u30dd\u30a4\u30f3\u30c8\u540d\uff09\u304cname mangling\u3055\u308c\u3001_Z6addonePfi\u306e\u3088\u3046\u306b\u306a\u308b\u3002<br \/>\n\uff08name mangling\u3055\u308c\u305f\u5f8c\u306e\u540d\u524d\u306f\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u3066\u51fa\u6765\u305fptx\u30d5\u30a1\u30a4\u30eb\u306e\u4e2d\u8eab\u3092\u307f\u308b\u3068\u5206\u304b\u308b\u3002\uff09<br \/>\n\uff08name mangling\u81ea\u4f53\u306fCUDA\u72ec\u7279\u306a\u3082\u306e\u3067\u306f\u306a\u304f\u3066\u3001\u30b3\u30f3\u30d1\u30a4\u30e9\u3068\u540d\u306e\u4ed8\u304f\u3082\u306e\u306f\u3060\u3044\u305f\u3044\u3069\u308c\u3082\u5185\u90e8\u3067\u4f3c\u305f\u3088\u3046\u306a\u51e6\u7406\u3092\u884c\u3063\u3066\u3044\u308b\u3002\uff09<br \/>\n\u3067\u3001Driver API\u3092\u4f7f\u3046\u5834\u5408\u30db\u30b9\u30c8\u30b3\u30fc\u30c9\u304b\u3089\u95a2\u6570\u540d\u3092\u6307\u5b9a\u3059\u308b\u3068\u304d\u306bname mangling\u3055\u308c\u305f\u5f8c\u306e\u540d\u524d\u3092\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u9762\u5012\u3002<br \/>\n\uff08\u3053\u306e\u5834\u5408\u3001\u5143\u306e\u540d\u524d\u3092\u6307\u5b9a\u3059\u308b\u3068cuModuleGetFunction\u95a2\u6570\u304cCUDA_ERROR_NOT_FOUND\u3092\u8fd4\u3059\uff09<br \/>\nextern &#8220;C&#8221; {}\u3067\u56f2\u3081\u3070name mangling\u3055\u308c\u306a\u3044\u306e\u3067\u30db\u30b9\u30c8\u30b3\u30fc\u30c9\u304b\u3089\u3082\u5143\u306e\u540d\u524d\u3067\u5229\u7528\u3067\u304d\u3066\u697d\u3002<\/p>\n<h3>4. kernel.cu\u3092\u30b3\u30f3\u30d1\u30a4\u30eb\u3059\u308b\u8a2d\u5b9a<\/h3>\n<p>TERGETS \u2192 Build Rules \u2192 Add Build Rule<br \/>\n\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u611f\u3058\u3002<br \/>\n<a href=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.48.33.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.48.33-300x171.jpg\" alt=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2013-03-26 13.48.33\" width=\"300\" height=\"171\" class=\"aligncenter size-medium wp-image-2086\" srcset=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.48.33-300x171.jpg 300w, https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.48.33-1024x585.jpg 1024w, https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.48.33-624x357.jpg 624w, https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.48.33.jpg 1038w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n\u30b9\u30af\u30ea\u30d7\u30c8\u3067\u306f\u3001nvcc\u3067\u30c7\u30d0\u30a4\u30b9\u30b3\u30fc\u30c9\u3092\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u3066\u51fa\u6765\u305fptx\u30d5\u30a1\u30a4\u30eb\u3092\u5b9f\u884c\u30d5\u30a1\u30a4\u30eb\u3068\u540c\u3058\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u30b3\u30d4\u30fc\u3057\u3066\u308b\u3060\u3051\u3002<\/p>\n<p>\u3055\u3089\u306b TERGETS \u2192 Build Phases \u2192 Compile Souces \u306b kernel.cu \u3092\u8ffd\u52a0\u3002<br \/>\n<a href=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.50.04.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.50.04-300x171.jpg\" alt=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2013-03-26 13.50.04\" width=\"300\" height=\"171\" class=\"aligncenter size-medium wp-image-2087\" srcset=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.50.04-300x171.jpg 300w, https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.50.04-1024x585.jpg 1024w, https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.50.04-624x356.jpg 624w, https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.50.04.jpg 1035w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h3>5. \u30db\u30b9\u30c8\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3002<\/h3>\n<p>\u3053\u3053\u307e\u3067\u6765\u305f\u3089\u3042\u3068\u306f main.c \u3067 #include &lt;CUDA\/CUDA.h&gt; \u3059\u308b\u3060\u3051\u3067Driver API\u3092\u4f7f\u3063\u305f\u30b3\u30fc\u30c9\u304c\u66f8\u3051\u308b\u3002<br \/>\n\u3068\u308a\u3042\u3048\u305a\u4eca\u56de\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u305f\u3002<br \/>\n\u51e6\u7406\u5185\u5bb9\u3068\u3057\u3066\u306f\u30012 * 3 * 1 * 4 * 5 * 6 = 720\u8981\u7d20\u306efloat\u306e\u914d\u5217\u306b+1.0\u3059\u308b\u306e\u307f\u3002<br \/>\n\u521d\u671f\u5024\u304c0.0\u301c719.0\u306a\u306e\u3067GPU\u3092\u901a\u3057\u305f\u5f8c\u306f1.0\u301c720.0\u306b\u306a\u308b\u306f\u305a\u3002<br \/>\n\u30b0\u30ea\u30c3\u30c9\u3068\u30d6\u30ed\u30c3\u30af\u306e\u5206\u5272\u304c\u30c6\u30ad\u30c8\u30fc\u306a\u4e8b\u306b\u306a\u3063\u3066\u308b\u304c\u3001\u901a\u5e381\u6b21\u5143\u914d\u5217\u3092\u64cd\u4f5c\u3059\u308b\u5834\u5408\u306f\u3069\u3061\u3089\u30821\u6b21\u5143\uff08\u305d\u308c\u305e\u308cy\u3068z\u304c1\uff09\u306b\u3057\u3068\u3044\u3066\u3001\u30c7\u30d0\u30a4\u30b9\u30b3\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30af\u30b9\u8a08\u7b97\u3092\u30b7\u30f3\u30d7\u30eb\u306b\u3059\u3079\u304d\u3060\u308d\u3046\u3002<br \/>\nCUDA\u306e\u5236\u9650\u7684\u306b\u30b0\u30ea\u30c3\u30c9\u306ez\u5206\u5272\u6570\u306f1\u56fa\u5b9a\u3060\u3063\u305f\u3068\u601d\u3046\u304c\u4eca\u306e\u6700\u65b0GPU\u3067\u3082\u305d\u3046\u306a\u306e\u304b\u306a\uff1f<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &lt;CUDA\/CUDA.h&gt;\r\n\r\n#ifdef DEBUG\r\n#define EC(a, b) errorCheck(a, b)\r\n#else\r\n#define EC(a, b) a\r\n#endif\r\n\r\ntypedef struct\r\n{\r\n    unsigned int x;\r\n    unsigned int y;\r\n    unsigned int z;\r\n} Dim3;\r\n\r\n\/\/ cu\u95a2\u6570\u306e\u623b\u308a\u5024\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u95a2\u6570\r\nvoid errorCheck(CUresult result, const char *title)\r\n{\r\n    static const char *resultStrings&#x5B;] = {\r\n        &#x5B;CUDA_SUCCESS] = &quot;CUDA_SUCCESS&quot;,\r\n        &#x5B;CUDA_ERROR_FILE_NOT_FOUND] = &quot;CUDA_ERROR_FILE_NOT_FOUND&quot;,\r\n        &#x5B;CUDA_ERROR_INVALID_IMAGE] = &quot;CUDA_ERROR_INVALID_IMAGE&quot;,\r\n        &#x5B;CUDA_ERROR_NOT_FOUND] = &quot;CUDA_ERROR_NOT_FOUND&quot;,\r\n        &#x5B;CUDA_ERROR_LAUNCH_TIMEOUT] = &quot;CUDA_ERROR_LAUNCH_TIMEOUT&quot;,\r\n        &#x5B;CUDA_ERROR_UNKNOWN] = &quot;CUDA_ERROR_UNKNOWN&quot;,\r\n    };\r\n    \r\n    if (result &lt; CUDA_SUCCESS || result &gt; CUDA_ERROR_UNKNOWN) {\r\n        printf(&quot;%s, out of CUResult renge(%d).\\n&quot;, title, result);\r\n        return;\r\n    }\r\n    \r\n    if (resultStrings&#x5B;result]) {\r\n        printf(&quot;%s, %s\\n&quot;, title, resultStrings&#x5B;result]);\r\n    } else {\r\n        printf(&quot;%s, no result string.\\n&quot;, title);\r\n    }\r\n}\r\n\r\n\r\nint main(int argc, const char * argv&#x5B;])\r\n{\r\n    \/\/ \u30b0\u30ea\u30c3\u30c9\u306e\u5206\u5272\u6570\u3001\u30d6\u30ed\u30c3\u30af\u306e\u5206\u5272\u6570\r\n    Dim3 gridDim, blockDim;\r\n    unsigned int threadPerBlock;\r\n    gridDim.x = 2;\r\n    gridDim.y = 3;\r\n    gridDim.z = 1;\r\n    blockDim.x = 4;\r\n    blockDim.y = 5;\r\n    blockDim.z = 6;\r\n    threadPerBlock = blockDim.x * blockDim.y * blockDim.z;\r\n    \r\n    \/\/ \u30c7\u30fc\u30bf\u306e\u8981\u7d20\u6570\u3068\u30b5\u30a4\u30ba\r\n    unsigned int n;\r\n    unsigned int byteCount;\r\n    n = gridDim.x * gridDim.y * gridDim.z * threadPerBlock;\r\n    byteCount = sizeof(float) * n;\r\n    \r\n    \/\/ CUDA\u306e\u521d\u671f\u5316\r\n    EC(cuInit(0), &quot;cuInit&quot;);\r\n    \r\n    \/\/ CUDA\u30b5\u30dd\u30fc\u30c8\u30c7\u30d0\u30a4\u30b9\u6570\u306e\u30c1\u30a7\u30c3\u30af\r\n    int deviceCount = 0;\r\n    EC(cuDeviceGetCount(&amp;deviceCount), &quot;cuDeviceGetCount&quot;);\r\n    if (deviceCount == 0) {\r\n        printf(&quot;Error: No CUDA Device.\\n&quot;);\r\n        exit(0);\r\n    }\r\n    \r\n    \/\/ 0\u756a\u76ee\u306e\u30c7\u30d0\u30a4\u30b9\u306e\u30cf\u30f3\u30c9\u30eb\u3092\u5f97\u308b\r\n    CUdevice device;\r\n    EC(cuDeviceGet(&amp;device, 0), &quot;cuDeviceGet&quot;);\r\n    \r\n    \/\/ \u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u306e\u4f5c\u6210\r\n    CUcontext context;\r\n    EC(cuCtxCreate_v2(&amp;context, 0, device), &quot;cuCtxCreate_v2&quot;);\r\n    \r\n    \/\/ \u30ab\u30fc\u30cd\u30eb\u306e\u30ed\u30fc\u30c9\r\n    CUmodule mod;\r\n    EC(cuModuleLoad(&amp;mod, &quot;kernel.ptx&quot;), &quot;cuModuleLoad&quot;);\r\n    \r\n    \/\/ \u30ab\u30fc\u30cd\u30eb\u95a2\u6570\u306e\u53d6\u5f97\r\n    CUfunction func;\r\n    EC(cuModuleGetFunction(&amp;func, mod, &quot;addone&quot;), &quot;cuModuleGetFunction&quot;);\r\n    \r\n    \/\/ \u30db\u30b9\u30c8\u30e1\u30e2\u30ea\u306b\u30c7\u30fc\u30bf\u9818\u57df\u3092\u78ba\u4fdd\r\n    float *h_v;\r\n    h_v = (float*)malloc(byteCount);\r\n\r\n    \/\/ \u30c7\u30fc\u30bf\u306e\u521d\u671f\u5316\r\n    for (int i = 0; i &lt; n; i++) {\r\n        h_v&#x5B;i] = (float)i;\r\n    }\r\n    \r\n    \/\/ \u30c7\u30d0\u30a4\u30b9\u30e1\u30e2\u30ea\u306b\u30c7\u30fc\u30bf\u9818\u57df\u3092\u78ba\u4fdd\r\n    CUdeviceptr d_v;\r\n    EC(cuMemAlloc_v2(&amp;d_v, byteCount), &quot;cuMemAlloc_v2&quot;);\r\n    \r\n    \/\/ \u30db\u30b9\u30c8\u304b\u3089\u30c7\u30d0\u30a4\u30b9\u3078\u30c7\u30fc\u30bf\u3092\u30b3\u30d4\u30fc\r\n    EC(cuMemcpyHtoD_v2(d_v, h_v, byteCount), &quot;cuMemcpyHtoD_v2&quot;);\r\n    \r\n    \/\/ \u30ab\u30fc\u30cd\u30eb\u306e\u5b9f\u884c\r\n    void* args&#x5B;] = {&amp;d_v, &amp;n};\r\n    EC(cuLaunchKernel(func, gridDim.x, gridDim.y, gridDim.z, blockDim.x, blockDim.y, blockDim.z, 0, 0, args, 0), &quot;cuLaunchKernel&quot;);\r\n    \r\n    \/\/ \u7d50\u679c\u3092\u30c7\u30d0\u30a4\u30b9\u304b\u3089\u30db\u30b9\u30c8\u3078\u30b3\u30d4\u30fc\r\n    EC(cuMemcpyDtoH_v2(h_v, d_v, byteCount), &quot;cuMemcpyDtoH_v2&quot;);\r\n    \r\n    \/\/ \u7d50\u679c\u306e\u8868\u793a\r\n    printf(&quot;result: %d elements\\n&quot;, n);\r\n    for (int i = 0; i &lt; n; i++) {\r\n        printf(&quot;%f, &quot;, h_v&#x5B;i]);\r\n    }\r\n    printf(&quot;\\n&quot;);\r\n    \r\n    \/\/ \u30db\u30b9\u30c8\u30e1\u30e2\u30ea\u3092\u89e3\u653e\r\n    free(h_v);\r\n    \r\n    \/\/ \u30c7\u30d0\u30a4\u30b9\u30e1\u30e2\u30ea\u3092\u89e3\u653e\r\n    EC(cuMemFree_v2(d_v), &quot;cuMemFree_v2&quot;);\r\n    \r\n    \/\/ \u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u30a2\u30f3\u30ed\u30fc\u30c9\r\n    EC(cuModuleUnload(mod), &quot;cuModuleUnload&quot;);\r\n    \r\n    \/\/ \u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u306e\u7834\u68c4\r\n    EC(cuCtxDestroy_v2(context), &quot;cuCtxDestroy_v2&quot;);\r\n    \r\n    return 0;\r\n}<\/pre>\n<p>Runtime API\u3092\u4f7f\u3063\u305f\u30b3\u30fc\u30c9\u3068\u6bd4\u3079\u308b\u3068\u304b\u306a\u308a\u9577\u3044\u304c\u3001\u30d1\u30bf\u30fc\u30f3\u304c\u6c7a\u307e\u3063\u3066\u308b\u306e\u3067\u6163\u308c\u308c\u3070\u96e3\u3057\u304f\u306a\u3044\u3002<br \/>\n\u500b\u4eba\u7684\u306bOpenGL\u3068\u304b\u306e\u201d\u304a\u307e\u3058\u306a\u3044\u201d\u306e\u65b9\u304c\u30d1\u30bf\u30fc\u30f3\u3084\u904e\u53bb\u306e\u3057\u304c\u3089\u307f\u304c\u591a\u304f\u3066\u96e3\u3057\u304f\u611f\u3058\u308b\u3002<\/p>\n<h3>6. \u4eca\u56de\u306e\u5b9f\u884c\u7d50\u679c<\/h3>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">cuInit, CUDA_SUCCESS\r\ncuDeviceGetCount, CUDA_SUCCESS\r\ncuDeviceGet, CUDA_SUCCESS\r\ncuCtxCreate_v2, CUDA_SUCCESS\r\ncuModuleLoad, CUDA_SUCCESS\r\ncuModuleGetFunction, CUDA_SUCCESS\r\ncuMemAlloc_v2, CUDA_SUCCESS\r\ncuMemcpyHtoD_v2, CUDA_SUCCESS\r\ncuLaunchKernel, CUDA_SUCCESS\r\ncuMemcpyDtoH_v2, CUDA_SUCCESS\r\nresult: 720 elements\r\n1.000000, 2.000000, 3.000000, 4.000000, 5.000000, 6.000000, 7.000000, 8.000000, 9.000000, 10.000000, 11.000000, 12.000000, 13.000000, 14.000000, 15.000000, 16.000000, 17.000000, 18.000000, 19.000000, 20.000000, 21.000000, 22.000000, 23.000000, 24.000000, 25.000000, 26.000000, 27.000000, \r\n\u30fc\u30fc\u30fc\u3000\u5927\u80c6\u306b\u7701\u7565\u3000\u30fc\u30fc\u30fc\r\n694.000000, 695.000000, 696.000000, 697.000000, 698.000000, 699.000000, 700.000000, 701.000000, 702.000000, 703.000000, 704.000000, 705.000000, 706.000000, 707.000000, 708.000000, 709.000000, 710.000000, 711.000000, 712.000000, 713.000000, 714.000000, 715.000000, 716.000000, 717.000000, 718.000000, 719.000000, 720.000000, \r\ncuMemFree_v2, CUDA_SUCCESS\r\ncuModuleUnload, CUDA_SUCCESS\r\ncuCtxDestroy_v2, CUDA_SUCCESS\r\n<\/pre>\n<h3>7. \u3053\u306e\u5f8c\u3069\u3046\u3059\u308b\u306e\uff1f<\/h3>\n<p>\u516c\u5f0f\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u304c\u5145\u5b9f\u3057\u3066\u308b\u306e\u3067\u5f15\u3063\u304b\u304b\u3063\u305f\u3089\u307e\u305a\u306f\u305d\u3053\u3092\u898b\u305f\u307b\u3046\u304c\u3044\u3044\u3002<br \/>\n<a href=\"http:\/\/docs.nvidia.com\/cuda\/index.html\" target=\"_blank\">http:\/\/docs.nvidia.com\/cuda\/index.html<\/a><\/p>\n<p>\u3042\u3068\u306f\u52b9\u7387\u306e\u3044\u3044\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u306a\u3089\u3001GPU\u306e\u4e2d\u306e\u30c7\u30fc\u30bf\u306e\u6d41\u308c\u304c\u624b\u306b\u53d6\u308b\u3088\u3046\u306b\u5206\u304b\u308b\u304f\u3089\u3044\u306b\u306a\u308b\u307e\u3067GPU\u8133\u3092\u935b\u3048\u308b\u3057\u304b\u306a\u3044\u3068\u601d\u3046\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u683c\u7684\u306bGPGPU\u3092\u306f\u3058\u3081\u3056\u308b\u3092\u5f97\u306a\u3044\u53ef\u80fd\u6027\u304c\u500b\u4eba\u7684\u306b\u9ad8\u307e\u3063\u3066\u304d\u305f\u306e\u3067\u30e1\u30e2\u3002<br \/>\n\u3044\u3061\u3070\u3093\u7c21\u5358\u306a\u3093\u3066\u30bf\u30a4\u30c8\u30eb\u4ed8\u3051\u3066\u308b\u3051\u3069\u5b9f\u306f\u4ed6\u306e\u3084\u308a\u65b9\u3092\u3042\u307e\u308a\u8abf\u3079\u3066\u306a\u3044\u306e\u3067\u3001\u3082\u3063\u3068\u3044\u3044\u65b9\u6cd5\u304c\u3042\u308c\u3070\u6559\u3048\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h3>0. \u3072\u3068\u304f\u3061\u306bCUDA\u3068\u8a00\u3063\u3066\u3082\u2026<\/h3>\n<p>CUDA\u306b\u306f\u5927\u304d\u304f\u5206\u3051\u3066CUDA Runtime API\u3092\u4f7f\u3046\u65b9\u6cd5\u3068\u3001CUDA Driver API\u3092\u4f7f\u3046\u65b9\u6cd5\u304c\u3042\u308b\u3002<br \/>\n\u3069\u3061\u3089\u3082\u30c7\u30d0\u30a4\u30b9\u30b3\u30fc\u30c9\u306e\u66f8\u304d\u65b9\u306f\u540c\u3058\u3060\u304c\u3001\u30db\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u66f8\u304d\u65b9\u304c\u9055\u3046\u3002<br \/>\nRuntime API\u304c\u4e0a\u4f4d\u3001Driver API\u304c\u4e0b\u4f4d\u306a\u306e\u3067\u3001\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u306e\u306f\u524d\u8005\u306e\u65b9\u304c\u77ed\u304f\u3066\u6e08\u3080\u3002<br \/>\n\u3057\u304b\u3057\u4eca\u56de\u306fDriver API\u3092\u4f7f\u3046\u65b9\u6cd5\u3092\u66f8\u3044\u3066\u3044\u304f\u3002<br \/>\nDriver API\u306e\u307b\u3046\u304c\u304b\u3086\u3044\u6240\u306b\u624b\u304c\u5c4a\u304f\u3057\u3001\u30c7\u30d0\u30a4\u30b9\u30b3\u30fc\u30c9\u3068\u30db\u30b9\u30c8\u30b3\u30fc\u30c9\u3092\u5206\u96e2\u51fa\u6765\u308b\u3057\u3001\u591a\u5206\u5b9f\u969b\u306e\u30a2\u30d7\u30ea\u3067\u306fDriver API\u3092\u4f7f\u3046\u4eba\u304c\u904e\u534a\u6570\u306a\u306e\u3067\u306f\u306a\u3044\u304b\u3068\u601d\u3046\u3002<br \/>\n\u305d\u308c\u306b\u4f55\u3088\u308aDriver API\u3060\u3068\u666e\u901a\u306e\u30b3\u30f3\u30d1\u30a4\u30e9\u3067\u30db\u30b9\u30c8\u30b3\u30fc\u30c9\u304c\u30b3\u30f3\u30d1\u30a4\u30eb\u53ef\u80fd\u3060\u304c\u3001Runtime API\u3060\u3068\u30db\u30b9\u30c8\u30b3\u30fc\u30c9\u3082nvcc(CUDA \u30b3\u30f3\u30d1\u30a4\u30e9)\u3092\u901a\u3055\u306a\u304d\u3083\u884c\u3051\u306a\u304f\u3066\u3001\u3053\u308c\u304cXcode\u3068\u76f8\u6027\u304c\u60aa\u304f\u8a2d\u5b9a\u304c\u9762\u5012\u306a\u306e\u3060\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u3001\u30db\u30b9\u30c8\u30b3\u30fc\u30c9\u306e\u66f8\u304d\u65b9\u3092\u30b0\u30b0\u3063\u305f\u3068\u304d\u3001\u305d\u308c\u304cRuntime API\u306e\u3082\u306e\u304bDriver API\u306e\u3082\u306e\u304b\u660e\u8a18\u3055\u308c\u3066\u306a\u3044\u5834\u5408\u304c\u591a\u3044\u304c\u3001\u95a2\u6570\u306e\u63a5\u982d\u8f9e\u304cRuntime API\u306e\u5834\u5408\u306fcuda\u3001Driver API\u306e\u5834\u5408\u306fcu\u306b\u306a\u3063\u3066\u308b\u306e\u3067\u3059\u3050\u898b\u5206\u3051\u304c\u3064\u304f\u3002<\/p>\n<h3>1. CUDA 5.0\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<p><a href=\"https:\/\/developer.nvidia.com\/cuda-downloads\" target=\"_blank\">https:\/\/developer.nvidia.com\/cuda-downloads<\/a><br \/>\n\u4eca\u56de\u306f\u3001 cuda_5.0.36_macos-2.pkg \u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3066\u30d5\u30eb\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u305f\u3002<br \/>\n\u53e4\u3044\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u3059\u3067\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3066\u3082\u81ea\u52d5\u7684\u306b\u3044\u3044\u611f\u3058\u306b\u3057\u3066\u304f\u308c\u308b\u307f\u305f\u3044\u3002<\/p>\n<p>\u591a\u5206\u4eca\u56de\u306f\u5fc5\u9808\u3058\u3083\u306a\u3044\u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u7d42\u308f\u3063\u305f\u3089~\/.bash_profile\u3068\u304b\u3067CUDA\u7528\u306e\u74b0\u5883\u5909\u6570\u3092\u8a2d\u5b9a\u3057\u3068\u304f\u3068\u5f8c\u3005\u697d\u304b\u3082\u3057\u308c\u306a\u3044\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">export PATH=\/Developer\/NVIDIA\/CUDA-5.0\/bin:$PATH\r\nexport DYLD_LIBRARY_PATH=\/Developer\/NVIDIA\/CUDA-5.0\/lib:$DYLD_LIBRARY_PATH<\/pre>\n<p>\u8a73\u3057\u3044\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u65b9\u6cd5\u306f\u3053\u3061\u3089\u3092\u53c2\u7167\u3002<br \/>\n<a href=\"http:\/\/docs.nvidia.com\/cuda\/cuda-getting-started-guide-for-mac-os-x\/index.html\" target=\"_blank\">http:\/\/docs.nvidia.com\/cuda\/cuda-getting-started-guide-for-mac-os-x\/index.html<\/a><\/p>\n<h3>2. Xcode\u3067\u65b0\u898f\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210<\/h3>\n<p>OS X \u2192 Application \u2192 Command Line Tool \u3092\u9078\u629e\u3002<br \/>\nDriver API\u3092\u4f7f\u3046\u306e\u3067\u591a\u5206\u8a00\u8a9e\u306fC\u3067\u3082C++\u3067\u3082Objective-C+Cocoa\u3067\u3082\u3044\u3044\u3068\u601d\u3046\u304c\u4eca\u56de\u306fC\u306b\u3057\u305f\u3002<br \/>\n\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u540d\u306fCUDATest\u3068\u3057\u305f\u3002<\/p>\n<p>\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u304c \/Library\/Frameworks\/CUDA.framework \u306b\u3042\u308b\u306e\u3067 TERGETS \u2192 Build Phases \u2192 Link Binary With Libraries \u306b\u3066\u8ffd\u52a0\u3002<br \/>\n\u3042\u3068\u3001 PROJECT \u2192 Build Settings \u2192 Search Paths \u2192 Framework Search Paths \u306b \/Library\/Frameworks \u3092\u30bb\u30c3\u30c8\u3002<\/p>\n<h3>3. \u30c7\u30d0\u30a4\u30b9\u30b3\u30fc\u30c9\u3092\u66f8\u304f<\/h3>\n<p>kernel.cu \u3068\u3044\u3046\u30d5\u30a1\u30a4\u30eb\u3092\u65b0\u898f\u8ffd\u52a0\u3057\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3002<br \/>\n\u4eca\u56de\u306f\u30c6\u30b9\u30c8\u3068\u3044\u3046\u3053\u3068\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30c7\u30fc\u30bf\u3092\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3059\u308b\u3060\u3051\u306e\u7c21\u5358\u306a\u30ab\u30fc\u30cd\u30eb\u3092\u66f8\u3044\u305f\u3002<br \/>\n\uff08\u305f\u3060\u3057\u30a4\u30f3\u30c7\u30af\u30b7\u30f3\u30b0\u3060\u3051\u306f\u30b0\u30ea\u30c3\u30c9\u3001\u30d6\u30ed\u30c3\u30af\u306e\u6700\u591a\u6b21\u5143\u5bfe\u5fdc\u3068\u3044\u3046\uff09<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">extern &quot;C&quot;\r\n{\r\n    __global__ void addone(float *v, int n)\r\n    {\r\n        int blockIndex = gridDim.x * blockIdx.y + blockIdx.x;\r\n        int threadPerBlock = blockDim.x * blockDim.y * blockDim.z;\r\n        int threadIndex = blockIndex * threadPerBlock\r\n                        + blockDim.x * blockDim.y * threadIdx.z\r\n                        + blockDim.x * threadIdx.y\r\n                        + threadIdx.x; \r\n        if (threadIndex &lt; n) {\r\n            v&#x5B;threadIndex] = v&#x5B;threadIndex] + 1.0f;\r\n        }\r\n    }\r\n}<\/pre>\n<p>extern &#8220;C&#8221; {}\u3067\u56f2\u307e\u306a\u3044\u5834\u5408\u3001\u3053\u306e\u30b3\u30fc\u30c9\u3092\u30b3\u30f3\u30d1\u30a4\u30eb\u3059\u308b\u3068\u95a2\u6570\u540d\uff08\u30a8\u30f3\u30c8\u30ea\u30dd\u30a4\u30f3\u30c8\u540d\uff09\u304cname mangling\u3055\u308c\u3001_Z6addonePfi\u306e\u3088\u3046\u306b\u306a\u308b\u3002<br \/>\n\uff08name mangling\u3055\u308c\u305f\u5f8c\u306e\u540d\u524d\u306f\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u3066\u51fa\u6765\u305fptx\u30d5\u30a1\u30a4\u30eb\u306e\u4e2d\u8eab\u3092\u307f\u308b\u3068\u5206\u304b\u308b\u3002\uff09<br \/>\n\uff08name mangling\u81ea\u4f53\u306fCUDA\u72ec\u7279\u306a\u3082\u306e\u3067\u306f\u306a\u304f\u3066\u3001\u30b3\u30f3\u30d1\u30a4\u30e9\u3068\u540d\u306e\u4ed8\u304f\u3082\u306e\u306f\u3060\u3044\u305f\u3044\u3069\u308c\u3082\u5185\u90e8\u3067\u4f3c\u305f\u3088\u3046\u306a\u51e6\u7406\u3092\u884c\u3063\u3066\u3044\u308b\u3002\uff09<br \/>\n\u3067\u3001Driver API\u3092\u4f7f\u3046\u5834\u5408\u30db\u30b9\u30c8\u30b3\u30fc\u30c9\u304b\u3089\u95a2\u6570\u540d\u3092\u6307\u5b9a\u3059\u308b\u3068\u304d\u306bname mangling\u3055\u308c\u305f\u5f8c\u306e\u540d\u524d\u3092\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u9762\u5012\u3002<br \/>\n\uff08\u3053\u306e\u5834\u5408\u3001\u5143\u306e\u540d\u524d\u3092\u6307\u5b9a\u3059\u308b\u3068cuModuleGetFunction\u95a2\u6570\u304cCUDA_ERROR_NOT_FOUND\u3092\u8fd4\u3059\uff09<br \/>\nextern &#8220;C&#8221; {}\u3067\u56f2\u3081\u3070name mangling\u3055\u308c\u306a\u3044\u306e\u3067\u30db\u30b9\u30c8\u30b3\u30fc\u30c9\u304b\u3089\u3082\u5143\u306e\u540d\u524d\u3067\u5229\u7528\u3067\u304d\u3066\u697d\u3002<\/p>\n<h3>4. kernel.cu\u3092\u30b3\u30f3\u30d1\u30a4\u30eb\u3059\u308b\u8a2d\u5b9a<\/h3>\n<p>TERGETS \u2192 Build Rules \u2192 Add Build Rule<br \/>\n\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u611f\u3058\u3002<br \/>\n<a href=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.48.33.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.48.33-300x171.jpg\" alt=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2013-03-26 13.48.33\" width=\"300\" height=\"171\" class=\"aligncenter size-medium wp-image-2086\" srcset=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.48.33-300x171.jpg 300w, https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.48.33-1024x585.jpg 1024w, https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.48.33-624x357.jpg 624w, https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.48.33.jpg 1038w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n\u30b9\u30af\u30ea\u30d7\u30c8\u3067\u306f\u3001nvcc\u3067\u30c7\u30d0\u30a4\u30b9\u30b3\u30fc\u30c9\u3092\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u3066\u51fa\u6765\u305fptx\u30d5\u30a1\u30a4\u30eb\u3092\u5b9f\u884c\u30d5\u30a1\u30a4\u30eb\u3068\u540c\u3058\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u30b3\u30d4\u30fc\u3057\u3066\u308b\u3060\u3051\u3002<\/p>\n<p>\u3055\u3089\u306b TERGETS \u2192 Build Phases \u2192 Compile Souces \u306b kernel.cu \u3092\u8ffd\u52a0\u3002<br \/>\n<a href=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.50.04.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.50.04-300x171.jpg\" alt=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2013-03-26 13.50.04\" width=\"300\" height=\"171\" class=\"aligncenter size-medium wp-image-2087\" srcset=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.50.04-300x171.jpg 300w, https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.50.04-1024x585.jpg 1024w, https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.50.04-624x356.jpg 624w, https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2013\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2013-03-26-13.50.04.jpg 1035w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h3>5. \u30db\u30b9\u30c8\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3002<\/h3>\n<p>\u3053\u3053\u307e\u3067\u6765\u305f\u3089\u3042\u3068\u306f main.c \u3067 #include &lt;CUDA\/CUDA.h&gt; \u3059\u308b\u3060\u3051\u3067Driver API\u3092\u4f7f\u3063\u305f\u30b3\u30fc\u30c9\u304c\u66f8\u3051\u308b\u3002<br \/>\n\u3068\u308a\u3042\u3048\u305a\u4eca\u56de\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u305f\u3002<br \/>\n\u51e6\u7406\u5185\u5bb9\u3068\u3057\u3066\u306f\u30012 * 3 * 1 * 4 * 5 * 6 = 720\u8981\u7d20\u306efloat\u306e\u914d\u5217\u306b+1.0\u3059\u308b\u306e\u307f\u3002<br \/>\n\u521d\u671f\u5024\u304c0.0\u301c719.0\u306a\u306e\u3067GPU\u3092\u901a\u3057\u305f\u5f8c\u306f1.0\u301c720.0\u306b\u306a\u308b\u306f\u305a\u3002<br \/>\n\u30b0\u30ea\u30c3\u30c9\u3068\u30d6\u30ed\u30c3\u30af\u306e\u5206\u5272\u304c\u30c6\u30ad\u30c8\u30fc\u306a\u4e8b\u306b\u306a\u3063\u3066\u308b\u304c\u3001\u901a\u5e381\u6b21\u5143\u914d\u5217\u3092\u64cd\u4f5c\u3059\u308b\u5834\u5408\u306f\u3069\u3061\u3089\u30821\u6b21\u5143\uff08\u305d\u308c\u305e\u308cy\u3068z\u304c1\uff09\u306b\u3057\u3068\u3044\u3066\u3001\u30c7\u30d0\u30a4\u30b9\u30b3\u30fc\u30c9\u306e\u30a4\u30f3\u30c7\u30af\u30b9\u8a08\u7b97\u3092\u30b7\u30f3\u30d7\u30eb\u306b\u3059\u3079\u304d\u3060\u308d\u3046\u3002<br \/>\nCUDA\u306e\u5236\u9650\u7684\u306b\u30b0\u30ea\u30c3\u30c9\u306ez\u5206\u5272\u6570\u306f1\u56fa\u5b9a\u3060\u3063\u305f\u3068\u601d\u3046\u304c\u4eca\u306e\u6700\u65b0GPU\u3067\u3082\u305d\u3046\u306a\u306e\u304b\u306a\uff1f<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &lt;CUDA\/CUDA.h&gt;\r\n\r\n#ifdef DEBUG\r\n#define EC(a, b) errorCheck(a, b)\r\n#else\r\n#define EC(a, b) a\r\n#endif\r\n\r\ntypedef struct\r\n{\r\n    unsigned int x;\r\n    unsigned int y;\r\n    unsigned int z;\r\n} Dim3;\r\n\r\n\/\/ cu\u95a2\u6570\u306e\u623b\u308a\u5024\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u95a2\u6570\r\nvoid errorCheck(CUresult result, const char *title)\r\n{\r\n    static const char *resultStrings&#x5B;] = {\r\n        &#x5B;CUDA_SUCCESS] = &quot;CUDA_SUCCESS&quot;,\r\n        &#x5B;CUDA_ERROR_FILE_NOT_FOUND] = &quot;CUDA_ERROR_FILE_NOT_FOUND&quot;,\r\n        &#x5B;CUDA_ERROR_INVALID_IMAGE] = &quot;CUDA_ERROR_INVALID_IMAGE&quot;,\r\n        &#x5B;CUDA_ERROR_NOT_FOUND] = &quot;CUDA_ERROR_NOT_FOUND&quot;,\r\n        &#x5B;CUDA_ERROR_LAUNCH_TIMEOUT] = &quot;CUDA_ERROR_LAUNCH_TIMEOUT&quot;,\r\n        &#x5B;CUDA_ERROR_UNKNOWN] = &quot;CUDA_ERROR_UNKNOWN&quot;,\r\n    };\r\n    \r\n    if (result &lt; CUDA_SUCCESS || result &gt; CUDA_ERROR_UNKNOWN) {\r\n        printf(&quot;%s, out of CUResult renge(%d).\\n&quot;, title, result);\r\n        return;\r\n    }\r\n    \r\n    if (resultStrings&#x5B;result]) {\r\n        printf(&quot;%s, %s\\n&quot;, title, resultStrings&#x5B;result]);\r\n    } else {\r\n        printf(&quot;%s, no result string.\\n&quot;, title);\r\n    }\r\n}\r\n\r\n\r\nint main(int argc, const char * argv&#x5B;])\r\n{\r\n    \/\/ \u30b0\u30ea\u30c3\u30c9\u306e\u5206\u5272\u6570\u3001\u30d6\u30ed\u30c3\u30af\u306e\u5206\u5272\u6570\r\n    Dim3 gridDim, blockDim;\r\n    unsigned int threadPerBlock;\r\n    gridDim.x = 2;\r\n    gridDim.y = 3;\r\n    gridDim.z = 1;\r\n    blockDim.x = 4;\r\n    blockDim.y = 5;\r\n    blockDim.z = 6;\r\n    threadPerBlock = blockDim.x * blockDim.y * blockDim.z;\r\n    \r\n    \/\/ \u30c7\u30fc\u30bf\u306e\u8981\u7d20\u6570\u3068\u30b5\u30a4\u30ba\r\n    unsigned int n;\r\n    unsigned int byteCount;\r\n    n = gridDim.x * gridDim.y * gridDim.z * threadPerBlock;\r\n    byteCount = sizeof(float) * n;\r\n    \r\n    \/\/ CUDA\u306e\u521d\u671f\u5316\r\n    EC(cuInit(0), &quot;cuInit&quot;);\r\n    \r\n    \/\/ CUDA\u30b5\u30dd\u30fc\u30c8\u30c7\u30d0\u30a4\u30b9\u6570\u306e\u30c1\u30a7\u30c3\u30af\r\n    int deviceCount = 0;\r\n    EC(cuDeviceGetCount(&amp;deviceCount), &quot;cuDeviceGetCount&quot;);\r\n    if (deviceCount == 0) {\r\n        printf(&quot;Error: No CUDA Device.\\n&quot;);\r\n        exit(0);\r\n    }\r\n    \r\n    \/\/ 0\u756a\u76ee\u306e\u30c7\u30d0\u30a4\u30b9\u306e\u30cf\u30f3\u30c9\u30eb\u3092\u5f97\u308b\r\n    CUdevice device;\r\n    EC(cuDeviceGet(&amp;device, 0), &quot;cuDeviceGet&quot;);\r\n    \r\n    \/\/ \u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u306e\u4f5c\u6210\r\n    CUcontext context;\r\n    EC(cuCtxCreate_v2(&amp;context, 0, device), &quot;cuCtxCreate_v2&quot;);\r\n    \r\n    \/\/ \u30ab\u30fc\u30cd\u30eb\u306e\u30ed\u30fc\u30c9\r\n    CUmodule mod;\r\n    EC(cuModuleLoad(&amp;mod, &quot;kernel.ptx&quot;), &quot;cuModuleLoad&quot;);\r\n    \r\n    \/\/ \u30ab\u30fc\u30cd\u30eb\u95a2\u6570\u306e\u53d6\u5f97\r\n    CUfunction func;\r\n    EC(cuModuleGetFunction(&amp;func, mod, &quot;addone&quot;), &quot;cuModuleGetFunction&quot;);\r\n    \r\n    \/\/ \u30db\u30b9\u30c8\u30e1\u30e2\u30ea\u306b\u30c7\u30fc\u30bf\u9818\u57df\u3092\u78ba\u4fdd\r\n    float *h_v;\r\n    h_v = (float*)malloc(byteCount);\r\n\r\n    \/\/ \u30c7\u30fc\u30bf\u306e\u521d\u671f\u5316\r\n    for (int i = 0; i &lt; n; i++) {\r\n        h_v&#x5B;i] = (float)i;\r\n    }\r\n    \r\n    \/\/ \u30c7\u30d0\u30a4\u30b9\u30e1\u30e2\u30ea\u306b\u30c7\u30fc\u30bf\u9818\u57df\u3092\u78ba\u4fdd\r\n    CUdeviceptr d_v;\r\n    EC(cuMemAlloc_v2(&amp;d_v, byteCount), &quot;cuMemAlloc_v2&quot;);\r\n    \r\n    \/\/ \u30db\u30b9\u30c8\u304b\u3089\u30c7\u30d0\u30a4\u30b9\u3078\u30c7\u30fc\u30bf\u3092\u30b3\u30d4\u30fc\r\n    EC(cuMemcpyHtoD_v2(d_v, h_v, byteCount), &quot;cuMemcpyHtoD_v2&quot;);\r\n    \r\n    \/\/ \u30ab\u30fc\u30cd\u30eb\u306e\u5b9f\u884c\r\n    void* args&#x5B;] = {&amp;d_v, &amp;n};\r\n    EC(cuLaunchKernel(func, gridDim.x, gridDim.y, gridDim.z, blockDim.x, blockDim.y, blockDim.z, 0, 0, args, 0), &quot;cuLaunchKernel&quot;);\r\n    \r\n    \/\/ \u7d50\u679c\u3092\u30c7\u30d0\u30a4\u30b9\u304b\u3089\u30db\u30b9\u30c8\u3078\u30b3\u30d4\u30fc\r\n    EC(cuMemcpyDtoH_v2(h_v, d_v, byteCount), &quot;cuMemcpyDtoH_v2&quot;);\r\n    \r\n    \/\/ \u7d50\u679c\u306e\u8868\u793a\r\n    printf(&quot;result: %d elements\\n&quot;, n);\r\n    for (int i = 0; i &lt; n; i++) {\r\n        printf(&quot;%f, &quot;, h_v&#x5B;i]);\r\n    }\r\n    printf(&quot;\\n&quot;);\r\n    \r\n    \/\/ \u30db\u30b9\u30c8\u30e1\u30e2\u30ea\u3092\u89e3\u653e\r\n    free(h_v);\r\n    \r\n    \/\/ \u30c7\u30d0\u30a4\u30b9\u30e1\u30e2\u30ea\u3092\u89e3\u653e\r\n    EC(cuMemFree_v2(d_v), &quot;cuMemFree_v2&quot;);\r\n    \r\n    \/\/ \u30e2\u30b8\u30e5\u30fc\u30eb\u306e\u30a2\u30f3\u30ed\u30fc\u30c9\r\n    EC(cuModuleUnload(mod), &quot;cuModuleUnload&quot;);\r\n    \r\n    \/\/ \u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u306e\u7834\u68c4\r\n    EC(cuCtxDestroy_v2(context), &quot;cuCtxDestroy_v2&quot;);\r\n    \r\n    return 0;\r\n}<\/pre>\n<p>Runtime API\u3092\u4f7f\u3063\u305f\u30b3\u30fc\u30c9\u3068\u6bd4\u3079\u308b\u3068\u304b\u306a\u308a\u9577\u3044\u304c\u3001\u30d1\u30bf\u30fc\u30f3\u304c\u6c7a\u307e\u3063\u3066\u308b\u306e\u3067\u6163\u308c\u308c\u3070\u96e3\u3057\u304f\u306a\u3044\u3002<br \/>\n\u500b\u4eba\u7684\u306bOpenGL\u3068\u304b\u306e\u201d\u304a\u307e\u3058\u306a\u3044\u201d\u306e\u65b9\u304c\u30d1\u30bf\u30fc\u30f3\u3084\u904e\u53bb\u306e\u3057\u304c\u3089\u307f\u304c\u591a\u304f\u3066\u96e3\u3057\u304f\u611f\u3058\u308b\u3002<\/p>\n<h3>6. \u4eca\u56de\u306e\u5b9f\u884c\u7d50\u679c<\/h3>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">cuInit, CUDA_SUCCESS\r\ncuDeviceGetCount, CUDA_SUCCESS\r\ncuDeviceGet, CUDA_SUCCESS\r\ncuCtxCreate_v2, CUDA_SUCCESS\r\ncuModuleLoad, CUDA_SUCCESS\r\ncuModuleGetFunction, CUDA_SUCCESS\r\ncuMemAlloc_v2, CUDA_SUCCESS\r\ncuMemcpyHtoD_v2, CUDA_SUCCESS\r\ncuLaunchKernel, CUDA_SUCCESS\r\ncuMemcpyDtoH_v2, CUDA_SUCCESS\r\nresult: 720 elements\r\n1.000000, 2.000000, 3.000000, 4.000000, 5.000000, 6.000000, 7.000000, 8.000000, 9.000000, 10.000000, 11.000000, 12.000000, 13.000000, 14.000000, 15.000000, 16.000000, 17.000000, 18.000000, 19.000000, 20.000000, 21.000000, 22.000000, 23.000000, 24.000000, 25.000000, 26.000000, 27.000000, \r\n\u30fc\u30fc\u30fc\u3000\u5927\u80c6\u306b\u7701\u7565\u3000\u30fc\u30fc\u30fc\r\n694.000000, 695.000000, 696.000000, 697.000000, 698.000000, 699.000000, 700.000000, 701.000000, 702.000000, 703.000000, 704.000000, 705.000000, 706.000000, 707.000000, 708.000000, 709.000000, 710.000000, 711.000000, 712.000000, 713.000000, 714.000000, 715.000000, 716.000000, 717.000000, 718.000000, 719.000000, 720.000000, \r\ncuMemFree_v2, CUDA_SUCCESS\r\ncuModuleUnload, CUDA_SUCCESS\r\ncuCtxDestroy_v2, CUDA_SUCCESS\r\n<\/pre>\n<h3>7. \u3053\u306e\u5f8c\u3069\u3046\u3059\u308b\u306e\uff1f<\/h3>\n<p>\u516c\u5f0f\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u304c\u5145\u5b9f\u3057\u3066\u308b\u306e\u3067\u5f15\u3063\u304b\u304b\u3063\u305f\u3089\u307e\u305a\u306f\u305d\u3053\u3092\u898b\u305f\u307b\u3046\u304c\u3044\u3044\u3002<br \/>\n<a href=\"http:\/\/docs.nvidia.com\/cuda\/index.html\" target=\"_blank\">http:\/\/docs.nvidia.com\/cuda\/index.html<\/a><\/p>\n<p>\u3042\u3068\u306f\u52b9\u7387\u306e\u3044\u3044\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u306a\u3089\u3001GPU\u306e\u4e2d\u306e\u30c7\u30fc\u30bf\u306e\u6d41\u308c\u304c\u624b\u306b\u53d6\u308b\u3088\u3046\u306b\u5206\u304b\u308b\u304f\u3089\u3044\u306b\u306a\u308b\u307e\u3067GPU\u8133\u3092\u935b\u3048\u308b\u3057\u304b\u306a\u3044\u3068\u601d\u3046\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[289,313],"class_list":["post-2083","post","type-post","status-publish","format-standard","hentry","category-tech","tag-cuda","tag-xcode"],"_links":{"self":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/2083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/comments?post=2083"}],"version-history":[{"count":0,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/2083\/revisions"}],"wp:attachment":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/media?parent=2083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/categories?post=2083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/tags?post=2083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}