【问题标题】:How to carry out a counter number in parallel_for_each如何在parallel_for_each中进行计数器编号
【发布时间】:2016-01-25 11:05:56
【问题描述】:

1)为什么我的示例代码中的可变长度在计算后不是62?这是
似乎每次条件都满足,但是输入条件, 数字“长度不是每次都加。

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;
        }
    });
}

【问题讨论】:

  • 如果没有进一步的保护length[0][0] = length[0][0] + 1; 我没有阅读足够的关于concurrency::array 的内容,即使它的要点包括使某些操作线程安全(那不会是线程安全的)以下操作不能是线程安全的在一个更简单的数组中)。但它对此无能为力。所以每次这条线在两个线程上重叠时,最好的情况是你失去一个计数。
  • 首先,感谢您的回答,但即使我不使用 concurrency:array 来保存我的计数器,而是使用 tile_static int 或 int 来保存它。这一切都行不通。 EX:tile_static int 长度; if (cache[row][col] == 0){ length++;} 所以在parallel_for_each中保存计数器编号没有任何关系??
  • 使用 for 循环似乎没问题,但这只是解决方案的一种解决方法? for (int i = 0; i

标签: c++-amp


【解决方案1】:
    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);
        }
    });
}

【讨论】:

    猜你喜欢
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    相关资源
    最近更新 更多