【发布时间】:2016-10-07 08:51:58
【问题描述】:
我尝试在代码中定义两个分支:一个用于 CUDA 执行,另一个 - 没有它(考虑到未来的 OMP)。但是当我使用宏__CUDA_ARCH__ 时,它看起来好像总是在执行主机代码。但我认为 Thrust 默认使用 CUDA(以及设备代码的分支)。我的代码有什么问题?
这里是:
#include <thrust/transform.h>
#include <thrust/functional.h>
#include <thrust/iterator/counting_iterator.h>
#include <stdio.h>
struct my_op
{
my_op(int init_const) : constanta(init_const) {}
__host__ __device__ int operator()(const int &x) const
{
#if defined(__CUDA_ARCH__)
return 2 * x * constanta; // never executed - why?
#else
return x * constanta; // always executed
#endif
}
private:
int constanta;
};
int main()
{
int data[7] = { 0, 0, 0, 0, 0, 0, 0 };
thrust::counting_iterator<int> first(10);
thrust::counting_iterator<int> last = first + 7;
int init_value = 1;
my_op op(init_value);
thrust::transform(first, last, data, op);
for each (int el in data)
std::cout << el << " ";
std::cout << std::endl;
}
我希望“变换”将向量定义为乘以 2*constanta,但我看到使用了主机代码 - 输出是“10 11 12 13 14 15 16”,而不是“20 22 24 26 28 30 32” (如预期的那样)。
为什么?
【问题讨论】: