All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
execute_util.hpp
Go to the documentation of this file.
#ifndef VIENNACL_SCHEDULER_EXECUTE_UTIL_HPP
#define VIENNACL_SCHEDULER_EXECUTE_UTIL_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 <assert.h>
#include "viennacl/forwards.h"
#include "viennacl/scalar.hpp"
#include "viennacl/vector.hpp"
#include "viennacl/matrix.hpp"
#include "viennacl/scheduler/forwards.h"
namespace viennacl
{
namespace scheduler
{
namespace detail
{
//
inline lhs_rhs_element const & extract_representative_vector(statement const & s, lhs_rhs_element const & element)
{
switch (element.type_family)
{
case VECTOR_TYPE_FAMILY:
return element;
case COMPOSITE_OPERATION_FAMILY:
{
statement_node const & leaf = s.array()[element.node_index];
if (leaf.op.type_family == OPERATION_UNARY_TYPE_FAMILY)
return extract_representative_vector(s, leaf.lhs);
switch (leaf.op.type)
{
case OPERATION_BINARY_ADD_TYPE:
case OPERATION_BINARY_SUB_TYPE:
case OPERATION_BINARY_MULT_TYPE:
case OPERATION_BINARY_DIV_TYPE:
case OPERATION_BINARY_ELEMENT_PROD_TYPE:
case OPERATION_BINARY_ELEMENT_DIV_TYPE:
case OPERATION_BINARY_ELEMENT_POW_TYPE:
return extract_representative_vector(s, leaf.lhs);
case OPERATION_BINARY_MAT_VEC_PROD_TYPE:
return extract_representative_vector(s, leaf.rhs);
default:
throw statement_not_supported_exception("Vector leaf encountered an invalid binary operation!");
}
}
default:
throw statement_not_supported_exception("Vector leaf encountered an invalid node type!");
}
}
// helper routines for extracting the scalar type
inline float convert_to_float(float f) { return f; }
inline float convert_to_float(double d) { return static_cast<float>(d); }
inline float convert_to_float(lhs_rhs_element const & el)
{
if (el.type_family == SCALAR_TYPE_FAMILY && el.subtype == HOST_SCALAR_TYPE && el.numeric_type == FLOAT_TYPE)
return el.host_float;
if (el.type_family == SCALAR_TYPE_FAMILY && el.subtype == DEVICE_SCALAR_TYPE && el.numeric_type == FLOAT_TYPE)
return *el.scalar_float;
throw statement_not_supported_exception("Cannot convert to float");
}
// helper routines for extracting the scalar type
inline double convert_to_double(float d) { return static_cast<double>(d); }
inline double convert_to_double(double d) { return d; }
inline double convert_to_double(lhs_rhs_element const & el)
{
if (el.type_family == SCALAR_TYPE_FAMILY && el.subtype == HOST_SCALAR_TYPE && el.numeric_type == DOUBLE_TYPE)
return el.host_double;
if (el.type_family == SCALAR_TYPE_FAMILY && el.subtype == DEVICE_SCALAR_TYPE && el.numeric_type == DOUBLE_TYPE)
return *el.scalar_double;
throw statement_not_supported_exception("Cannot convert to double");
}
inline viennacl::context extract_context(statement_node const & root_node)
{
if (root_node.lhs.type_family == SCALAR_TYPE_FAMILY)
{
switch (root_node.lhs.numeric_type)
{
case CHAR_TYPE:
return viennacl::traits::context(*root_node.lhs.scalar_char);
case UCHAR_TYPE:
return viennacl::traits::context(*root_node.lhs.scalar_char);
case SHORT_TYPE:
return viennacl::traits::context(*root_node.lhs.scalar_short);
case USHORT_TYPE:
return viennacl::traits::context(*root_node.lhs.scalar_ushort);
case INT_TYPE:
return viennacl::traits::context(*root_node.lhs.scalar_int);
case UINT_TYPE:
return viennacl::traits::context(*root_node.lhs.scalar_uint);
case LONG_TYPE:
return viennacl::traits::context(*root_node.lhs.scalar_long);
case ULONG_TYPE:
return viennacl::traits::context(*root_node.lhs.scalar_ulong);
//case HALF_TYPE:
//return viennacl::traits::context(*root_node.lhs.scalar_half);
case FLOAT_TYPE:
return viennacl::traits::context(*root_node.lhs.scalar_float);
case DOUBLE_TYPE:
return viennacl::traits::context(*root_node.lhs.scalar_double);
default:
throw statement_not_supported_exception("Invalid numeric type for extraction of context from scalar");
}
}
else if (root_node.lhs.type_family == VECTOR_TYPE_FAMILY)
{
switch (root_node.lhs.numeric_type)
{
case CHAR_TYPE:
return viennacl::traits::context(*root_node.lhs.vector_char);
case UCHAR_TYPE:
return viennacl::traits::context(*root_node.lhs.vector_char);
case SHORT_TYPE:
return viennacl::traits::context(*root_node.lhs.vector_short);
case USHORT_TYPE:
return viennacl::traits::context(*root_node.lhs.vector_ushort);
case INT_TYPE:
return viennacl::traits::context(*root_node.lhs.vector_int);
case UINT_TYPE:
return viennacl::traits::context(*root_node.lhs.vector_uint);
case LONG_TYPE:
return viennacl::traits::context(*root_node.lhs.vector_long);
case ULONG_TYPE:
return viennacl::traits::context(*root_node.lhs.vector_ulong);
//case HALF_TYPE:
//return viennacl::traits::context(*root_node.lhs.vector_half);
case FLOAT_TYPE:
return viennacl::traits::context(*root_node.lhs.vector_float);
case DOUBLE_TYPE:
return viennacl::traits::context(*root_node.lhs.vector_double);
default:
throw statement_not_supported_exception("Invalid numeric type for extraction of context from vector");
}
}
else if (root_node.lhs.type_family == MATRIX_TYPE_FAMILY)
{
switch (root_node.lhs.numeric_type)
{
case CHAR_TYPE:
return viennacl::traits::context(*root_node.lhs.matrix_char);
case UCHAR_TYPE:
return viennacl::traits::context(*root_node.lhs.matrix_char);
case SHORT_TYPE:
return viennacl::traits::context(*root_node.lhs.matrix_short);
case USHORT_TYPE:
return viennacl::traits::context(*root_node.lhs.matrix_ushort);
case INT_TYPE:
return viennacl::traits::context(*root_node.lhs.matrix_int);
case UINT_TYPE:
return viennacl::traits::context(*root_node.lhs.matrix_uint);
case LONG_TYPE:
return viennacl::traits::context(*root_node.lhs.matrix_long);
case ULONG_TYPE:
return viennacl::traits::context(*root_node.lhs.matrix_ulong);
//case HALF_TYPE:
//return viennacl::traits::context(*root_node.lhs.matrix_half);
case FLOAT_TYPE:
return viennacl::traits::context(*root_node.lhs.matrix_float);
case DOUBLE_TYPE:
return viennacl::traits::context(*root_node.lhs.matrix_double);
default:
throw statement_not_supported_exception("Invalid numeric type for extraction of context from matrix");
}
}
throw statement_not_supported_exception("Invalid type for context extraction");
}
inline void new_element(lhs_rhs_element & new_elem, lhs_rhs_element const & old_element, viennacl::context const & ctx)
{
new_elem.type_family = old_element.type_family;
new_elem.subtype = old_element.subtype;
new_elem.numeric_type = old_element.numeric_type;
if (new_elem.type_family == SCALAR_TYPE_FAMILY)
{
assert(new_elem.subtype == DEVICE_SCALAR_TYPE && bool("Expected a device scalar in root node"));
switch (new_elem.numeric_type)
{
case FLOAT_TYPE:
new_elem.scalar_float = new viennacl::scalar<float>(0, ctx);
return;
case DOUBLE_TYPE:
new_elem.scalar_double = new viennacl::scalar<double>(0, ctx);
return;
default:
throw statement_not_supported_exception("Invalid vector type for vector construction");
}
}
else if (new_elem.type_family == VECTOR_TYPE_FAMILY)
{
assert(new_elem.subtype == DENSE_VECTOR_TYPE && bool("Expected a dense vector in root node"));
switch (new_elem.numeric_type)
{
case FLOAT_TYPE:
new_elem.vector_float = new viennacl::vector<float>((old_element.vector_float)->size(), ctx);
return;
case DOUBLE_TYPE:
new_elem.vector_double = new viennacl::vector<double>((old_element.vector_float)->size(), ctx);
return;
default:
throw statement_not_supported_exception("Invalid vector type for vector construction");
}
}
else if (new_elem.type_family == MATRIX_TYPE_FAMILY)
{
assert( (new_elem.subtype == DENSE_MATRIX_TYPE) && bool("Expected a dense matrix in root node"));
if (new_elem.subtype == DENSE_MATRIX_TYPE)
{
switch (new_elem.numeric_type)
{
case FLOAT_TYPE:
new_elem.matrix_float = new viennacl::matrix_base<float>((old_element.matrix_float)->size1(), (old_element.matrix_float)->size2(), (old_element.matrix_float)->row_major(), ctx);
return;
case DOUBLE_TYPE:
new_elem.matrix_double = new viennacl::matrix_base<double>((old_element.matrix_double)->size1(), (old_element.matrix_double)->size2(), (old_element.matrix_double)->row_major(), ctx);
return;
default:
throw statement_not_supported_exception("Invalid vector type for vector construction");
}
}
else
throw statement_not_supported_exception("Expected a dense matrix in root node when creating a temporary");
}
else
throw statement_not_supported_exception("Unknown type familty when creating new temporary object");
}
inline void delete_element(lhs_rhs_element & elem)
{
if (elem.type_family == SCALAR_TYPE_FAMILY)
{
switch (elem.numeric_type)
{
case FLOAT_TYPE:
delete elem.scalar_float;
return;
case DOUBLE_TYPE:
delete elem.scalar_double;
return;
default:
throw statement_not_supported_exception("Invalid vector type for vector destruction");
}
}
else if (elem.type_family == VECTOR_TYPE_FAMILY)
{
switch (elem.numeric_type)
{
case FLOAT_TYPE:
delete elem.vector_float;
return;
case DOUBLE_TYPE:
delete elem.vector_double;
return;
default:
throw statement_not_supported_exception("Invalid vector type for vector destruction");
}
}
else if (elem.type_family == MATRIX_TYPE_FAMILY)
{
if (elem.subtype == DENSE_MATRIX_TYPE)
{
switch (elem.numeric_type)
{
case FLOAT_TYPE:
delete elem.matrix_float;
return;
case DOUBLE_TYPE:
delete elem.matrix_double;
return;
default:
throw statement_not_supported_exception("Invalid vector type for vector destruction");
}
}
else
throw statement_not_supported_exception("Expected a dense matrix in root node when deleting temporary");
}
else
throw statement_not_supported_exception("Unknown type familty when deleting temporary object");
}
} // namespace detail
} // namespace scheduler
} // namespace viennacl
#endif
viennacl::scheduler::INT_TYPE
Definition: forwards.h:294
viennacl::scheduler::OPERATION_BINARY_MAT_VEC_PROD_TYPE
Definition: forwards.h:144
viennacl::scalar< float >
viennacl::scheduler::lhs_rhs_element::scalar_float
viennacl::scalar< float > * scalar_float
Definition: forwards.h:373
viennacl::scheduler::OPERATION_BINARY_ELEMENT_POW_TYPE
Definition: forwards.h:134
viennacl::scheduler::lhs_rhs_element::vector_ushort
viennacl::vector_base< unsigned short > * vector_ushort
Definition: forwards.h:380
viennacl::scheduler::lhs_rhs_element::vector_uint
viennacl::vector_base< unsigned int > * vector_uint
Definition: forwards.h:382
viennacl::scheduler::lhs_rhs_element::matrix_short
viennacl::matrix_base< short > * matrix_short
Definition: forwards.h:403
viennacl::scheduler::UCHAR_TYPE
Definition: forwards.h:291
viennacl::scheduler::lhs_rhs_element::vector_short
viennacl::vector_base< short > * vector_short
Definition: forwards.h:379
viennacl::scheduler::lhs_rhs_element::scalar_short
viennacl::scalar< short > * scalar_short
Definition: forwards.h:367
matrix.hpp
Implementation of the dense matrix class.
viennacl::scheduler::detail::extract_context
viennacl::context extract_context(statement_node const &root_node)
Helper routine for extracting the context in which a statement is executed.
Definition: execute_util.hpp:105
viennacl::scheduler::lhs_rhs_element::matrix_char
viennacl::matrix_base< char > * matrix_char
Definition: forwards.h:401
viennacl::scheduler::DEVICE_SCALAR_TYPE
Definition: forwards.h:269
viennacl::scheduler::DENSE_VECTOR_TYPE
Definition: forwards.h:271
viennacl::traits::size1
vcl_size_t size1(MatrixType const &mat)
Generic routine for obtaining the number of rows of a matrix (ViennaCL, uBLAS, etc.)
Definition: size.hpp:163
viennacl::scheduler::OPERATION_BINARY_ELEMENT_DIV_TYPE
Definition: forwards.h:127
viennacl::scheduler::DOUBLE_TYPE
Definition: forwards.h:300
viennacl::scheduler::detail::convert_to_double
double convert_to_double(float d)
Definition: execute_util.hpp:89
viennacl::matrix_base< float >
viennacl::scheduler::lhs_rhs_element::vector_int
viennacl::vector_base< int > * vector_int
Definition: forwards.h:381
viennacl::scheduler::lhs_rhs_element::subtype
statement_node_subtype subtype
Definition: forwards.h:340
viennacl::scheduler::MATRIX_TYPE_FAMILY
Definition: forwards.h:260
viennacl::scheduler::statement_node::op
op_element op
Definition: forwards.h:481
viennacl::scheduler::SCALAR_TYPE_FAMILY
Definition: forwards.h:254
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::scheduler::lhs_rhs_element::type_family
statement_node_type_family type_family
Definition: forwards.h:339
viennacl::scheduler::lhs_rhs_element
A class representing the 'data' for the LHS or RHS operand of the respective node.
Definition: forwards.h:337
viennacl::scheduler::statement::array
container_type const & array() const
Definition: forwards.h:528
viennacl::scheduler::lhs_rhs_element::scalar_uint
viennacl::scalar< unsigned int > * scalar_uint
Definition: forwards.h:370
viennacl::scheduler::lhs_rhs_element::scalar_long
viennacl::scalar< long > * scalar_long
Definition: forwards.h:371
viennacl::scheduler::HOST_SCALAR_TYPE
Definition: forwards.h:268
viennacl::scheduler::op_element::type_family
operation_node_type_family type_family
Definition: forwards.h:473
viennacl::traits::size2
result_of::size_type< MatrixType >::type size2(MatrixType const &mat)
Generic routine for obtaining the number of columns of a matrix (ViennaCL, uBLAS, etc...
Definition: size.hpp:201
viennacl::scheduler::OPERATION_BINARY_ELEMENT_PROD_TYPE
Definition: forwards.h:126
viennacl::scheduler::lhs_rhs_element::matrix_double
viennacl::matrix_base< double > * matrix_double
Definition: forwards.h:410
viennacl::scheduler::detail::delete_element
void delete_element(lhs_rhs_element &elem)
Definition: execute_util.hpp:266
viennacl::context
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Definition: context.hpp:39
viennacl::scheduler::COMPOSITE_OPERATION_FAMILY
Definition: forwards.h:251
viennacl::scheduler::lhs_rhs_element::scalar_ulong
viennacl::scalar< unsigned long > * scalar_ulong
Definition: forwards.h:372
viennacl::scheduler::SHORT_TYPE
Definition: forwards.h:292
viennacl::scheduler::lhs_rhs_element::matrix_long
viennacl::matrix_base< long > * matrix_long
Definition: forwards.h:407
viennacl::scheduler::LONG_TYPE
Definition: forwards.h:296
viennacl::scheduler::DENSE_MATRIX_TYPE
Definition: forwards.h:274
viennacl::scheduler::lhs_rhs_element::host_double
double host_double
Definition: forwards.h:360
viennacl::scheduler::lhs_rhs_element::matrix_ushort
viennacl::matrix_base< unsigned short > * matrix_ushort
Definition: forwards.h:404
viennacl::traits::size
vcl_size_t size(VectorType const &vec)
Generic routine for obtaining the size of a vector (ViennaCL, uBLAS, etc.)
Definition: size.hpp:239
viennacl::scheduler::CHAR_TYPE
Definition: forwards.h:290
viennacl::scheduler::lhs_rhs_element::numeric_type
statement_node_numeric_type numeric_type
Definition: forwards.h:341
viennacl::scheduler::lhs_rhs_element::vector_ulong
viennacl::vector_base< unsigned long > * vector_ulong
Definition: forwards.h:384
viennacl::scheduler::lhs_rhs_element::vector_float
viennacl::vector_base< float > * vector_float
Definition: forwards.h:385
viennacl::scheduler::statement_node::rhs
lhs_rhs_element rhs
Definition: forwards.h:482
viennacl::scheduler::lhs_rhs_element::matrix_float
viennacl::matrix_base< float > * matrix_float
Definition: forwards.h:409
viennacl::scheduler::statement_node::lhs
lhs_rhs_element lhs
Definition: forwards.h:480
viennacl::scheduler::lhs_rhs_element::vector_double
viennacl::vector_base< double > * vector_double
Definition: forwards.h:386
viennacl::vector
Definition: forwards.h:266
viennacl::scheduler::lhs_rhs_element::matrix_ulong
viennacl::matrix_base< unsigned long > * matrix_ulong
Definition: forwards.h:408
viennacl::scheduler::lhs_rhs_element::scalar_double
viennacl::scalar< double > * scalar_double
Definition: forwards.h:374
forwards.h
Provides the datastructures for dealing with a single statement such as 'x = y + z;'.
viennacl::scheduler::lhs_rhs_element::scalar_ushort
viennacl::scalar< unsigned short > * scalar_ushort
Definition: forwards.h:368
viennacl::scheduler::OPERATION_BINARY_SUB_TYPE
Definition: forwards.h:119
viennacl::scheduler::USHORT_TYPE
Definition: forwards.h:293
viennacl::scheduler::lhs_rhs_element::vector_char
viennacl::vector_base< char > * vector_char
Definition: forwards.h:377
viennacl::scheduler::lhs_rhs_element::host_float
float host_float
Definition: forwards.h:359
viennacl::traits::context
viennacl::context context(T const &t)
Returns an ID for the currently active memory domain of an object.
Definition: context.hpp:40
viennacl::scheduler::detail::new_element
void new_element(lhs_rhs_element &new_elem, lhs_rhs_element const &old_element, viennacl::context const &ctx)
Definition: execute_util.hpp:204
vector.hpp
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
viennacl::traits::row_major
bool row_major(T const &)
Definition: row_major.hpp:38
viennacl::scheduler::lhs_rhs_element::scalar_int
viennacl::scalar< int > * scalar_int
Definition: forwards.h:369
viennacl::scheduler::detail::convert_to_float
float convert_to_float(float f)
Definition: execute_util.hpp:76
viennacl::scheduler::op_element::type
operation_node_type type
Definition: forwards.h:474
viennacl::scheduler::OPERATION_BINARY_DIV_TYPE
Definition: forwards.h:121
viennacl::scheduler::detail::extract_representative_vector
lhs_rhs_element const & extract_representative_vector(statement const &s, lhs_rhs_element const &element)
Definition: execute_util.hpp:41
viennacl::scheduler::UINT_TYPE
Definition: forwards.h:295
viennacl::scheduler::FLOAT_TYPE
Definition: forwards.h:299
viennacl::scheduler::statement
The main class for representing a statement such as x = inner_prod(y,z); at runtime.
Definition: forwards.h:502
viennacl::scheduler::lhs_rhs_element::scalar_char
viennacl::scalar< char > * scalar_char
Definition: forwards.h:365
viennacl::scheduler::OPERATION_BINARY_ADD_TYPE
Definition: forwards.h:118
viennacl::scheduler::lhs_rhs_element::node_index
vcl_size_t node_index
Definition: forwards.h:346
viennacl::scheduler::ULONG_TYPE
Definition: forwards.h:297
viennacl::scheduler::lhs_rhs_element::matrix_int
viennacl::matrix_base< int > * matrix_int
Definition: forwards.h:405
scalar.hpp
Implementation of the ViennaCL scalar class.
viennacl::scheduler::lhs_rhs_element::vector_long
viennacl::vector_base< long > * vector_long
Definition: forwards.h:383
viennacl::scheduler::VECTOR_TYPE_FAMILY
Definition: forwards.h:257
viennacl::scheduler::statement_node
Main datastructure for an node in the statement tree.
Definition: forwards.h:478
viennacl::scheduler::statement_not_supported_exception
Exception for the case the scheduler is unable to deal with the operation.
Definition: forwards.h:38
viennacl::scheduler::lhs_rhs_element::matrix_uint
viennacl::matrix_base< unsigned int > * matrix_uint
Definition: forwards.h:406
viennacl::scheduler::OPERATION_UNARY_TYPE_FAMILY
Definition: forwards.h:58
viennacl::scheduler::OPERATION_BINARY_MULT_TYPE
Definition: forwards.h:120