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.

F.4. CHAPTER 4: COUNTING 281hardware for the sole purpose of doing counting,addition, subtraction, and lots more besides, don’tthey???Answer:Because the straightforward counting algorithms,for example, atomic operations on a shared counter,are slow and scale badly, as will be seen inSection 4.1.Quick Quiz 4.2:Network-packet counting problem. Supposethat you need to collect statistics on the numberof networking packets (or total number of bytes)transmitted and/or received. Packets might betransmitted or received by any CPU on the system.Suppose further that this large machine is capableof handling a million packets per second, and thatthere is a systems-monitoring package that readsout the count every five seconds. How would youimplement this statistical counter?Answer:Hint: the act of updating the counter must beblazingly fast, but because the counter is read outonly about once in five million updates, the actof reading out the counter can be quite slow. Inaddition, the value read out normally need notbe all that accurate—after all, since the counteris updated a thousand times per millisecond, weshould be able to work with a value that is within afew thousand counts of the “true value”, whatever“true value” might mean in this context. However,the value read out should maintain roughly thesame absolute error over time. For example, a 1%error might be just fine when the count is on theorder of a million or so, but might be absolutelyunacceptable once the count reaches a trillion. SeeSection 4.2.Quick Quiz 4.3:Approximate structure-allocation limit problem.Suppose that you need to maintain a countof the number of structures allocated in order tofail any allocations once the number of structures inuse exceeds a limit (say, 10,000). Suppose furtherthat these structures are short-lived, that the limitis rarely exceeded, and that a “sloppy” approximatelimit is acceptable.Answer:Hint: the act of updating the counter must beblazingly fast, but the counter is read out eachtime that the counter is increased. However, thevalue read out need not be accurate except thatit absolutely must distinguish perfectly betweenvalues below the limit and values greater than orequal to the limit. See Section 4.3.Quick Quiz 4.4:Exact structure-allocation limit problem.Suppose that you need to maintain a count of thenumber of structures allocated in order to fail anyallocations once the number of structures in use exceedsan exact limit (say, 10,000). Suppose furtherthat these structures are short-lived, and that thelimit is rarely exceeded, that there is almost alwaysat least one structure in use, and suppose furtherstill that it is necessary to know exactly when thiscounter reaches zero, for example, in order to freeup some memory that is not required unless thereis at least one structure in use.Answer:Hint: the act of updating the counter must beblazingly fast, but the counter is read out eachtime that the counter is increased. However, thevalue read out need not be accurate except thatit absolutely must distinguish perfectly betweenvalues between the limit and zero on the one hand,and values that either are less than or equal to zeroor are greater than or equal to the limit on theother hand. See Section 4.4.Quick Quiz 4.5:Removable I/O device access-count problem.Suppose that you need to maintain a referencecount on a heavily used removable mass-storagedevice, so that you can tell the user when it is safeto removed the device. This device follows the usualremoval procedure where the user indicates a desireto remove the device, and the system tells the userwhen it is safe to do so.Answer:Hint: the act of updating the counter must beblazingly fast and scalable in order to avoid slowingdown I/O operations, but because the counter isread out only when the user wishes to removethe device, the counter read-out operation can beextremely slow. Furthermore, there is no need to beable to read out the counter at all unless the userhas already indicated a desire to remove the device.In addition, the value read out need not be accurate

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

Saved successfully!

Ooh no, something went wrong!