toUpperCase

A plain node example of using distributed computing to capitalize a string:

First, sign up for a DCP account at https://portal.distributed.computer and download your id.keystore and default.keystore associated with your account. For more information on how to do that, refer to this tutorial.

In this example, the DCP app takes a string, splits it into characters, sends each character to a worker who capitalizes it, and reports the results. You wouldn’t distribute such a small task often, but it helps this example show DCP-specific code, without worrying about app details.

Setup

Create a directory for your project, and download the DCP packages.

npm i dcp-client

Next, make a JavaScript file and create the asynchronous main function. The program requires the dcp-client module to initialize access to the Compute API.

async function main() {
  const compute = require('dcp/compute');
  // Rest of the code will go in the following sections:
  /* INPUT DATA */

  /* WORK FUNCTION */

  /* COMPUTE FOR */

  /* PROCESS RESULTS */
}
require('dcp-client').init('https://scheduler.distributed.computer').then(main);

Input data

In this example, the input set is an array of characters, taken from the input string "yelling!".

/* INPUT SET */
const inputSet = Array.from('yelling!');

Work function

The work function is what each worker runs. In this case, each worker takes one input character, and turn it to upper case.

/* WORK FUNCTION */
async function workFunction(letter) {
  progress();
  return letter.toUpperCase();
}

Compute for

Now set up the DCP job. DCP sends each value from the inputSet to a sandbox. The work function capitalizes that input.

/* COMPUTE FOR */
const job = compute.for(inputSet, workFn);
job.public.name = 'toUpperCase';

Compute group

If you don’t need a compute group, skip this section. If you do, enter this line with the key information to join that group.

// SKIP IF: you do not need a compute group
job.computeGroups = [{ joinKey: 'Your Key', joinSecret: 'Your Key' }];

Process results

Next, await the results. This is an array of capitalized characters. Put them back into one string and print them.

/* PROCESS RESULTS */
let resultSet = await job.exec();
resultSet = Array.from(resultSet);
console.log(resultSet.toString().replace(',', ''));

Run it

Run node your-file.js, and watch as your work distributes.

Full code

Click to see full code
async function main() {
  const compute = require('dcp/compute');

  /* INPUT SET */
  const inputSet = Array.from('yelling!');

  /* WORK FUNCTION */
  async function workFn(letter) {
    progress();
    return letter.toUpperCase();
  }

  /* COMPUTE FOR */
  const job = compute.for(inputSet, workFn);
  job.public.name = 'toUpperCompute';

  // SKIP IF: you do not need a compute group
  // job.computeGroups = [{ joinKey: 'KEY', joinSecret: 'SECRET' }];

  /* PROCESS RESULTS */
  let resultSet = await job.exec();
  resultSet = Array.from(resultSet);
  console.log(resultSet.toString().replace(',', ''));
}

require('dcp-client').init('https://scheduler.distributed.computer').then(main);