我目前正在做一个算法的实现,一个在C中,另一个在CUDA中,并且计划在运行时方面对两者进行比较。我的问题是,考虑到我将比较C和CUDA中的运行时间,最好使用什么C计时器。对于CUDA,我将使用Events,并且我已经阅读了C中的挂钟定时器,例如clock()和gettimeofday()以及clock_gettime()等高分辨率定时器,但我不确定要使用哪个C如果我将比较C时代和CUDA时代?比较C代码和CUDA代码时使用哪个计时器?
感谢:-)
我目前正在做一个算法的实现,一个在C中,另一个在CUDA中,并且计划在运行时方面对两者进行比较。我的问题是,考虑到我将比较C和CUDA中的运行时间,最好使用什么C计时器。对于CUDA,我将使用Events,并且我已经阅读了C中的挂钟定时器,例如clock()和gettimeofday()以及clock_gettime()等高分辨率定时器,但我不确定要使用哪个C如果我将比较C时代和CUDA时代?比较C代码和CUDA代码时使用哪个计时器?
感谢:-)
它可能充其量只是坚持的东西比较简单,我建议gettimeofday的,这将提供微秒精度的时间戳。只需记录计算前后的时间,然后减去两者。您可以使用timersub宏来执行此操作。
对于在应用程序级的端至端的测量,我建议使用一个高精度主机计时器,如在下面的代码,其中我已经使用了超过一十年。对于潜在的极短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
#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)));
我用非常/准确的成功下面的代码:
#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;
}
然后在你想要的时间代码把get_tick方法之前和之后与两者相减变量来获得结果。将答案除以1000以在几秒钟内得到答案