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 
11 #ifndef BOOST_COMPUTE_SVM_HPP
12 #define BOOST_COMPUTE_SVM_HPP
13 
14 #include <boost/compute/config.hpp>
15 #include <boost/compute/context.hpp>
16 #include <boost/compute/memory/svm_ptr.hpp>
17 
18 // svm functions require OpenCL 2.0
19 #if defined(BOOST_COMPUTE_CL_VERSION_2_0) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
20 
21 namespace boost {
22 namespace compute {
23 
24 /// Allocates a shared virtual memory (SVM) buffer.
25 //
26 /// \opencl_version_warning{2,0}
27 ///
28 /// \see_opencl2_ref{clSVMAlloc}
29 ///
30 /// \see svm_free()
31 template<class T>
svm_alloc(const context & context,size_t size,cl_svm_mem_flags flags=CL_MEM_READ_WRITE,unsigned int alignment=0)32 inline svm_ptr<T> svm_alloc(const context &context,
33                             size_t size,
34                             cl_svm_mem_flags flags = CL_MEM_READ_WRITE,
35                             unsigned int alignment = 0)
36 {
37     svm_ptr<T> ptr(
38         clSVMAlloc(context.get(), flags, size * sizeof(T), alignment),
39         context
40     );
41     if(!ptr.get()){
42         BOOST_THROW_EXCEPTION(opencl_error(CL_MEM_OBJECT_ALLOCATION_FAILURE));
43     }
44     return ptr;
45 }
46 
47 /// Deallocates a shared virtual memory (SVM) buffer.
48 ///
49 /// \opencl_version_warning{2,0}
50 ///
51 /// \see_opencl2_ref{clSVMFree}
52 ///
53 /// \see svm_alloc(), command_queue::enqueue_svm_free()
54 template<class T>
svm_free(svm_ptr<T> ptr)55 inline void svm_free(svm_ptr<T> ptr)
56 {
57     clSVMFree(ptr.get_context(), ptr.get());
58 }
59 
60 /// \overload
61 template<class T>
svm_free(const context & context,svm_ptr<T> ptr)62 inline void svm_free(const context &context, svm_ptr<T> ptr)
63 {
64     clSVMFree(context.get(), ptr.get());
65 }
66 
67 } // end compute namespace
68 } // end boost namespace
69 
70 #endif // BOOST_COMPUTE_CL_VERSION_2_0
71 
72 #endif // BOOST_COMPUTE_PIPE_HPP
73