Question

I am just starting working with processors and can't understand the following.

Asume that we have an array declared as

static double x[1000][3]

that we access in a function

double up (double *a, int i)
{
    double t=*(a+i*3);
    return t;
}

int main(int argc, char *argv[])
{
    static double x[1000][3];
    //some manipulations//
    double b;
    for (int i=0;i<10;i++)
    {
        b=up(&x[0][0],i);
     }
}

In this case, what is loaded into cache memory - actual values that a pointer is pointing to or only addresses? If addresses - does it mean there will be another cycle to load actual values? Is it all-in-all good for cache utilisation with respect to this loop?

Était-ce utile?

La solution

The exact answer is platform specific, but generally speaking, three things cause data in external memory to be loaded into the processor cache.

The most obvious is an actual instruction that accesses the memory in the ordinary way. If that memory address is not resident in processor cache, the processor will have to acquire it. The second way is speculative prefetching. Some processors will acquire memory into cache if they believe the code might access it. Lastly, compilers can actually emit instructions to put things into cache.

All your operations are on pointers, so that's all they'll load, except this one: double t=*(a+i*3);. That actually dereferences a pointer and accesses its value. So unless the compiler is smart enough to optimize the access away (because you never use the result) that is likely the only part of your code that will result in something other than a pointer being read from main memory into cache. (Assuming it ever gets to main memory, which depends on your cache size.)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top