keeping values in cache

This is a discussion on keeping values in cache within the C Programming forums, part of the General Programming Boards category; Is there a way to explicit tell the compiler to keep a value in the cache for the whole loop? ...

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    Thumbs up keeping values in cache

    Is there a way to explicit tell the compiler to keep a value in the cache for the whole loop? Like
    Code:
    for (j=0; j<MAX; j++)
       for (i=0; i<MAX; i++)
          y[j] += a[i] * b[i]
    ...and tell the compiler to keep the value y[i] for the whole inner loop, gaining optimization. I know it is possible, but i don't know how or if the compiler would do this anyway.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    I am not sure if the compiler has control over what remains in the cache (and from what I understand modern compilers routinely ignore suggestions of what should be placed in a register), but in any case is this a bottle neck in your code to begin with?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    You can tell the compiler you want something placed in a register with the "register" keyword.
    However, it's not advised, as the compiler can probably do better optimizations than you.
    In fact, many compilers would put both i and j in registers for this loop.

    What remains in the CPU cache cannot be controlled (for x86/x86-64).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    y[i] is not used in the loop except when i == j. Why would you want to cache it?

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by robwhit View Post
    y[i] is not used in the loop except when i == j. Why would you want to cache it?
    y[j] you mean. What do you mean exactly? Why isn't it used?

    Yeah, that's more or less the code I want to optimize. I read a paper that said that they had the system write back in the main memory y[j] only after the inner loop was done, gaining a speed advantage, since it will be used in every inner loop. But I don't know how it is done. Only that it is done in a C program on a linux system.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you see the symbols "y[i]" anywhere in your code?

    And I would be extraordinarily surprised if any compiler at all doesn't do that optimatization as a matter of course.

    (Edit: I mean adding a bunch of stuff to y[j], and only then writing it back. I'm not talking about cache.)
    Last edited by tabstop; 08-01-2008 at 03:37 PM.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    It sounds like some platform specific call to mess about with the cache control registers. But it would have to be a pretty large loop to offset the overheads of two system calls.

    Unless there's some specific compiler flag (you'd have to look yourself).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by C_ntua View Post
    Is there a way to explicit tell the compiler to keep a value in the cache for the whole loop? Like
    Code:
    for (j=0; j<MAX; j++)
       for (i=0; i<MAX; i++)
          y[j] += a[i] * b[i]
    ...and tell the compiler to keep the value y[i] for the whole inner loop, gaining optimization. I know it is possible, but i don't know how or if the compiler would do this anyway.
    It sounds like you want to do this (assuming you mean y[j], not y[i] in your description):
    Code:
    for (j=0; j<MAX; j++)
    {
       double t=0;
       for (i=0; i<MAX; i++)
          t += a[i] * b[i];
       y[j] += t;
    }
    (Assuming a and b hold an array of doubles)
    Don't bother unless you find that it is a bottleneck though.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-30-2007, 09:08 AM
  2. Computing Large Values
    By swbluto in forum C++ Programming
    Replies: 8
    Last Post: 04-07-2005, 03:04 AM
  3. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 06:30 AM
  4. Replies: 1
    Last Post: 02-03-2005, 02:33 AM
  5. cache memory
    By xddxogm3 in forum Tech Board
    Replies: 15
    Last Post: 12-20-2004, 06:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21