cuda - Compiler error when using thrust placeholders with the ternary operator ?: -


i using thrust 1.8 , 2 compiler errors when try compile below code :

#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) ) ); } 

when try compile, following compiler time errors :

(for placeholders::_1)

error : expression must of bool type (or convertible bool)

(for placeholders::_2)

error : operand types incompatible ("const thrust::detail::functional::actor < thrust::detail::functional::argument<1u>>" , "float")

how correct this?

you cannot use placeholders tried to, i.e. in combination ternary operator.

instead, create own functor:

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)); 

Comments