AMP C ++加快了音量计算

时间:2013-09-03 07:46:02

标签: c++ performance parallel-processing gpu c++-amp

  • 装置:特斯拉C2050
  • 操作系统:Windows 7企业版
  • IDE:VS 2012

大家好。我正在使用AMP C ++进行一些体积计算。

我有数百万个四面体,其中一个点位于(0,0,0)。所以我可以用一种简单的方式得到四面体的体积:

sum += triangle.x1 * triangle.y2 * triangle.z3 + \
       triangle.y1 * triangle.z2 * triangle.x3 + \
       triangle.x2 * triangle.y3 * triangle.z1 - \
       triangle.x3 * triangle.y2 * triangle.z1 - \
       triangle.x2 * triangle.y1 * triangle.z3 - \
       triangle.y3 * triangle.z2 * triangle.x1;

所以,我想通过使用AMP C ++来加速我的计算。

这是代码。

typedef struct
{
    double x1;
    double y1;
    double z1;
    double x2;
    double y2;
    double z2;
    double x3;
    double y3;
    double z3;
} Triangle;

主要功能是:

accelerator my_accelerator(accelerator::default_accelerator);
accelerator_view acc_view = my_accelerator.get_default_view();

const int BLOCK_SIZE = 64;
int outputSize = int(numTriangles / BLOCK_SIZE);

int dimA = int(numTriangles / BLOCK_SIZE) * BLOCK_SIZE;
std::cout<<dimA<<std::endl;

//copy triangles from host to device
array<Triangle,1> triangle(numTriangles);
copy(vTriangle.begin(),vTriangle.end(), triangle);

//Volume
std::vector<double> volumeCPP;
for (int i=0; i < outputSize; i++)
{
    volumeCPP.push_back(double(0));
}
array_view<double,1> volume(outputSize,volumeCPP);
volume.discard_data();

clock_t start,finish;
start = clock();
parallel_for_each(
    volume.extent.tile<1>(),
    [=, &triangle](tiled_index<1> t_idx) restrict(amp)
    {
        double sum = 0.0f;
        tile_static Triangle tile_triangle[4];
        tile_triangle[t_idx.local[0]] = triangle[t_idx.global];
        if (t_idx.local[0] == 0)
        {
            for (int idx=0; idx < BLOCK_SIZE; idx++){
                sum += tile_triangle[idx].x1 * tile_triangle[idx].y2 * tile_triangle[idx].z3 + tile_triangle[idx].y1 * tile_triangle[idx].z2 * tile_triangle[idx].x3 + tile_triangle[idx].x2 * tile_triangle[idx].y3 * tile_triangle[idx].z1 - tile_triangle[idx].x3 * tile_triangle[idx].y2 * tile_triangle[idx].z1 - tile_triangle[idx].x2 * tile_triangle[idx].y1 * tile_triangle[idx].z3 - tile_triangle[idx].y3 * tile_triangle[idx].z2 * tile_triangle[idx].x1;
                //t_idx.barrier.wait();
            }
            //t_idx.barrier.wait();
        }
        volume[t_idx.global] = sum;
    }
);

acc_view.wait();
finish = clock();
copy(volume, volumeCPP.begin());

所以,每项工作都已失效。但有趣的是。它的成本高于CPU(单核)代码。

CPU上的C ++(单核)花费0.085秒来完成1024 * 1024 * 2三角形计算。 但AMP C ++代码的成本为0.530秒。比c ++代码更多。

在互联网上搜索后,有一个提示:如果我们首先预热设备,我们可以在计算中获得“实际”时间成本。

所以我首先计算128个三角形来预热设备(成本约为0.2秒),然后通过计算1024 * 1024 * 2个三角形来获得音量。它变得更快(成本约为0.091秒),但仍然比CPU(单核)代码慢。

我想知道为什么以及任何可以帮助我加快计算的人。

非常感谢。

2 个答案:

答案 0 :(得分:2)

首先,下面是我认为稍微更好的实现和一些评论。你的代码正在做一些可以避免的事情。

然而,你在这里真正做的是减少。这是一项经过深入研究和优化的算法。在AMP Algorithms Codeplex site上有一个C ++ AMP实现它实现为STL样式的算法。在得出结论C ++ AMP不能满足您的需求之前,我会尝试使用这个reduce实现,因为这样做很简单,可能会给你更好的性能。我很想知道你是怎么过的。

AMP Book Codeplex site包含一个用于计时C ++ AMP内核的辅助类。随书还讨论了实施减少问题。它有一整章。

void Foo()
{
    const int numTriangles = 128;
    std::vector<Triangle> vTriangle;

    accelerator my_accelerator(accelerator::default_accelerator);
    accelerator_view acc_view = my_accelerator.get_default_view();

    const int BLOCK_SIZE = 64;
    int outputSize = int(numTriangles / BLOCK_SIZE);

    const int dimA = numTriangles;
    std::cout<<dimA<<std::endl;

    //copy triangles from host to device
    // Use and array_view to automatically sync your data. 
    // You can use acc_view.flush() to make sure that copy is complete 
    // when you are running your timing code. Make this const so that AMP does
    // not copy your input data back to the CPU.

    array_view<const Triangle, 1> triangle(vTriangle.size(), vTriangle.data());

    //Volume
    // Don't push_back this causes (re)allocation as the vector grows. 
    // Set size and fill at the same time.

    std::vector<double> volumeCPP(outputSize, 0.0);

    array_view<double, 1> volume(outputSize, volumeCPP);
    volume.discard_data();

    // I would use the timing code on CodePlex. 
    // It will be more accurate than this.
    clock_t start, finish;
    start = clock();
    parallel_for_each(
        // Not sure a tile size of 1 will be handled that 
        // well by the runtime in terms of perf. I see why you
        // are doing it to get tile_static. You might be better off having larger tiles.

        volume.extent.tile<1>(),
        [=](tiled_index<1> t_idx) restrict(amp)
        {
            double sum = 0.0f;
            for (int idx = 0; idx < BLOCK_SIZE; idx++)
            {
                // Loading the single triangle into tiled memory is a good idea because
                // elements are read more than once.
                tile_static Triangle tile_triangle;
                tile_triangle = triangle[t_idx.global * BLOCK_SIZE + idx];

                sum += tile_triangle.x1 * tile_triangle.y2 * tile_triangle.z3 + 
                    tile_triangle.y1 * tile_triangle.z2 * tile_triangle.x3 + 
                    tile_triangle.x2 * tile_triangle.y3 * tile_triangle.z1 - 
                    tile_triangle.x3 * tile_triangle.y2 * tile_triangle.z1 - 
                    tile_triangle.x2 * tile_triangle.y1 * tile_triangle.z3 - 
                    tile_triangle.y3 * tile_triangle.z2 * tile_triangle.x1;
            }
            volume[t_idx.global] = sum;
        }
    );
    // Force data copy back to CPU.
    volume.synchronize();
    double sum = std::accumulate(begin(volumeCPP), end(volumeCPP), 0.0);
}

这是使用AMP算法库使用map / reduce模式实现问题解决方案的另一个示例。

std::vector<Triangle> triangles_cpu(1000);

array_view<const Triangle, 1> triangles_gpu(triangles_cpu.size(), triangles_cpu.data());
concurrency::array<double, 1> volumes_gpu(triangles_cpu.size());
array_view<double, 1> volumes_gpuvw(volumes_gpu);
amp_stl_algorithms::transform(begin(triangles_gpu), end(triangles_gpu), begin(volumes_gpuvw), 
    [=](const triangle& t) restrict(amp)
{
    return t.x1 * (t.y2 * t.z3 - t.y3 * t.z2)
        + t.y1 * (t.z2 * t.x3 - t.x2 * t.z3)
        + t.z1 * (t.x2 * t.y3 - t.x3 * t.y2);
});
double sum = amp_stl_algorithms::reduce(begin(volumes_gpuvw), end(volumes_gpuvw), 0.0);

答案 1 :(得分:0)

你应该能够通过分解来加快速度。

请注意您的四面体体积公式:

+ x1 * y2 * z3
+ y1 * z2 * x3
+ x2 * y3 * z1
- x3 * y2 * z1
- x2 * y1 * z3
- y3 * z2 * x1

相当于:

+ x1 * (y2 * z3 - y3 * z2)
+ y1 * (z2 * x3 - x2 * z3)
+ z1 * (x2 * y3 - x3 * y2)

原始公式有12次乘法,等效公式有9次乘法(减少25%)。很难说它会有多大的改善,但如果它给你20%的话,我也不会感到惊讶。