[comp.lang.c.moderated]Re: how to put my structure variable into CPU caches to eliminate main memory

Discussion in 'C Programming' started by Stefan Ram, Apr 15, 2011.

  1. Stefan Ram

    Stefan Ram Guest

    fallsmile <> writes:
    >It's clear that there is no explicit way or certain system calls that
    >help programmers to put a variable into CPU cache.


    There is »register« (suggests that access to the object be
    as fast as possible; cannot be used for arrays, but only for
    small objects).

    >But I think that a certain programming style or well designed
    >algorithm can make it possible to increase the possibilities that the
    >variable can be cached into the CPU caches.


    It should help to keep the overall memory footprint of the
    program small: A small program that uses little memory has
    a higher chance of being placed into a cache. The details
    depend on the specific environment.
    Stefan Ram, Apr 15, 2011
    #1
    1. Advertising

  2. -berlin.de (Stefan Ram) writes:
    > fallsmile <> writes:
    >> It's clear that there is no explicit way or certain system calls that
    >> help programmers to put a variable into CPU cache.

    >
    > There is »register« (suggests that access to the object be
    > as fast as possible; cannot be used for arrays, but only for
    > small objects).


    There's actually no rule forbidding the "register" storage-class
    specifier on an array, but it doesn't make much sense to do so.
    You can't take the address of a register variable, and since array
    indexing is defined in terms of address operations, you can't
    index into a register variable. In fact, as the standard says in a
    footnote, "Thus, the only operator that can be applied to an array
    declared with storage-class specifier register is sizeof.".

    There's also no language restriction on the size of a register
    object. You could apply "register" to a declaration of a
    multi-megabyte structure if you wanted to. But the compiler would
    almost certainly ignore the "register" keyword (other than forbidding
    taking the address of the object or any sub-object).

    In any case, as far as I know compilers take "register" only as a hint
    to store an object in a CPU register.

    >> But I think that a certain programming style or well designed
    >> algorithm can make it possible to increase the possibilities that the
    >> variable can be cached into the CPU caches.

    >
    > It should help to keep the overall memory footprint of the
    > program small: A small program that uses little memory has
    > a higher chance of being placed into a cache. The details
    > depend on the specific environment.


    The whole point of a cache is that the system determines, based on
    actual usage, what should be placed in the cache for faster access.
    You might in some cases be able to do a better job than the OS
    of deciding what should be cached, but that's not usually the way
    to bet.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Apr 15, 2011
    #2
    1. Advertising

  3. Stefan Ram

    Stefan Ram Guest

    Keith Thompson <> writes:
    >There's actually no rule forbidding the "register" storage-class
    >specifier on an array, but it doesn't make much sense to do so.


    (I read »If the array object has register storage class, the
    behavior is undefined.« in ISO/IEC 9899:1999 (E), 6.3.2.1#3,
    and thought it refered to the mere act of declaring such an
    array, but now I see that this seems to refer only to an
    array in the case of the sentence preceding this quoted
    sentence in ISO/IEC 9899:1999 (E).)
    Stefan Ram, Apr 15, 2011
    #3
  4. Stefan Ram

    Eric Sosman Guest

    Re: [comp.lang.c.moderated]Re: how to put my structure variable intoCPU caches to eliminate main memory page access time?

    On 4/15/2011 3:28 PM, Keith Thompson wrote:
    > [...]
    > The whole point of a cache is that the system determines, based on
    > actual usage, what should be placed in the cache for faster access.
    > You might in some cases be able to do a better job than the OS
    > of deciding what should be cached, but that's not usually the way
    > to bet.


    It *is* usually the way to bet -- but not at the level of C code.
    Compilers can and do insert things like "speculative pre-fetches" of
    data items they think the program will soon use, in order to get them
    into cache a few cycles before they're needed. I've even seen CPU
    architectures with "scout threads" that run ahead of the instruction
    sequence, not actually executing instructions but just snooping out
    the locations of likely operands. (I've seen CPU's that devote as
    much silicon to the "scouts" as to the main execution units.)

    But, as I said: This sort of thing isn't accessible at the C level,
    nor at the Java/Ada/COBOL/Python/Unameit level. I've seen lots of
    attempts to do things cleverly in hopes of improving cache behavior,
    and *every* one of them has crashed and burned with the next version
    of "the same" CPU chip. That's why high-performance math libraries,
    for example, are not only ISA-dependent but chip-version-dependent.

    --
    Eric Sosman
    d
    Eric Sosman, Apr 16, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page