我正在尝试实现一个函数,它计算Gauss-Laguerre数值积分方法的权重和横坐标,使用C ++ AMP来并行化流程,运行时我得到DXGI_ERROR_DEVICE_HUNG
错误。
这是我在GPU上计算伽玛函数对数的辅助方法:
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 ERROR#2097214:RESOURCE_MAP_DEVICEREMOVED_RETURN]
对于无法制作一个可重复的小例子,我深表歉意;我希望这仍然是一个可以接受的问题,因为我自己无法解决这个问题。
答案 0 :(得分:3)
使用DirectCompute时,主要的挑战是编写不会与Direct3D自动“GPU挂起”检测超时相冲突的计算。默认情况下,系统假设着色器花费的时间超过几秒钟,GPU实际上是挂起的。此启发式适用于可视着色器,但您可以轻松创建需要很长时间才能完成的DirectCompute着色器。
解决方案是禁用超时检测。您可以通过D3D11_CREATE_DEVICE_DISABLE_GPU_TIMEOUT
创建包含Disabling TDR on Windows 8 for your C++ AMP algorithms博文的Direct3D 11设备来实现此目的。要记住的主要事情是D3D11_CREATE_DEVICE_DISABLE_GPU_TIMEOUT
需要Windows 8.x附带的DirectX 11.1或更高版本的运行时,并且可以安装在带有KB 2670838的Windows 7 Service Pack 1上。有关使用KB2670838的一些注意事项,请参阅DirectX 11.1 and Windows 7,DirectX 11.1 and Windows 7 Update和MSDN。