2

我正在尝试实现一个函数,该函数使用 C++AMP 计算 Gauss-Laguerre 数值积分方法的权重和横坐标以并行化该过程,并且在运行它时出现DXGI_ERROR_DEVICE_HUNG错误。

这是我在 GPU 上计算 gamma 函数对数的辅助方法:

template <typename T>
T gammaln_fast( T tArg ) restrict( amp )
{
    const T tCoefficients[] = { T( 57.1562356658629235f ), T( -59.5979603554754912f ),
        T( 14.1360979747417471f ), T( -0.491913816097620199f ), T( 0.339946499848118887E-4f ),
        T( 0.465236289270485756E-4f ), T( -0.983744753048795646E-4f ), T( 0.158088703224912494E-3f ),
        T( -0.210264441724104883E-3f ), T( 0.217439618115212643E-3f ), T( -0.164318106536763890E-3f ),
        T( 0.844182239838527433E-4f ), T( -0.261908384015814087E-4f ), T( 0.386991826595316234E-5f ) };

    T y = tArg, tTemp = tArg + T( 5.2421875f );
    tTemp = (tArg + T( 0.5f )) * concurrency::fast_math::log( tTemp ) - tTemp;

    T tSer = T( 0.999999999999997092f );

    for( std::size_t s = 0; s < (sizeof( tCoefficients ) / sizeof( T )); ++s )
    {
        tSer += tCoefficients[s] / ++y;
    }

    return tTemp + concurrency::fast_math::log( T( 2.5066282746310005f ) * tSer / tArg );
}

这是我计算权重和横坐标的函数:

template <typename T>
ArrayPair<T> CalculateGaussLaguerreWeights_fast( const T tExponent, const std::size_t sNumPoints, T tEps = std::numeric_limits<T>::epsilon() )
{
    static_assert(std::is_floating_point<T>::value, "You can only instantiate this function with a floating point data type");
    static_assert(!std::is_same<T, long double>::value, "You can not instantiate this function with long double type"); // The long double type is not currently supported by C++AMP

    T tCurrentGuess, tFatherGuess, tGrandFatherGuess;
    std::vector<T> vecInitialGuesses( sNumPoints );
    for( std::size_t s = 0; s < sNumPoints; ++s )
    {
        if( s == 0 )
        {
            tCurrentGuess = (T( 1.0f ) + tExponent) * (T( 3.0f ) + T( 0.92f ) * tExponent) / (T( 1.0f ) + T( 2.4f ) * sNumPoints + T( 1.8f ) * tExponent);
        }
        else if( s == 1 )
        {
            tFatherGuess = tCurrentGuess;
            tCurrentGuess += (T( 15.0f ) + T( 6.25f ) * tExponent) / (T( 1.0f ) + T( 0.9f ) * tExponent + T( 2.5f ) * sNumPoints);
        }
        else
        {
            tGrandFatherGuess = tFatherGuess;
            tFatherGuess = tCurrentGuess;
            std::size_t sDec = s - 1U;
            tCurrentGuess += ((T( 1.0f ) + T( 2.55f ) * sDec) / (T( 1.9f ) * sDec) + T( 1.26f ) * sDec * tExponent
                / (T( 1.0f ) + T( 3.5f ) * sDec)) * (tCurrentGuess - tGrandFatherGuess) / (T( 1.0f ) + T( 0.3f ) * tExponent);
        }
        vecInitialGuesses[s] = tCurrentGuess;
    }

    concurrency::array<T> arrWeights( sNumPoints ), arrAbsciasses( sNumPoints, std::begin(vecInitialGuesses) );

    try {
        concurrency::parallel_for_each( arrAbsciasses.extent, [=, &arrAbsciasses, &arrWeights]( concurrency::index<1> index ) restrict( amp ) {
            T tVal = arrAbsciasses[index], tIntermediate;
            T tPolynomial1 = T( 1.0f ), tPolynomial2 = T( 0.0f ), tPolynomial3, tDerivative;
            std::size_t sIterationNum = 0;
            do {
                tPolynomial1 = T( 1.0f ), tPolynomial2 = T( 0.0f );

                for( std::size_t s = 0; s < sNumPoints; ++s )
                {
                    tPolynomial3 = tPolynomial2;
                    tPolynomial2 = tPolynomial1;
                    tPolynomial1 = ((2 * s + 1 + tExponent - tVal) * tPolynomial2 - (s + tExponent) * tPolynomial3) / (s + 1);
                }

                tDerivative = (sNumPoints * tPolynomial1 - (sNumPoints + tExponent) * tPolynomial2) / tVal;
                tIntermediate = tVal;
                tVal = tIntermediate - tPolynomial1 / tDerivative;
                ++sIterationNum;

            } while( concurrency::fast_math::fabs( tVal - tIntermediate ) > tEps || sIterationNum < 10 );

            arrAbsciasses[index] = tVal;
            arrWeights[index] = -concurrency::fast_math::exp( gammaln_fast( tExponent + sNumPoints ) - gammaln_fast( T( sNumPoints ) ) ) / (tDerivative * sNumPoints * tPolynomial2);
        } );
    }
    catch( concurrency::runtime_exception& e )
    {
        std::cerr << "Runtime error, code: " << e.get_error_code() << "; message: " << e.what() << std::endl;
    }

    return std::make_pair( std::move( arrAbsciasses ), std::move( arrWeights ) );
}

这是来自调试控制台的完整跟踪:

D3D11:移除设备。D3D11 错误:ID3D11Device::RemoveDevice:设备删除已被触发,原因如下(DXGI_ERROR_DEVICE_HUNG:设备执行其命令的时间不合理,或硬件崩溃/挂起。因此,TDR(超时检测和恢复)机制已被触发。当前的设备上下文在挂起时正在执行命令。应用程序可能希望重新生成并回退到不太积极地使用显示硬件)。[执行错误 #378:DEVICE_REMOVAL_PROCESS_AT_FAULT] D3D11 错误:ID3D11DeviceContext::Map:返回 DXGI_ERROR_DEVICE_REMOVED,当资源尝试使用 READ 或 READWRITE 进行映射时。[ RESOURCE_MANIPULATION 错误 #2097214:RESOURCE_MAP_DEVICEREMOVED_RETURN]

我很抱歉无法制作一个可重复的小例子;我希望这仍然是一个可以接受的问题,因为我自己无法解决这个问题。

4

1 回答 1

3

使用 DirectCompute 时,主要挑战是编写不违反 Direct3D 自动“GPU 挂起”检测超时的计算。默认情况下,系统假设如果着色器花费的时间超过几秒钟,则 GPU 实际上已挂起。这种启发式方法适用于视觉着色器,但您可以轻松创建需要很长时间才能完成的 DirectCompute 着色器。

解决方案是禁用超时检测。您可以通过创建 Direct3D 11 设备来执行此操作,D3D11_CREATE_DEVICE_DISABLE_GPU_TIMEOUT请参阅在 Windows 8 上禁用 TDR 以了解您的 C++ AMP 算法博客文章。要记住的主要事情是,它D3D11_CREATE_DEVICE_DISABLE_GPU_TIMEOUT需要 DirectX 11.1 或更高版本的运行时,它包含在 Windows 8.x 中,并且可以使用 KB 2670838安装在 Windows 7 Service Pack 1 上。有关使用 KB2670838 的一些注意事项,请参阅DirectX 11.1 和 Windows 7DirectX 11.1 和 Windows 7 更新以及MSDN 。

于 2014-08-19T23:17:15.507 回答