Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. const { GPU } = require('gpu.js');
  2.  
  3. const gpu = new GPU({ mode: 'gpu' });
  4.  
  5. for (let i = 0; i < 1000; i++) { //1000 seems... so small...
  6. const setup = setupBenchmark(i * 512);
  7. benchmark(setup);
  8. }
  9.  
  10. function setupBenchmark(size) {
  11. const width = size;
  12. const height = size;
  13. return {
  14. kernel: makeKernel(width, height),
  15. inputA: randomMatrix(width, height),
  16. inputB: randomMatrix(width, height),
  17. };
  18. }
  19.  
  20. function benchmark(setup) {
  21. setup.kernel(setup.inputA, setup.inputB);
  22. }
  23.  
  24. function makeKernel(width, height) {
  25. const multiplyMatrix = gpu.createKernel(function(a, b) {
  26. let sum = 0;
  27. for (let i = 0; i < 512; i++) {
  28. sum += a[this.thread.y][i] * b[i][this.thread.x];
  29. }
  30. return sum;
  31. }, { output: [width, height] });
  32. return multiplyMatrix;
  33. }
  34.  
  35. function randomMatrix(width, height) {
  36. const rows = [];
  37. for (let y = 0; y < height; y++) {
  38. const columns = new Float32Array(width);
  39. rows.push(columns);
  40. for (let x = 0; x < width; x++) {
  41. columns[x] = Math.random();
  42. }
  43. }
  44. return rows;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement