Name: _____________________________ 6 Digit StudentID: ___ ___ ___ ___ ___ ___

Worksheet 23: Phaser

In the code below, the phased_join_void_fork_loop creates tasks for the letters "A" and "B". The phaser passed to each task has an arriveAndAwaitAdvance method inspired by X10's next construct.

Write equivalent pseudeocode for the code below using non-phased join_void_fork_loops:

That is: use however many standard join_void_fork_loops you need to produce an equivalent parallel program. Do not use phased_join_void_fork_loop or arriveAndAwaitAdvance.

x10.phased_join_void_fork_loop(Arrays.asList("A", "B"), (phaser, letter) -> {
    for (int i : Arrays.asList(1, 2)) {
        System.out.println("enter " + letter + i);
        phaser.arriveAndAwaitAdvance();
        System.out.println("leave " + letter + i);
    }
});

 

 

 

 

2 Ranges: What is wrong with this code and how would you fix it?

Range[] slices = Ranges.slice(0, input.length, TASK_COUNT);
join_void_fork_loop(slices, (slice) -> {
    join_void_fork_loop(slice.min(), slice.maxExclusive(), (i) -> {
        process(i);
    });
});

3 Locks: What is wrong with this code and how would you fix it?

Task Maria Task Frances
// ti
synchronized(jam) {
    synchronized(bread) {
        // critical section     
    }
}
// breakfast, lunch, snack, dinner
synchronized(bread) {
    synchronized(jam) {
        // critical section     
    }
}

 

 

 

Post Lecture

Synthesize today's class session

 

 

What is unclear?