{"id":206615,"date":"2020-12-14T08:20:19","date_gmt":"2020-12-14T00:20:19","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=206615"},"modified":"2020-12-07T10:20:59","modified_gmt":"2020-12-07T02:20:59","slug":"gpu-js","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/gpu-js.html","title":{"rendered":"GPU.js\u52a9\u60a8\u6539\u5584JavaScript\u6027\u80fd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u4e86GPU.js\uff0c\u5206\u6790\u4e86\u5b83\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u5e76\u6f14\u793a\u4e86\u5982\u4f55\u8fdb\u884c\u5e76\u884c\u8ba1\u7b97\u3002\u6211\u4eec\u8fd8\u6f14\u793a\u4e86\u5982\u4f55\u5728\u4f60\u7684Node.js\u5e94\u7528\u4e2d\u8bbe\u7f6eGPU.js\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4f7f\u7528GPU.js\u6539\u5584JavaScript\u6027\u80fd<\/strong><\/div>\n

\u4f60\u662f\u5426\u66fe\u7ecf\u5c1d\u8bd5\u8fc7\u8fd0\u884c\u590d\u6742\u7684\u8ba1\u7b97\uff0c\u5374\u53d1\u73b0\u5b83\u9700\u8981\u82b1\u8d39\u5f88\u957f\u65f6\u95f4\uff0c\u5e76\u4e14\u62d6\u6162\u4e86\u4f60\u7684\u8fdb\u7a0b?<\/p>\n

\u6709\u5f88\u591a\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f8b\u5982\u4f7f\u7528web worker\u6216\u540e\u53f0\u7ebf\u7a0b\u3002GPU\u51cf\u8f7b\u4e86CPU\u7684\u5904\u7406\u8d1f\u8377\uff0c\u7ed9\u4e86CPU\u66f4\u591a\u7684\u7a7a\u95f4\u6765\u5904\u7406\u5176\u4ed6\u8fdb\u7a0b\u3002\u540c\u65f6\uff0cweb worker\u4ecd\u7136\u8fd0\u884c\u5728CPU\u4e0a\uff0c\u4f46\u662f\u8fd0\u884c\u5728\u4e0d\u540c\u7684\u7ebf\u7a0b\u4e0a\u3002<\/p>\n

\u5728\u8be5\u521d\u5b66\u8005\u6307\u5357\u4e2d\uff0c\u6211\u4eec\u5c06\u6f14\u793a\u5982\u4f55\u4f7f\u7528GPU.js\u6267\u884c\u590d\u6742\u7684\u6570\u5b66\u8ba1\u7b97\u5e76\u63d0\u9ad8JavaScript\u5e94\u7528\u7684\u6027\u80fd\u3002<\/p>\n

\u4ec0\u4e48\u662fGPU.js?<\/strong><\/div>\n

GPU.js\u662f\u4e00\u4e2a\u9488\u5bf9Web\u548cNode.js\u6784\u5efa\u7684JavaScript\u52a0\u901f\u5e93\uff0c\u7528\u4e8e\u5728\u56fe\u5f62\u5904\u7406\u5355\u5143(GPGPU)\u4e0a\u8fdb\u884c\u901a\u7528\u7f16\u7a0b\uff0c\u5b83\u4f7f\u4f60\u53ef\u4ee5\u5c06\u590d\u6742\u4e14\u8017\u65f6\u7684\u8ba1\u7b97\u79fb\u4ea4\u7ed9GPU\u800c\u4e0d\u662fCPU\uff0c\u4ee5\u5b9e\u73b0\u66f4\u5feb\u7684\u8ba1\u7b97\u548c\u64cd\u4f5c\u3002\u8fd8\u6709\u4e00\u4e2a\u5907\u7528\u9009\u9879\uff1a\u5728\u7cfb\u7edf\u4e0a\u6ca1\u6709GPU\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e9b\u529f\u80fd\u4ecd\u5c06\u5728\u5e38\u89c4JavaScript\u5f15\u64ce\u4e0a\u8fd0\u884c\u3002<\/p>\n

\u5f53\u4f60\u8981\u6267\u884c\u590d\u6742\u7684\u8ba1\u7b97\u65f6\uff0c\u5b9e\u8d28\u4e0a\u662f\u5c06\u8fd9\u79cd\u8d1f\u62c5\u8f6c\u79fb\u7ed9\u7cfb\u7edf\u7684GPU\u800c\u4e0d\u662fCPU\uff0c\u4ece\u800c\u589e\u52a0\u4e86\u5904\u7406\u901f\u5ea6\u548c\u65f6\u95f4\u3002<\/p>\n

\u9ad8\u6027\u80fd\u8ba1\u7b97\u662f\u4f7f\u7528GPU.js\u7684\u4e3b\u8981\u4f18\u52bf\u4e4b\u4e00\u3002\u5982\u679c\u4f60\u60f3\u5728\u6d4f\u89c8\u5668\u4e2d\u8fdb\u884c\u5e76\u884c\u8ba1\u7b97\uff0c\u800c\u4e0d\u4e86\u89e3WebGL\uff0c\u90a3\u4e48GPU.js\u662f\u4e00\u4e2a\u9002\u5408\u4f60\u7684\u5e93\u3002<\/p>\n

\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528GPU.js<\/strong><\/div>\n

\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528GPU\u6267\u884c\u590d\u6742\u7684\u8ba1\u7b97\u7684\u539f\u56e0\u4e0d\u80dc\u679a\u4e3e\uff0c\u6709\u592a\u591a\u7684\u539f\u56e0\u65e0\u6cd5\u5728\u4e00\u7bc7\u6587\u7ae0\u4e2d\u63a2\u8ba8\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528GPU\u7684\u4e00\u4e9b\u6700\u503c\u5f97\u6ce8\u610f\u7684\u597d\u5904\u3002<\/p>\n

    \n
  1. GPU\u53ef\u7528\u4e8e\u6267\u884c\u5927\u89c4\u6a21\u5e76\u884cGPGPU\u8ba1\u7b97\u3002\u8fd9\u662f\u9700\u8981\u5f02\u6b65\u5b8c\u6210\u7684\u8ba1\u7b97\u7c7b\u578b<\/li>\n
  2. \u5f53\u7cfb\u7edf\u4e2d\u6ca1\u6709GPU\u65f6\uff0c\u5b83\u4f1a\u4f18\u96c5\u5730\u9000\u56de\u5230JavaScript<\/li>\n
  3. GPU\u5f53\u524d\u5728\u6d4f\u89c8\u5668\u548cNode.js\u4e0a\u8fd0\u884c\uff0c\u975e\u5e38\u9002\u5408\u901a\u8fc7\u5927\u91cf\u8ba1\u7b97\u6765\u52a0\u901f\u7f51\u7ad9<\/li>\n
  4. GPU.js\u662f\u5728\u8003\u8651JavaScript\u7684\u60c5\u51b5\u4e0b\u6784\u5efa\u7684\uff0c\u56e0\u6b64\u8fd9\u4e9b\u529f\u80fd\u5747\u4f7f\u7528\u5408\u6cd5\u7684JavaScript\u8bed\u6cd5<\/li>\n<\/ol>\n

    \u5982\u679c\u4f60\u8ba4\u4e3a\u4f60\u7684\u5904\u7406\u5668\u53ef\u4ee5\u80dc\u4efb\uff0c\u4f60\u4e0d\u9700\u8981GPU.js\uff0c\u770b\u770b\u4e0b\u9762\u8fd9\u4e2aGPU\u548cCPU\u8fd0\u884c\u8ba1\u7b97\u7684\u7ed3\u679c\u3002<\/p>\n

    \"\"<\/p>\n

    \u5982\u4f60\u6240\u89c1\uff0cGPU\u6bd4CPU\u5feb22.97\u500d\u3002<\/p>\n

    GPU.js\u7684\u5de5\u4f5c\u65b9\u5f0f<\/strong><\/div>\n

    \u8003\u8651\u5230\u8fd9\u79cd\u901f\u5ea6\u6c34\u5e73\uff0cJavaScript\u751f\u6001\u7cfb\u7edf\u4eff\u4f5b\u5f97\u5230\u4e86\u4e00\u4e2a\u53ef\u4ee5\u4e58\u5750\u7684\u706b\u7bad\u3002GPU\u53ef\u4ee5\u5e2e\u52a9\u7f51\u7ad9\u66f4\u5feb\u5730\u52a0\u8f7d\uff0c\u7279\u522b\u662f\u5fc5\u987b\u5728\u9996\u9875\u4e0a\u6267\u884c\u590d\u6742\u8ba1\u7b97\u7684\u7f51\u7ad9\u3002\u4f60\u4e0d\u518d\u9700\u8981\u62c5\u5fc3\u4f7f\u7528\u540e\u53f0\u7ebf\u7a0b\u548c\u52a0\u8f7d\u5668\uff0c\u56e0\u4e3aGPU\u8fd0\u884c\u8ba1\u7b97\u7684\u901f\u5ea6\u662f\u666e\u901aCPU\u768422.97\u500d\u3002<\/p>\n

    gpu.createKernel \u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u4eceJavaScript\u51fd\u6570\u79fb\u690d\u8fc7\u6765\u7684GPU\u52a0\u901f\u5185\u6838\u3002<\/p>\n

    \u4e0eGPU\u5e76\u884c\u8fd0\u884c\u5185\u6838\u51fd\u6570\u4f1a\u5bfc\u81f4\u66f4\u5feb\u7684\u8ba1\u7b97\u901f\u5ea6\u2014\u2014\u5feb1-15\u500d\uff0c\u8fd9\u53d6\u51b3\u4e8e\u4f60\u7684\u786c\u4ef6\u3002<\/p>\n

    GPU.js\u5165\u95e8<\/strong><\/div>\n

    \u4e3a\u4e86\u5c55\u793a\u5982\u4f55\u4f7f\u7528GPU.js\u66f4\u5feb\u5730\u8ba1\u7b97\u590d\u6742\u7684\u8ba1\u7b97\uff0c\u8ba9\u6211\u4eec\u5feb\u901f\u542f\u52a8\u4e00\u4e2a\u5b9e\u9645\u7684\u6f14\u793a\u3002<\/p>\n

    \u5b89\u88c5<\/p>\n

    sudo apt install mesa-common-dev libxi-dev  \/\/ using Linux <\/pre>\n

    npm<\/p>\n

    npm install gpu.js --save \r\n\/\/ OR \r\nyarn add gpu.js <\/pre>\n

    \u5728\u4f60\u7684Node\u9879\u76ee\u4e2d\u8981\u5bfc\u5165GPU.js\u3002<\/p>\n

    import { GPU } from ('gpu.js')  \r\n\/\/ OR \r\nconst { GPU } = require('gpu.js')  \r\nconst gpu = new GPU(); \r\n<\/pre>\n
    \u4e58\u6cd5\u6f14\u793a<\/strong><\/span><\/div>\n

    \u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u8ba1\u7b97\u662f\u5728GPU\u4e0a\u5e76\u884c\u5b8c\u6210\u7684\u3002<\/p>\n

    \u9996\u5148\uff0c\u751f\u6210\u5927\u91cf\u6570\u636e\u3002<\/p>\n

    \r\nconst getArrayValues = () => { \r\n \r\n  \/\/ \u5728\u6b64\u5904\u521b\u5efa2D arrary \r\n  const values = [[], []] \r\n \r\n  \/\/ \u5c06\u503c\u63d2\u5165\u7b2c\u4e00\u4e2a\u6570\u7ec4 \r\n  for (let y = 0; y < 600; y++){ \r\n    values[0].push([]) \r\n    values[1].push([]) \r\n \r\n    \/\/ \u5c06\u503c\u63d2\u5165\u7b2c\u4e8c\u4e2a\u6570\u7ec4 \r\n    for (let x = 0; x < 600; x++){ \r\n      values\\[0\\][y].push(Math.random()) \r\n      values\\[1\\][y].push(Math.random()) \r\n    } \r\n  } \r\n \r\n  \/\/ \u8fd4\u56de\u586b\u5145\u6570\u7ec4 \r\n  return values \r\n} <\/pre>\n

    \u521b\u5efa\u5185\u6838(\u8fd0\u884c\u5728GPU\u4e0a\u7684\u51fd\u6570\u7684\u53e6\u4e00\u4e2a\u8bcd)\u3002<\/p>\n

    \r\nconst gpu = new GPU(); \r\n \r\n\/\/ \u4f7f\u7528 `createKernel()` \u65b9\u6cd5\u5c06\u6570\u7ec4\u76f8\u4e58 \r\nconst multiplyLargeValues = gpu.createKernel(function(a, b) { \r\n  let sum = 0; \r\n  for (let i = 0; i < 600; i++) { \r\n    sum += a\\[this.thread.y\\][i] * b\\[i\\][this.thread.x]; \r\n  } \r\n  return sum; \r\n}).setOutput([600, 600]) <\/pre>\n

    \u4f7f\u7528\u77e9\u9635\u4f5c\u4e3a\u53c2\u6570\u8c03\u7528\u5185\u6838\u3002<\/p>\n

    \r\nconst largeArray = getArrayValues()  \r\nconst out = multiplyLargeValues(largeArray[0], largeArray[1]) <\/pre>\n

    \u8f93\u51fa<\/p>\n

    \r\nconsole.log(out\\[y\\][x]) \/\/ \u5c06\u5143\u7d20\u8bb0\u5f55\u5728\u6570\u7ec4\u7684\u7b2cx\u884c\u548c\u7b2cy\u5217  \r\nconsole.log(out\\[10\\][12]) \/\/ \u8bb0\u5f55\u8f93\u51fa\u6570\u7ec4\u7b2c10\u884c\u548c\u7b2c12\u5217\u7684\u5143\u7d20 <\/pre>\n

    \u8fd0\u884cGPU\u57fa\u51c6\u6d4b\u8bd5<\/p>\n

    \u4f60\u53ef\u4ee5\u6309\u7167GitHub\u4e0a\u6307\u5b9a\u7684\u6b65\u9aa4\u8fd0\u884c\u57fa\u51c6\u6d4b\u8bd5\u3002<\/p>\n

    \r\nnpm install @gpujs\/benchmark  \r\nconst benchmark = require('@gpujs\/benchmark')  \r\nconst benchmarks = benchmark.benchmark(options); <\/pre>\n

    options \u5bf9\u8c61\u5305\u542b\u53ef\u4ee5\u4f20\u9012\u7ed9\u57fa\u51c6\u7684\u5404\u79cd\u914d\u7f6e\u3002<\/p>\n

    \u524d\u5f80GPU.js\u5b98\u65b9\u7f51\u7ad9\u67e5\u770b\u5b8c\u6574\u7684\u8ba1\u7b97\u57fa\u51c6\uff0c\u8fd9\u5c06\u5e2e\u52a9\u4f60\u4e86\u89e3\u4f7f\u7528GPU.js\u8fdb\u884c\u590d\u6742\u8ba1\u7b97\u53ef\u4ee5\u83b7\u5f97\u591a\u5c11\u901f\u5ea6\u3002<\/p>\n

    \u7ed3\u675f<\/strong><\/div>\n

    \u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u4e86GPU.js\uff0c\u5206\u6790\u4e86\u5b83\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u5e76\u6f14\u793a\u4e86\u5982\u4f55\u8fdb\u884c\u5e76\u884c\u8ba1\u7b97\u3002\u6211\u4eec\u8fd8\u6f14\u793a\u4e86\u5982\u4f55\u5728\u4f60\u7684Node.js\u5e94\u7528\u4e2d\u8bbe\u7f6eGPU.js\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u4f60\u662f\u5426\u66fe\u7ecf\u5c1d\u8bd5\u8fc7\u8fd0\u884c\u590d\u6742\u7684\u8ba1\u7b97\uff0c\u5374\u53d1\u73b0\u5b83\u9700\u8981\u82b1\u8d39\u5f88\u957f\u65f6\u95f4\uff0c\u5e76\u4e14\u62d6\u6162\u4e86\u4f60\u7684\u8fdb\u7a0b? \u6709\u5f88\u591a\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f8b […]<\/p>\n","protected":false},"author":317,"featured_media":197632,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-206615","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/206615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/317"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=206615"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/206615\/revisions"}],"predecessor-version":[{"id":206618,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/206615\/revisions\/206618"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/197632"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=206615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=206615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=206615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}