tags:

views:

534

answers:

2

I need to copy the content of one buffer to another one in blocks of n bytes (n might vary), several times to check the cache performance.

I use memcpy, but I'm afraid I'm not getting successful results. The block size is variable from some kbytes to Mbytes. And I have to reserve the maximum block to use (long double).

I'm a little lost in the copy. I'm just wondering if somebody has faced this problem, and can help me with some tips or pseudo code.

I edit the topic to include some code:

int main (int argc, char *argv[])
{
 FILE *fp;
 fp= fopen("part1.dat", "w");
 struct timeval time, oldtime;
 float segundos, microsegundos, total;
 //        float rendimiento;    
 pid_t pid;
 struct sched_param parametros;
 int i, v,p;
 char buffer1[100024];
 char buffer2[100024];

 pid = getpid();
 parametros.sched_priority = sched_get_priority_max(SCHED_FIFO);
 sched_setscheduler(pid, SCHED_FIFO, &parametros);
 p=0;

 gettimeofday(&oldtime, NULL);

 for (i=1;i<65;i++)
 {
  size_t datos=i*1024;
  for (v=0; p>i;v++)
  {
    memcpy(buffer1, buffer2, datos);
    p=(MAX_SIZE/i*1024);

  }
 }

 gettimeofday(&time, NULL);
 segundos = (float) (time.tv_sec - oldtime.tv_sec);
 microsegundos = (float) (time.tv_usec - oldtime.tv_usec);
 total = (float) ((segundos * 1000000 + microsegundos));


 //            printf ("Dimension %d \t Tiempo 1: %.2f \t Fallos Metodo 1:%d \t Tiempo 2: %.2f \t Fallos Metodo 2:%d \t Multiplica: %f \t Rendimiento: %.2f\n", i, total, fallos1, total2, fallos2, iteraciones, rendimiento);
 //              fprintf (fp, "%d \t %.3f %.3f %.3f\n", i, total, total2,rendimiento);


 fclose(fp);
 printf("Se ha creado el archivo de datos: part1.dat\n");

 return(0);
}

The idea is to copy from buffer1 to buffer2, in datos blocks 'p' times, in this case is supposed to be done 256000 times (i=1, datos=1024).

+1  A: 

You probably shouldn't use memcpy() for this, since you don't know how it acts internally. It might for instance touch memory out of order, which could perhaps make your measurements odd. It would of course be assumed to do "the best thing", but your task seems to imply that you don't care. :)

So just use a straight copying loop, maybe rounding out non-aligned accesses first (which is another thing memcpy() very probably already does).

Also, you can't measure memory block sizes using a non-integer type such as double. You should use size_t.

unwind
I believe memmove() is guaranteed to copy memory in order, since it can be used to copy overlapping regions. Would this help?
Thomi
Hi unwind, I don't want to know the memory block size, just copy it from one buffer to another. The porpuse is to see that when the block size increases, the data to move doesn't fit in the cache memory, so the time spend in the copy increases
Peter
@Peter: I realize that, I just pointed out that you should use size_t since you mentioned "long double" for block sizes.
unwind
memmove( ) is not guaranteed to copy in order, only to do the right thing if the buffers alias each other. Among other things, it would be completely valid for memmove to check for aliasing and call memcpy if they don't alias.
Stephen Canon
+1  A: 

A problem with the code - the inner for() loop won't execute. You are initializing p to zero

p = 0;

and then the loop condition compares p > i where i ranges from 1 to 64.

for (i=1;i<65;i++)
{
   size_t datos=i*1024;
   for (v=0; p>i;v++)
   {
      .....

So memcpy() is never actually called...

You also said:

The idea is to copy from buffer1 to buffer2, in datos blocks 'p' times, in this case is supposed to be done 256000 times (i=1, datos=1024).

memcpy() takes the destination array as the first argument and the source as second. Your parameters are backwards.

mocj