svm.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_SVM_HPP
  11. #define BOOST_COMPUTE_SVM_HPP
  12. #include <boost/compute/config.hpp>
  13. #include <boost/compute/context.hpp>
  14. #include <boost/compute/memory/svm_ptr.hpp>
  15. // svm functions require OpenCL 2.0
  16. #if defined(BOOST_COMPUTE_CL_VERSION_2_0) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
  17. namespace boost {
  18. namespace compute {
  19. /// Allocates a shared virtual memory (SVM) buffer.
  20. //
  21. /// \opencl_version_warning{2,0}
  22. ///
  23. /// \see_opencl2_ref{clSVMAlloc}
  24. ///
  25. /// \see svm_free()
  26. template<class T>
  27. inline svm_ptr<T> svm_alloc(const context &context,
  28. size_t size,
  29. cl_svm_mem_flags flags = CL_MEM_READ_WRITE,
  30. unsigned int alignment = 0)
  31. {
  32. svm_ptr<T> ptr(
  33. clSVMAlloc(context.get(), flags, size * sizeof(T), alignment),
  34. context
  35. );
  36. if(!ptr.get()){
  37. BOOST_THROW_EXCEPTION(opencl_error(CL_MEM_OBJECT_ALLOCATION_FAILURE));
  38. }
  39. return ptr;
  40. }
  41. /// Deallocates a shared virtual memory (SVM) buffer.
  42. ///
  43. /// \opencl_version_warning{2,0}
  44. ///
  45. /// \see_opencl2_ref{clSVMFree}
  46. ///
  47. /// \see svm_alloc(), command_queue::enqueue_svm_free()
  48. template<class T>
  49. inline void svm_free(svm_ptr<T> ptr)
  50. {
  51. clSVMFree(ptr.get_context(), ptr.get());
  52. }
  53. /// \overload
  54. template<class T>
  55. inline void svm_free(const context &context, svm_ptr<T> ptr)
  56. {
  57. clSVMFree(context.get(), ptr.get());
  58. }
  59. } // end compute namespace
  60. } // end boost namespace
  61. #endif // BOOST_COMPUTE_CL_VERSION_2_0
  62. #endif // BOOST_COMPUTE_PIPE_HPP