Tutorial

English

Français

Explicit Parallelism

This example shows how to express parallel components in a reactive program using the merge primitive.

Source: Parallelism.java
import inria.meije.rc.sugarcubes.*;

//In this example one dynamicaly adds a new copy of a program while
//the machine is running.

class Parallelism
{
    public static void main(String[] args){
        Program p = SC.seq(
                           SC.print("Hello World! 1\n")
                           ,SC.stop()
                           ,SC.print("Hello World! 2\n")
                           ,SC.stop()
                           ,SC.print("Hello World! 3\n")
                           ,SC.stop()
                           ,SC.print("Hello World! 4\n")
                           ,SC.stop()
                           );
        // One adds a program into a machine which uses 3 copies of
        // the program p in parallel. But the last one is delayed by
        // one instant due to a sequence with stop.
        Machine machine = SC.machine(SC.merge(SC.merge(p,p.getCopy()),SC.seq(SC.stop(),p.getCopy())));
        for(int i = 1; i <= 10; i++){
            System.out.println("instant "+i+": ");
            machine.react();
        }
    }
}
The merge instruction is a binary operator. It takes two reactive program and make them two to progress for one instant at each instant. It simulates parallelism by interleaving the execution of the branches during one instant. The semantics of the merge instruction can be more precisely considered : during an instant it always executes the first branch until this one suspends its execution then it gives the control to the other branch, and do this cyclically until both branches reach the end of the instant.
SC.merge(p,p.getCopy())
To combine multiple branch in parallel one builds a tree of multiple merge instructions.

How to compile?

(Instructions provided for a UNIX system using JDK 1.1.x or higher version number).
javac -classpath SugarCubesv3.1.0.jar Parallelism.java

How to execute?

java -classpath SugarCubesv3.1.0.jar:. Parallelism

You should get the following result:

instant 1: 
Hello World! 1
Hello World! 1
instant 2: 
Hello World! 2
Hello World! 2
Hello World! 1
instant 3: 
Hello World! 3
Hello World! 3
Hello World! 2
instant 4: 
Hello World! 4
Hello World! 4
Hello World! 3
instant 5: 
Hello World! 4
instant 6: 
instant 7: 
instant 8: 
instant 9: 
instant 10: 


The Junior API

Source: Junior_Parallelism.java
import junior.*;

class Junior_Parallelism
{
    public static void main(String[] args){
        Program p = Jr.Seq(Jr.Print("Hello World! 1\n"),Jr.Stop());
        p = Jr.Seq(p,Jr.Seq(Jr.Print("Hello World! 2\n"),Jr.Stop()));
        p = Jr.Seq(p,Jr.Seq(Jr.Print("Hello World! 3\n"),Jr.Stop()));
        p = Jr.Seq(p,Jr.Seq(Jr.Print("Hello World! 4\n"),Jr.Stop()));

        Machine machine = Jr.Machine(Jr.Par(Jr.Par(p,p.copy()),Jr.Seq(Jr.Stop(),p.copy())));
        for(int i = 1; i <= 10; i++){
            System.out.println("instant "+i+": ");
            machine.react();
        }
    }
}

How to compile?

(Instructions provided for a UNIX system using JDK 1.1.x or higher version number).
javac -classpath SugarCubesv3.1.0.jar Junior_Parallelism.java

How to execute?

java -classpath SugarCubesv3.1.0.jar:. Junior_Parallelism