Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GPGPU Programming: A tutorial about Thrust

GPGPU Programming: A tutorial about Thrust

I made this presentation in the framework of a lecture about the use of GPU for scientific computing in my University.
These slides intends to show some interesting aspects of Thrust : mixing handwritten kernels with generic thrust code, How to harness multi GPU using Thrust, writting generic code for CPU and GPU,...
At the end I present some small usecase in the field of simple linear algebra and variational signal processing.
Code : https://github.com/gnthibault/Cuda_Thrust_Introduction

Thibault Notargiacomo

June 17, 2015
Tweet

Other Decks in Programming

Transcript

  1. gipsa-lab Plan •Introduction : What is Thrust ? •1: Studying

    the device_vector class •2: Thrust, an asynchronous library •3: Thrust versatility : CPU/GPU •4: Thrust UVA and Multi GPU •5: Convex optimization using Thrust •Interesting links •Conclusion 2
  2. gipsa-lab Compiling : Don’t be Afraid ! 4 notargth@archi-005:~/Projets/Cuda_Thrust_Introduction/build$ make

    install [ 20%] Built target HostDeviceVector [ 40%] Built target DeviceBackend [ 60%] Built target AsynchronousLaunch [ 80%] Built target MultiGpuThrust [100%] Building NVCC (Device) object ThrustVectorWrappingCublas/CMakeFiles/ThrustVectorWrappingCublas.dir/ThrustVectorWrappingCublas_generated_main.cu.o /softs/cuda-7.0.28/include/thrust/detail/internal_functional.h(322): error: expression must be a modifiable lvalue detected during: instantiation of "thrust::detail::enable_if_non_const_reference_or_tuple_of_iterator_references<thrust::tuple_element<1, Tuple>::type>::type thrust::detail::unary_transform_functor<UnaryFunction>::operator()(Tuple) [with UnaryFunction=thrust::identity<float>, Tuple=thrust::detail::tuple_of_iterator_references<float &, const float &, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>]" /softs/cuda-7.0.28/include/thrust/detail/function.h(60): here instantiation of "Result thrust::detail::wrapped_function<Function, Result>::operator()(const Argument &) const [with Function=thrust::detail::unary_transform_functor<thrust::identity<float>>, Result=void, Argument=thrust::detail::tuple_of_iterator_references<thrust::device_reference<float>, thrust::device_reference<const float>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>]" /softs/cuda-7.0.28/include/thrust/system/cuda/detail/for_each.inl(57): here instantiation of "void thrust::system::cuda::detail::for_each_n_detail::for_each_kernel::operator()(thrust::system::cuda::detail::bulk_::parallel_group<thrust::system::cuda::detail::bulk_::concurrent_group<thrust::system::cuda::detail::bulk_::agent<1UL>, 0UL>, 0UL> &, Iterator, Function, Size) [with Iterator=thrust::zip_iterator<thrust::tuple<thrust::device_ptr<float>, thrust::device_ptr<const float>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, Function=thrust::detail::wrapped_function<thrust::detail::unary_transform_functor<thrust::identity<float>>, void>, Size=unsigned int]" /softs/cuda-7.0.28/include/thrust/system/cuda/detail/bulk/detail/apply_from_tuple.hpp(71): here instantiation of "void thrust::system::cuda::detail::bulk_::detail::apply_from_tuple(Function, const thrust::tuple<Arg1, Arg2, Arg3, Arg4, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type> &) [with Function=thrust::system::cuda::detail::for_each_n_detail::for_each_kernel, Arg1=thrust::system::cuda::detail::bulk_::parallel_group<thrust::system::cuda::detail::bulk_::concurrent_group<thrust::system::cuda::detail::bulk_::agent<1UL>, 0UL>, 0UL> &, Arg2=thrust::zip_iterator<thrust::tuple<thrust::device_ptr<float>, thrust::device_ptr<const float>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, Arg3=thrust::detail::wrapped_function<thrust::detail::unary_transform_functor<thrust::identity<float>>, void>, Arg4=unsigned int]" /softs/cuda-7.0.28/include/thrust/system/cuda/detail/bulk/detail/closure.hpp(50): here instantiation of "void thrust::system::cuda::detail::bulk_::detail::closure<Function, Tuple>::operator()() [with Function=thrust::system::cuda::detail::for_each_n_detail::for_each_kernel, Tuple=thrust::tuple<thrust::system::cuda::detail::bulk_::parallel_group<thrust::system::cuda::detail::bulk_::concurrent_group<thrust::system::cuda::detail::bulk_::agent<1UL>, 0UL>, 0UL> &, thrust::zip_iterator<thrust::tuple<thrust::device_ptr<float>, thrust::device_ptr<const float>, 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::wrapped_function<thrust::detail::unary_transform_functor<thrust::identity<float>>, void>, unsigned int, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>]" /softs/cuda-7.0.28/include/thrust/system/cuda/detail/bulk/detail/cuda_task.hpp(58): here [ 33 instantiation contexts not shown ] instantiation of "OutputIterator thrust::adjacent_difference(const thrust::detail::execution_policy_base<DerivedPolicy> &, InputIterator, InputIterator, OutputIterator, BinaryFunction) [with DerivedPolicy=thrust::system::cuda::detail::tag, InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const float>>, BinaryFunction=thrust::minus<float>]" /softs/cuda-7.0.28/include/thrust/system/detail/generic/adjacent_difference.inl(44): here instantiation of "OutputIterator thrust::system::detail::generic::adjacent_difference(thrust::execution_policy<DerivedPolicy> &, InputIterator, InputIterator, OutputIterator) [with DerivedPolicy=thrust::system::cuda::detail::tag, InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const float>>]" /softs/cuda-7.0.28/include/thrust/detail/adjacent_difference.inl(39): here instantiation of "OutputIterator thrust::adjacent_difference(const thrust::detail::execution_policy_base<DerivedPolicy> &, InputIterator, InputIterator, OutputIterator) [with DerivedPolicy=thrust::system::cuda::detail::tag, InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const float>>]" /softs/cuda-7.0.28/include/thrust/detail/adjacent_difference.inl(68): here instantiation of "OutputIterator thrust::adjacent_difference(InputIterator, InputIterator, OutputIterator) [with InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const float>>]" /home/notargth/Projets/Cuda_Thrust_Introduction/ThrustVectorWrappingCublas/ThrustWrapper.cu.h(126): here instantiation of "void ThrustVectorWrapper<T>::FiniteForwardDifference(const ThrustVectorWrapper<T> &) [with T=float]" /home/notargth/Projets/Cuda_Thrust_Introduction/ThrustVectorWrappingCublas/Optimisation.cu.h(162): here /softs/cuda-7.0.28/include/thrust/system/cuda/detail/assign_value.h(91): error: expression must be a modifiable lvalue detected during: instantiation of "void thrust::system::cuda::detail::assign_value(thrust::system::cuda::detail::execution_policy<DerivedPolicy> &, Pointer1, Pointer2) [with DerivedPolicy=thrust::system::cuda::detail::tag, Pointer1=thrust::device_ptr<const float>, Pointer2=thrust::device_ptr<float>]" /softs/cuda-7.0.28/include/thrust/detail/reference.inl(171): here instantiation of "void thrust::reference<Element, Pointer, Derived>::strip_const_assign_value(const System &, OtherPointer) [with Element=const float, Pointer=thrust::device_ptr<const float>, Derived=thrust::device_reference<const float>, System=thrust::device_system_tag, OtherPointer=thrust::device_ptr<float>]" /softs/cuda-7.0.28/include/thrust/detail/reference.inl(139): here instantiation of "void thrust::reference<Element, Pointer, Derived>::assign_from(System1 *, System2 *, OtherPointer) [with Element=const float, Pointer=thrust::device_ptr<const float>, Derived=thrust::device_reference<const float>, System1=thrust::device_system_tag, System2=thrust::device_system_tag, OtherPointer=thrust::device_ptr<float>]" /softs/cuda-7.0.28/include/thrust/detail/reference.inl(158): here instantiation of "void thrust::reference<Element, Pointer, Derived>::assign_from(OtherPointer) [with Element=const float, Pointer=thrust::device_ptr<const float>, Derived=thrust::device_reference<const float>, OtherPointer=thrust::device_ptr<float>]" /softs/cuda-7.0.28/include/thrust/detail/reference.inl(86): here instantiation of "thrust::reference<Element, Pointer, Derived>::derived_type &thrust::reference<Element, Pointer, Derived>::operator=(const thrust::reference<OtherElement, OtherPointer, OtherDerived> &) [with Element=const float, Pointer=thrust::device_ptr<const float>, Derived=thrust::device_reference<const float>, OtherElement=float, OtherPointer=thrust::device_ptr<float>, OtherDerived=thrust::device_reference<float>]" /softs/cuda-7.0.28/include/thrust/detail/device_reference.inl(34): here [ 10 instantiation contexts not shown ] instantiation of "OutputIterator thrust::adjacent_difference(const thrust::detail::execution_policy_base<DerivedPolicy> &, InputIterator, InputIterator, OutputIterator, BinaryFunction) [with DerivedPolicy=thrust::system::cuda::detail::tag,
  3. gipsa-lab Compiling : Don’t be Afraid ! 5 InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const

    float>>, BinaryFunction=thrust::minus<float>]" /softs/cuda-7.0.28/include/thrust/system/detail/generic/adjacent_difference.inl(44): here instantiation of "OutputIterator thrust::system::detail::generic::adjacent_difference(thrust::execution_policy<DerivedPolicy> &, InputIterator, InputIterator, OutputIterator) [with DerivedPolicy=thrust::system::cuda::detail::tag, InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const float>>]" /softs/cuda-7.0.28/include/thrust/detail/adjacent_difference.inl(39): here instantiation of "OutputIterator thrust::adjacent_difference(const thrust::detail::execution_policy_base<DerivedPolicy> &, InputIterator, InputIterator, OutputIterator) [with DerivedPolicy=thrust::system::cuda::detail::tag, InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const float>>]" /softs/cuda-7.0.28/include/thrust/detail/adjacent_difference.inl(68): here instantiation of "OutputIterator thrust::adjacent_difference(InputIterator, InputIterator, OutputIterator) [with InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const float>>]" /home/notargth/Projets/Cuda_Thrust_Introduction/ThrustVectorWrappingCublas/ThrustWrapper.cu.h(126): here instantiation of "void ThrustVectorWrapper<T>::FiniteForwardDifference(const ThrustVectorWrapper<T> &) [with T=float]" /home/notargth/Projets/Cuda_Thrust_Introduction/ThrustVectorWrappingCublas/Optimisation.cu.h(162): here /softs/cuda-7.0.28/include/thrust/system/cuda/detail/trivial_copy.inl(108): error: a value of type "const float *" cannot be used to initialize an entity of type "void *" detected during: instantiation of "void thrust::system::cuda::detail::trivial_copy_n(thrust::system::cuda::detail::cross_system<System1, System2> &, RandomAccessIterator1, Size, RandomAccessIterator2) [with System1=thrust::host_system_tag, System2=thrust::system::cuda::detail::tag, RandomAccessIterator1=const float *, Size=std::ptrdiff_t, RandomAccessIterator2=thrust::device_ptr<const float>]" /softs/cuda-7.0.28/include/thrust/system/cuda/detail/copy_cross_system.inl(151): here instantiation of "RandomAccessIterator2 thrust::system::cuda::detail::copy_cross_system(thrust::system::cuda::detail::cross_system<System1, System2>, RandomAccessIterator1, RandomAccessIterator1, RandomAccessIterator2, thrust::random_access_traversal_tag, thrust::random_access_traversal_tag, thrust::detail::true_type) [with System1=thrust::host_system_tag, System2=thrust::system::cuda::detail::tag, RandomAccessIterator1=const float *, RandomAccessIterator2=thrust::device_ptr<const float>]" /softs/cuda-7.0.28/include/thrust/system/cuda/detail/copy_cross_system.inl(245): here instantiation of "RandomAccessIterator2 thrust::system::cuda::detail::copy_cross_system(thrust::system::cuda::detail::cross_system<System1, System2>, RandomAccessIterator1, RandomAccessIterator1, RandomAccessIterator2, thrust::random_access_traversal_tag, thrust::random_access_traversal_tag) [with System1=thrust::host_system_tag, System2=thrust::system::cuda::detail::tag, RandomAccessIterator1=const float *, RandomAccessIterator2=thrust::device_ptr<const float>]" /softs/cuda-7.0.28/include/thrust/system/cuda/detail/copy_cross_system.inl(279): here instantiation of "OutputIterator thrust::system::cuda::detail::copy_cross_system(thrust::system::cuda::detail::cross_system<System1, System2>, InputIterator, InputIterator, OutputIterator) [with System1=thrust::host_system_tag, System2=thrust::system::cuda::detail::tag, InputIterator=const float *, OutputIterator=thrust::device_ptr<const float>]" /softs/cuda-7.0.28/include/thrust/system/cuda/detail/copy.inl(54): here instantiation of "OutputIterator thrust::system::cuda::detail::copy(thrust::system::cuda::detail::cross_system<System1, System2>, InputIterator, InputIterator, OutputIterator) [with System1=thrust::host_system_tag, System2=thrust::system::cuda::detail::tag, InputIterator=const float *, OutputIterator=thrust::device_ptr<const float>]" /softs/cuda-7.0.28/include/thrust/detail/copy.inl(37): here [ 16 instantiation contexts not shown ] instantiation of "OutputIterator thrust::adjacent_difference(const thrust::detail::execution_policy_base<DerivedPolicy> &, InputIterator, InputIterator, OutputIterator, BinaryFunction) [with DerivedPolicy=thrust::system::cuda::detail::tag, InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const float>>, BinaryFunction=thrust::minus<float>]" /softs/cuda-7.0.28/include/thrust/system/detail/generic/adjacent_difference.inl(44): here instantiation of "OutputIterator thrust::system::detail::generic::adjacent_difference(thrust::execution_policy<DerivedPolicy> &, InputIterator, InputIterator, OutputIterator) [with DerivedPolicy=thrust::system::cuda::detail::tag, InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const float>>]" /softs/cuda-7.0.28/include/thrust/detail/adjacent_difference.inl(39): here instantiation of "OutputIterator thrust::adjacent_difference(const thrust::detail::execution_policy_base<DerivedPolicy> &, InputIterator, InputIterator, OutputIterator) [with DerivedPolicy=thrust::system::cuda::detail::tag, InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const float>>]" /softs/cuda-7.0.28/include/thrust/detail/adjacent_difference.inl(68): here instantiation of "OutputIterator thrust::adjacent_difference(InputIterator, InputIterator, OutputIterator) [with InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const float>>]" /home/notargth/Projets/Cuda_Thrust_Introduction/ThrustVectorWrappingCublas/ThrustWrapper.cu.h(126): here instantiation of "void ThrustVectorWrapper<T>::FiniteForwardDifference(const ThrustVectorWrapper<T> &) [with T=float]" /home/notargth/Projets/Cuda_Thrust_Introduction/ThrustVectorWrappingCublas/Optimisation.cu.h(162): here /softs/cuda-7.0.28/include/thrust/detail/internal_functional.h(322): error: expression must be a modifiable lvalue detected during: instantiation of "thrust::detail::enable_if_non_const_reference_or_tuple_of_iterator_references<thrust::tuple_element<1, Tuple>::type>::type thrust::detail::unary_transform_functor<UnaryFunction>::operator()(Tuple) [with UnaryFunction=thrust::identity<float>, Tuple=thrust::detail::tuple_of_iterator_references<const float &, const float &, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>]" /softs/cuda-7.0.28/include/thrust/detail/function.h(60): here instantiation of "Result thrust::detail::wrapped_function<Function, Result>::operator()(const Argument &) const [with Function=thrust::detail::unary_transform_functor<thrust::identity<float>>, Result=void, Argument=thrust::detail::tuple_of_iterator_references<const float &, thrust::device_reference<const float>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>]" /softs/cuda-7.0.28/include/thrust/system/cuda/detail/for_each.inl(57): here instantiation of "void thrust::system::cuda::detail::for_each_n_detail::for_each_kernel::operator()(thrust::system::cuda::detail::bulk_::parallel_group<thrust::system::cuda::detail::bulk_::concurrent_group<thrust::system::cuda::detail::bulk_::agent<1UL>, 0UL>, 0UL> &, Iterator, Function, Size) [with Iterator=thrust::zip_iterator<thrust::tuple<const float *, thrust::device_ptr<const float>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, Function=thrust::detail::wrapped_function<thrust::detail::unary_transform_functor<thrust::identity<float>>, void>, Size=unsigned int]" /softs/cuda-7.0.28/include/thrust/system/cuda/detail/bulk/detail/apply_from_tuple.hpp(71): here instantiation of "void thrust::system::cuda::detail::bulk_::detail::apply_from_tuple(Function, const thrust::tuple<Arg1, Arg2, Arg3, Arg4, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type> &) [with Function=thrust::system::cuda::detail::for_each_n_detail::for_each_kernel, Arg1=thrust::system::cuda::detail::bulk_::parallel_group<thrust::system::cuda::detail::bulk_::concurrent_group<thrust::system::cuda::detail::bulk_::agent<1UL>, 0UL>, 0UL> &, Arg2=thrust::zip_iterator<thrust::tuple<const float *, thrust::device_ptr<const float>, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>>, Arg3=thrust::detail::wrapped_function<thrust::detail::unary_transform_functor<thrust::identity<float>>, void>, Arg4=unsigned int]" /softs/cuda-7.0.28/include/thrust/system/cuda/detail/bulk/detail/closure.hpp(50): here instantiation of "void thrust::system::cuda::detail::bulk_::detail::closure<Function, Tuple>::operator()() [with Function=thrust::system::cuda::detail::for_each_n_detail::for_each_kernel, Tuple=thrust::tuple<thrust::system::cuda::detail::bulk_::parallel_group<thrust::system::cuda::detail::bulk_::concurrent_group<thrust::system::cuda::detail::bulk_::agent<1UL>, 0UL>, 0UL> &, thrust::zip_iterator<thrust::tuple<const float *, thrust::device_ptr<const float>, 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::wrapped_function<thrust::detail::unary_transform_functor<thrust::identity<float>>, void>, unsigned int, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type, thrust::null_type>]" /softs/cuda-7.0.28/include/thrust/system/cuda/detail/bulk/detail/cuda_task.hpp(58): here
  4. gipsa-lab Compiling : Don’t be Afraid ! 6 [ 34

    instantiation contexts not shown ] instantiation of "OutputIterator thrust::adjacent_difference(const thrust::detail::execution_policy_base<DerivedPolicy> &, InputIterator, InputIterator, OutputIterator, BinaryFunction) [with DerivedPolicy=thrust::system::cuda::detail::tag, InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const float>>, BinaryFunction=thrust::minus<float>]" /softs/cuda-7.0.28/include/thrust/system/detail/generic/adjacent_difference.inl(44): here instantiation of "OutputIterator thrust::system::detail::generic::adjacent_difference(thrust::execution_policy<DerivedPolicy> &, InputIterator, InputIterator, OutputIterator) [with DerivedPolicy=thrust::system::cuda::detail::tag, InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const float>>]" /softs/cuda-7.0.28/include/thrust/detail/adjacent_difference.inl(39): here instantiation of "OutputIterator thrust::adjacent_difference(const thrust::detail::execution_policy_base<DerivedPolicy> &, InputIterator, InputIterator, OutputIterator) [with DerivedPolicy=thrust::system::cuda::detail::tag, InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const float>>]" /softs/cuda-7.0.28/include/thrust/detail/adjacent_difference.inl(68): here instantiation of "OutputIterator thrust::adjacent_difference(InputIterator, InputIterator, OutputIterator) [with InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const float>>]" /home/notargth/Projets/Cuda_Thrust_Introduction/ThrustVectorWrappingCublas/ThrustWrapper.cu.h(126): here instantiation of "void ThrustVectorWrapper<T>::FiniteForwardDifference(const ThrustVectorWrapper<T> &) [with T=float]" /home/notargth/Projets/Cuda_Thrust_Introduction/ThrustVectorWrappingCublas/Optimisation.cu.h(162): here /softs/cuda-7.0.28/include/thrust/system/cuda/detail/assign_value.h(91): error: expression must be a modifiable lvalue detected during: instantiation of "void thrust::system::cuda::detail::assign_value(thrust::system::cuda::detail::execution_policy<DerivedPolicy> &, Pointer1, Pointer2) [with DerivedPolicy=thrust::system::cuda::detail::tag, Pointer1=thrust::device_ptr<const float>, Pointer2=const float *]" (179): here instantiation of "void thrust::system::cuda::detail::assign_value(thrust::system::cuda::detail::cross_system<System1, System2> &, Pointer1, Pointer2) [with System1=thrust::system::cuda::detail::tag, System2=thrust::host_system_tag, Pointer1=thrust::device_ptr<const float>, Pointer2=const float *]" /softs/cuda-7.0.28/include/thrust/detail/reference.inl(171): here instantiation of "void thrust::reference<Element, Pointer, Derived>::strip_const_assign_value(const System &, OtherPointer) [with Element=const float, Pointer=thrust::device_ptr<const float>, Derived=thrust::device_reference<const float>, System=thrust::system::cuda::detail::cross_system<thrust::system::cuda::detail::tag, thrust::host_system_tag>, OtherPointer=const float *]" /softs/cuda-7.0.28/include/thrust/detail/reference.inl(139): here instantiation of "void thrust::reference<Element, Pointer, Derived>::assign_from(System1 *, System2 *, OtherPointer) [with Element=const float, Pointer=thrust::device_ptr<const float>, Derived=thrust::device_reference<const float>, System1=thrust::device_system_tag, System2=thrust::host_system_tag, OtherPointer=const float *]" /softs/cuda-7.0.28/include/thrust/detail/reference.inl(158): here instantiation of "void thrust::reference<Element, Pointer, Derived>::assign_from(OtherPointer) [with Element=const float, Pointer=thrust::device_ptr<const float>, Derived=thrust::device_reference<const float>, OtherPointer=const float *]" /softs/cuda-7.0.28/include/thrust/detail/reference.inl(65): here [ 11 instantiation contexts not shown ] instantiation of "OutputIterator thrust::adjacent_difference(const thrust::detail::execution_policy_base<DerivedPolicy> &, InputIterator, InputIterator, OutputIterator, BinaryFunction) [with DerivedPolicy=thrust::system::cuda::detail::tag, InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const float>>, BinaryFunction=thrust::minus<float>]" /softs/cuda-7.0.28/include/thrust/system/detail/generic/adjacent_difference.inl(44): here instantiation of "OutputIterator thrust::system::detail::generic::adjacent_difference(thrust::execution_policy<DerivedPolicy> &, InputIterator, InputIterator, OutputIterator) [with DerivedPolicy=thrust::system::cuda::detail::tag, InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const float>>]" /softs/cuda-7.0.28/include/thrust/detail/adjacent_difference.inl(39): here instantiation of "OutputIterator thrust::adjacent_difference(const thrust::detail::execution_policy_base<DerivedPolicy> &, InputIterator, InputIterator, OutputIterator) [with DerivedPolicy=thrust::system::cuda::detail::tag, InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const float>>]" /softs/cuda-7.0.28/include/thrust/detail/adjacent_difference.inl(68): here instantiation of "OutputIterator thrust::adjacent_difference(InputIterator, InputIterator, OutputIterator) [with InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<float>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<const float>>]" /home/notargth/Projets/Cuda_Thrust_Introduction/ThrustVectorWrappingCublas/ThrustWrapper.cu.h(126): here instantiation of "void ThrustVectorWrapper<T>::FiniteForwardDifference(const ThrustVectorWrapper<T> &) [with T=float]" /home/notargth/Projets/Cuda_Thrust_Introduction/ThrustVectorWrappingCublas/Optimisation.cu.h(162): here 5 errors detected in the compilation of "/tmp/tmpxft_000007bd_00000000-7_main.cpp1.ii". CMake Error at ThrustVectorWrappingCublas_generated_main.cu.o.cmake:264 (message): Error generating file /home/notargth/Projets/Cuda_Thrust_Introduction/build/ThrustVectorWrappingCublas/CMakeFiles/ThrustVectorWrappingCublas.dir//./ThrustVectorWrappingCublas_generated_main.cu.o make[2]: *** [ThrustVectorWrappingCublas/CMakeFiles/ThrustVectorWrappingCublas.dir/ThrustVectorWrappingCublas_generated_main.cu.o] Erreur 1 make[1]: *** [ThrustVectorWrappingCublas/CMakeFiles/ThrustVectorWrappingCublas.dir/all] Erreur 2 make: *** [all] Erreur 2
  5. gipsa-lab 1: device_vector class 8 •What it is: •A «

    container » •Cuda buffer Wrapper •Equivalent of std::vector<T> •What it allows: •Equivalent of <algorithm> : fill, generate, reduce, sort, … •Automatic allocation/destruction •Handle some cuda error •Ease host/device copy management. •What it cannot do: •Wrap cuda array, 1D,2D,3D textures nor surfaces •Bound checking per se
  6. gipsa-lab 1:Classic usage 9 //Thrust Device vectors intend to mimic

    std::vector class from stl, plus its algorithms thrust::device_vector<int> deviceVector; //Also available in host flavour thrust::host_vector<int> hostVector; //Allocate vector on device deviceVector.resize( VEC_SIZE ); //Initialize host vector as size 8 elements, each containing the value 111 hostVector.resize( VEC_SIZE, 111 ); //Explicit copy to device thrust::copy( hostVector.begin(), hostVector.end(), deviceVector.begin() ); //Compute on device, here inclusive scan, for histogram equalization for instance thrust::inclusive_scan( deviceVector.begin(), deviceVector.end(), deviceVector.begin() ); //Copy back to host thrust::copy( deviceVector.begin(), deviceVector.end(), hostVector.begin() ); Declaration Allocation Copy To device Copy To host Compute on device
  7. gipsa-lab 1:Better practical expressivity 10 //Declare and initialize device vector

    in one line thrust::device_vector<int> deviceVector( VEC_SIZE, 111 ); //Compute algorithm thrust::inclusive_scan( deviceVector.begin(), deviceVector.end(), deviceVector.begin() ); //Print results std::cout << "Version 2, vector contains: "; for( auto it = deviceVector.begin(); it != deviceVector.end(); it++ ) { std::cout << " / " << *it; //Dereferencing iterator for reading: can also be done for writing ! } Declaration + Allocation Computation on device Read or write without explicit copy
  8. gipsa-lab 1: Compatibility with user allocated memory 11 //Raw pointer

    to device memory int * raw_ptr; checkCudaErrors( cudaMalloc((void **) &raw_ptr, VEC_SIZE * sizeof(int) ) ); //Wrap raw pointer with a device_ptr thrust::device_ptr<int> dev_ptr(raw_ptr); //Use device_ptr in thrust algorithms thrust::fill(dev_ptr, dev_ptr + VEC_SIZE, (int) 111); //Compute on device, here inclusive scan, for histogram equalization for instance thrust::inclusive_scan( dev_ptr, dev_ptr + VEC_SIZE, dev_ptr ); //Print results std::cout << "Version 3, vector contains: "; for( int i = 0; i != VEC_SIZE; i++ ) { std::cout << " / " << dev_ptr[i]; //Dereferencing pointer for reading: can also be done for writing ! } Handmade allocation Thrust raw pointer wrapper Initializing using thrust utility Compute on device Wrapper is iconvenient
  9. gipsa-lab 1:Compatibility with user written kernels 12 __global__ void naive_sequential_scan(

    T* ptr ) { T val = 0; #pragma unroll for( auto i = 0; i < SIZE; i++ ) { ptr[i] += val; val = ptr[i]; } } //Declare and initialize device vector in one line thrust::device_vector<int> deviceVector( VEC_SIZE, 111 ); //Compute algorithm cudaStream_t stream; checkCudaErrors( cudaStreamCreate(&stream) ); naive_sequential_scan<int,VEC_SIZE><<<1,1,0,stream>>>( thrust::raw_pointer_cast(deviceVector.data() ) ); checkCudaErrors( cudaStreamSynchronize( stream) ); Handwritten cuda kernel Declaration + Allocation Declare Synchronization tool Synchronize Launch handwritten kernel
  10. gipsa-lab 1:Handle cuda error as exceptions 13 try { //Declare

    and initialize device vector in one line thrust::device_vector<int> deviceVector( VEC_SIZE, 111 ); //Compute algorithm std::cout << "Version 5, we are going to catch an exception: "; thrust::inclusive_scan( deviceVector.begin(), deviceVector.end()+1, deviceVector.begin() ); //This line purposely contains an error } catch( thrust::system_error &e ) { std::cerr << "Thrust mechanism for handling error : " << e.what() << std::endl; } Classic catch block Declaration + Allocation Compute on device : wrong iterator
  11. gipsa-lab 2: Thrust: An asynchronous library 15 •Asynchronous behaviour in

    cuda •The compute / copy paradigm •Streams concept in cuda •Execution_policy in Thrust •Asynchronous traps •Beware of pageable memory ! •Data chunk size •Problem with default stream ( --default-stream per-thread ) •Copy engine ressource
  12. gipsa-lab 2: Thrust: An asynchronous library 16 •Execution_policy in Thrust

    could be •thrust::host •thrust::device •thrust::seq •thrust::system::omp::par •thrust::system::tbb::par •thrust::system::cuda::par( cudaStream_t )
  13. gipsa-lab 2: Thrust: Multiple stream approach 17 Achieving Copy /

    Compute overlapping Avoid large datasets Prefere small data chunks
  14. gipsa-lab 2: Thrust: Multiple stream approach V1 18 //Declare and

    initialize cuda stream std::vector<cudaStream_t> vStream(nbOfStrip); for( auto it = vStream.begin(); it != vStream.end(); it++ ) { cudaStreamCreate( &(*it) ); } //Now, we would like to perform an alternate scheme copy/compute in a loop using the copyToDevice/Compute/CopyToHost for each stream scheme: for( int j=0; j!=nbOfStrip; j++) { size_t offset = stripSize*j; size_t nextOffset = stripSize*(j+1); cudaStreamSynchronize(vStream.at(j)); cudaMemcpyAsync(thrust::raw_pointer_cast(deviceVector.data())+offset, hostVector+offset, stripSize*sizeof(float), cudaMemcpyHostToDevice, vStream.at(j)); thrust::transform( thrust::cuda::par.on(vStream.at(j)), deviceVector.begin()+offset, deviceVector.begin()+nextOffset, deviceVector.begin()+offset, computeFunctor<float>() ); cudaMemcpyAsync(hostVector+offset, thrust::raw_pointer_cast(deviceVector.data())+offset, stripSize*sizeof(float), cudaMemcpyDeviceToHost, vStream.at(j)); } Stream vector Only one loop Synchronize Copy to device Copy to host Compute
  15. gipsa-lab 2: Thrust: Multiple stream approach V2 19 for( int

    j=0; j!=nbOfStrip; j++) { cudaStreamSynchronize(vStream.at(j)); } for( int j=0; j!=nbOfStrip; j++) { size_t offset = stripSize*j; cudaMemcpyAsync(thrust::raw_pointer_cast(deviceVector.data())+offset, hostVector+offset, stripSize*sizeof(float), cudaMemcpyHostToDevice, vStream } for( int j=0; j!=nbOfStrip; j++) { size_t offset = stripSize*j; size_t nextOffset = stripSize*(j+1); thrust::transform( thrust::cuda::par.on(vStream.at(j)), deviceVector.begin()+offs deviceVector.begin()+nextOffset, deviceVector.begin()+offset, computeFunctor<float>() ); } for( int j=0; j!=nbOfStrip; j++) { size_t offset = stripSize*j; cudaMemcpyAsync(hostVector+offset, thrust::raw_pointer_cast(deviceVector.data())+offset, stripSize*sizeof(float), cudaMemcpyDeviceToHost, vStream.at(j)); } Synchronize loop Copy to device loop Compute loop Copy to host loop
  16. gipsa-lab 3: Thrust versatility : CPU/GPU 22 •Versatility •Code once,

    get all implementations •Ease GPU speedup calculation •Intel vs Nvidia : grab Popcorn and sit
  17. gipsa-lab 3: Thrust device system 23 •High level concept •

    Multiple possible backends : •THRUST_DEVICE_SYSTEM_CUDA •THRUST_DEVICE_SYSTEM_OMP •THRUST_DEVICE_SYSTEM_TBB •Compile time decision •Using option -DTHRUST_DEVICE_SYSTEM
  18. gipsa-lab 3: Benchmarking backends on sort 24 ######################################## # Miscellaneous

    parallel computing lib # ######################################## #Change device execution for fun ! set(THRUST_DEVICE_SYSTEM THRUST_DEVICE_SYSTEM_CUDA) #set(THRUST_DEVICE_SYSTEM "THRUST_DEVICE_SYSTEM_OMP -Xcompiler -fopenmp" ) #set(THRUST_DEVICE_SYSTEM THRUST_DEVICE_SYSTEM_TBB) list( APPEND CUDA_NVCC_FLAGS -DTHRUST_DEVICE_SYSTEM=${THRUST_DEVICE_SYSTEM} CmakeLists.txt
  19. gipsa-lab 3: Benchmarking backends on sort 25 Core code //Now

    measure how many time it take to perform sorting operation auto begin = std::chrono::high_resolution_clock::now(); thrust::sort( deviceVector.begin(), deviceVector.end() ); #if THRUST_DEVICE_SYSTEM == THRUST_DEVICE_SYSTEM_CUDA //Synchronize because of aynchronous behaviour in cuda mode cudaDeviceSynchronize(); #endif // THRUST_DEVICE_SYSTEM == THRUST_DEVICE_SYSTEM_CUDA auto end = std::chrono::high_resolution_clock::now(); Start timer Stop timer Compute Conditional synchronizati on point
  20. gipsa-lab 3: Benchmarking backends on sort 26 //OpenMP backend sorted

    134’217’728 elements in 2.01271 seconds (66.685 Millions of elements/s ) //TBB backend sorted 134’217’728 elements in 1.42055 seconds (94.4827 Millions of elements/s ) //Cuda backend sorted 134’217’728 elements in 0.485675 seconds (276.353 Millions of elements/s ) •Results 0 50 100 150 200 250 300 OMP TBB CUDA Throughput in Millions of elements sorted/s Throughput in Millions of elements sorted/s
  21. gipsa-lab 4: Thrust UVA and Multi GPU 28 •What is

    Unified Virtual Addressing ? Source: http://on-demand.gputechconf.com/gtc-express/2011/presentations/cuda_webinars_GPUDirect_uva.pdf
  22. gipsa-lab 4: Thrust UVA and Multi GPU 29 •Peer to

    peer memory access Source: http://on-demand.gputechconf.com/gtc-express/2011/presentations/cuda_webinars_GPUDirect_uva.pdf
  23. gipsa-lab 4: Thrust UVA and Multi GPU 30 •Peer to

    peer memory reduction through thrust Input: 8 Gpu, each containing a vector Output: addition of all vectors to one on GPU 0
  24. gipsa-lab 4: Thrust UVA and Multi GPU •Peer to peer

    memory reduction through thrust for( int i = 0; i != nb_device; i++ ) { //Set device as the current device checkCudaErrors( cudaSetDevice( i ) ); //Initialize memory vpDeviceVector.emplace_back( std::make_shared<thrust::device_vector<int> >( sizeVector, 111 ) ); //Enable Peer to Peer access, ie, current device can acces to memory of all superior device IDs for( int j = i+1; j < nb_device; j++ ) { checkCudaErrors( cudaDeviceEnablePeerAccess(j, 0) ); } } Set current device Memory is allocated on right device Grant access to all device having superior IDs
  25. gipsa-lab 4: Thrust UVA and Multi GPU •Peer to peer

    memory reduction through thrust // This is where reduction take place int maxTid = giveReductionSize(nb_device); while( maxTid != 0 ) { //Reduce from high IDs to low ones for(int i = 0; i < maxTid; ++i) { reduceVector( vpDeviceVector, i, maxTid ); } //Half the work is remaining maxTid /= 2; } Get upper power of 2 Perform a associative binary operation Reduction is log2(n) in number of steps
  26. gipsa-lab 4: Thrust UVA and Multi GPU •Peer to peer

    memory reduction through thrust void reduceVector( std::vector<std::shared_ptr<thrust::device_vector<int> > >& v, int tid, int maxTid { if( tid + maxTid < v.size() ) { //Set current device cudaSetDevice( tid ); // We add vector tid and vector tid+maxTid and put the result into vector tid thrust::transform( v.at(tid)->begin(), v.at(tid)->end(), v.at(tid+maxTid)->begin(), v.at(tid)->begin(), thrust::plus<int>() ); } } Check bound Set current active GPU Transparent thrust transformation
  27. gipsa-lab 5: Convex optimization using Thrust and Cublas •Why convex

    optimization on GPU ? • Unnecessary on small well posed systems • Ill-posed problems needs iterative methods • Iterative methods are expensive for large systems • Large problems needs parallelism 35
  28. gipsa-lab 5: Convex optimization using Thrust : Steepest descent •Simple

    algorithm for convex linear systems • Quadratic objectif function: easily differentiable • Aka Least square solution • Solved by step each time going in the opposite sense of the gradient: 36 Source: Gabriel Peyré
  29. gipsa-lab 5: Convex optimization using Thrust : What is Cublas

    ? •A powerful library (Basic Linear Algebra Subprogram) 37
  30. gipsa-lab 5: Convex optimization using Thrust and Cublas •Our strategy:

    Wrap everything inside a higher level interface 38 cublasSgemv(handle, transA, m, n, alpha, A, lda, B, ldb, beta, C, ldc) void Prod(const ThrustVectorWrapper<T>& Input, ThrustVectorWrapper<T>& Output) thrust::transform( m_deviceVector.begin(), m_deviceVector.end(), in.begin(), m_deviceVector.begin(), thrust::plus<T>() ); void Add( const ThrustVectorWrapper<T>& Input ) Cublas official interface Our wrapper interface Thrust interface Our wrapper interface
  31. gipsa-lab 5: Convex optimization using Thrust and Cublas •Resulting algorithm:

    39 while( (niter < nbIteration) && (L2Error > convergenceTol) ) { A.Prod( X, Ax ); // Ax = A * x Ax.Substract( B ); // Ax = Ax - b A.transProd( Ax, grad ); // grad = A^t(Ax - B) A.Prod( grad, Ag ); // Ag = A * gradient gradstep = grad.GetNorm22()/Ag.GetNorm22(); // Compute gradient step X.Saxpy( grad, -gradstep, false ); // Update solution L2Error = Ax.GetNorm22(); // Compute functional at current step niter++; // Ready for next iteration } ./ThrustVectorWrappingCublas Iteration : 0 over 1000 , L2 error = 653.522 Iteration : 1 over 1000 , L2 error = 164.205 Iteration : 2 over 1000 , L2 error = 82.2171 Iteration : 3 over 1000 , L2 error = 68.4766 Iteration : 4 over 1000 , L2 error = 59.1165 Iteration : 5 over 1000 , L2 error = 52.7413 Output:
  32. gipsa-lab 5: Convex optimization using Thrust and Cublas : Benchmark

    40 //CPU code linked with default gsl_cblas lib and default gcc gomp threading library //OpenMP backend performed 1000 iterations of gradient descent elements in 19.6776 seconds (50.8192 iterations per seconds ) //TBB backend performed 1000 iterations of gradient descent elements in 13.6715 seconds (73.145 iterations per seconds ) //CPU code Linked with MKL from Intel, and openMP runtime from intel (iomp5 instead of gomp //OpenMP backend performed 1000 iterations of gradient descent elements in 2.46626 seconds (405.473 iterations per seconds ) //TBB backend performed 1000 iterations of gradient descent elements in 2.163 seconds (462.32 iterations per seconds ) //Cuda Backend //Cuda backend performed 1000 iterations of gradient descent elements in 0.725926 seconds (1377.55 iterations per seconds 0 200 400 600 800 1000 1200 1400 1600 Classic Intel Library Nb of iterations/sec OMP TBB CUDA
  33. gipsa-lab 5: Gradient descent for signal processing •Exploiting gradient sparsity

    in signals: 41 Source: Gabriel Peyré, http://www.numerical-tours.com/slides/ Gradient TV
  34. gipsa-lab 5: Gradient descent for signal processing •Denoising as an

    optimization problem: •Helps crafting our objective function 42 y x
  35. gipsa-lab 5: Gradient descent for signal processing •Gradient of objective

    function gives: •Deriving the Total Variation ? •Ready for the gradient descent  43
  36. gipsa-lab 5: Gradient descent for signal processing •Algorithm is: •Helpers

    from Thrust: 44 while( niter < nbIteration ) { grad.Assign( X ); // grad = X grad.Substract( Y ); // grad = X – Y TvGradientTmp.FiniteForwardDifference( X ); // TvGradient = G(X) TvGradientTmp.ApplySmoothedTVGradient(epsilonNorm); // TvGradient = TvGradient / ||TvGradient||e TvGradient.FiniteBackwarDifference(TvGradientTmp); // TvGradient = div( TvGradient / ||TvGradient||e ) grad.Saxpy( TvGradient, -lambda, false ); // grad = X - Y + GradientTV X.Saxpy( grad, -stepSize, false ); // Update solution niter++; // Ready for next iteration } thrust::adjacent_difference( in.begin(), in.end(), m_deviceVector.begin());
  37. gipsa-lab 5: Gradient descent for signal processing : Benchmark 46

    //CPU code linked with default gcc gomp threading library //OpenMP backend performed 10000 iterations of gradient descent over 33’554’432 elements in 1672.89 seconds (5.97768 iterations per seconds ) //TBB backend performed 10000 iterations of gradient descent over 33’554’432 elements in 1648.48 seconds (6.0662 iterations per seconds ) //Cuda Backend //Cuda backend performed 10000 iterations of gradient descent over 33’554’432 elements in 105.78 seconds (94.5358 iterations per seconds ) 0 20 40 60 80 100 Benchmark 10k Nb of iterations/sec OMP TBB CUDA
  38. gipsa-lab Cuda Community and Useful links •Cuda Official Documentation •

    http://docs.nvidia.com/cuda/cuda-c-programming-guide/ • http://docs.nvidia.com/cuda/cuda-runtime-api/index.html •Thrust Official documentation • http://thrust.github.io/doc/modules.html • https://github.com/thrust/thrust/tree/master/examples •Nvidia Cuda official forum • https://devtalk.nvidia.com/default/board/57/ •Stack Overflow • http://stackoverflow.com/search?q=cuda •Udacity (Best MOOC for Cuda) • https://www.udacity.com/wiki/cs344 •Mark Harris (Chief Technologist, GPU Computing at NVIDIA) • https://twitter.com/harrism • https://twitter.com/GPUComputing • https://github.com/harrism •This tutorial • https://github.com/gnthibault/Cuda_Thrust_Introduction • https://twitter.com/gnthibault 47
  39. gipsa-lab Conclusion •Thrust allows: • Saving coding time • Clearer

    code • Intensive parameter exploration • Portability : CPU/GPU •Take Home message • Think parallel • Don’t reinvent the wheel : use libraries • Use wrappers 48