c++ - 如何使用带有独立迭代器的推力::变换?

标签 c++ iterator thrust

我在标题中的意思是,如果我想生成一个取决于 vector 中两个不同值的 vector ,我该如何在推力中做到这一点?

想象一下这个函数:

void foo(int rows, int columns)
{
    std::vector<int> blubb;
    for (int x = 0; x < rows; x++)
    {
        for (int y = 0; y < columns; y++)
        {
            blubb.push_back(x * y);
        }
    }
}

我似乎不知道如何轻松地将其转换为推力。

如果我将thrust::transform与thrust::counting_iterator一起使用,两个迭代器都会在每一步中递增,但我实际上想要可能的排列:

void fooGPU(int rows, int columns)
{
    thrust::device_vector<int> blubb(rows * columns);

    thrust::transform(thrust::make_counting_iterator<int>(0),
                      thrust::make_counting_iterator<int>(rows), 
                      thrust::make_counting_iterator<int>(0), 
                      blubb.begin(), 
                      thrust::multiplies<int>());
}

我感觉这个问题有一个非常简单的解决方案,但我没有看到。

最佳答案

您可以先将线性索引转换为行索引和列索引,然后将它们相乘。

thrust::transform(
  thrust::make_transform_iterator(thrust::make_counting_iterator(0),
                                  _1 % columns),
  thrust::make_transform_iterator(thrust::make_counting_iterator(0),
                                  _1 % columns) + rows * columns,
  thrust::make_transform_iterator(thrust::make_counting_iterator(0),
                                  _1 / columns),
  blubb.begin(), 
  _1 * _2);

您可以在此处找到有关占位符的简单示例。

https://thrust.github.io/doc/namespaceplaceholders.html

关于c++ - 如何使用带有独立迭代器的推力::变换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39206519/

相关文章:

java - 为什么向下转换在 C++ 中是一种不好的做法,而不是在另一种语言中?

c++ - 我可以在 QDomDocument 中找到所有具有给定属性的 XML 节点吗?

php - 将键值对的 PHP 数组转换为分层嵌套树结构

c++ - clock_gettime() CUDA 的时间问题

c++ - thrust::max_element 比较慢 cublasIsamax - 更有效的实现?

c++ - "Cross-compatible"代码在 linux 中吐出错误并在 windows 上编译正常! (GLM)

c++ - Qt - 没有滚动条的 QGraphicsView

java - Java 中集合的迭代

python - 装饰迭代器以在 python 中调用 next 之前更改值的好方法是什么?

c++ - 带偏移量的推力函数访问元素