Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container" id="root">
  <div id="log"></div>
  <div class="input-field">
    <label for="size">Size of Matrix(uniform)</label>
    <input id="size" class="size" type="number" placeholder="128" />
  </div>
  <h6> Select Benchmarks</h6>
   <p>
    <label>
      <input type="checkbox" id="matmult" />
      <span>Matrix Multiplication</span>
    </label>
  </p>
   <p>
    <label>
      <input type="checkbox" id="conv" />
      <span>Kernel Convolution</span>
    </label>
  </p>
  <h6> Select Modes</h6>
  <p>
    <label>
      <input type="checkbox" id="cpu" />
      <span>CPU</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" id="gpujscpu" />
      <span>CPU(GPUjs)</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" id="gpu" />
      <span>GPU</span>
    </label>
  </p>
  <p>
    <label>
      <input type="checkbox" id="gputex" />
      <span>GPU(Texture Mode)</span>
    </label>
  </p>
  <button id="bench" class='btn waves-effect' type="button">benchmark!</button>
  <br><br>
  <div class="out"></div>
</div>

      <footer class="page-footer blue accent-3">
          <div class="footer-copyright">
            <div class="container">
            GPU.JS Example
            <a class="grey-text text-lighten-4 right" href="https://codepen.io/imnotacoder/pen/KEyYjm">View The Code</a>
            </div>
          </div>
        </footer>
              
            
!

CSS

              
                
#root {
    display: flex;
    min-height: 100vh;
    flex-direction: column;
  }

  main {
    flex: 1 0 auto;
  }
      
              
            
!

JS

              
                $('#conv').click(() => {
  $('#gpujscpu').prop('disabled', $('#conv').prop('checked'))
})

const domLog = msg => {
  //$("#log").append(`<span>Log: ${msg} </span><br>`);
};
$("#bench").click(() => {
  $("#log").html("");
  let size = $(".size").val() || 128;
  domLog("start");

  let checkboxes = {
    cpu: document.getElementById("cpu").checked,
    gpujsCpu: document.getElementById("gpujscpu").checked,
    gpu: document.getElementById("gpu").checked,
    gpuTex: document.getElementById("gputex").checked,
    matMult: document.getElementById("matmult").checked,
    conv: document.getElementById("conv").checked
  };
  
  if (checkboxes.conv) checkboxes.gpujsCpu = false;

  domLog("checks");

  const kernel = [[1, 2, 1], [2, 0, 2], [1, 2, 1]];

  size = parseInt(size);

  const gpu = new GPU({ mode: "gpu" });
  const cpu = new GPU({ mode: "cpu" });

  const matMultFunc = `function (a, b){
    var sum = 0;
    for (var i = 0; i < ${size}; i++){
      sum += a[this.thread.y][i] * b[i][this.thread.x];
    }
    return sum;
  }`;

  const matConvFunc = `function (array, kernel) {
  var sum = 0;
  for (var i = 0; i < 3; i++){
    for (var j = 0; j < 3; j++){
      sum += kernel[j][i] * array[this.thread.x + i][this.thread.y + j]
    }
  }
  return sum;
}`;
  domLog("kernel funcs");
  const cpuMatMult = (arr1, arr2) => {
    let out = [];
    for (var i = 0; i < size; i++) {
      out.push([]);
      for (var j = 0; j < size; j++) {
        let sum = 0;
        for (var k = 0; k < size; k++) {
          sum += arr1[i][k] * arr2[k][j];
        }
        out[i][j] = sum;
      }
    }
    return out;
  };

  domLog("cpu mat mult func");
  const cpuConv = (array, ker) => {
    let out = [];
    for (var i = 1; i < size - 1; i++) {
      out.push([]);
      for (var j = 1; j < size - 1; j++) {
        let sum = 0;
        for (var k = 0; k < 3; k++) {
          for (var l = 0; l < 3; l++) {
            sum += array[i - 1 + k][j - 1 + l] * ker[l][k];
          }
        }
        out[i - 1][j - 1] = sum;
      }
    }
    return out;
  };
  domLog("cpu mat conv func");

  function generateMatrices() {
    let arr = [],
      arx = [];
    for (var i = 0; i < size; i++) {
      arr.push([]);
      arx.push([]);
      for (var j = 0; j < size; j++) {
        arr[i][j] = Math.random();
        arx[i][j] = Math.random();
      }
    }
    return [arr, arx];
  }
  domLog("mat gen func");

  const funcs = {
    gpuTexMatMult: gpu.createKernel(matMultFunc, {
      output: [size, size],
      outputToTexture: true
    }),
    gpuMatMult: gpu.createKernel(matMultFunc, {
      output: [size, size],
      outputToTexture: false
    }),
    gpujsCpuMatMult: cpu.createKernel(matMultFunc, { output: [size, size] }),
    cpuMatMult,

    gpuConv: gpu.createKernel(matConvFunc, {
      output: [size - 1, size - 1],
      outputToTexture: false
    }),
    gpuTexConv: gpu.createKernel(matConvFunc, {
      output: [size - 1, size - 1],
      outputToTexture: true
    }),
    gpujsCpuConv: cpu.createKernel(matConvFunc, {
      output: [size - 1, size - 1]
    }),
    cpuConv
  };
  domLog("funcs obj");

  function benchIt(func) {
    let time = -1 * performance.now();
    const ret = func();
    time += performance.now();
    time = Math.floor(time);
    return { time, ret };
  }
  domLog("bench func");

  let matMultBenches = {};
  let matConvBenches = {};

  const mat = benchIt(generateMatrices);
  matGen = mat.time;
  const matrices = mat.ret;
  domLog("mat gen bench");

  if (checkboxes.matMult) {
    if (checkboxes.gpuTex)
      matMultBenches.gpuTexCompilePerf = benchIt(function() {
        funcs.gpuTexMatMult.build(matrices[0], matrices[1]);
      }).time;

    if (checkboxes.gpu)
      matMultBenches.gpuCompilePerf = benchIt(function() {
        funcs.gpuMatMult.build(matrices[0], matrices[1]);
      }).time;

    if (checkboxes.gpujsCpu)
      matMultBenches.cpuCompilePerf = benchIt(function() {
        console.log('cpu', funcs.gpujsCpuMatMult.build(matrices[0], matrices[1]));
      }).time;

    if (checkboxes.gpu)
      matMultBenches.gpuPerform = benchIt(function() {
        console.log('gpu', funcs.gpuMatMult(matrices[0], matrices[1]));
      }).time;

    if (checkboxes.cpu)
      matMultBenches.cpuPerform = benchIt(function() {
        funcs.cpuMatMult(matrices[0], matrices[1]);
      }).time;

    if (checkboxes.gpujsCpu)
      matMultBenches.gpujsCpuPerform = benchIt(function() {
        funcs.gpujsCpuMatMult(matrices[0], matrices[1]);
      }).time;

    if (checkboxes.gpuTex)
      matMultBenches.gpuTexPerform = benchIt(function() {
        funcs.gpuTexMatMult(matrices[0], matrices[1]);
      }).time;
    domLog("mat mult benches");
  }

  // Convolution

  if (checkboxes.conv) {
    if (checkboxes.gpuTex)
      matConvBenches.gpuTexCompilePerf = benchIt(function() {
        funcs.gpuTexConv.build(matrices[0], kernel);
      }).time;

    if (checkboxes.gpu)
      matConvBenches.gpuCompilePerf = benchIt(function() {
        funcs.gpuConv.build(matrices[0], kernel);
      }).time;

    if (checkboxes.gpujsCpu)
      matConvBenches.cpuCompilePerf = benchIt(function() {
        funcs.gpujsCpuConv.build(matrices[0], kernel);
      }).time;
    domLog("cpu compile");
    if (checkboxes.gpu)
      matConvBenches.gpuPerform = benchIt(function() {
        funcs.gpuConv(matrices[0], kernel);
      }).time;

    if (checkboxes.cpu)
      matConvBenches.cpuPerform = benchIt(function() {
        funcs.cpuConv(matrices[0], kernel);
      }).time;

    if (checkboxes.gpujsCpu)
      matConvBenches.gpujsCpuPerform = benchIt(function() {
        funcs.gpujsCpuConv(matrices[0], kernel);
      }).time;

    if (checkboxes.gpuTex)
      matConvBenches.gpuTexPerform = benchIt(function() {
        funcs.gpuTexConv(matrices[0], kernel);
      }).time;
    domLog("mat conv benches");
  }

  $(".out").html(`
  <p><b>Matrix Gen Time</b>: ${matGen}ms</p>
  ${
    checkboxes.matMult
      ? `<h5>Matrix Multiplication</h5>
  <table class="striped centered">
    <thead>
      <tr>
        <th>Benchmark</th>
        <th>Time Taken</th>
        <th>Compile Time</th>
        <th>Total</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>CPU</td>
        <td>${
          checkboxes.cpu ? matMultBenches.cpuPerform + "ms" : "Not Benchmarked"
        }</td>
        <td>No Compilation Required</td>
        <td>${
          checkboxes.cpu ? matMultBenches.cpuPerform + "ms" : "Not Benchmarked"
        }
      </tr>
      <tr>
        <td>CPU(GPUjs)</td>
        <td>${
          checkboxes.gpujsCpu
            ? matMultBenches.gpujsCpuPerform + "ms"
            : "Not Benchmarked"
        }</td>
        <td>${
          checkboxes.gpujsCpu
            ? matMultBenches.cpuCompilePerf + "ms"
            : "Not Benchmarked"
        }</td>
        <td>${
          checkboxes.gpujsCpu
            ? matMultBenches.cpuCompilePerf +
              matMultBenches.gpujsCpuPerform +
              "ms"
            : "Not Benchmarked"
        }</td>
      </tr>
      <tr>
        <td>GPU</td>
        <td>${
          checkboxes.gpu ? matMultBenches.gpuPerform + "ms" : "Not Benchmarked"
        }</td>
        <td>${
          checkboxes.gpu
            ? matMultBenches.gpuCompilePerf + "ms"
            : "Not Benchmarked"
        }</td>
         <td>${
           checkboxes.gpu
             ? matMultBenches.gpuCompilePerf + matMultBenches.gpuPerform + "ms"
             : "Not Benchmarked"
         }</td>
      </tr>
      <tr>
        <td>GPU(Texture Mode)</td>
        <td>${
          checkboxes.gpuTex
            ? matMultBenches.gpuTexPerform + "ms"
            : "Not Benchmarked"
        }</td>
        <td>${
          checkboxes.gpuTex
            ? matMultBenches.gpuTexCompilePerf + "ms"
            : "Not Benchmarked"
        }</td>
        <td>${
          checkboxes.gpuTex
            ? matMultBenches.gpuTexCompilePerf +
              matMultBenches.gpuTexPerform +
              "ms"
            : "Not Benchmarked"
        }</td>
      </tr>
    </tbody>
  </table>`
      : ""
  }
${
    checkboxes.conv
      ? `<h5>Matrix Convolution</h5>
  <table class="striped centered">
    <thead>
      <tr>
        <th>Benchmark</th>
        <th>Time Taken</th>
        <th>Compile Time</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>CPU</td>
        <td>${
          checkboxes.cpu ? matConvBenches.cpuPerform + "ms" : "Not Benchmarked"
        }</td>
        <td>Not Applicable</td>
      </tr>
      <tr>
        <td>CPU(GPUjs)</td>
        <td>${
          checkboxes.gpujsCpu
            ? matConvBenches.gpujsCpuPerform + "ms"
            : "Not Benchmarked"
        }</td>
        <td>${
          checkboxes.gpujsCpu
            ? matConvBenches.gpuCompilePerf + "ms"
            : "Not Benchmarked"
        }</td>
      </tr>
      <tr>
        <td>GPU</td>
        <td>${
          checkboxes.gpu ? matConvBenches.gpuPerform + "ms" : "Not Benchmarked"
        }</td>
        <td>${
          checkboxes.gpu
            ? matConvBenches.gpuCompilePerf + "ms"
            : "Not Benchmarked"
        }</td>
      </tr>
      <tr>
        <td>GPU(Texture Mode)</td>
        <td>${
          checkboxes.gpuTex
            ? matConvBenches.gpuTexPerform + "ms"
            : "Not Benchmarked"
        }</td>
        <td>${
          checkboxes.gpuTex
            ? matConvBenches.gpuTexCompilePerf + "ms"
            : "Not Benchmarked"
        }</td>
      </tr>
    </tbody>
  </table>`
      : ""
  }
`);
  domLog("end");
});

              
            
!
999px

Console