【问题标题】:Compiler error when using thrust placeholders with the ternary operator ?:将推力占位符与三元运算符一起使用时出现编译器错误?:
【发布时间】:2015-07-25 15:39:12
【问题描述】:

我正在使用 Thrust 1.8,当我尝试编译以下代码时出现两个编译器错误:

#include <thrust/device_vector.h>
#include <thrust/functional.h>


int main(int argc, char* argv[])
{
    thrust::device_vector<bool> condition(100); 
    thrust::device_vector<int> input(100); 
    thrust::device_vector<float> result(100);
    float mean = 10.4f;

    thrust::transform(condition.begin(),condition.end(),input.begin(),result.begin(), ( (thrust::placeholders::_1 ) ? ( thrust::placeholders::_2) : (mean) ) );
}

当我尝试编译时,我得到以下编译时错误:

(对于占位符::_1)

错误:表达式必须是 bool 类型(或可转换为 bool)

(对于占位符::_2)

错误:操作数类型不兼容(“const推力::detail::functional::actor >”和“float”)

如何解决这个问题?

【问题讨论】:

    标签: cuda thrust


    【解决方案1】:

    您不能像尝试那样使用占位符,即与三元运算符结合使用。

    相反,您可以创建自己的函子:

    struct my_fun : public thrust::binary_function<bool,float,float>
    {
       float mean;
       my_fun(float mean) : mean(mean) {}
       __host__ __device__
       float operator()(bool condition, float input) const
       {
            float result = mean;
            if (condition)
            {
                 result = input;
            }
            return result;
        }
    };
    
    ...
    
    thrust::transform(condition.begin(),
                      condition.end(),
                      input.begin(),
                      result.begin(),
                      my_fun(mean));
    

    【讨论】:

    • @RobertCrovella 我只用零和一对其进行了测试,这很有效。你是对的,它不适用于任意值,所以我删除了它。感谢您指出。
    猜你喜欢
    • 2013-06-07
    • 2017-05-12
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    • 2012-01-09
    • 2012-12-18
    • 1970-01-01
    • 2020-10-21
    相关资源
    最近更新 更多