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.

160 APPENDIX B. SYNCHRONIZATION PRIMITIVESB.4.3 per thread()The per_thread() primitive accesses the specifiedthread’s variable.B.4.4get thread var()The __get_thread_var() primitive accesses thecurrent thread’s variable.@@@ Also work in something about criticalsectionsize? Or put later?The difference in performance is quite large, toput it mildly. The purpose of this book is to helpyou write SMP programs, perhaps with realtimeresponse, while avoiding such performance pitfalls.The next section starts this process by describingsome of the reasons for this performance shortfall.B.4.5 init per thread()The init_per_thread() primitive sets all threads’instances of the specified variable to the specifiedvalue.B.4.6 Usage ExampleSuppose that we have a counter that is incrementedvery frequently but read out quite rarely. As willbecome clear in Section B.5, it is helpful to implementsuch a counter using a per-CPU variable. Sucha variable can be defined as follows:DEFINE_PER_THREAD(int, counter);The counter must be initialized as follows:init_per_thread(counter, 0);Athreadcanincrementitsinstanceofthiscounteras follows:__get_thread_var(counter)++;The value of the counter is then the sum of itsinstances. A snapshot of the value of the countercan thus be collected as follows:for_each_thread(i)sum += per_thread(counter, i);Again, it is possible to gain a similar effect usingother mechanisms, but per-thread variables combineconvenience and high performance.B.5 Performance<strong>It</strong> is instructive to compare the performance of thelocked increment shown in Section B.3 to that ofper-thread variables (see Section B.4), as well as toconventional increment (as in “counter++”).@@@ need parable on cache thrashing.@@@ more here using performance results from amodest multiprocessor.

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

Saved successfully!

Ooh no, something went wrong!