svm.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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(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(clSVMAlloc(context.get(), flags, size * sizeof(T), alignment));
  33. if(!ptr.get()){
  34. BOOST_THROW_EXCEPTION(opencl_error(CL_MEM_OBJECT_ALLOCATION_FAILURE));
  35. }
  36. return ptr;
  37. }
  38. /// Deallocates a shared virtual memory (SVM) buffer.
  39. ///
  40. /// \opencl_version_warning{2,0}
  41. ///
  42. /// \see_opencl2_ref{clSVMFree}
  43. ///
  44. /// \see svm_alloc(), command_queue::enqueue_svm_free()
  45. template<class T>
  46. inline void svm_free(const context &context, svm_ptr<T> ptr)
  47. {
  48. clSVMFree(context.get(), ptr.get());
  49. }
  50. } // end compute namespace
  51. } // end boost namespace
  52. #endif // CL_VERSION_2_0
  53. #endif // BOOST_COMPUTE_PIPE_HPP