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.

58 CHAPTER 5. PARTITIONING AND SYNCHRONIZATION DESIGN1 struct hash_table2 {3 long nbuckets;4 struct bucket **buckets;5 };67 struct bucket {8 spinlock_t bucket_lock;9 node_t *list_head;10 };1112 typedef struct node {13 unsigned long key;14 struct node *next;15 } node_t;1617 int hash_search(struct hash_table *h, long key)18 {19 struct bucket *bp;20 struct node *cur;21 int retval;2223 bp = h->buckets[key % h->nbuckets];24 spin_lock(&bp->bucket_lock);25 cur = bp->list_head;26 while (cur != NULL) {27 if (cur->key >= key) {28 retval = (cur->key == key);29 spin_unlock(&bp->hash_lock);30 return retval;31 }32 cur = cur->next;33 }34 spin_unlock(&bp->hash_lock);35 return 0;36 }Figure 5.19: Data LockingFigure 5.18: Data-Locking Hash Table Searchtime that its lock was being acquired.Quick Quiz 5.11: <strong>What</strong> are some ways of preventinga structure from being freed while its lock isbeing acquired?5.3.4 Data OwnershipData ownership partitions a given data structureover the threads or CPUs, so that each thread/CPUaccesses its subset of the data structure withoutany synchronization overhead whatsoever. However,if one thread wishes to access some other thread’sdata, the first thread is unable to do so directly. Instead,the first thread must communicate with thesecond thread, so that the second thread performsthe operation on behalf of the first, or, alternatively,migrates the data to the first thread.Data ownership might seem arcane, but it is usedvery frequently:1. Any variables accessible by only one CPU orthread (such as auto variables in C and C++)are owned by that CPU or process.2. An instance of a user interface owns the correspondinguser’s context. <strong>It</strong> is very common forapplications interacting with parallel databaseengines to be written as if they were entirely sequentialprograms. Such applications own theuser interface and his current action. Explicitparallelism is thus confined to the database engineitself.3. Parametric simulations are often trivially parallelizedby granting each thread ownership ofa particular region of the parameter space.<strong>If</strong> there is significant sharing, communication betweenthe threads or CPUs can result in significantcomplexity and overhead. Furthermore, if the mostheavilyuseddatahappenstobethatownedbyasingleCPU, that CPU will be a “hot spot”, sometimeswith results resembling that shown in Figure 5.20.However, in situations where no sharing is required,dataownershipachievesidealperformance, andwithcodethatcanbeassimpleasthesequential-programcase shown in Figure 5.15. Such situations are oftenreferred to as “embarrassingly parallel”, and, in thebest case, resemble the situation previously shownin Figure 5.19.Another important instance of data ownership occurswhen the data is read-only, in which case, allthreads can “own” it via replication.

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

Saved successfully!

Ooh no, something went wrong!