All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
handle.hpp
Go to the documentation of this file.
#ifndef VIENNACL_TRAITS_HANDLE_HPP_
#define VIENNACL_TRAITS_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 <string>
#include <fstream>
#include <sstream>
#include "viennacl/forwards.h"
#include "viennacl/backend/mem_handle.hpp"
namespace viennacl
{
namespace traits
{
//
// Generic memory handle
//
template<typename T>
viennacl::backend::mem_handle & handle(T & obj)
{
return obj.handle();
}
template<typename T>
viennacl::backend::mem_handle const & handle(T const & obj)
{
return obj.handle();
}
inline char handle(char val) { return val; } //for unification purposes when passing CPU-scalars to kernels
inline short handle(short val) { return val; } //for unification purposes when passing CPU-scalars to kernels
inline int handle(int val) { return val; } //for unification purposes when passing CPU-scalars to kernels
inline long handle(long val) { return val; } //for unification purposes when passing CPU-scalars to kernels
inline float handle(float val) { return val; } //for unification purposes when passing CPU-scalars to kernels
inline double handle(double val) { return val; } //for unification purposes when passing CPU-scalars to kernels
template<typename LHS, typename RHS, typename OP>
viennacl::backend::mem_handle & handle(viennacl::scalar_expression< const LHS, const RHS, OP> & obj)
{
return handle(obj.lhs());
}
template<typename LHS, typename RHS, typename OP>
viennacl::backend::mem_handle const & handle(viennacl::matrix_expression<LHS, RHS, OP> const & obj);
template<typename LHS, typename RHS, typename OP>
viennacl::backend::mem_handle const & handle(viennacl::vector_expression<LHS, RHS, OP> const & obj);
template<typename LHS, typename RHS, typename OP>
viennacl::backend::mem_handle const & handle(viennacl::scalar_expression< const LHS, const RHS, OP> const & obj)
{
return handle(obj.lhs());
}
// proxy objects require extra care (at the moment)
template<typename T>
viennacl::backend::mem_handle & handle(viennacl::vector_base<T> & obj)
{
return obj.handle();
}
template<typename T>
viennacl::backend::mem_handle const & handle(viennacl::vector_base<T> const & obj)
{
return obj.handle();
}
template<typename T>
viennacl::backend::mem_handle & handle(viennacl::matrix_range<T> & obj)
{
return obj.get().handle();
}
template<typename T>
viennacl::backend::mem_handle const & handle(viennacl::matrix_range<T> const & obj)
{
return obj.get().handle();
}
template<typename T>
viennacl::backend::mem_handle & handle(viennacl::matrix_slice<T> & obj)
{
return obj.get().handle();
}
template<typename T>
viennacl::backend::mem_handle const & handle(viennacl::matrix_slice<T> const & obj)
{
return obj.get().handle();
}
template<typename LHS, typename RHS, typename OP>
viennacl::backend::mem_handle const & handle(viennacl::vector_expression<LHS, RHS, OP> const & obj)
{
return handle(obj.lhs());
}
template<typename LHS, typename RHS, typename OP>
viennacl::backend::mem_handle const & handle(viennacl::matrix_expression<LHS, RHS, OP> const & obj)
{
return handle(obj.lhs());
}
//
// RAM handle extraction
//
template<typename T>
typename viennacl::backend::mem_handle::ram_handle_type & ram_handle(T & obj)
{
return viennacl::traits::handle(obj).ram_handle();
}
template<typename T>
typename viennacl::backend::mem_handle::ram_handle_type const & ram_handle(T const & obj)
{
return viennacl::traits::handle(obj).ram_handle();
}
inline viennacl::backend::mem_handle::ram_handle_type & ram_handle(viennacl::backend::mem_handle & h)
{
return h.ram_handle();
}
inline viennacl::backend::mem_handle::ram_handle_type const & ram_handle(viennacl::backend::mem_handle const & h)
{
return h.ram_handle();
}
//
// OpenCL handle extraction
//
#ifdef VIENNACL_WITH_OPENCL
template<typename T>
viennacl::ocl::handle<cl_mem> & opencl_handle(T & obj)
{
return viennacl::traits::handle(obj).opencl_handle();
}
template<typename T>
viennacl::ocl::handle<cl_mem> const & opencl_handle(T const & obj)
{
return viennacl::traits::handle(obj).opencl_handle();
}
inline cl_char opencl_handle(char val) { return val; } //for unification purposes when passing CPU-scalars to kernels
inline cl_short opencl_handle(short val) { return val; } //for unification purposes when passing CPU-scalars to kernels
inline cl_int opencl_handle(int val) { return val; } //for unification purposes when passing CPU-scalars to kernels
inline cl_long opencl_handle(long val) { return val; } //for unification purposes when passing CPU-scalars to kernels
inline cl_uchar opencl_handle(unsigned char val) { return val; } //for unification purposes when passing CPU-scalars to kernels
inline cl_ushort opencl_handle(unsigned short val) { return val; } //for unification purposes when passing CPU-scalars to kernels
inline cl_uint opencl_handle(unsigned int val) { return val; } //for unification purposes when passing CPU-scalars to kernels
inline cl_ulong opencl_handle(unsigned long val) { return val; } //for unification purposes when passing CPU-scalars to kernels
inline float opencl_handle(float val) { return val; } //for unification purposes when passing CPU-scalars to kernels
inline double opencl_handle(double val) { return val; } //for unification purposes when passing CPU-scalars to kernels
// for user-provided matrix-vector routines:
template<typename LHS, typename NumericT>
viennacl::ocl::handle<cl_mem> const & opencl_handle(viennacl::vector_expression<LHS, const vector_base<NumericT>, op_prod> const & obj)
{
return viennacl::traits::handle(obj.rhs()).opencl_handle();
}
template<typename T>
viennacl::ocl::context & opencl_context(T const & obj)
{
return const_cast<viennacl::ocl::context &>(opencl_handle(obj).context());
}
#endif
//
// OpenCL context extraction
//
//
// Active handle ID
//
template<typename T>
viennacl::memory_types active_handle_id(T const & obj)
{
return handle(obj).get_active_handle_id();
}
template<typename T>
viennacl::memory_types active_handle_id(circulant_matrix<T> const &) { return OPENCL_MEMORY; }
template<typename T>
viennacl::memory_types active_handle_id(hankel_matrix<T> const &) { return OPENCL_MEMORY; }
template<typename T>
viennacl::memory_types active_handle_id(toeplitz_matrix<T> const &) { return OPENCL_MEMORY; }
template<typename T>
viennacl::memory_types active_handle_id(vandermonde_matrix<T> const &) { return OPENCL_MEMORY; }
template<typename LHS, typename RHS, typename OP>
viennacl::memory_types active_handle_id(viennacl::vector_expression<LHS, RHS, OP> const &);
template<typename LHS, typename RHS, typename OP>
viennacl::memory_types active_handle_id(viennacl::scalar_expression<LHS, RHS, OP> const & obj)
{
return active_handle_id(obj.lhs());
}
template<typename LHS, typename RHS, typename OP>
viennacl::memory_types active_handle_id(viennacl::vector_expression<LHS, RHS, OP> const & obj)
{
return active_handle_id(obj.lhs());
}
template<typename LHS, typename RHS, typename OP>
viennacl::memory_types active_handle_id(viennacl::matrix_expression<LHS, RHS, OP> const & obj)
{
return active_handle_id(obj.lhs());
}
// for user-provided matrix-vector routines:
template<typename LHS, typename NumericT>
viennacl::memory_types active_handle_id(viennacl::vector_expression<LHS, const vector_base<NumericT>, op_prod> const & obj)
{
return active_handle_id(obj.rhs());
}
} //namespace traits
} //namespace viennacl
#endif
viennacl::matrix_slice
Class for representing strided submatrices of a bigger matrix A.
Definition: forwards.h:443
viennacl::ocl::context
Manages an OpenCL context and provides the respective convenience functions for creating buffers...
Definition: context.hpp:55
viennacl::OPENCL_MEMORY
Definition: forwards.h:349
viennacl::matrix_expression
Expression template class for representing a tree of expressions which ultimately result in a matrix...
Definition: forwards.h:341
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::scalar_expression
A proxy for scalar expressions (e.g. from inner vector products)
Definition: forwards.h:230
viennacl::vector_expression::lhs
lhs_reference_type lhs() const
Get left hand side operand.
Definition: vector.hpp:74
viennacl::vector_expression
An expression template class that represents a binary operation that yields a vector.
Definition: forwards.h:239
mem_handle.hpp
Implements the multi-memory-domain handle.
viennacl::vector_base
Common base class for dense vectors, vector ranges, and vector slices.
Definition: vector_def.hpp:104
viennacl::tools::shared_ptr< char >
viennacl::matrix_base< MatrixType::cpu_value_type >::handle
handle_type & handle()
Returns the OpenCL handle, non-const-version.
Definition: matrix_def.hpp:244
viennacl::traits::active_handle_id
viennacl::memory_types active_handle_id(T const &obj)
Returns an ID for the currently active memory domain of an object.
Definition: handle.hpp:218
viennacl::matrix_expression::rhs
RHS & rhs() const
Get right hand side operand.
Definition: matrix.hpp:69
viennacl::scalar_expression::lhs
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:56
viennacl::backend::mem_handle
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
viennacl::circulant_matrix
A Circulant matrix class.
Definition: circulant_matrix.hpp:41
viennacl::matrix_range
Class for representing non-strided submatrices of a bigger matrix A.
Definition: forwards.h:440
viennacl::traits::handle
viennacl::backend::mem_handle & handle(T &obj)
Returns the generic memory handle of an object. Non-const version.
Definition: handle.hpp:41
viennacl::memory_types
memory_types
Definition: forwards.h:345
viennacl::matrix_expression::lhs
LHS & lhs() const
Get left hand side operand.
Definition: matrix.hpp:66
viennacl::traits::ram_handle
viennacl::backend::mem_handle::ram_handle_type & ram_handle(T &obj)
Generic helper routine for extracting the RAM handle of a ViennaCL object. Non-const version...
Definition: handle.hpp:138
viennacl::vector_base::handle
const handle_type & handle() const
Returns the memory handle.
Definition: vector_def.hpp:128
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::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