如何在parallel_for_each中执行计数器编号

时间:2016-01-25 11:05:56

标签: c++-amp

1)为什么我的示例代码中的变量长度在计算后不是62?它' S
  似乎每次满足条件,但进入条件,   数字"长度不是每次都添加。

2)如果我不使用concurrency :: array length(1,1,& V [0]);保存柜台   但是使用tile_static int,长度也是错误的。

//if my 8 x 8 local data are:
//cache[TS][TS] 
//{
//  -69, 0, 0, 1, 0, 0, 0, 0,
//    0, 0, 0, 0, 0, 0, 0, 0,
//    0, 0, 0, 0, 0, 0, 0, 0,
//    0, 0, 0, 0, 0, 0, 0, 0,
//    0, 0, 0, 0, 0, 0, 0, 0,
//    0, 0, 0, 0, 0, 0, 0, 0,
//    0, 0, 0, 0, 0, 0, 0, 0,
//    0, 0, 0, 0, 0, 0, 0, 0
//}

我的示例代码:

void sample()
{

    std::vector<int> V;
    V.push_back(0);
    concurrency::array<int, 2> length(1, 1, &V[0]); 

    const int TS = 8;
    concurrency::parallel_for_each(data.extent.tile<TS, TS>(), [=, &length](tiled_index<TS, TS> index) restrict(amp)
    {
        const int row = index.local[0];
        const int col = index.local[1];

        //tile_static int length; ---------2)
        tile_static  int cache[TS][TS];
        cache[row][col] = data[index.global];
        index.barrier.wait();

        if (cache[row][col] == 0)
        {
            //length++; -------------------2)
            length[0][0] = length[0][0] + 1;
        }
    });
}

1 个答案:

答案 0 :(得分:0)

    std::vector<int> V;

    for (int i = 0; i < 256; i++)
        V.push_back(0);

    concurrency::array_view<int, 1> AC_count(256, &V[0]);

    const int TS = 8;
    concurrency::parallel_for_each(data.extent.tile<TS, TS>(), [=, &length](tiled_index<TS, TS> index) restrict(amp)
    {
        const int row = index.local[0];
        const int col = index.local[1];

        tile_static  int cache[TS][TS];
        cache[row][col] = data[index.global];
        index.barrier.wait();

        if (cache[row][col] == 0)
        {
            atomic_fetch_add(&AC_count[0], 1);
        }
    });
}