Chapter 7. PI (3.14...) - Step By Step

In this document we show how to create a distributed application to compute the number PI using the ProActive Grid Middleware. Distributed programming is achieved using the ProActive deployment framework combined with the active object model.

7.1. Software Installation

7.1.1. Installing the Java Virtual Machine

  • Download and install the JDK 5.0 Update 9 from here.

  • Set the environment variable JAVA_HOME to the java installation location.

7.1.2. Download and install ProActive

Download and decompress ProActive from http://

7.2. Implementation

Go into the tutorial directory: ProActive/src/org/objectweb/proactive/examples/pi/. This directory contains:

config/            <-- Configuration directory
descriptors/       <-- Deployment descriptors directory
doc/               <-- Documentation directory
fractal/           <-- Component directory
scripts/           <-- Launch scripts directory
Interval.java      <-- The parameter passed to remote objects
PiBPP.java         <-- The main code
PiComputer.java    <-- The remote object code (worker)
Results.java       <-- The results returned by the workers
MyPi.java          <-- Base class to test Pi with ProActive

In this step by step we will implement our own version of PiBPP.java.

7.2.1. MyPi.java

Create the file MyPi.java inside the tutorial directory with initially the following content:

package org.objectweb.proactive.examples.pi;

import org.objectweb.proactive.ProActive;
import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor;
import org.objectweb.proactive.core.descriptor.data.VirtualNode;
import org.objectweb.proactive.core.group.ProActiveGroup;
import org.objectweb.proactive.core.node.Node;

class MyPi{

// global variables will go here

public static void main(String args[]) throws Exception{
  
  Integer numberOfDecimals =  new Integer(args[0]);
  String descriptorPath = args[1];

  // the main code will go here
} 

7.2.2. Add the Deployment Descriptor

Inside the main we add the code for acquiring the resources.

  ProActiveDescriptor descriptor = ProActive.getProactiveDescriptor(descriptorPath); //Parse the xml descriptor
  descriptor.activateMappings(); //Acquire the resources
  VirtualNode virtualNode = descriptor.getVirtualNode("computers-vn"); //Get the virtual node named "computers-vn"
  Node[] nodes = virtualNode.getNodes();

7.2.3. Instantiate The Remote Objects

  PiComputer piComputer = (PiComputer) ProActiveGroup.newGroupInParallel(
                          PiComputer.class.getName(),
                          new Object[] { numberOfDecimals }, 
                          nodes);

  int numberOfWorkers = ProActiveGroup.getGroup(piComputer).size();

7.2.4. Divide, Compute and Conquer

  Interval intervals = PiUtil.dividePI(numberOfWorkers, numberOfDecimals.intValue());
  ProActiveGroup.setScatterGroup(intervals);

  Result results = piComputer.compute(intervals);

  Result result= PiUtil.conquerPI(results);
  System.out.println("Pi:"+result);

7.2.5. Clean up

  descriptor.killall(true);
  System.exit(0);

7.2.6. Executing the application

pi$ cd scripts
scripts$ ./build mypi -Ddecimals=100 -Ddescriptor=../descriptors/localhost.xml

7.3. Putting it all together

package org.objectweb.proactive.examples.pi;

import org.objectweb.proactive.ProActive;
import org.objectweb.proactive.core.descriptor.data.ProActiveDescriptor;
import org.objectweb.proactive.core.descriptor.data.VirtualNode;
import org.objectweb.proactive.core.group.ProActiveGroup;
import org.objectweb.proactive.core.node.Node;

public class MyPi {

  public static void main(String args[]) throws Exception{

    Integer numberOfDecimals =  new Integer(args[0]);
    String descriptorPath = args[1];
    
    ProActiveDescriptor descriptor = ProActive.getProactiveDescriptor(descriptorPath); 
    descriptor.activateMappings();
    VirtualNode virtualNode = descriptor.getVirtualNode("computers-vn");
    Node[] nodes = virtualNode.getNodes();
    
    PiComputer piComputer = (PiComputer) ProActiveGroup.newGroupInParallel(
                            PiComputer.class.getName(),
                            new Object[] { numberOfDecimals },
                            nodes);
    
    int numberOfWorkers = ProActiveGroup.getGroup(piComputer).size();
    
    Interval intervals = PiUtil.dividePI(numberOfWorkers, numberOfDecimals.intValue());
    ProActiveGroup.setScatterGroup(intervals);

    Result results = piComputer.compute(intervals);

    Result result= PiUtil.conquerPI(results);
    System.out.println("Pi:"+result);
    
    descriptor.killall(true);
    System.exit(0);
  }
}