Sieve of Eratosthenes example

Description

This program uses the Sieve of Eratosthenes algorithm to find prime numbers. Numbers of increasing size are tested if dividible by any previously found prime number. If this is not the case, the number is a new prime number.
Active objects are used to store the prime numbers and to make the tests, which allows to distribute the calculation on different machines.

Run it!

Scripts

Unix ProActive/scripts/unix/ eratosthenes.sh [-nogui] [XML deployment descriptor file]
Windows ProActive/scripts/windows/ eratosthenes.bat [-nogui] [XML deployment descriptor file]

All active objects except Main are migratable using IC2D. You can use this to explore the different latency times implied by calls to Nodes on other VMs or hosts. Pause the application and wait for all containers to finish their current work before migrating.

The parameter -nogui starts the calculation immediately without opening a control window.

Screenshot of the interface

The control window has two buttons:

Prime number output is done on the console where you started the application, or on the node where the ConsolePrimeOutputListener is located if you migrated it.

Design

The program stores the prime numbers in a linked list of passive PrimeNumberImpl objects that are controlled by active containers. Each PrimeNumberImpl object has a pointer to the next PrimeNumber, which may be a passive object in the same container, the next container or null, if a bigger prime number has not yet been found.
Testing requests travel the chain of objects inside a container until a divisor is found or the end of the container is reached and the request is automatically being handed over to the next container. If the end of the chain is reached, a new prime number has been found. FIFO serving is required for the active objects in order to ensure that a number is tested with all smaller prime numbers before being declared a new prime number.
The source that enumerates the numbers, the result output handler and the creator of new containers are active objects too.
ActivePrimeContainers have a link towards the previous container, so that they can slow it down if they are overcharged with requests.

The program can be started on a single node or on VirtualNodes described by an XML deployment descriptor. The number source is created on a node described by NumberSource, the output handler on a node described by OutputListener and the active containers in the node(s) described by Containers. Four succeeding ActivePrimeContainer objects are created on a node before continuing with the next node in order to limit network traffic to a reasonable amount. An example of a descriptor is given in descriptors/Eratosthenes.xml.

As a lot of method calls on active objects are produced in this example, requests accumulating in the request queues of the active objects can be observed using IC2D.

Interfaces

public interface PrimeNumber public interface PrimeOutputListener public interface ActivePrimeContainerCreator public interface Slowable

Active Objects

public class Main implements ActivePrimeContainerCreator, InitActive public class ActivePrimeContainer implements PrimeNumber, Slowable, RunActive public class NumberSource implements Slowable, RunActive public class ConsolePrimeOutputListener implements PrimeOutputListener

Object Graph

Benchmark

To make a basic test about the influence of distributed calculation, the program was run in a cluster environment with two different settings:

The time needed to find a certain number of prime numbers is shown below:

Prime number found Value Time:
1 CPU
Time:
8 CPU
2000th 7919 7s 19s
10000th 104729 111s 75s
20000th 224737 494s 187s

The difference between the single CPU and the multiple CPU setting shows the cost of inter-JVM-communication. With increasing numbers the distributed calculation profits from its CPU power, although the communication between the machines has to be done.

Full Sources

Source code index

 

The Eratosthenes example was written by Jonathan Streit.


Feel free to send any suggestions to proactive@objectweb.org © 2001-2007 Inria Sophia Antipolis