我在标题中的意思是,如果我想生成一个取决于 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);
您可以在此处找到有关占位符的简单示例。
关于c++ - 如何使用带有独立迭代器的推力::变换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39206519/