【问题标题】:Implement sleep() in OpenCL C [duplicate]在 OpenCL C 中实现 sleep() [重复]
【发布时间】:2015-07-03 09:26:38
【问题描述】:

我想测量不同设备(即 CPU 和 GPU)的性能。 这是我的内核代码:

__kernel void dataParallel(__global int* A)
{  
    sleep(10);
    A[0]=2;
    A[1]=3;
    A[2]=5;
    int pnp;//pnp=probable next prime
    int pprime;//previous prime
    int i,j;
    for(i=3;i<10;i++)
    {
        j=0;
        pprime=A[i-1];
        pnp=pprime+2;
        while((j<i) && A[j]<=sqrt((float)pnp))
        {
            if(pnp%A[j]==0)
                {
                    pnp+=2;
                    j=0;
                }
            j++;

        }
        A[i]=pnp;

    }
}

但是sleep() 函数不起作用。我在构建日志中收到以下错误:

<kernel>:4:2: warning: implicit declaration of function 'sleep' is      invalid in C99
    sleep(10);
builtins: link error: Linking globals named '__gpu_suld_1d_i8_trap': symbol multiply defined!

有没有其他方法可以实现该功能。还有有没有办法记录执行这段代码sn-p所用的时间。

P.S.我已将#include &lt;unistd.h&gt; 包含在我的主机代码中。

【问题讨论】:

标签: c linux parallel-processing opencl gpgpu


【解决方案1】:

您不需要在内核中使用 sleep 来测量执行时间。

有两种方法可以测量时间。 1.使用opencl固有的profiling 看这里:cl api

  1. 在您的主机代码中获取时间戳并在执行前后进行比较。 示例:

        double start = getTimeInMS();
        //The kernel starts here
        clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &tasksize, &local_size_in, 0, NULL, NULL)
    //wait for kernel execution
    clFinish(command_queue);
    cout << "kernel execution time " << (getTimeInMS() - start) << endl;
    

其中 getTimeinMs() 是一个返回双精度毫秒值的函数: (特定于 Windows,如果您不使用 Windows,请使用其他实现覆盖)

static inline double getTimeInMS(){

SYSTEMTIME st;
GetLocalTime(&st);

return (double)st.wSecond * (double)1000 + (double)st.wMilliseconds;}

你还想:

#include <time.h>

对于 Mac,它会是(也可以在 Linux 上运行,不确定):

 static inline double getTime() {
    struct timeval starttime;
    gettimeofday(&starttime, 0x0);


    return (double)starttime.tv_sec * (double)1000 + (double)starttime.tv_usec / (double)1000;}

【讨论】:

  • 我一定会尝试的。谢谢!另外,正如我所说,我希望比较我的 CPU 和 GPU 的性能,实现这一目标的方法之一是计算内核代码的运行时间,而如果有另一种方法可以让代码同时开始在所有设备上执行,然后我只需要列出它们相应的执行结束时间,这也可以达到目的!有可能吗?
  • 为此,您需要为每个设备创建一个 opencl 上下文。您也可以只测量时间并更换选择的设备 3 次,然后比较时间。
猜你喜欢
  • 2020-10-24
  • 2017-01-21
  • 1970-01-01
  • 2011-03-09
  • 2015-03-28
  • 1970-01-01
  • 2016-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多