为什么连续启动调度需要不同的时间

Why Dispatches started consecutively take different time?

本文关键字:时间 连续 启动 调度 为什么      更新时间:2023-10-16

我有这个程序:

        for (int i = 0; i < STEPS; ++i)
        {
            context->CSSetShader(computeShader, NULL, 0);
            ID3D11UnorderedAccessView *aUAViews[1] = {bufferOut_UAV};
            context->CSSetUnorderedAccessViews(0, 1, aUAViews, NULL);
            context->Dispatch(32, 32, 1);
            in[i] = t.GetTime();
            if (i == STEPS / 2)
            {
context->End(pEventQuery);
while( context->GetData( pEventQuery, NULL, 0, 0 ) == S_FALSE ) {}
            }
        }
        double out = t.GetTime();
context->End(pEventQuery);
while( context->GetData( pEventQuery, NULL, 0, 0 ) == S_FALSE ) {}

第一次迭代仅持续26ms,第二次迭代持续46ms ?

只是以防着色器:

RWStructuredBuffer<float> Output : register(u0);
[numthreads(GROUP_SIZE_X, GROUP_SIZE_Y, 1)]
void arrayTest(uint3 DTid : SV_DispatchThreadID)
{
    float i = DTid.x * 32 + DTid.y;
    Output[i] = 0;
    for (int k = 0; k < 100; ++k)
    {
        Output[i] += sqrt(i + k);
    }
}

但我认为不应该每次出发都花不同的时间。

有趣的如果我注释掉"if (I == STEPS/2)"这行会怎么样?

编辑:据我所知,这是由于兑现

最可能的选择(在我看来)是操作系统中线程之间的上下文切换。操作时间越长,操作系统就越有可能在中途暂停。

你的程序在任何时候都不是唯一在进行的事情,有时操作系统会让你暂停,而其他事情会得到一些处理时间。