【问题标题】:How to implement 2D torus without branches如何在没有分支的情况下实现 2D 环面
【发布时间】:2015-05-28 21:51:16
【问题描述】:

我需要为 CUDA 内核实现 2D 圆环,因此如果可能,它应该没有分支。

例如

  |  |  |
--0--1--2--
--3--4--5--
--6--7--8--
  |  |  |

所以线程 0 应该知道他的邻居是 1、3、6 和 2;等等。

我得到左右邻居如下:

right = (id + 1) % columns + firstRowId
left  = (id - 1) % columns + firstRowId

但我不知道如何有效地获取上下 id。

更新:firstRowId 表示其行的第一个元素的 id,不好的名字,对不起。

【问题讨论】:

    标签: c arrays algorithm multidimensional-array


    【解决方案1】:

    我认为这些应该可以解决问题:

    n = rows * columns;
    up = (id - columns + n) % n;
    down = (id + columns) % n;
    

    【讨论】:

    • 对于 firstRowId,我的意思是它的行的第一个 id,但无论如何我认为它可以忽略你的 firstRowId。我去测试一下,谢谢!
    • @rxp90 - 明白了。那是我最初拥有的,所以现在又回到了那个状态。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2010-09-10
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    相关资源
    最近更新 更多