summaryrefslogtreecommitdiffstats
path: root/tools/gfx/cuda/cuda-query.cpp
blob: a671d43fbdbc1902941018c06b7c98481b1cee55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// cuda-query.cpp
#include "cuda-query.h"

namespace gfx
{
#ifdef GFX_ENABLE_CUDA
using namespace Slang;

namespace cuda
{

Result QueryPoolImpl::init(const IQueryPool::Desc& desc)
{
    cuEventCreate(&m_startEvent, 0);
    cuEventRecord(m_startEvent, 0);
    m_events.setCount(desc.count);
    for (SlangInt i = 0; i < m_events.getCount(); i++)
    {
        cuEventCreate(&m_events[i], 0);
    }
    return SLANG_OK;
}

QueryPoolImpl::~QueryPoolImpl()
{
    for (auto& e : m_events)
    {
        cuEventDestroy(e);
    }
    cuEventDestroy(m_startEvent);
}

SLANG_NO_THROW Result SLANG_MCALL
QueryPoolImpl::getResult(GfxIndex queryIndex, GfxCount count, uint64_t* data)
{
    for (GfxIndex i = 0; i < count; i++)
    {
        float time = 0.0f;
        cuEventSynchronize(m_events[i + queryIndex]);
        cuEventElapsedTime(&time, m_startEvent, m_events[i + queryIndex]);
        data[i] = (uint64_t)((double)time * 1000.0f);
    }
    return SLANG_OK;
}

} // namespace cuda
#endif
} // namespace gfx