1

我目前正在执行一种算法的两种实现,一种在 C 中,另一种在 CUDA 中,并计划在运行时对两者进行比较。我的问题是,考虑到我将比较 C 和 CUDA 中的运行时,最好的 C 计时器是什么。对于 CUDA,我将使用事件,并且我已经阅读了 C 中的挂钟计时器,例如 clock() 和 gettimeofday() 以及高分辨率计时器,例如 clock_gettime(),但不确定使用哪个 C如果我要将我的 C 时间与 CUDA 时间进行比较?

谢谢 :-)

4

4 回答 4

3

对于应用程序级别的端到端测量,我建议使用高精度主机定时器,如下面的代码所示,我已经使用了十多年。对于可能极短的 GPU 活动的详细测量,我建议使用 CUDA 事件。

#if defined(_WIN32)
#if !defined(WIN32_LEAN_AND_MEAN)
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
double second (void)
{
    LARGE_INTEGER t;
    static double oofreq;
    static int checkedForHighResTimer;
    static BOOL hasHighResTimer;

    if (!checkedForHighResTimer) {
        hasHighResTimer = QueryPerformanceFrequency (&t);
        oofreq = 1.0 / (double)t.QuadPart;
        checkedForHighResTimer = 1;
    }
    if (hasHighResTimer) {
        QueryPerformanceCounter (&t);
        return (double)t.QuadPart * oofreq;
    } else {
        return (double)GetTickCount() * 1.0e-3;
    }
}
#elif defined(__linux__) || defined(__APPLE__)
#include <stddef.h>
#include <sys/time.h>
double second (void)
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return (double)tv.tv_sec + (double)tv.tv_usec * 1.0e-6;
}
#else
#error unsupported platform
#endif
于 2012-07-30T16:26:44.780 回答
1

最好只坚持一些相对简单的方法,我建议使用 gettimeofday,它将提供具有微秒精度的时间戳。只需记录计算前后的时间,然后将两者相减。您可以使用 timersub 宏来执行此操作。

http://linux.die.net/man/2/gettimeofday

http://linux.die.net/man/3/timercmp

于 2012-07-30T08:57:55.947 回答
0
#include "time.h"

clock_t init, final;

init=clock();

...
//your sequential algoritm
...

final=clock()-init;
float seq_time ((double)final / ((double)CLOCKS_PER_SEC));
printf("\nThe sequential duration is %f seconds.", seq_time);

//Clock is initialized again
init=clock();

...
//your parallel algoritm
...

final=clock()-init;
float par_time ((double)final / ((double)CLOCKS_PER_SEC));
printf("\nThe parallel duration is %f seconds.", par_time);

printf("\n\nSpped up is %f seconds. (%dX Faster)", (seq_time - par_time), ((int)(seq_time / par_time)));
于 2012-07-30T21:08:26.867 回答
0

我使用以下代码取得了巨大/准确的成功:

#include <time.h>

long unsigned int get_tick()
{
  struct timespec ts;
  if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) return (0);
  return ts.tv_sec*(long int)1000 + ts.tv_nsec / (long int) 1000000;
}

Then in the code you want to time put the get_tick method before and after it and subtract the two variables to get the result. Divide the answer by 1000 to get it in seconds

于 2015-02-09T18:19:06.567 回答