Dashboard > GridGain User Guide > Table Of Contents > Examples Gallery > MapReduce with Prime Numbers > GridPrimeExample.java
GridPrimeExample.java
Added by ghost, last edited by ghost on Feb 04, 2008  (view change)
Labels: 
(None)


Main bootstrap class for API-based Prime Number calculation on the grid. This class starts up GridGain Grid and executes GridPrimeTask.java for every number we want to check for prime.

Refer to GridPrimeTask.java for documentation on how prime numbers are calculated.

Starting Remote Nodes.

To try this example you should (but don't have to) start remote grid instances. You can start as many as you like by executing the following script:
GRIDGAIN_HOME/bin/gridgain.bat|sh

Once remote instances are started, you can execute this example from Eclipse, Idea, or NetBeans (or any other IDE) by simply hitting run button. You will witness that all nodes discover each other and all of the nodes will participate in task execution (check node output).

Note that when running this example on a multi-core box, simply starting additional grid node on the same box will speed up prime number calculation by a factor of 2.

XML Configuration

If no specific configuration is provided, GridGain will start with all defaults. For information about GridGain default configuration refer to GridFactory Javadoc documentation. If you would like to try out different configurations you should pass a path to Spring configuration file as 1st command line argument into this example. The path can be relative to GRIGAIN_HOME environment variable. You should also pass the same configuration file to all other grid nodes by executing startup script as follows (you will need to change the actual file name):
GRIDGAIN_HOME/bin/gridgain.bat|sh examples/config/specific-config-file.xml

GridGain examples come with multiple configuration files you can try. All configuration files are located under GRIDGAIN_HOME/examples/config folder. You are free to try any of these configurations, but whenever using 3rd party configurations, such as JBoss JMS, ActiveMQ JMS, Sun MQ JMS, or GigaSpaces, make sure to download these respective products and include all the necessary libraries into classpath at node startup. All these libraries are already specified in commented format in GRIDGAIN_HOME/bin/setenv.bat|sh files which get executed automatically by GridGain startup scripts. You can simply uncomment the necessary classpath portions as you need.

Full Source Code

GridPrimeExample.java
package org.gridgain.examples.primenumbers.api;

import java.util.*;
import org.gridgain.grid.*;

public final class GridPrimeExample {
    /**
     * Enforces singleton.
     */
    private GridPrimeExample() {
        // No-op.
    }
    
    /**
     * Starts up grid and checks all provided values for prime.
     *
     * @param args Command line arguments, none required but if provided
     *      first one should point to the Spring XML configuration file. See
     *      <tt>"examples/config/"</tt> for configuration file examples.
     * @throws GridException If example execution failed.
     */
    public static void main(String[] args) throws GridException {
        Grid grid = null;

        // Start grid.
        if (args.length == 0) {
            grid = GridFactory.start();
        }
        else {
            grid = GridFactory.start(args[0]);
        }

        // Values we want to check for prime.
        long[] checkVals = { 32452841, 32452843, 32452847, 32452849, 236887699, 217645199 };

        System.out.println(">>>");
        System.out.println(">>> Starting to check the following numbers for primes: " + Arrays.toString(checkVals));

        try {
            long start = System.currentTimeMillis();

            for (long checkVal : checkVals) {
                // Execute grid task to check a specific value for prime.
                // Find any divisor. If the divisor is null,
                // then the number is prime.
                Long divisor = grid.execute(GridPrimeTask.class, checkVal).get();

                if (divisor == null) {
                    System.out.println(">>> Value '" + checkVal + "' is a prime number");
                }
                else {
                    System.out.println(">>> Value '" + checkVal + "' is divisible by '" + divisor + '\'');
                }
            }

            long totalTime = System.currentTimeMillis() - start;

            System.out.println(">>> Total time to calculate all primes (milliseconds): " + totalTime);
            System.out.println(">>>");
        }
        finally {
            // Stop grid.
            GridFactory.stop(grid.getName(), true);
        }
    }
}

Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.10 Build:#528 Nov 29, 2006) - Bug/feature request - Contact Administrators