3

我在编写一些 C++ AMP 代码时遇到问题。我已经包含了一个样本。它在模拟加速器上运行良好,但在我的硬件(Windows 7、NVIDIA GeForce GTX 660、最新驱动程序)上崩溃了显示驱动程序,但我看不出我的代码有任何问题。

我的代码有问题还是硬件/驱动程序/编译器问题?

#include "stdafx.h"

#include <vector>
#include <iostream>
#include <amp.h>

int _tmain(int argc, _TCHAR* argv[])
{
    // Prints "NVIDIA GeForce GTX 660"
    concurrency::accelerator_view target_view = concurrency::accelerator().create_view();
    std::wcout << target_view.accelerator.description << std::endl;

    // lower numbers do not cause the issue
    const int x = 2000;
    const int y = 30000;

    // 1d array for storing result
    std::vector<unsigned int> resultVector(y);
    Concurrency::array_view<unsigned int, 1> resultsArrayView(resultVector.size(), resultVector);

    // 2d array for data for processing 
    std::vector<unsigned int> dataVector(x * y);
    concurrency::array_view<unsigned int, 2> dataArrayView(y, x, dataVector);
    parallel_for_each(
        // Define the compute domain, which is the set of threads that are created.
        resultsArrayView.extent,
        // Define the code to run on each thread on the accelerator.
        [=](concurrency::index<1> idx) restrict(amp)
    {
        concurrency::array_view<unsigned int, 1> buffer = dataArrayView[idx[0]];
        unsigned int bufferSize = buffer.get_extent().size();

        // needs both loops to cause crash
        for (unsigned int outer = 0; outer < bufferSize; outer++)
        {
            for (unsigned int i = 0; i < bufferSize; i++)
            {
                // works without this line, also if I change to buffer[0] it works?
                dataArrayView[idx[0]][0] = 0;
            }
        }
        // works without this line
        resultsArrayView[0] = 0;
    });

    std::cout << "chash on next line" << std::endl; 
    resultsArrayView.synchronize();
    std::cout << "will never reach me" << std::endl; 

    system("PAUSE");
    return 0;
}
4

1 回答 1

8

您的计算很可能超过了允许的量子时间(默认为 2 秒)。在那之后,操作系统进入并强制重启 GPU,这称为超时检测和恢复 (TDR)。软件适配器(参考设备)没有启用 TDR,这就是计算可能超过允许的量子时间的原因。

您的计算是否真的需要 3000 个线程(变量 x),每个线程执行 2000 * 3000 (x * y) 循环迭代?您可以对计算进行分块,这样每个块的计算时间不到 2 秒。您还可以考虑禁用 TDR 或超过允许的量程时间以满足您的需要。

我强烈建议阅读有关如何在 C++ AMP 中处理 TDR 的博客文章,其中详细解释了 TDR:http: //blogs.msdn.com/b/nativeconcurrency/archive/2012/03/07/handling-tdrs-in- c-amp.aspx

此外,这里是关于如何在 Windows 8 上禁用 TDR 的单独博客文章:http: //blogs.msdn.com/b/nativeconcurrency/archive/2012/03/06/disabling-tdr-on-windows-8- for-your-c-amp-algorithms.aspx

于 2013-03-14T21:39:20.920 回答