Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #263

Re: False sharing

From "Chris M. Thomasson" <cristom@charter.net>
Newsgroups comp.programming.threads
References <18fa2208-ba1c-42c5-bf1f-280e6cbc41e6@bl1g2000vbb.googlegroups.com>
Subject Re: False sharing
Message-ID <D3SIp.1908$g12.189@newsfe20.iad> (permalink)
Date 2011-06-11 15:34 -0700

Show all headers | View raw


"curious" <curious.oskar58@gmail.com> wrote in message 
news:18fa2208-ba1c-42c5-bf1f-280e6cbc41e6@bl1g2000vbb.googlegroups.com...
> Hi All,
>
> I wanted to see the effects of false sharing and wrote small test
> program below:

[...]

IMVHO, a "decent test" should probably measure the effect that false sharing 
has on "reader threads". A simple scenario could be: thread A is a writer 
and threads B-N are readers. Thread A works on completely unrelated data wrt 
threads B-N; they should not interfere with each other... A simple setup in 
pseudo-code might be something like:
______________________________________________________
#define ITERS 100000000
#define WRITERS 1
#define READERS 4
#define WRITER_YIELD 100


static atomic_word_t g_state_1; // = 0
static atomic_word_t g_state_2; // = 0
static atomic_word_t g_writers_complete; // = 0


void writer_threads()
{
    for (unsigned i = 0; i < ITERS; ++i)
    {
        ATOMIC_FETCH_ADD(&g_state_1, 1);

        if (! (i % WRITER_YIELD)) thread_yield();
    }

    ATOMIC_FETCH_ADD(&g_writers_complete, 1);
}


void reader_threads()
{
    unsigned iterations = 0;

    while (ATOMIC_LOAD(&g_writers_complete) != WRITERS)
    {
        unsigned state = ATOMIC_LOAD(&g_state_2);

        ++iterations;
    }

    printf("reader_thread iterations: %u\n", iterations);
}
______________________________________________________




The writer threads atomic RMW operations on the `g_state_1' variable will 
probably be false shared with the reader threads load operations on 
`g_state_2'. This should have an impact on the number of iterations a reader 
thread can perform. Compare the performance on the program above with 
something like this:
______________________________________________________
#define ITERS 100000000
#define WRITERS 1
#define READERS 4
#define WRITER_YIELD 100
#define L2_CACHE 128
#define L2_CACHE_ATOMIC_WORD_PAD \
    (L2_CACHE - sizeof(atomic_word_t))


COMPILER_PADDING(OFF)
struct shared_state
{
    atomic_word_t state_1;
    char l2_cache_pad_1[L2_CACHE_ATOMIC_WORD_PAD];

    atomic_word_t state_2;
    char l2_cache_pad_2[L2_CACHE_ATOMIC_WORD_PAD];

    atomic_word_t writers_complete;
    char l2_cache_pad_3[L2_CACHE_ATOMIC_WORD_PAD];
};


// assume that the following is aligned on a L2_CACHE line boundary!!!
static COMPILER_ALIGN(L2_CACHE)
    struct shared_state g_shared_state = { 0 };


void writer_threads()
{
    for (unsigned i = 0; i < ITERS; ++i)
    {
        ATOMIC_FETCH_ADD(&g_shared_state.state_1, 1);

        if (! (i % WRITER_YIELD)) thread_yield();
    }

    ATOMIC_FETCH_ADD(&g_shared_state.writers_complete, 1);
}


void reader_threads()
{
    unsigned iterations = 0;

    while (ATOMIC_LOAD(&g_shared_state.writers_complete)
              != WRITERS)
    {
        unsigned state = ATOMIC_LOAD(&g_shared_state.state_2);

        ++iterations;
    }

    printf("reader_thread iterations: %u\n", iterations);
}
______________________________________________________



The latter program "should" be reporting higher reader thread iteration 
counts than the one that is crippled from false sharing.

Humm... 

Back to comp.programming.threads | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

False sharing curious <curious.oskar58@gmail.com> - 2011-05-30 04:26 -0700
  Re: False sharing Edek <edek.pienkowski@gmail.com> - 2011-05-30 22:29 +0200
    Re: False sharing curious <curious.oskar58@gmail.com> - 2011-05-31 03:21 -0700
      Re: False sharing Marcel Müller <news.5.maazl@spamgourmet.com> - 2011-05-31 15:41 +0200
        Re: False sharing curious <curious.oskar58@gmail.com> - 2011-05-31 10:18 -0700
        Re: False sharing Edek <edek.pienkowski@gmail.com> - 2011-05-31 20:10 +0200
      Re: False sharing Edek <edek.pienkowski@gmail.com> - 2011-05-31 20:37 +0200
        Re: False sharing Edek <edek.pienkowski@gmail.com> - 2011-05-31 21:18 +0200
          Re: False sharing curious <curious.oskar58@gmail.com> - 2011-05-31 14:40 -0700
            Re: False sharing "Balog Pal" <pasa@lib.hu> - 2011-06-01 03:17 +0200
            Re: False sharing Edek <edek.pienkowski@gmail.com> - 2011-06-01 22:20 +0200
        Re: False sharing curious <curious.oskar58@gmail.com> - 2011-05-31 14:29 -0700
  Re: False sharing David Schwartz <davids@webmaster.com> - 2011-05-31 14:12 -0700
  Re: False sharing "Chris M. Thomasson" <cristom@charter.net> - 2011-06-11 15:34 -0700
    Re: False sharing "Chris M. Thomasson" <cristom@charter.net> - 2011-06-11 19:56 -0700
      Re: False sharing "Chris M. Thomasson" <cristom@charter.net> - 2011-06-11 21:07 -0700

csiph-web