Giter Site home page Giter Site logo

mcanthony / cl.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from toaarnio/cl.js

1.0 2.0 0.0 760 KB

A lightweight middleware library for WebCL.

Home Page: http://toaarnio.github.io/CL.js

JavaScript 66.22% Makefile 1.04% CSS 32.48% Shell 0.26%

cl.js's Introduction

CL.js

CL.js is a lightweight utility library for WebCL. It relieves the developer from writing most of the boilerplate code that is otherwise required to get anything running, while preserving the look and feel of WebCL and providing easy access to the underlying raw API. See http://toaarnio.github.io/CL.js for documentation.

Please visit http://webcl.nokiaresearch.com for demos, tutorials, and more information on WebCL.

Example

To get started, we need to include CL.js from our HTML source:

<script type="text/javascript" src="/path/to/CL.js"></script>

Now, let's initialize the global CL object and populate it with information about the WebCL platforms and devices that are available in this system:

CL.setup({ debug: true });

With debug mode enabled, any exceptions will be reported on the console together with helpful information about the function and arguments that triggered the exception. Next, let's create the CL resources that we're going to need in this example:

var src = CL.loadSource('kernels/random.cl');
for (var d=0; d < CL.devices.length; d++) {
  var ctx = CL.createContext({ device: CL.devices[d], name: 'device'+d });
  var buffer1M = ctx.createBuffer({ size: 1024*1024, name: 'results' });
  var kernel = ctx.buildKernel({ source: src });
  kernel.setArgs(buffer1M);
  ctx.createCommandQueue({ name: 'theQueue' });
}

Note that we assigned a plain-text name for each of our contexts, buffers and queues. This makes it easy to find the resources that we need later on. The Kernel object that we created for each Context is assigned the same name as the kernel function in random.cl. For the purposes of this example, let's assume that the kernel function is called generateRandomNumbers. Now, let's proceed to run it on each Device in turn, reading back the results into an ArrayBuffer:

var randomNumbers = new Uint8Array(1024*1024);
for (var d=0; d < CL.devices.length; d++) {
   var ctx = CL.getContext('device'+d);
   var queue = ctx.getQueue('theQueue');
   var kernel = ctx.getKernel('generateRandomNumbers');
   var buffer1M = ctx.getBuffer('results');
   queue.enqueueKernel(kernel, [randomNumbers.length]);
   queue.enqueueReadBuffer(buffer1M, randomNumbers);
   queue.finish();
   /* Do something with randomNumbers */
}

cl.js's People

Stargazers

Michael Anthony avatar

Watchers

Michael Anthony avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.