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.

38 CHAPTER 4. COUNTING1 unsigned long __thread counter = 0;2 unsigned long __thread countermax = 0;3 unsigned long globalcountmax = 10000;4 unsigned long globalcount = 0;5 unsigned long globalreserve = 0;6 unsigned long *counterp[NR_THREADS] = { NULL };7 DEFINE_SPINLOCK(gblcnt_mutex);8 #define MAX_COUNTERMAX 100Figure 4.13: Approximate Limit Counter Variables1 static void balance_count(void)2 {3 countermax = globalcountmax - globalcount - globalreserve;4 countermax /= num_online_threads();5 if (countermax > MAX_COUNTERMAX)6 countermax = MAX_COUNTERMAX;7 globalreserve += countermax;8 counter = countermax / 2;9 if (counter > globalcount)10 counter = globalcount;11 globalcount -= counter;12 }Figure 4.14: Approximate Limit Counter Balancingof lines 5 and 6, which enforce the MAX_COUNTERMAXlimit on the per-thread countermax variable.1 atomic_t __thread counterandmax = ATOMIC_INIT(0);2 unsigned long globalcountmax = 10000;3 unsigned long globalcount = 0;4 unsigned long globalreserve = 0;5 atomic_t *counterp[NR_THREADS] = { NULL };6 DEFINE_SPINLOCK(gblcnt_mutex);7 #define CM_BITS (sizeof(atomic_t) * 4)8 #define MAX_COUNTERMAX ((1 > CM_BITS) & MAX_COUNTERMAX;14 *cm = cami & MAX_COUNTERMAX;15 }1617 static void18 split_counterandmax(atomic_t *cam, int *old,19 int *c, int *cm)20 {21 unsigned int cami = atomic_read(cam);2223 *old = cami;24 split_counterandmax_int(cami, c, cm);25 }2627 static int merge_counterandmax(int c, int cm)28 {29 unsigned int cami;3031 cami = (c

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

Saved successfully!

Ooh no, something went wrong!