This tutorial gives a step by step narrative for creating an example application. The application built throughout this section is a pi calculator that uses the dartboard algorithm.

In this section the following is explained:

Creating Task class

The very first step is defining a task. A task represents the code that will run. User defined tasks must derive from Task. The most important aspect of writing the Task class is overriding the execute() method. This Execute method is the entry point into the Task.

For this pi calculator, the Execute method of the task contains the logic of the dartboard algorithm. Here is the code:

Java  Copy imageCopy
public static class PiDartboardAlgorithmTask extends Task {
    private PiDartboardAlgorithmTask() {}

    private int dartsPerTask;

    public PiDartboardAlgorithmTask(int dartsPerTask) {
        this.dartsPerTask = dartsPerTask;
    }

    @Override
    public void execute() {
        Random random = new Random(System.currentTimeMillis());
        int numDartsInCircle = 0;
        for (int i = 0; i < dartsPerTask; i++) {
            double x = (random.nextDouble() - 0.5) * 2;
            double y = (random.nextDouble() - 0.5) * 2;
            if ((x * x) + (y * y) <= 1.0) {
                numDartsInCircle++;
            }
        }
        this.setResult(numDartsInCircle);
    }
}

Connect to the job scheduler

Connecting to the Coordinator consists of one line. The hostname is the DNS name or IP address of the Coordinator. If the coordinator is not running on the default port (9090), also specify the port number. The connect() method must be called before any of the operations on the coordinator can be performed.

Java  Copy imageCopy
IJobScheduler scheduler = new ClusterJobScheduler("localhost");
scheduler.connect();

Once Connect is successfully called, call dispose() on the client. Otherwise, unnecessary coordinator resources will be consumed and the application may not exit properly.

Note

If an exception is thrown at the Connect method and says something like "Cannot connect to Coordinator", the most likely reason is that the Coordinator is not running on the specified machine and port. Make sure coordinator is running on that machine and confirm a firewall is not blocking the port.

Submit Job

After connecting to the coordinator, specify tasks to submit. First, create a Job using createJob(). Next, instantiate the Task(s) written above. Add the tasks to the Job using addTask(agi.parallel.infrastructure.Task).

For the pi calculator, add multiple tasks to the job to get more samples. When finished adding tasks, the submit() method will actually submit the job to the coordinator.

Note

The are also many options on the job that you could specify. For this tutorial, you will just use two of the options, Name and Description.

The pi calculator submit code should look like this:

Java  Copy imageCopy
int numDartsPerTask = 1000000;
int numTasks = 8;
Job job = scheduler.createJob();
job.setName("PiJob");
job.setDescription("Computes digits of PI using dartboard algorithm.");
for (int i = 0; i < numTasks; i++) {
    job.addTask(new PiDartboardAlgorithmTask(numDartsPerTask));
}
job.submit();

Waiting for task results

Calling waitUntilDone() will block the current thread until all of the job's tasks are completed. Once the job is completed, the result of each task is sent back to the client. One of the results you can get from the task is from the Result property.

For the pi calculator, you want to sum up the task's result. The WaitUntilDone code will look like this:

Java  Copy imageCopy
job.waitUntilDone();
int sumOfDarts = 0;
for (int i = 0; i < numTasks; i++) {
    sumOfDarts += (Integer) job.getTasks().get(i).getResult();
}

System.out.format("PI is approximately %.15s", (4.0 * (double) sumOfDarts) / (numDartsPerTask * numTasks));

// Make sure you Dispose the scheduler after you are done.
scheduler.dispose();

The complete application

Here is the complete application after all the steps are applied:

Java  Copy imageCopy
package stkscalabilitysdk;


import agi.parallel.client.ClusterJobScheduler;
import agi.parallel.client.IJobScheduler;
import agi.parallel.client.Job;
import agi.parallel.infrastructure.Task;

import java.util.Random;


public class PiCalculatorTutorial {
    public static void main(String[] args) {
        IJobScheduler scheduler = new ClusterJobScheduler("localhost");
        scheduler.connect();

        int numDartsPerTask = 1000000;
        int numTasks = 8;
        Job job = scheduler.createJob();
        job.setName("PiJob");
        job.setDescription("Computes digits of PI using dartboard algorithm.");
        for (int i = 0; i < numTasks; i++) {
            job.addTask(new PiDartboardAlgorithmTask(numDartsPerTask));
        }
        job.submit();

        job.waitUntilDone();
        int sumOfDarts = 0;
        for (int i = 0; i < numTasks; i++) {
            sumOfDarts += (Integer) job.getTasks().get(i).getResult();
        }

        System.out.format("PI is approximately %.15s", (4.0 * (double) sumOfDarts) / (numDartsPerTask * numTasks));

        // Make sure you Dispose the scheduler after you are done.
        scheduler.dispose();
    }

    public static class PiDartboardAlgorithmTask extends Task {
        private PiDartboardAlgorithmTask() {}

        private int dartsPerTask;

        public PiDartboardAlgorithmTask(int dartsPerTask) {
            this.dartsPerTask = dartsPerTask;
        }

        @Override
        public void execute() {
            Random random = new Random(System.currentTimeMillis());
            int numDartsInCircle = 0;
            for (int i = 0; i < dartsPerTask; i++) {
                double x = (random.nextDouble() - 0.5) * 2;
                double y = (random.nextDouble() - 0.5) * 2;
                if ((x * x) + (y * y) <= 1.0) {
                    numDartsInCircle++;
                }
            }
            this.setResult(numDartsInCircle);
        }
    }
}

Usage

Setup project:

  Copy imageCopy
mkdir lib
mkdir classes

Copy agi.parallel.client-1.1.0.jar to the lib directory. For instance:

  Copy imageCopy
copy [PathToSTKScalabilityApiJavaFolder]\Jars\agi.parallel.client-1.1.0.jar lib

Compile PiCalculatorTutorial.java:

  Copy imageCopy
javac -cp lib\agi.parallel.client-1.1.0.jar -d classes stkscalabilitysdk\PiCalculatorTutorial.java

Create a JAR:

  Copy imageCopy
jar cvf PiCalculatorTutorial.jar -C classes/ .

Run the application:

  Copy imageCopy
java -cp "lib\agi.parallel.client-1.1.0.jar;PiCalculatorTutorial.jar" stkscalabilitysdk.PiCalculatorTutorial

Output:

  Copy imageCopy
pi is approximately 3.1423015

Next Steps

With an example application completed, explore other concepts at Key concepts or review the Library Reference.

See Also

Other Resources