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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

5.3. SYNCHRONIZATION GRANULARITY 571 struct hash_table2 {3 long nbuckets;4 struct node **buckets;5 };67 typedef struct node {8 unsigned long key;9 struct node *next;10 } node_t;1112 int hash_search(struct hash_table *h, long key)13 {14 struct node *cur;1516 cur = h->buckets[key % h->nbuckets];17 while (cur != NULL) {18 if (cur->key >= key) {19 return (cur->key == key);20 }21 cur = cur->next;22 }23 return 0;24 }Figure 5.15: Sequential-Program Hash Table Search1 spinlock_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 spin_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 spin_unlock(&hash_lock);25 return retval;26 }27 cur = cur->next;28 }29 spin_unlock(&hash_lock);30 return 0;31 }Figure 5.16: Code-Locking Hash Table SearchFigure 5.17: Lock ContentionHowever, as those how have taken care of smallchildren can again attest, even providing enough togo around is no guarantee of tranquillity. The analogoussituation can arise in SMP programs. For example,the Linux kernel maintains a cache of filesand directories (called “dcache”). Each entry in thiscache has its own lock, but the entries correspondingto the root directory and its direct descendantsare much more likely to be traversed than are moreobscure entries. This can result in many CPUs contendingfor the locks of these popular entries, resultingin a situation not unlike that shown in Figure5.20.In many cases, algorithms can be designed to reducethe instance of data skew, and in some caseseliminate it entirely (as appears to be possible withthe Linux kernel’s dcache [MSS04]). Data lockingis often used for partitionable data structures suchas hash tables, as well as in situations where multipleentities are each represented by an instance of agiven data structure. The task list in version 2.6.17of the Linux kernel is an example of the latter, eachtask structure having its own proc_lock.A key challenge with data locking on dynamicallyallocatedstructuresisensuringthatthestructureremainsin existence while the lock is being acquired.The code in Figure 5.18 finesses this challenge byplacing the locks in the statically allocated hashbuckets, which are never freed. However, this trickwould not work if the hash table were resizeable, sothat the locks were now dynamically allocated. Inthis case, there would need to be some means toprevent the hash bucket from being freed during the

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

Saved successfully!

Ooh no, something went wrong!