Prefechind data from main memory into cache

This is a discussion on Prefechind data from main memory into cache within the C Programming forums, part of the General Programming Boards category; I have a huge program which do a lot of different tasks. There are hundreds, and one of them is ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    145

    Prefechind data from main memory into cache

    I have a huge program which do a lot of different tasks. There are hundreds, and one of them is Transposition Table access. This TT is very big, and I access it from time to time to retrieve a cell of around 8 or 10 ints size.

    As my program has to be tuned for speed I have used a _mm_prefetch intrinsic instruction for trying to prefetch data a few instruction before real call.

    I have trying to measuring time doing differents test but I have not get any improvement, so I ask myselft if I have programmed the code well.

    Is there any way of knowing if the data have move into cache after the prefetch? .... or is there any way of knowing if _mm_prefecth instructions has been sucessfull?

    My code is near like this:

    Code:
    	unsigned int nt = n->hash & total_hash_entries;
    	t_entrada_hash *p = &ptrTT[nt];
    
            _mm_prefetch((char*) p, _MM_HINT_NTA)
    
            .... a few lines of code here.....
    
    	use of p.... i.e.:
            if (p->x = z...)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Well a long read of chapter 7 of this would be good.
    http://www.intel.com/design/processo...als/248966.pdf
    In particular, I think you need a different constant, and more code between prefetch and 'use' to let it happen.

    This might be as well.
    http://vast.uccs.edu/~tboult/FRAME/Cutler/frame99.htm
    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.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    Have you actually used a profiler on your code? What makes you think a change in CPU cache usage will provide any speed-up?

    gg

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The prefetch instruction is "advisory" to the processor, so depending on what is going on, the processor may not perform a prefetch at all.

    To determine if it "helps", you will have to benchmark the program with and without the prefetch instruction.

    Edit: And unless you have several dozen instructions between the prefetch and the actual use of the data, it's unlikely that you will gain very much. If you do fetches in a loop, the recommended "advance" is that you fetch some 256 or 512 bytes ahead of where you are processing. But that only really helps if you have loops that access in a predictable pattern. This looks like a hash-table access, so you (probably) can't determine where the next access will be.

    --
    Mats
    Last edited by matsp; 02-04-2009 at 03:11 PM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    145
    Quote Originally Posted by Codeplug View Post
    What makes you think a change in CPU cache usage will provide any speed-up?
    gg
    well, commmon sense: if can prefetch data into cache a hundreds instructions before use them, I will have a few cycles gain.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    Use a profiler to tell you where to spend your time, otherwise your wasting it.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  3. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  4. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  5. C diamonds and perls :)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM

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