成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何使用GPU改善JavaScript性能

開發 前端
作為開發者,我們總是尋找機會來提高應用程序的性能。當涉及到網絡應用時,我們主要在代碼中進行這些改進。但是,你有沒有想過將 GPU 的力量結合到你的網絡應用中來提高性能?本文將向你介紹一個名為 GPU.js 的 JavaScript 加速庫,并告訴你如何改進復雜的計算。

[[402117]]

本文轉載自微信公眾號「TianTianUp」,作者小弋。轉載本文請聯系TianTianUp公眾號。

正文

用 GPU.js 使你的應用程序快 10 倍。

作為開發者,我們總是尋找機會來提高應用程序的性能。當涉及到網絡應用時,我們主要在代碼中進行這些改進。

但是,你有沒有想過將 GPU 的力量結合到你的網絡應用中來提高性能?

本文將向你介紹一個名為 GPU.js 的 JavaScript 加速庫,并告訴你如何改進復雜的計算。

什么是 GPU.js

首先,官網地址:

https://gpu.rocks/#/

Source: https://gpu.rocks/#/

簡而言之,GPU.js 是一個 JavaScript 加速庫,可用于使用 JavaScript 在 GPU 上進行通用計算。它支持瀏覽器、Node.js 和 TypeScript。

除了性能提升外,我推薦使用 GPU.js 的原因還有以下幾點:

  • GPU.js 使用 JavaScript 作為基礎,允許你使用 JavaScript 語法。
  • 它承擔著將 JavaScript 自動轉譯為著色器語言的責任,并對它們進行編譯。
  • 如果設備中沒有 GPU,它可以退回到普通的 JavaScript 引擎。因此,使用 GPU.js 不會有任何不利因素。
  • GPU.js 也可以用于并行計算。此外,你可以同時在 CPU 和 GPU 上異步地進行多項計算。

所有這些東西加在一起,我不認為有理由不使用 GPU.js。因此,讓我們看看如何開始使用它。

如何設置 GPU.js?

為您的項目安裝 GPU.js 與其他的 JavaScript 庫類似。

對于 Node 項目

  1. npm install gpu.js --save 
  2. or 
  3. yarn add gpu.js 
  4. import { GPU } from ('gpu.js'
  5. --- or --- 
  6. const { GPU } = require('gpu.js'
  7. --- or --- 
  8. import { GPU } from 'gpu.js'; // Use this for TypeScript 
  9. const gpu = new GPU(); 

對于 Bowsers

在本地下載 GPU.js 或使用其 CDN。

  1. <script src="dist/gpu-browser.min.js"></script> 
  2. --- or --- 
  3. <script 
  4.   src="https://unpkg.com/gpu.js@latest/dist/gpu- browser.min.js"
  5. </script> 
  6. <script 
  7.   rc="https://cdn.jsdelivr.net/npm/gpu.js@latest/dist/gpu-browser.min.js"
  8. </script> 
  9. <script> 
  10.  const gpu = new GPU(); 
  11.  ... 
  12. </script> 

 

 

注意:

如果你使用的是 Linux,你需要確保你安裝了正確的文件,運行:sudo apt install mesa-common-dev libxi-dev

這就是你需要知道的關于安裝和導入 GPU.js 的情況。

現在,你可以開始在你的應用程序中使用 GPU 編程。

此外,我強烈建議理解 GPU.js 的基本功能和概念。所以,讓我們從 GPU.js 的一些基礎知識開始。

創建函數

你可以在 GPU.js 中定義函數以在 GPU 中運行,使用一般的 JavaScript 語法。

  1. const exampleKernel = gpu.createKernel(function() { 
  2.     ... 
  3. }, settings); 

上面的代碼樣本顯示了一個 GPU.js 函數的基本結構。我將該函數命名為 exampleKernel。正如你所看到的,我使用了 createKernel 函數,利用 GPU 進行計算。

另外,定義輸出的大小是必須的。在上面的例子中,我使用了一個名為 settings 的參數來指定輸出大小。

  1. const settings = { 
  2.     output: [100] 
  3. }; 

內核函數的輸出可以是 1D、2D 或 3D,這意味著它最多可以有 3 個線程。你可以使用 this.thread 命令在內核中訪問這些線程。

  • 1D : [長度] - 值[this.thread.x]
  • 2D : [寬度,高度] - 值[this.thread.y][this.thread.x]
  • 3D: [寬度,高度,深度] - 值[this.thread.z][this.thread.y][this.thread.x]。

最后,創建的函數可以像其他的 JavaScript 函數一樣使用函數名來調用:exampleKernel()

內部支持的變量

Number

你可以在 GPU.js 函數中使用任何整數或浮點數。

  1. const exampleKernel = gpu.createKernel(function() { 
  2.  const number1 = 10; 
  3.  const number2 = 0.10; 
  4.  return number1 + number2; 
  5. }, settings); 

Boolean

GPU.js 中也支持布爾值,與 JavaScript 類似。

  1. const kernel = gpu.createKernel(function() { 
  2.   const bool = true
  3.   if (bool) { 
  4.     return 1; 
  5.   }else
  6.     return 0; 
  7.   } 
  8. },settings); 

Arrays

你可以在內核函數中定義任何大小的數字數組,并返回它們。

  1. const exampleKernel = gpu.createKernel(function() { 
  2.  const array1 = [0.01, 1, 0.1, 10]; 
  3.  return array1; 
  4. }, settings); 

Functions

在內核函數中使用私有函數,在 GPU.js 中也是允許的。

  1. const exampleKernel = gpu.createKernel(function() { 
  2.   function privateFunction() { 
  3.     return [0.01, 1, 0.1, 10]; 
  4.   } 
  5.   return privateFunction(); 
  6. }, settings); 

支持的輸入類型

除了上述變量類型外,你還可以向內核函數傳遞幾種輸入類型。

Numbers

與變量聲明類似,你可以向內核函數傳遞整數或浮點數,如下所示。

  1. const exampleKernel = gpu.createKernel(function(x) { 
  2.  return x; 
  3. }, settings); 
  4. exampleKernel(25); 

1D,2D, or 3D Array of Numbers

你可以將 Array、Float32Array、Int16Array、Int8Array、Uint16Array、uInt8Array 等數組類型傳入 GPU.js 內核。

  1. const exampleKernel = gpu.createKernel(function(x) { 
  2.  return x; 
  3. }, settings); 
  4. exampleKernel([1, 2, 3]); 

預扁平化的 2D 和 3D 數組也被內核函數所接受。這種方法使上傳的速度更快,你必須使用 GPU.js 的輸入選項來實現這一點。

  1. const { input } = require('gpu.js'); 
  2. const value = input(flattenedArray, [width, height, depth]); 

HTML Images

與傳統的 JavaScript 相比,將圖像傳遞到函數中是我們在 GPU.js 中可以看到的一個新東西。使用 GPU.js,你可以將一個或多個 HTML 圖像作為數組傳遞給內核函數。

  1. //Single Image 
  2. const kernel = gpu.createKernel(function(image) { 
  3.     ... 
  4. }) 
  5.   .setGraphical(true
  6.   .setOutput([100, 100]); 
  7.  
  8. const image = document.createElement('img'); 
  9. image.src = 'image1.png'
  10. image.onload = () => { 
  11.   kernel(image); 
  12.   document.getElementsByTagName('body')[0].appendChild(kernel.canvas); 
  13. }; 
  14. //Multiple Images 
  15. const kernel = gpu.createKernel(function(image) { 
  16.     const pixel = image[this.thread.z][this.thread.y][this.thread.x]; 
  17.     this.color(pixel[0], pixel[1], pixel[2], pixel[3]); 
  18. }) 
  19.   .setGraphical(true
  20.   .setOutput([100, 100]); 
  21.  
  22. const image1 = document.createElement('img'); 
  23. image1.src = 'image1.png'
  24. image1.onload = onload; 
  25. .... 
  26. //add another 2 images 
  27. .... 
  28. const totalImages = 3; 
  29. let loadedImages = 0; 
  30. function onload() { 
  31.   loadedImages++; 
  32.   if (loadedImages === totalImages) { 
  33.     kernel([image1, image2, image3]); 
  34.      document.getElementsByTagName('body')[0].appendChild(kernel.canvas); 
  35.   } 
  36. }; 

除了上述配置外,還有許多令人興奮的事情可以用 GPU.js 進行實驗。你可以在其文檔中找到它們。既然你現在了解了幾種配置,讓我們用 GPU.js 寫一個函數并比較其性能。

使用 GPU.js 的第一個功能

通過結合我們之前討論的所有內容,我寫了一個小型的 angular 應用程序,通過將兩個有 1000 個元素的數組相乘來比較 GPU 和 CPU 的計算性能。

第 1 步,生成 1000 個元素的數組的函數

我將生成一個每個元素有 1000 個數字的 2D 數組,并在接下來的步驟中使用它們進行計算。

  1. generateMatrices() { 
  2.  this.matrices = [[], []]; 
  3.  for (let y = 0; y < this.matrixSize; y++) { 
  4.   this.matrices[0].push([]) 
  5.   this.matrices[1].push([]) 
  6.   for (let x = 0; x < this.matrixSize; x++) { 
  7.    const value1 = parseInt((Math.random() * 10).toString()) 
  8.    const value2 = parseInt((Math.random() * 10).toString()) 
  9.    this.matrices[0][y].push(value1) 
  10.    this.matrices[1][y].push(value2) 
  11.   } 
  12.  } 

第 2 步,內核函數

這是這個應用程序中最關鍵的函數,因為所有的 GPU 計算都發生在這里。

在這里,multiplyMatrix 函數將接收兩個數字數組和矩陣的大小作為輸入。

然后,它將把兩個數組相乘并返回總和,同時使用性能 API 測量時間。

  1. gpuMultiplyMatrix() { 
  2.   const gpu = new GPU(); 
  3.   const multiplyMatrix = gpu.createKernel(function (a: number[][], b: number[][], matrixSize: number) { 
  4.    let sum = 0; 
  5.  
  6.    for (let i = 0; i < matrixSize; i++) { 
  7.     sum += a[this.thread.y][i] * b[i][this.thread.x]; 
  8.    } 
  9.    return sum
  10.   }).setOutput([this.matrixSize, this.matrixSize]) 
  11.   const startTime = performance.now(); 
  12.   const resultMatrix = multiplyMatrix(this.matrices[0],  this.matrices[1], this.matrixSize); 
  13.  
  14.   const endTime = performance.now(); 
  15.   this.gpuTime = (endTime - startTime) + " ms"
  16.  
  17.   console.log("GPU TIME : "+ this.gpuTime); 
  18.   this.gpuProduct = resultMatrix as number[][]; 

步驟 3,CPU 乘法函數。

這是一個傳統的 TypeScript 函數,用于測量相同數組的計算時間。

  1. cpuMutiplyMatrix() { 
  2.   const startTime = performance.now(); 
  3.   const a = this.matrices[0]; 
  4.   const b = this.matrices[1]; 
  5.   let productRow = Array.apply(null, new Array(this.matrixSize)).map(Number.prototype.valueOf, 0); 
  6.   let product = new Array(this.matrixSize); 
  7.  
  8.   for (let p = 0; p < this.matrixSize; p++) { 
  9.     product[p] = productRow.slice(); 
  10.   } 
  11.  
  12.   for (let i = 0; i < this.matrixSize; i++) { 
  13.     for (let j = 0; j < this.matrixSize; j++) { 
  14.       for (let k = 0; k < this.matrixSize; k++) { 
  15.         product[i][j] += a[i][k] * b[k][j]; 
  16.       } 
  17.     } 
  18.   } 
  19.   const endTime = performance.now(); 
  20.   this.cpuTime = (endTime — startTime) + “ ms”; 
  21.   console.log(“CPU TIME : “+ this.cpuTime); 
  22.   this.cpuProduct = product; 

CPU vs GPU,性能比較

現在是時候看看圍繞著 GPU.js 和 GPU 計算的所有討論是否真實。由于我在上一節中創建了一個 Angular 應用程序,所以我用它來測量性能。

CPU vs GPU — Execution Time

你可以清楚地看到,GPU 編程的計算只花了 799ms,而 CPU 花了 7511ms,這幾乎是 10 倍的時間。

我沒有就此罷休,通過改變數組大小,對同樣的測試進行了幾個循環。

CPU vs GPU

首先,我試著用較小的數組大小,我注意到 CPU 比 GPU 花費的時間要少。例如,當我把數組大小減少到 10 個元素時,CPU 只花了 0.14ms,而 GPU 花了 108ms。

但隨著數組大小的增加,GPU 和 CPU 所花的時間有明顯的差距。正如你在上圖中看到的,GPU 是贏家。

結論

根據我使用 GPU.js 的實驗,它可以提高 JavaScript 應用程序的性能。

 

但是,我們必須注意只將 GPU 用于復雜的任務。否則,我們將浪費資源,最終會降低應用程序的性能,如上圖所示。不過,如果你還沒有嘗試過 GPU.js,我邀請大家使用它。

 

責任編輯:武曉燕 來源: TianTianUp
相關推薦

2020-12-01 06:53:37

GPUJavaScript

2017-12-14 14:32:30

.Net內存代碼

2011-07-27 14:10:43

javascript

2015-03-31 14:47:22

JavaJava性能

2009-02-09 18:02:00

2012-03-09 09:51:35

2012-06-01 09:54:03

2020-03-18 10:04:34

存儲機器學習服務器

2020-05-06 07:32:05

物聯網IOT企業

2013-06-18 10:30:42

虛擬化存儲虛擬化

2010-08-03 10:04:51

Linux Kerne

2014-08-14 10:04:19

OpenStackDHCP

2015-04-22 14:41:04

云遷移Redis緩存數據模型調整

2010-06-10 12:47:01

UML2.0

2020-07-15 07:53:41

VSCode Task腳本命令

2020-10-19 08:00:00

PowerToysWindows 10Windows

2011-04-20 10:51:56

網絡流量分析工具網絡監測

2009-06-10 22:00:57

JavaScript腳

2009-06-11 17:15:23

JavaScript性

2024-08-16 18:42:23

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 91看片网| 久草在线| 在线一区| 欧美一区二区三区国产精品 | 天天干天天想 | 狠狠色综合欧美激情 | 日韩视频免费在线 | 国产精品亚洲欧美日韩一区在线 | 国产成人精品一区二区三区网站观看 | 日本aⅴ中文字幕 | 就操在线 | 看av网 | 国产精品亚洲成在人线 | 亚洲精品一区二区在线观看 | 精品无码久久久久久久动漫 | 精区3d动漫一品二品精区 | 成人一级视频在线观看 | 亚洲精品免费视频 | 四虎影视免费观看 | 精品一区国产 | 毛片免费视频 | 日日干天天操 | 亚洲国产一区二区三区在线观看 | 美女视频黄的 | 国产成人短视频在线观看 | 精精久久| 99精品视频在线观看免费播放 | 精品日韩一区 | 91九色porny首页最多播放 | 久久另类 | 波多野结衣在线观看一区二区三区 | 国产精品久久 | 九色在线视频 | 日韩一区二区三区在线视频 | 91欧美| 亚洲一二三视频 | 国产精品久久久久久高潮 | 一区二区三区国产好的精 | 日韩欧美在线一区 | 在线视频一区二区三区 | 成人综合一区 |