Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > [comp.lang.c.moderated]Re: how to put my structure variable into CPU caches to eliminate main memory page access time?

Reply
Thread Tools

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

 
 
Stefan Ram
Guest
Posts: n/a
 
      04-15-2011
fallsmile <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      04-15-2011
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Stefan Ram) writes:
> fallsmile <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      04-15-2011
Keith Thompson <(E-Mail Removed)> 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).)

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      04-16-2011
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
(E-Mail Removed)d
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
XmlDataSource caches between page loads? Wessel Troost ASP .Net 8 05-05-2008 11:56 AM
Google caches Greek page in Russian I.N. Galidakis HTML 4 01-13-2008 04:56 PM
Memory sensitive caches Mark McKay Java 2 11-13-2004 06:50 PM



Advertisments