Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to lock some data in CPU cache?

Tags:

c++

cpu-cache

I have a problem.... I'm writing a data into array in the while-loop. And the point is that I'm doing it really frequently. It seems to be that this writing is now a bottle-neck in the code. So as i presume it's caused by the writing to memory. This array is not really large (smth like 300 elements). The question is it possible to do it in that way: to store it in the cache and update in the memory only after the while-loop is finished?

[edit - copied from an answer added by Alex]

double* array1  = new double[1000000]; // this array has elements  
unsigned long* array2  = unsigned long[300];
double varX,t,sum=0;
int iter=0,i=0;
while(i<=max_steps)
{
   varX+=difX;
   nm0 =  int(varX);
   if(nm1!=nm0)
   {
        array2[iter] = nm0;  // if you comment this string application works more then 2 times faster :)
        nm1=nm0;
        t = array1[nm0]; // if you comment this string , there is almost no change in time 
        ++iter;
   }
   sum+=t;
   ++i;
}

For the first I'd like to thank all of you for answers. Indeed, it was a little dumb not to place a code. So i decided to do it now.

double* array1  = new double[1000000]; // this array has elements  
unsigned long* array2  = unsigned long[300];
double varX,t,sum=0;
int iter=0,i=0;
while(i<=max_steps)
{
   varX+=difX;
   nm0 =  int(varX);
   if(nm1!=nm0)
   {
        array2[iter] = nm0;  // if you comment this string application works more then 2 times faster :)
        nm1=nm0;
        t = array1[nm0]; // if you comment this string , there is almost no change in time 
        ++iter;
   }
   sum+=t;
   ++i;
}

So that was it. It would be nice if someone will have any ideas. Thank you very much again.

Sincerely Alex

like image 924
Alex Avatar asked Oct 06 '09 14:10

Alex


People also ask

What is the CPU cache?

The CPU cache is a very small memory module, mounted on the CPU chip, which stores files that are frequently used to operate different applications. It’s actually a very fast type of random-access memory. Its architecture allows the processor to access information stored in the cache memory module at ultra-fast transfer speeds.

What is cache locking and how does it work?

Now, the easiest way to explain what cache locking is, is to think about a scenario where multiple people are hitting a single page at the exact same time. In a case like this, there are actually two (or more) server processes running together simultaneously. To keep this example simple, let's pretend there are just two server processes.

Does the number of cores in a CPU affect cache memory?

Well, truth be told, not that much. Depending on the overall performance of the CPU, cache memory will increase with the number of cores and the clock speed. Higher-end CPUs will have more, lower end CPUs will have less. What’s most important when shopping for CPUs and PC components, in general, is to prevent bottlenecking.

Why does my PC need to cache data?

It is not unique to processors, and it is necessary for your PC to work as efficiently as it does. For example, your browser constantly caches data of website images, templates, videos, etc. This means the next time you open that website, your computer won’t have to waste any extra time, bandwidth, and energy to process that data again.


1 Answers

Not intentionally, no. Among other things, you have no idea how big the cache is, so you have no idea of what's going to fit. Furthermore, if the app were allowed to lock off part of the cache, the effects on the OS might be devastating to overall system performance. This falls squarely onto my list of "you can't do it because you shouldn't do it. Ever."

What you can do is to improve your locality of reference - try to arrange the loop such that you don't access the elements more than once, and try to access them in order in memory.

Without more clues about your application, I don't think more specific advice can be given.

like image 112
Michael Kohne Avatar answered Sep 19 '22 23:09

Michael Kohne