ViennaCL - The Vienna Computing Library  1.5.0
viennacl/backend/opencl.hpp
Go to the documentation of this file.
00001 #ifndef VIENNACL_BACKEND_OPENCL_HPP_
00002 #define VIENNACL_BACKEND_OPENCL_HPP_
00003 
00004 /* =========================================================================
00005    Copyright (c) 2010-2013, Institute for Microelectronics,
00006                             Institute for Analysis and Scientific Computing,
00007                             TU Wien.
00008    Portions of this software are copyright by UChicago Argonne, LLC.
00009 
00010                             -----------------
00011                   ViennaCL - The Vienna Computing Library
00012                             -----------------
00013 
00014    Project Head:    Karl Rupp                   rupp@iue.tuwien.ac.at
00015 
00016    (A list of authors and contributors can be found in the PDF manual)
00017 
00018    License:         MIT (X11), see file LICENSE in the base directory
00019 ============================================================================= */
00020 
00026 #include <vector>
00027 #include "viennacl/ocl/handle.hpp"
00028 #include "viennacl/ocl/backend.hpp"
00029 
00030 namespace viennacl
00031 {
00032   namespace backend
00033   {
00034     namespace opencl
00035     {
00036 
00037       // Requirements for backend:
00038 
00039       // * memory_create(size, host_ptr)
00040       // * memory_copy(src, dest, offset_src, offset_dest, size)
00041       // * memory_write_from_main_memory(src, offset, size,
00042       //                                 dest, offset, size)
00043       // * memory_read_to_main_memory(src, offset, size
00044       //                              dest, offset, size)
00045       // *
00046       //
00047 
00055       inline cl_mem memory_create(viennacl::ocl::context const & ctx, vcl_size_t size_in_bytes, const void * host_ptr = NULL)
00056       {
00057         //std::cout << "Creating buffer (" << size_in_bytes << " bytes) host buffer " << host_ptr << " in context " << &ctx << std::endl;
00058         return ctx.create_memory_without_smart_handle(CL_MEM_READ_WRITE, static_cast<unsigned int>(size_in_bytes), const_cast<void *>(host_ptr));
00059       }
00060 
00069       inline void memory_copy(viennacl::ocl::handle<cl_mem> const & src_buffer,
00070                        viennacl::ocl::handle<cl_mem> & dst_buffer,
00071                        vcl_size_t src_offset,
00072                        vcl_size_t dst_offset,
00073                        vcl_size_t bytes_to_copy)
00074       {
00075         assert( &src_buffer.context() == &dst_buffer.context() && bool("Transfer between memory buffers in different contexts not supported yet!"));
00076 
00077         viennacl::ocl::context & memory_context = const_cast<viennacl::ocl::context &>(src_buffer.context());
00078         cl_int err = clEnqueueCopyBuffer(memory_context.get_queue().handle().get(),
00079                                          src_buffer.get(),
00080                                          dst_buffer.get(),
00081                                          src_offset,
00082                                          dst_offset,
00083                                          bytes_to_copy,
00084                                          0, NULL, NULL);  //events
00085         VIENNACL_ERR_CHECK(err);
00086       }
00087 
00088 
00097       inline void memory_write(viennacl::ocl::handle<cl_mem> & dst_buffer,
00098                         vcl_size_t dst_offset,
00099                         vcl_size_t bytes_to_copy,
00100                         const void * ptr,
00101                         bool async = false)
00102       {
00103         //std::cout << "Writing data (" << bytes_to_copy << " bytes, offset " << dst_offset << ") to OpenCL buffer" << std::endl;
00104         viennacl::ocl::context & memory_context = const_cast<viennacl::ocl::context &>(dst_buffer.context());
00105         cl_int err = clEnqueueWriteBuffer(memory_context.get_queue().handle().get(),
00106                                           dst_buffer.get(),
00107                                           async ? CL_FALSE : CL_TRUE,             //blocking
00108                                           dst_offset,
00109                                           bytes_to_copy,
00110                                           ptr,
00111                                           0, NULL, NULL);      //events
00112         VIENNACL_ERR_CHECK(err);
00113       }
00114 
00115 
00124       inline void memory_read(viennacl::ocl::handle<cl_mem> const & src_buffer,
00125                        vcl_size_t src_offset,
00126                        vcl_size_t bytes_to_copy,
00127                        void * ptr,
00128                        bool async = false)
00129       {
00130         //std::cout << "Reading data (" << bytes_to_copy << " bytes, offset " << src_offset << ") from OpenCL buffer " << src_buffer.get() << " to " << ptr << std::endl;
00131         viennacl::ocl::context & memory_context = const_cast<viennacl::ocl::context &>(src_buffer.context());
00132         cl_int err =  clEnqueueReadBuffer(memory_context.get_queue().handle().get(),
00133                                           src_buffer.get(),
00134                                           async ? CL_FALSE : CL_TRUE,             //blocking
00135                                           src_offset,
00136                                           bytes_to_copy,
00137                                           ptr,
00138                                           0, NULL, NULL);      //events
00139         VIENNACL_ERR_CHECK(err);
00140       }
00141 
00142 
00143     }
00144   } //backend
00145 } //viennacl
00146 #endif