Search code examples
lambdacudagpuplaceholderthrust

How to use thrust::placeholders to initialize the device vector in CUDA?


I'm new to lambda expression. I'm trying to initialize a thrust device vector data[10] with the following formula,

data[i] = i * 0.1 + 0.5;

I've included these header files and namespace

#include <thrust/device_vector.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/iterator/transform_iterator.h>
#include <thrust/functional.h>
using namespace thrust::placeholders;

Here's the code defining & initializing data,

thrust::device_vector<double> data(
        thrust::make_transform_iterator(
                thrust::counting_iterator<double>(0), _1 * 0.1 + 0.5),
        thrust::make_transform_iterator(
                thrust::counting_iterator<double>(0), _1 * 0.1 + 0.5) + 10);

But it can not be compiled, showing very long error msg as shown at the end of the post.

Am I doing anything wrong?

I'm using CUDA 5.0, CentOS 6.3 x86_64, gcc 4.4.6

/usr/local/cuda-5.0/bin/../include/thrust/iterator/detail/transform_iterator.inl(94): error: no instance of overloaded function "thrust::detail::functional::actor<Eval>::operator() [with Eval=thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::plus>, thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::multiplies>, thrust::detail::functional::actor<thrust::detail::functional::argument<0U>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>]" matches the argument list
            argument types are: (double)
            object type is: thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::plus>, thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::multiplies>, thrust::detail::functional::actor<thrust::detail::functional::argument<0U>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>
          detected during:
            instantiation of "thrust::detail::backend::dereference_result<thrust::transform_iterator<UnaryFunc, Iterator, Reference, Value>>::type thrust::detail::backend::dereference(const thrust::transform_iterator<UnaryFunc, Iterator, Reference, Value> &) [with UnaryFunc=thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::plus>, thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::multiplies>, thrust::detail::functional::actor<thrust::detail::functional::argument<0U>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, Iterator=thrust::counting_iterator<double, thrust::use_default, thrust::use_default, thrust::use_default>, Reference=thrust::use_default, Value=thrust::use_default]" 
/usr/local/cuda-5.0/bin/../include/thrust/iterator/detail/zip_iterator_base.h(128): here
            instantiation of "thrust::detail::device_dereference_iterator::apply<Iterator>::type thrust::detail::device_dereference_iterator::operator()(const Iterator &) [with Iterator=thrust::transform_iterator<thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::plus>, thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::multiplies>, thrust::detail::functional::actor<thrust::detail::functional::argument<0U>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::counting_iterator<double, thrust::use_default, thrust::use_default, thrust::use_default>, thrust::use_default, thrust::use_default>]" 
/usr/local/cuda-5.0/bin/../include/thrust/detail/tuple_transform.h(102): here
            instantiation of "thrust::detail::tuple_meta_transform<Tuple, UnaryMetaFunction, thrust::tuple_size<Tuple>::value>::type thrust::detail::tuple_transform_functor<Tuple, UnaryMetaFunction, UnaryFunction, 2U>::do_it_on_the_host_or_device(const Tuple &, UnaryFunction) [with Tuple=thrust::tuple<thrust::transform_iterator<thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::plus>, thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::multiplies>, thrust::detail::functional::actor<thrust::detail::functional::argument<0U>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::counting_iterator<double, thrust::use_default, thrust::use_default, thrust::use_default>, thrust::use_default, thrust::use_default>, thrust::detail::normal_iterator<thrust::device_ptr<double>>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, UnaryMetaFunction=thrust::detail::device_dereference_iterator::apply, UnaryFunction=thrust::detail::device_dereference_iterator]" 
/usr/local/cuda-5.0/bin/../include/thrust/detail/tuple_transform.h(412): here
            instantiation of "thrust::detail::tuple_meta_transform<Tuple, UnaryMetaFunction, thrust::tuple_size<Tuple>::value>::type thrust::detail::tuple_host_device_transform<UnaryMetaFunction,Tuple,UnaryFunction>(const Tuple &, UnaryFunction) [with UnaryMetaFunction=thrust::detail::device_dereference_iterator::apply, Tuple=thrust::tuple<thrust::transform_iterator<thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::plus>, thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::multiplies>, thrust::detail::functional::actor<thrust::detail::functional::argument<0U>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::counting_iterator<double, thrust::use_default, thrust::use_default, thrust::use_default>, thrust::use_default, thrust::use_default>, thrust::detail::normal_iterator<thrust::device_ptr<double>>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>, UnaryFunction=thrust::detail::device_dereference_iterator]" 
/usr/local/cuda-5.0/bin/../include/thrust/iterator/detail/zip_iterator.inl(160): here
            instantiation of "thrust::detail::backend::dereference_result<thrust::zip_iterator<IteratorTuple>>::type thrust::detail::backend::dereference(const thrust::zip_iterator<IteratorTuple> &) [with IteratorTuple=thrust::tuple<thrust::transform_iterator<thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::plus>, thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::multiplies>, thrust::detail::functional::actor<thrust::detail::functional::argument<0U>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::counting_iterator<double, thrust::use_default, thrust::use_default, thrust::use_default>, thrust::use_default, thrust::use_default>, thrust::detail::normal_iterator<thrust::device_ptr<double>>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>]" 
/usr/local/cuda-5.0/bin/../include/thrust/detail/backend/cuda/for_each.inl(75): here
            [ 24 instantiation contexts not shown ]
            instantiation of "void thrust::detail::vector_base<T, Alloc>::allocate_and_copy(thrust::detail::vector_base<T, Alloc>::size_type, ForwardIterator, ForwardIterator, thrust::detail::vector_base<T, Alloc>::storage_type &) [with T=double, Alloc=thrust::device_malloc_allocator<double>, ForwardIterator=thrust::transform_iterator<thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::plus>, thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::multiplies>, thrust::detail::functional::actor<thrust::detail::functional::argument<0U>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::counting_iterator<double, thrust::use_default, thrust::use_default, thrust::use_default>, thrust::use_default, thrust::use_default>]" 
/usr/local/cuda-5.0/bin/../include/thrust/detail/vector_base.inl(191): here
            instantiation of "void thrust::detail::vector_base<T, Alloc>::range_init(ForwardIterator, ForwardIterator, thrust::detail::false_type) [with T=double, Alloc=thrust::device_malloc_allocator<double>, ForwardIterator=thrust::transform_iterator<thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::plus>, thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::multiplies>, thrust::detail::functional::actor<thrust::detail::functional::argument<0U>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::counting_iterator<double, thrust::use_default, thrust::use_default, thrust::use_default>, thrust::use_default, thrust::use_default>]" 
/usr/local/cuda-5.0/bin/../include/thrust/detail/vector_base.inl(168): here
            instantiation of "void thrust::detail::vector_base<T, Alloc>::init_dispatch(IteratorOrIntegralType, IteratorOrIntegralType, thrust::detail::false_type) [with T=double, Alloc=thrust::device_malloc_allocator<double>, IteratorOrIntegralType=thrust::transform_iterator<thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::plus>, thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::multiplies>, thrust::detail::functional::actor<thrust::detail::functional::argument<0U>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::counting_iterator<double, thrust::use_default, thrust::use_default, thrust::use_default>, thrust::use_default, thrust::use_default>]" 
/usr/local/cuda-5.0/bin/../include/thrust/detail/vector_base.inl(207): here
            instantiation of "thrust::detail::vector_base<T, Alloc>::vector_base(InputIterator, InputIterator) [with T=double, Alloc=thrust::device_malloc_allocator<double>, InputIterator=thrust::transform_iterator<thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::plus>, thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::multiplies>, thrust::detail::functional::actor<thrust::detail::functional::argument<0U>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::counting_iterator<double, thrust::use_default, thrust::use_default, thrust::use_default>, thrust::use_default, thrust::use_default>]" 
/usr/local/cuda-5.0/bin/../include/thrust/device_vector.h(131): here
            instantiation of "thrust::device_vector<T, Alloc>::device_vector(InputIterator, InputIterator) [with T=double, Alloc=thrust::device_malloc_allocator<double>, InputIterator=thrust::transform_iterator<thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::plus>, thrust::detail::functional::actor<thrust::detail::functional::composite<thrust::detail::functional::binary_operator<thrust::multiplies>, thrust::detail::functional::actor<thrust::detail::functional::argument<0U>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::detail::functional::value<double>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, thrust::counting_iterator<double, thrust::use_default, thrust::use_default, thrust::use_default>, thrust::use_default, thrust::use_default>]" 
test/lambda.cu(27): here

Solution

  • Thanks to @RobertCrovella's suggestion.

    After upgrading thrust to v1.6/v1.7, the issue is fixed without changing the code showing in the question. thrust v1.53 in cuda 5.0 seems too old for the lambda expression feature.