






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







High-level classification of barriers Hardware and software barriers Will focus on two types of software barriers .Centralized barrier: every processor polls a single count Distributed tree barrier: shows much better scalability Performance goals of a barrier implementation Low latency: after all processors have arrived at the barrier, they should be able to leave quickly Low traffic: minimize bus transaction and contention Scalability: latency and traffic should scale slowly with the number of processors Low storage: barrier state should not be big Fairness: Preserve some strict order of barrier exit (could be FIFO according to arrival order); a particular processor should not always be the last one to exit
struct bar_type { int counter; struct lock_type lock; int flag = 0; } bar_name;
BARINIT (bar_name) { LOCKINIT(bar_name.lock); bar_name.counter = 0; }
BARRIER (bar_name, P) { int my_count; LOCK (bar_name.lock); if (!bar_name.counter) { bar_name.flag = 0; /* first one / } my_count = ++bar_name.counter; UNLOCK (bar_name.lock); if (my_count == P) { bar_name.counter = 0; bar_name.flag = 1; / last one */ } else { while (!bar_name.flag); } }
How fast is it? Assume that the program is perfectly balanced and hence all processors arrive at the barrier at the same time Latency is proportional to P due to the critical section (assume that the lock algorithm exhibits at most O(P) latency) The amount of traffic of acquire section (the CS) depends on the lock algorithm; after everyone has settled in the waiting loop the last processor will generate a BusRdX during release (flag write) and others will subsequently generate BusRd before releasing: O(P) Scalability turns out to be low partly due to the critical section and partly due to O(P) traffic of release No fairness in terms of who exits first
Does not need a lock, only uses flags Arrange the processors logically in a binary tree (higher degree also possible) Two siblings tell each other of arrival via simple flags (i.e. one waits on a flag while the other sets it on arrival) One of them moves up the tree to participate in the next level of the barrier Introduces concurrency in the barrier algorithm since independent subtrees can proceed in parallel Takes log(P) steps to complete the acquire A fixed processor starts a downward pass of release waking up other processors that in turn set other flags Shows much better scalability compared to centralized barriers in DSM multiprocessors; the advantage in small bus-based systems is not much, since all transactions are any way serialized on the bus; in fact the additional log (P) delay may hurt performance in bus-based SMPs
TreeBarrier (pid, P) { unsigned int i, mask; for (i = 0, mask = 1; (mask & pid) != 0; ++i, mask <<= 1) { while (!flag[pid][i]); flag[pid][i] = 0; } if (pid < (P - 1)) { flag[pid + mask][i] = 1; while (!flag[pid][MAX- 1]); flag[pid][MAX - 1] = 0; } for (mask >>= 1; mask > 0; mask >>= 1) { flag[pid - mask][MAX-1] = 1; } }
Convince yourself that this works Take 8 processors and arrange them on leaves of a tree of depth 3 You will find that only odd nodes move up at every level during acquire (implemented in the first for loop) The even nodes just set the flags (the first statement in the if condition): they bail out of the first loop with mask= The release is initiated by the last processor in the last for loop; only odd nodes execute this loop (7 wakes up 3, 5, 6; 5 wakes up 4; 3 wakes up 1, 2; 1 wakes up 0) Each processor will need at most log (P) + 1 flags Avoid false sharing: allocate each processor’s flags on a separate chunk of cache lines With some memory wastage (possibly worth it) allocate each processor’s flags on a separate page and map that page locally in that processor’s physical memory Avoid remote misses in DSM multiprocessor Does not matter in bus-based SMPs
For certain input values it may happen that the processes could actually update the hash table concurrently
Speculative locks Every processor comes to the critical section and tries to acquire the lock One of them succeeds and the rest fail The successful processor becomes the safe thread The failed ones don’t retry but venture into the critical section speculatively as if they have the lock; at this point a speculative thread also takes a checkpoint of its register state in case a rollback is needed The safe thread executes the critical section as usual The speculative threads are allowed to consume values produced by the safe thread but not by the sp. threads All stores from a speculative thread are kept inside its cache hierarchy in a special “speculative modified” state; these lines cannot be sent to memory until it is known to be safe; if such a line is replaced from cache either it can be kept in a small buffer or the thread can be stalled Speculative locks (continued) If a speculative thread receives a request for a cache line that is in speculative M state, that means there is a data race inside the critical section and by design the receiver thread is rolled back to the beginning of critical section Why can’t the requester thread be rolled back? In summary, the safe thread is never squashed and the speculative threads are not squashed if there is no cross-thread data race If a speculative thread finishes executing the critical section without getting squashed, it still must wait for the safe thread to finish the critical section before committing the speculative state (i.e. changing speculative M lines to M); why? Speculative locks (continued) Upon finishing the critical section, a speculative thread can continue executing beyond the CS, but still remaining in speculative mode When the safe thread finishes the CS all speculative threads that have already completed CS, can commit in some non-deterministic order and revert to normal execution The speculative threads that are still inside the critical section remain speculative; a dedicated hardware unit elects one of them the lock owner and that becomes the safe non-speculative thread; the process continues Clearly, under favorable conditions speculative synchronization can reduce lock contention enormously