【问题标题】:CUDA fill smaller arrays based on conditionsCUDA 根据条件填充较小的数组
【发布时间】:2019-03-04 11:08:42
【问题描述】:

假设我有一个数组

X = [1,2,3,4,5,6,7,8,9,10]

是否可以创建更小的数组并根据某些条件填充它们。例如,如果我想将 X 中的数字分成数组,例如

divisibleByTwo = [2,4,6,8,10]
divisibleByThree = [3,6,9]
divisibleByFour = [4,8]

如果我有非并行代码,它会是这样的

std::vector<int> divisibleByTwo;
for (int i=0; i<sizeof(x); i++)
{
    if (X[i]/2 == 0)
    {
        divisibleByTwo.emplace_back(X[i]);
    }
}

但我不能在 CUDA 中做同样的事情,因为那将是一个竞争条件

我真正想做的是比较两个数组,并将索引存储在条件匹配的新数组中。

例如,

A = [1,2,3]
B = [3,3,2]

我必须将 A 的所有元素与 B 进行比较,并找到元素相等的 B 的索引。所以结果将是一个数组数组,使得

C[0] = [ ]  // indexes of B matching element at index 0 of A (1)
c[1] = [2] // indexes of B matching element at index 1 of A (2)
c[2] = [0, 1] // indexes of B matching element at index 2 of A (3)

【问题讨论】:

  • 这与 CUDA 有什么关系?为什么您认为这可能是不可能的?
  • 我已经用更多细节编辑了我的问题

标签: cuda


【解决方案1】:

例如 divisibleByTwo,你可以启动 10 个 cuda 线程,然后执行以下操作:

__global__ void decimate(const float *x, float *y) {
   if(threadIdx.x<10 && threadIdx.x%2==0)
      y[threadIdx.x/2] = x[threadIdx.x];
}

在上面的例子中,一半的线程什么都不做。或者你可以用 5 个线程启动内核,

__global__ void decimate(const float *x, float *y) {
   if(threadIdx.x<5)
      y[threadIdx.x] = x[threadIdx.x*2];
}

【讨论】:

    猜你喜欢
    • 2015-03-08
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 2021-06-04
    • 1970-01-01
    • 2021-07-25
    相关资源
    最近更新 更多