All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mem_handle.hpp
Go to the documentation of this file.
#ifndef VIENNACL_BACKEND_MEM_HANDLE_HPP
#define VIENNACL_BACKEND_MEM_HANDLE_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 <cassert>
#include "viennacl/forwards.h"
#include "viennacl/tools/shared_ptr.hpp"
#include "viennacl/backend/cpu_ram.hpp"
#ifdef VIENNACL_WITH_OPENCL
#include "viennacl/backend/opencl.hpp"
#endif
#ifdef VIENNACL_WITH_CUDA
#include "viennacl/backend/cuda.hpp"
#endif
namespace viennacl
{
namespace backend
{
namespace detail
{
inline memory_types get_set_default_memory_type(memory_types * new_mem_type)
{
// if a user compiles with CUDA, it is reasonable to expect that CUDA should be the default
#ifdef VIENNACL_WITH_CUDA
static memory_types mem_type = CUDA_MEMORY;
#elif defined(VIENNACL_WITH_OPENCL)
static memory_types mem_type = OPENCL_MEMORY;
#else
static memory_types mem_type = MAIN_MEMORY;
#endif
if (new_mem_type)
mem_type = *new_mem_type;
return mem_type;
}
}
inline memory_types default_memory_type() { return detail::get_set_default_memory_type(NULL); }
inline memory_types default_memory_type(memory_types new_memory_type) { return detail::get_set_default_memory_type(&new_memory_type); }
class mem_handle
{
public:
typedef viennacl::tools::shared_ptr<char> ram_handle_type;
typedef viennacl::tools::shared_ptr<char> cuda_handle_type;
mem_handle() : active_handle_(MEMORY_NOT_INITIALIZED), size_in_bytes_(0) {}
ram_handle_type & ram_handle() { return ram_handle_; }
ram_handle_type const & ram_handle() const { return ram_handle_; }
#ifdef VIENNACL_WITH_OPENCL
viennacl::ocl::handle<cl_mem> & opencl_handle() { return opencl_handle_; }
viennacl::ocl::handle<cl_mem> const & opencl_handle() const { return opencl_handle_; }
#endif
#ifdef VIENNACL_WITH_CUDA
cuda_handle_type & cuda_handle() { return cuda_handle_; }
cuda_handle_type const & cuda_handle() const { return cuda_handle_; }
#endif
memory_types get_active_handle_id() const { return active_handle_; }
void switch_active_handle_id(memory_types new_id)
{
if (new_id != active_handle_)
{
if (active_handle_ == MEMORY_NOT_INITIALIZED)
active_handle_ = new_id;
else if (active_handle_ == MAIN_MEMORY)
{
active_handle_ = new_id;
}
else if (active_handle_ == OPENCL_MEMORY)
{
#ifdef VIENNACL_WITH_OPENCL
active_handle_ = new_id;
#else
throw memory_exception("compiled without OpenCL suppport!");
#endif
}
else if (active_handle_ == CUDA_MEMORY)
{
#ifdef VIENNACL_WITH_CUDA
active_handle_ = new_id;
#else
throw memory_exception("compiled without CUDA suppport!");
#endif
}
else
throw memory_exception("invalid new memory region!");
}
}
bool operator==(mem_handle const & other) const
{
if (active_handle_ != other.active_handle_)
return false;
switch (active_handle_)
{
case MAIN_MEMORY:
return ram_handle_.get() == other.ram_handle_.get();
#ifdef VIENNACL_WITH_OPENCL
case OPENCL_MEMORY:
return opencl_handle_.get() == other.opencl_handle_.get();
#endif
#ifdef VIENNACL_WITH_CUDA
case CUDA_MEMORY:
return cuda_handle_.get() == other.cuda_handle_.get();
#endif
default: break;
}
return false;
}
bool operator<(mem_handle const & other) const
{
if (active_handle_ != other.active_handle_)
return false;
switch (active_handle_)
{
case MAIN_MEMORY:
return ram_handle_.get() < other.ram_handle_.get();
#ifdef VIENNACL_WITH_OPENCL
case OPENCL_MEMORY:
return opencl_handle_.get() < other.opencl_handle_.get();
#endif
#ifdef VIENNACL_WITH_CUDA
case CUDA_MEMORY:
return cuda_handle_.get() < other.cuda_handle_.get();
#endif
default: break;
}
return false;
}
bool operator!=(mem_handle const & other) const { return !(*this == other); }
void swap(mem_handle & other)
{
// swap handle type:
memory_types active_handle_tmp = other.active_handle_;
other.active_handle_ = active_handle_;
active_handle_ = active_handle_tmp;
// swap ram handle:
ram_handle_type ram_handle_tmp = other.ram_handle_;
other.ram_handle_ = ram_handle_;
ram_handle_ = ram_handle_tmp;
// swap OpenCL handle:
#ifdef VIENNACL_WITH_OPENCL
opencl_handle_.swap(other.opencl_handle_);
#endif
#ifdef VIENNACL_WITH_CUDA
cuda_handle_type cuda_handle_tmp = other.cuda_handle_;
other.cuda_handle_ = cuda_handle_;
cuda_handle_ = cuda_handle_tmp;
#endif
}
vcl_size_t raw_size() const { return size_in_bytes_; }
void raw_size(vcl_size_t new_size) { size_in_bytes_ = new_size; }
private:
memory_types active_handle_;
ram_handle_type ram_handle_;
#ifdef VIENNACL_WITH_OPENCL
viennacl::ocl::handle<cl_mem> opencl_handle_;
#endif
#ifdef VIENNACL_WITH_CUDA
cuda_handle_type cuda_handle_;
#endif
vcl_size_t size_in_bytes_;
};
} //backend
} //viennacl
#endif
viennacl::memory_exception
Exception class in case of memory errors.
Definition: forwards.h:572
viennacl::backend::mem_handle::operator<
bool operator<(mem_handle const &other) const
Compares the two handles and returns true if the active memory handles in the two mem_handles point a...
Definition: mem_handle.hpp:179
viennacl::backend::mem_handle::ram_handle_type
viennacl::tools::shared_ptr< char > ram_handle_type
Definition: mem_handle.hpp:92
viennacl::OPENCL_MEMORY
Definition: forwards.h:349
viennacl::backend::mem_handle::ram_handle
ram_handle_type const & ram_handle() const
Returns the handle to a buffer in CPU RAM. NULL is returned if no such buffer has been allocated...
Definition: mem_handle.hpp:101
viennacl::backend::mem_handle::swap
void swap(mem_handle &other)
Implements a fast swapping method. No data is copied, only the handles are exchanged.
Definition: mem_handle.hpp:206
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::backend::mem_handle::mem_handle
mem_handle()
Default CTOR. No memory is allocated.
Definition: mem_handle.hpp:96
viennacl::tools::shared_ptr::swap
void swap(shared_ptr< T > &other)
Definition: shared_ptr.hpp:133
shared_ptr.hpp
Implementation of a shared pointer class (cf. std::shared_ptr, boost::shared_ptr). Will be used until C++11 is widely available.
viennacl::MEMORY_NOT_INITIALIZED
Definition: forwards.h:347
viennacl::CUDA_MEMORY
Definition: forwards.h:350
viennacl::backend::mem_handle::raw_size
void raw_size(vcl_size_t new_size)
Sets the size of the currently active buffer. Use with care!
Definition: mem_handle.hpp:233
cpu_ram.hpp
Implementations for the OpenCL backend functionality.
viennacl::backend::mem_handle::cuda_handle_type
viennacl::tools::shared_ptr< char > cuda_handle_type
Definition: mem_handle.hpp:93
viennacl::backend::mem_handle::operator==
bool operator==(mem_handle const &other) const
Compares the two handles and returns true if the active memory handles in the two mem_handles point t...
Definition: mem_handle.hpp:153
viennacl::tools::shared_ptr< char >
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
cuda.hpp
Implementations for the CUDA backend functionality.
viennacl::backend::default_memory_type
memory_types default_memory_type()
Returns the default memory type for the given configuration.
Definition: mem_handle.hpp:73
viennacl::MAIN_MEMORY
Definition: forwards.h:348
viennacl::backend::mem_handle::switch_active_handle_id
void switch_active_handle_id(memory_types new_id)
Switches the currently active handle. If no support for that backend is provided, an exception is thr...
Definition: mem_handle.hpp:121
viennacl::backend::mem_handle::operator!=
bool operator!=(mem_handle const &other) const
Definition: mem_handle.hpp:203
viennacl::tools::shared_ptr::get
T * get() const
Definition: shared_ptr.hpp:101
viennacl::backend::mem_handle
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
viennacl::backend::mem_handle::raw_size
vcl_size_t raw_size() const
Returns the number of bytes of the currently active buffer.
Definition: mem_handle.hpp:230
opencl.hpp
Implementations for the OpenCL backend functionality.
viennacl::memory_types
memory_types
Definition: forwards.h:345
viennacl::ocl::handle< cl_mem >
viennacl::backend::mem_handle::ram_handle
ram_handle_type & ram_handle()
Returns the handle to a buffer in CPU RAM. NULL is returned if no such buffer has been allocated...
Definition: mem_handle.hpp:99
viennacl::backend::detail::get_set_default_memory_type
memory_types get_set_default_memory_type(memory_types *new_mem_type)
Singleton for managing the default memory type.
Definition: mem_handle.hpp:51
viennacl::backend::mem_handle::get_active_handle_id
memory_types get_active_handle_id() const
Returns an ID for the currently active memory buffer. Other memory buffers might contain old or no da...
Definition: mem_handle.hpp:118