All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
opencl.hpp
Go to the documentation of this file.
#ifndef VIENNACL_BACKEND_OPENCL_HPP_
#define VIENNACL_BACKEND_OPENCL_HPP_
/* =========================================================================
Copyright (c) 2010-2016, Institute for Microelectronics,
Institute for Analysis and Scientific Computing,
TU Wien.
Portions of this software are copyright by UChicago Argonne, LLC.
-----------------
ViennaCL - The Vienna Computing Library
-----------------
Project Head: Karl Rupp rupp@iue.tuwien.ac.at
(A list of authors and contributors can be found in the manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
#include <vector>
#include "viennacl/ocl/handle.hpp"
#include "viennacl/ocl/backend.hpp"
namespace viennacl
{
namespace backend
{
namespace opencl
{
// Requirements for backend:
// * memory_create(size, host_ptr)
// * memory_copy(src, dest, offset_src, offset_dest, size)
// * memory_write_from_main_memory(src, offset, size,
// dest, offset, size)
// * memory_read_to_main_memory(src, offset, size
// dest, offset, size)
// *
//
inline cl_mem memory_create(viennacl::ocl::context const & ctx, vcl_size_t size_in_bytes, const void * host_ptr = NULL)
{
//std::cout << "Creating buffer (" << size_in_bytes << " bytes) host buffer " << host_ptr << " in context " << &ctx << std::endl;
return ctx.create_memory_without_smart_handle(CL_MEM_READ_WRITE, static_cast<unsigned int>(size_in_bytes), const_cast<void *>(host_ptr));
}
inline 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)
{
assert( &src_buffer.context() == &dst_buffer.context() && bool("Transfer between memory buffers in different contexts not supported yet!"));
viennacl::ocl::context & memory_context = const_cast<viennacl::ocl::context &>(src_buffer.context());
cl_int err = clEnqueueCopyBuffer(memory_context.get_queue().handle().get(),
src_buffer.get(),
dst_buffer.get(),
src_offset,
dst_offset,
bytes_to_copy,
0, NULL, NULL); //events
VIENNACL_ERR_CHECK(err);
}
inline 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)
{
viennacl::ocl::context & memory_context = const_cast<viennacl::ocl::context &>(dst_buffer.context());
#if defined(VIENNACL_DEBUG_ALL) || defined(VIENNACL_DEBUG_DEVICE)
std::cout << "Writing data (" << bytes_to_copy << " bytes, offset " << dst_offset << ") to OpenCL buffer " << dst_buffer.get() << " with queue " << memory_context.get_queue().handle().get() << " from " << ptr << std::endl;
#endif
cl_int err = clEnqueueWriteBuffer(memory_context.get_queue().handle().get(),
dst_buffer.get(),
async ? CL_FALSE : CL_TRUE, //blocking
dst_offset,
bytes_to_copy,
ptr,
0, NULL, NULL); //events
VIENNACL_ERR_CHECK(err);
}
inline 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)
{
//std::cout << "Reading data (" << bytes_to_copy << " bytes, offset " << src_offset << ") from OpenCL buffer " << src_buffer.get() << " to " << ptr << std::endl;
viennacl::ocl::context & memory_context = const_cast<viennacl::ocl::context &>(src_buffer.context());
cl_int err = clEnqueueReadBuffer(memory_context.get_queue().handle().get(),
src_buffer.get(),
async ? CL_FALSE : CL_TRUE, //blocking
src_offset,
bytes_to_copy,
ptr,
0, NULL, NULL); //events
VIENNACL_ERR_CHECK(err);
}
}
} //backend
} //viennacl
#endif
viennacl::backend::opencl::memory_create
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
viennacl::ocl::context::create_memory_without_smart_handle
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:196
viennacl::ocl::context::get_queue
viennacl::ocl::command_queue & get_queue()
Definition: context.hpp:266
viennacl::ocl::context
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Definition: context.hpp:55
viennacl::backend::opencl::memory_write
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
viennacl::backend::opencl::memory_read
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:129
viennacl::ocl::handle::context
viennacl::ocl::context const & context() const
Definition: handle.hpp:193
viennacl::ocl::command_queue::handle
viennacl::ocl::handle< cl_command_queue > const & handle() const
Definition: command_queue.hpp:81
VIENNACL_ERR_CHECK
#define VIENNACL_ERR_CHECK(err)
Definition: error.hpp:681
viennacl::ocl::handle::get
const OCL_TYPE & get() const
Definition: handle.hpp:191
viennacl::backend::opencl::memory_copy
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
handle.hpp
Implementation of a smart-pointer-like class for handling OpenCL handles.
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
backend.hpp
Implementations of the OpenCL backend, where all contexts are stored in.
viennacl::ocl::handle< cl_mem >