Times two

No Node? No problem. DCP also supports vanilla JavaScript in the browser.

This application demonstrates how to multiply values by 2 in a distributed manner using the Distributed Compute Protocol (DCP). If you have a good understanding of how DCP works and the terminology already please feel free to skip the refresher and jump straight to the set up.

A refresher

Click to see the refresher

DCP is a distributed computing framework made from web-based technology.

Computers and devices in classrooms, computer labs, households, and enterprises are turned into computing clusters with a single click with DCP. The entire workload, a job, is sub-divided into smaller parts, called slices. Slices are executed in parallel using DCP. Each slice of the job will correspond to a value in an array that we wish to sort. Slices will be sent to different Workers (for example, laptops, desktops, cell phones, etc.) which contain sandboxes. A sandbox is a clean environment on your Worker that runs your work function.

DCP is a powerful parallel computing framework that allows users to express a computational job as effortlessly as:

job = compute.for(inputSet, workFunction)

resultSet = await job.exec()

A workFunction is mapped onto each element in an inputSet. Each mapping represents a slice. Slices are distributed across DCP networks for computation. Results are returned to the user from DCP networks coherently.

Remember, the work function must contain progress();. Progress is a call made to tell the scheduler that the job is still alive and running. Progress is considered the heart beat of the job, without it the job would die and no results would be returned.

  1. Create compute nodes: go to https://dcp.work on as many devices as you want and click Start to join the public compute group, or to dcp.work/joinKey if you have a private compute group joinKey and joinSecret.

  2. Configure dev environment: load dcp-client and any required packages.

  3. Specify the inputSet: an arbitrary, but enumerable input dataset (parameters, mp3 files, images, blender project file, etc)

  4. Specify the workFunction: an arbitrary work function (physics simulation, inference model, rendering process, etc)

  5. Express the job: Map the workFunction onto the inputSet with job = compute.for(inputSet, workFunction)

  6. (optional) Specify a compute group with job.computeGroups=[{joinKey: name, joinSecret: password}]

  7. Await the resultSet: Execute the job in parallel on DCP via resultSet = await job.exec()

Here is a small example of using DCP in a single HTML file. Script tags encapsulate all the JavaScript code. No module system is needed.

First, insert a script element to make the dcp-client library visible in order for your job to run. Here, specifying the src as https://scheduler.distributed.computer/dcp-client/dcp-client.js imports the latest version of the library, but you can also use the path to your dcp-client npm package.

<script src="https://scheduler.distributed.computer/dcp-client/dcp-client.js"></script>

Next, the functions that perform the job are contained in another script element.

<script>
  /**
   * This program doubles numbers in an array
   */
  const { compute } = dcp;
  const $ = document.querySelector.bind(document);

  function workFn(num) {
    progress();
    return num * 2;
  }

  async function go() {
    const inputSet = [1, 2, 3];
    const job = compute.for(inputSet, workFn);

    // Not mandatory console logs for status updates
    job.on('accepted', () => {
      console.log(` - Job accepted with id: ${job.id}`);
    });
    job.on('result', (ev) => {
      console.log(Object.entries(ev).join(' ').replace(/,/g, ': '));
    });

    const resultSet = await job.exec();
    console.log(`results: ${Array.from(resultSet)}`);
    console.log(' - Job Complete');
  }

  go();
</script>

Loading an HTML page with these script tags prompts you for a default.keystore. Doing so invokes the go() function which logs distributed results in the console.