vector.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Boost.uBLAS
  2. //
  3. // Copyright (c) 2018 Fady Essam
  4. // Copyright (c) 2018 Stefan Seefeld
  5. //
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // (See accompanying file LICENSE_1_0.txt or
  8. // copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef boost_numeric_ublas_opencl_vector_hpp_
  10. #define boost_numeric_ublas_opencl_vector_hpp_
  11. #include <boost/numeric/ublas/opencl/library.hpp>
  12. #include <boost/numeric/ublas/functional.hpp>
  13. #include <boost/compute/core.hpp>
  14. #include <boost/compute/algorithm.hpp>
  15. #include <boost/compute/buffer.hpp>
  16. #include <boost/compute/container/vector.hpp>
  17. namespace boost { namespace numeric { namespace ublas { namespace opencl {
  18. class storage;
  19. namespace compute = boost::compute;
  20. } // namespace opencl
  21. template <class T>
  22. class vector<T, opencl::storage> : public boost::compute::vector<T>
  23. {
  24. typedef std::size_t size_type;
  25. public:
  26. vector() : compute::vector<T>() {}
  27. vector(size_type size, compute::context context)
  28. : compute::vector<T>(size, context)
  29. { device_ = context.get_device();}
  30. vector(size_type size, T value, compute::command_queue queue)
  31. : compute::vector<T>(size, value, queue.get_context())
  32. {
  33. queue.finish();
  34. device_ = queue.get_device();
  35. }
  36. template <typename A>
  37. vector(vector<T, A> const &v, compute::command_queue &queue)
  38. : vector(v.size(), queue.get_context())
  39. {
  40. this->from_host(v, queue);
  41. }
  42. const compute::device device() const { return device_;}
  43. compute::device device() { return device_;}
  44. template<class A>
  45. void from_host(ublas::vector<T, A> const &v, compute::command_queue & queue)
  46. {
  47. assert(this->device() == queue.get_device());
  48. compute::copy(v.begin(),
  49. v.end(),
  50. this->begin(),
  51. queue);
  52. queue.finish();
  53. }
  54. template<class A>
  55. void to_host(ublas::vector<T, A>& v, compute::command_queue& queue) const
  56. {
  57. assert(this->device() == queue.get_device());
  58. compute::copy(this->begin(),
  59. this->end(),
  60. v.begin(),
  61. queue);
  62. queue.finish();
  63. }
  64. void fill(T value, compute::command_queue & queue)
  65. {
  66. assert(this->device() == queue.get_device());
  67. compute::fill(this->begin(), this->end(), value, queue);
  68. queue.finish();
  69. }
  70. private:
  71. compute::device device_;
  72. };
  73. }}}
  74. #endif