ViennaCL - The Vienna Computing Library  1.5.1
opencl.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_BACKEND_OPENCL_HPP_
2 #define VIENNACL_BACKEND_OPENCL_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2014, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8  Portions of this software are copyright by UChicago Argonne, LLC.
9 
10  -----------------
11  ViennaCL - The Vienna Computing Library
12  -----------------
13 
14  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15 
16  (A list of authors and contributors can be found in the PDF manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
26 #include <vector>
27 #include "viennacl/ocl/handle.hpp"
28 #include "viennacl/ocl/backend.hpp"
29 
30 namespace viennacl
31 {
32  namespace backend
33  {
34  namespace opencl
35  {
36 
37  // Requirements for backend:
38 
39  // * memory_create(size, host_ptr)
40  // * memory_copy(src, dest, offset_src, offset_dest, size)
41  // * memory_write_from_main_memory(src, offset, size,
42  // dest, offset, size)
43  // * memory_read_to_main_memory(src, offset, size
44  // dest, offset, size)
45  // *
46  //
47 
55  inline cl_mem memory_create(viennacl::ocl::context const & ctx, vcl_size_t size_in_bytes, const void * host_ptr = NULL)
56  {
57  //std::cout << "Creating buffer (" << size_in_bytes << " bytes) host buffer " << host_ptr << " in context " << &ctx << std::endl;
58  return ctx.create_memory_without_smart_handle(CL_MEM_READ_WRITE, static_cast<unsigned int>(size_in_bytes), const_cast<void *>(host_ptr));
59  }
60 
69  inline void memory_copy(viennacl::ocl::handle<cl_mem> const & src_buffer,
70  viennacl::ocl::handle<cl_mem> & dst_buffer,
71  vcl_size_t src_offset,
72  vcl_size_t dst_offset,
73  vcl_size_t bytes_to_copy)
74  {
75  assert( &src_buffer.context() == &dst_buffer.context() && bool("Transfer between memory buffers in different contexts not supported yet!"));
76 
77  viennacl::ocl::context & memory_context = const_cast<viennacl::ocl::context &>(src_buffer.context());
78  cl_int err = clEnqueueCopyBuffer(memory_context.get_queue().handle().get(),
79  src_buffer.get(),
80  dst_buffer.get(),
81  src_offset,
82  dst_offset,
83  bytes_to_copy,
84  0, NULL, NULL); //events
85  VIENNACL_ERR_CHECK(err);
86  }
87 
88 
97  inline void memory_write(viennacl::ocl::handle<cl_mem> & dst_buffer,
98  vcl_size_t dst_offset,
99  vcl_size_t bytes_to_copy,
100  const void * ptr,
101  bool async = false)
102  {
103  //std::cout << "Writing data (" << bytes_to_copy << " bytes, offset " << dst_offset << ") to OpenCL buffer" << std::endl;
104  viennacl::ocl::context & memory_context = const_cast<viennacl::ocl::context &>(dst_buffer.context());
105  cl_int err = clEnqueueWriteBuffer(memory_context.get_queue().handle().get(),
106  dst_buffer.get(),
107  async ? CL_FALSE : CL_TRUE, //blocking
108  dst_offset,
109  bytes_to_copy,
110  ptr,
111  0, NULL, NULL); //events
112  VIENNACL_ERR_CHECK(err);
113  }
114 
115 
124  inline void memory_read(viennacl::ocl::handle<cl_mem> const & src_buffer,
125  vcl_size_t src_offset,
126  vcl_size_t bytes_to_copy,
127  void * ptr,
128  bool async = false)
129  {
130  //std::cout << "Reading data (" << bytes_to_copy << " bytes, offset " << src_offset << ") from OpenCL buffer " << src_buffer.get() << " to " << ptr << std::endl;
131  viennacl::ocl::context & memory_context = const_cast<viennacl::ocl::context &>(src_buffer.context());
132  cl_int err = clEnqueueReadBuffer(memory_context.get_queue().handle().get(),
133  src_buffer.get(),
134  async ? CL_FALSE : CL_TRUE, //blocking
135  src_offset,
136  bytes_to_copy,
137  ptr,
138  0, NULL, NULL); //events
139  VIENNACL_ERR_CHECK(err);
140  }
141 
142 
143  }
144  } //backend
145 } //viennacl
146 #endif
std::size_t vcl_size_t
Definition: forwards.h:58
cl_mem memory_create(viennacl::ocl::context const &ctx, vcl_size_t size_in_bytes, const void *host_ptr=NULL)
Creates an array of the specified size in the current OpenCL context. If the second argument is provi...
Definition: opencl.hpp:55
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Definition: context.hpp:51
void memory_write(viennacl::ocl::handle< cl_mem > &dst_buffer, vcl_size_t dst_offset, vcl_size_t bytes_to_copy, const void *ptr, bool async=false)
Writes data from main RAM identified by 'ptr' to the OpenCL buffer identified by 'dst_buffer'.
Definition: opencl.hpp:97
cl_mem create_memory_without_smart_handle(cl_mem_flags flags, unsigned int size, void *ptr=NULL) const
Creates a memory buffer within the context. Does not wrap the OpenCL handle into the smart-pointer-li...
Definition: context.hpp:179
viennacl::ocl::handle< cl_command_queue > const & handle() const
Definition: command_queue.hpp:81
void memory_read(viennacl::ocl::handle< cl_mem > const &src_buffer, vcl_size_t src_offset, vcl_size_t bytes_to_copy, void *ptr, bool async=false)
Reads data from an OpenCL buffer back to main RAM.
Definition: opencl.hpp:124
#define VIENNACL_ERR_CHECK(err)
Definition: error.hpp:655
viennacl::ocl::command_queue & get_queue()
Definition: context.hpp:249
void memory_copy(viennacl::ocl::handle< cl_mem > const &src_buffer, viennacl::ocl::handle< cl_mem > &dst_buffer, vcl_size_t src_offset, vcl_size_t dst_offset, vcl_size_t bytes_to_copy)
Copies 'bytes_to_copy' bytes from address 'src_buffer + src_offset' in the OpenCL context to memory s...
Definition: opencl.hpp:69
const OCL_TYPE & get() const
Definition: handle.hpp:189
Implementation of a smart-pointer-like class for handling OpenCL handles.
Implementations of the OpenCL backend, where all contexts are stored in.
viennacl::ocl::context const & context() const
Definition: handle.hpp:191