Thrust and streams

Hello everyone,
I am having difficulties compiling Thrust example on streams.

I am using CUDA 8 with Thrust upgraded to 1.8.1, but I cannot compile this example, found in thrust\system\cuda\execution_policy.h

#include <thrust/for_each.h>
#include <thrust/system/cuda/execution_policy.h>
 
struct printf_functor
{
  cudaStream_t s;

  printf_functor(cudaStream_t s) : s(s) {}
 
  __host__ __device__
  void operator()(int)
  {
    printf("Hello, world from stream %p\n", static_cast<void*>(s));
  }
};
 
int main()
{
  // create two CUDA streams
  cudaStream_t s1, s2;
  cudaStreamCreate(&s1);
  cudaStreamCreate(&s2);

  thrust::counting_iterator<int> iter(0);

  // execute for_each on two different streams
  thrust::for_each(thrust::cuda::par(s1), iter, iter + 1, printf_functor(s1));
  thrust::for_each(thrust::cuda::par(s2), iter, iter + 1, printf_functor(s2));

  // synchronize with both streams
  cudaStreamSynchronize(s1);
  cudaStreamSynchronize(s2);

  // destroy streams
  cudaStreamDestroy(s1);
  cudaStreamDestroy(s2);

  return 0;
}

The problem is that thrust::cuda::par cannot take stream as argument, compiler (VS2012 x64) for lines 27 and 28 says “error : no instance of function template “thrust::system::cuda::detail::par_t::operator()” matches the argument list, argument types are: (cudaStream_t), object type is: const thrust::system::cuda::detail::par_t”

Compilation via CMAKE as

CUDA_ADD_LIBRARY(${MY_MODULE_NAME} SHARED ${SRC_CUDA_MY_MODULE} OPTIONS -DTHRUSTSORT_BUILD;--use_fast_math;-arch=compute_30)

(yes its built like lib, but I’ve just commented out my code and placed the example instead, just to test whether it compiles or not)

when I create a .cpp file with the same code, the error in VS2012 is as follows: error C2893: Failed to specialize function template ‘thrust::detail::enable_if<thrust::detail::is_allocator::value,thrust::detail::execute_with_allocator<Allocator,thrust::system::cuda::detail::execute_on_stream_base>>::type thrust::system::cuda::detail::par_t::operator ()(Allocator &) const’ With the following template arguments: ‘cudaStream_t’

What am I doing wrong?

Thanks for any help

change this:

thrust::for_each(thrust::cuda::par(s1), iter, iter + 1, printf_functor(s1));
thrust::for_each(thrust::cuda::par(s2), iter, iter + 1, printf_functor(s2));

to this:

thrust::for_each(thrust::cuda::par.on(s1), iter, iter + 1, printf_functor(s1));
thrust::for_each(thrust::cuda::par.on(s2), iter, iter + 1, printf_functor(s2));
                                //^^^

I’m not sure what you mean by this:

“I cannot compile this example, found in thrust\system\cuda\execution_policy.h”

That example code is not found in thrust\system\cuda\execution_policy.h

Thanks, will try.

And the example code is indeed there, in comments :)

Yes, you’re correct, it is there in thrust 1.8.x

The version of thrust that shipped with CUDA 8 GA2 (8.0.61) identifies itself as 1.8.3, so I’m not sure what you mean by this statement:

“I am using CUDA 8 with Thrust upgraded to 1.8.1”

That looks like a downgrade to me.

Anyway I was looking at CUDA 9RC by mistake, and the example has been stripped from the header there.

And there is a difference between 1.8.1 and 1.8.3 in this respect. The 1.8.3 (or current master, and what shipped with CUDA 8 GA2) has this particular example-in-the-comments fixed:

[url]thrust/execution_policy.h at 1.8.3 · NVIDIA/thrust · GitHub

I read here Thrust :: CUDA Toolkit Documentation

that 8.0 got shipped with 1.7.0 (ehm googled before actually checking it out, moreover my installation is 8.0.44)

Anyway, issue sorted out, thanks for Your help :)