开发者

Help with a copy between buffers using memcpy C

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, &开发者_Go百科;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).


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜