Counting, sorting, and distributed coordination
Prev: Stacks and elimination Next: Concurrent hashing and natural parallelism
Exercises
Exercise 12.1. Prove Lemma 12.5.1.
Exercise 12.2. Implement a trinary CombiningTree, that is, one that
allows up to three threads coming from three subtrees to combine at a
given node. Can you estimate the advantages and disadvantages of such a
tree when compared with a binary combining tree?
Exercise 12.3. Implement a CombiningTree using Exchanger objects to
perform the coordination among threads ascending and descending the
tree. What are the possible disadvantages of your construction when
compared to the CombiningTree class presented in Section 12.3?
Exercise 12.4. Implement the cyclic array-based shared pool described
in Section 12.2 using two simple counters and a ReentrantLock per
array entry.
Exercise 12.5. Provide an efficient lock-free implementation of a
Balancer.
Exercise 12.6. (Hard) Provide an efficient wait-free implementation of
a Balancer (i.e., not by using the universal construction).
Exercise 12.7. Prove that the TREE[2^k] balancing network
constructed in Section 12.6 is a counting network, that is, that in any
quiescent state, the sequences of tokens on its output wires have the
step property.
Exercise 12.8. Let be a width- balancing network of depth in a quiescent state . Let . Prove that if tokens enter the network on the same wire, pass through the network, and exit, then will have the same state after the tokens exit as it did before they entered.
Exercise 12.9. Let and be -smooth sequences of length . A matching layer of balancers for and is one where each element of is joined by a balancer to an element of in a one-to-one correspondence.
Prove that if and are each -smooth and is the result of matching and , then is -smooth.
Exercise 12.10. Consider a BLOCK[k] network in which each balancer
has been initialized to an arbitrary state (either up or down). Show
that no matter what the input distribution is, the output distribution
is -smooth.
Hint: You may use the claim in Exercise 12.9.
Exercise 12.11. A smoothing network is a balancing network that ensures that in any quiescent state, the output sequence is -smooth. Counting networks are smoothing networks, but not vice versa.
A Boolean sorting network is one in which all inputs are guaranteed to be Boolean. Define a pseudosorting balancing network to be a balancing network with a layout isomorphic to a Boolean sorting network.
Let be the balancing network constructed by taking a smoothing network of width , taking a pseudosorting balancing network also of width , and joining the th output wire of to the th input wire of .
Show that is a counting network.
Exercise 12.12. A 3-balancer is a balancer with three input lines and
three output lines. Like its 2-line relative, its output sequences
have the step property in any quiescent state. Construct a depth-
counting network with six input and output lines from 2-balancers and
3-balancers. Explain why it works.
Exercise 12.13. Suggest ways to modify the BitonicSort class so that
it will sort an input array of width , where is not a power
of .
Exercise 12.14. Consider the following -thread counting algorithm. Each thread first uses a bitonic counting network of width to take a counter value . It then goes through a waiting filter, in which each thread waits for threads with lower values to catch up.
The waiting filter is an array filter[] of Boolean values.
Define the phase function
.
A thread that exits with value spins on
filter[(v - 1) mod w] until that value is set to .
The thread responds by setting filter[v mod w] to , and
then returns .
- Explain why this counter implementation is linearizable.
- An exercise here shows that any linearizable counting network has
depth at least . Explain why the
filter[]construction does not contradict this claim. - On a bus-based multiprocessor, would this
filter[]construction have better throughput than a single variable protected by a spin lock? Explain.
Exercise 12.15. If a sequence is -smooth, then the result of passing through a balancing network is -smooth.
Exercise 12.16. Prove that the BITONIC[w] network has depth
and uses
balancers.
Exercise 12.17. Show that the OddEven network in Fig. 12.28 is a
sorting network but not a counting network.
Exercise 12.18. Can counting networks do anything besides increments?
Consider a new kind of token, called an antitoken, which we use for
decrements. Recall that when a token visits a balancer, it executes
getAndComplement(): It atomically reads the toggle value and
complements it, and then departs on the output wire indicated by the
old toggle value. Instead, an antitoken complements the toggle value,
and then departs on the output wire indicated by the new toggle value.
Informally, an antitoken “cancels” the effect of the most recent token
on the balancer’s toggle state, and vice versa.
public synchronized int antiTraverse() {
try {
if (toggle) {
return 1;
} else {
return 0;
}
} finally {
toggle = !toggle;
}
}Instead of simply balancing the number of tokens that emerge on each
wire, we assign a weight of +1 to each token and -1 to each
antitoken. We generalize the step property to require that the sums of
the weights of the tokens and antitokens that emerge on each wire have
the step property. We call this property the weighted step property.
Figure 12.30 shows an antiTraverse() method that moves an antitoken
through a balancer. Other networks would need different
antiTraverse() methods.
Let be a width- balancing network of depth in a quiescent state . Let . Show that if tokens enter the network on the same wire, pass through the network, and exit, then will have the same state after the tokens exit as it did before they entered.
Exercise 12.19. Let be a balancing network in a quiescent state , and suppose a token enters on wire and passes through the network, leaving the network in state . Show that if an antitoken now enters on wire and passes through the network, then the network goes back to state .
Exercise 12.20. Show that if balancing network is a counting network for tokens alone, then it is also a balancing network for tokens and antitokens.
Exercise 12.21. A switching network is a directed graph, where edges are called wires and nodes are called switches. Each thread shepherds a token through the network. Switches and tokens are allowed to have internal states. A token arrives at a switch via an input wire. In one atomic step, the switch absorbs the token, changes its state and possibly the token’s state, and emits the token on an output wire. For simplicity, switches have two input and output wires. Note that switching networks are more powerful than balancing networks, since switches can have arbitrary state instead of a single bit, and tokens also have state.
An adding network is a switching network that allows threads to add or subtract arbitrary values.
We say that a token is in front of a switch if it is on one of the switch’s input wires. Start with the network in a quiescent state , where the next token to run will take value . Imagine we have one token of weight and tokens all of weight , where , each on a distinct input wire. Denote by the set of switches that traverses if it traverses the network by starting in .
Prove that if we run the one at a time through the network, we can halt each in front of a switch of .
At the end of this construction, tokens are in front of
switches of . Since switches have two input wires, it follows that
‘s path through the network encompasses at least
switches, so any adding network must have depth at least ,
where is the maximum number of concurrent tokens. This bound is
discouraging because it implies that the size of the network depends on
the number of threads, also true for CombiningTrees but not counting
networks, and that the network has inherently high latency.
Exercise 12.22. Extend the proof of Exercise 12.21 to show that a linearizable counting network has depth at least .
Prev: Stacks and elimination Next: Concurrent hashing and natural parallelism