10.07.2015 Views

Is Parallel Programming Hard, And, If So, What Can You Do About It?

Is Parallel Programming Hard, And, If So, What Can You Do About It?

Is Parallel Programming Hard, And, If So, What Can You Do About It?

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

5.4. PARALLEL FASTPATH 61the workload that the algorithm will be subjectedto. Great creativity and design effort is often requiredto construct a parallel fastpath.<strong>Parallel</strong> fastpath combines different patterns (onefor the fastpath, one elsewhere) and is therefore atemplatepattern. Thefollowinginstancesofparallelfastpath occur often enough to warrant their ownpatterns, as depicted in Figure 5.23:<strong>Parallel</strong>FastpathReader/WriterLockingRCUHierarchicalLocking1 rwlock_t hash_lock;23 struct hash_table4 {5 long nbuckets;6 struct node **buckets;7 };89 typedef struct node {10 unsigned long key;11 struct node *next;12 } node_t;1314 int hash_search(struct hash_table *h, long key)15 {16 struct node *cur;17 int retval;1819 read_lock(&hash_lock);20 cur = h->buckets[key % h->nbuckets];21 while (cur != NULL) {22 if (cur->key >= key) {23 retval = (cur->key == key);24 read_unlock(&hash_lock);25 return retval;26 }27 cur = cur->next;28 }29 read_unlock(&hash_lock);30 return 0;31 }AllocatorCachesFigure 5.23: <strong>Parallel</strong>-Fastpath Design PatternsFigure 5.24: Reader-Writer-Locking Hash TableSearchused in several clustered systems. Locking in generaland reader-writer locking in particular is describedextensively in Chapter 6.1. Reader/WriterLocking(describedbelowinSection5.4.1).2. Read-copy update (RCU), which is very brieflydescribed below in Section 5.4.2).3. Hierarchical Locking ([McK96]), which istouched upon in Section 5.4.3.4. Resource Allocator Caches ([McK96, MS93]).See Section 5.4.4 for more detail.5.4.1 Reader/Writer Locking<strong>If</strong> synchronization overhead is negligible (for example,if the program uses coarse-grained parallelism),and if only a small fraction of the criticalsections modify data, then allowing multiple readersto proceed in parallel can greatly increase scalability.Writers exclude both readers and each other.Figure 5.24 shows how the hash search might be implementedusing reader-writer locking.Reader/writer locking is a simple instance ofasymmetric locking. Snaman [ST87] describes amore ornate six-mode asymmetric locking design5.4.2 Read-Copy Update IntroductionRead-copy update (RCU) is a mutual-exclusionmechanism which can be used as an alternative toreader-writer locking. RCU features extremely lowoverheadwait-free read-side critical sections, however,updates can be expensive, as they must leaveold versions of the data structure in place for thesake of pre-existing readers. These old versions maybe reclaimed once all such pre-existing readers completetheir accesses. [MPA + 06].<strong>It</strong> turns out that our example hash-search programis well-suited to RCU. Other programs maybe more difficult to adapt to RCU, and more detailon such adaptation may be found in Section 8.3.In some implementations of RCU (such as that ofHart et al. [HMB06]), the search code can be implementedexactly as in sequential programs, as wasshown in Figure 5.15. However, other environments,including the Linux kernel, require that the RCUread-side critical sections be marked explicitly, asshown in Figure 5.25. Such marking can be a greatfavor to whoever must later read the code!

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!