1 #ifndef VIENNACL_SCALAR_HPP_
2 #define VIENNACL_SCALAR_HPP_
33 #ifdef VIENNACL_WITH_OPENCL
46 template<
typename LHS,
typename RHS,
typename OP>
47 class scalar_expression
49 typedef typename LHS::value_type DummyType;
56 LHS &
lhs()
const {
return lhs_; }
58 RHS &
rhs()
const {
return rhs_; }
81 template<
typename LHS,
typename RHS>
91 LHS &
lhs()
const {
return lhs_; }
93 RHS &
rhs()
const {
return rhs_; }
114 template<
typename LHS,
typename RHS>
124 LHS &
lhs()
const {
return lhs_; }
126 RHS &
rhs()
const {
return rhs_; }
146 template<
typename LHS,
typename RHS>
156 LHS &
lhs()
const {
return lhs_; }
158 RHS &
rhs()
const {
return rhs_; }
179 template<
typename LHS,
typename RHS>
189 LHS &
lhs()
const {
return lhs_; }
191 RHS &
rhs()
const {
return rhs_; }
211 template<
typename LHS,
typename RHS>
221 LHS &
lhs()
const {
return lhs_; }
223 RHS &
rhs()
const {
return rhs_; }
244 template<
typename LHS,
typename RHS>
254 LHS &
lhs()
const {
return lhs_; }
256 RHS &
rhs()
const {
return rhs_; }
278 template<
typename LHS,
typename RHS>
288 LHS &
lhs()
const {
return lhs_; }
290 RHS &
rhs()
const {
return rhs_; }
315 template<
class NumericT>
318 typedef scalar<NumericT> self_type;
335 #ifdef VIENNACL_WITH_OPENCL
344 val_.opencl_handle() = mem;
345 val_.opencl_handle().inc();
350 template<
typename T1,
typename T2,
typename OP>
372 operator NumericT()
const
403 NumericT value =
static_cast<NumericT
>(cpu_other);
412 NumericT value =
static_cast<NumericT
>(cpu_other);
421 NumericT value =
static_cast<NumericT
>(cpu_other);
430 NumericT value =
static_cast<NumericT
>(cpu_other);
439 NumericT value =
static_cast<NumericT
>(cpu_other);
448 NumericT value =
static_cast<NumericT
>(cpu_other);
454 template<
typename T1,
typename T2>
464 template<
typename T1,
typename T2>
474 template<
typename T1,
typename T2>
484 template<
typename T1,
typename T2>
494 template<
typename T1,
typename T2>
504 template<
typename T1,
typename T2>
515 template<
typename T1,
typename T2>
525 template<
typename T1,
typename T2>
541 *
this, NumericT(1.0), 1,
false,
false,
542 other, NumericT(1.0), 1,
false,
false);
551 *
this, NumericT(1.0), 1,
false,
false,
552 other, NumericT(1.0), 1,
false,
false);
563 *
this, NumericT(1.0), 1,
false,
false,
564 other, NumericT(-1.0), 1,
false,
false);
573 *
this, NumericT(1.0), 1,
false,
false,
574 other, NumericT(-1.0), 1,
false,
false);
585 *
this, other, 1,
false,
false);
594 *
this, other, 1,
false,
false);
606 *
this, other, 1,
true,
false);
615 *
this, other, 1,
true,
false);
629 *
this, NumericT(1.0), 1,
false,
false,
630 other, NumericT(1.0), 1,
false,
false);
635 template<
typename T1,
typename T2,
typename OP>
643 *
this, NumericT(1.0), 1,
false,
false,
644 result, NumericT(1.0), 1,
false,
false);
656 *
this, NumericT(1.0), 1,
false,
false,
657 other, NumericT(1.0), 1,
false,
false);
680 *
this, NumericT(1.0), 1,
false,
false,
681 other, NumericT(-1.0), 1,
false,
false);
686 template<
typename T1,
typename T2,
typename OP>
694 *
this, NumericT(1.0), 1 ,
false,
false,
695 result, NumericT(-1.0), 1,
false,
false);
707 *
this, NumericT(1.0), 1,
false,
false,
708 other, NumericT(-1.0), 1,
false,
false);
722 *
this, other, 1,
false,
false);
727 template<
typename T1,
typename T2,
typename OP>
735 *
this, result, 1,
false,
false);
747 *
this, other, 1,
false,
false);
761 *
this, other, 1,
true,
false);
766 template<
typename T1,
typename T2,
typename OP>
774 *
this, result, 1,
true,
false);
786 *
this, other, 1,
true,
false);
813 template<
class NumericT>
814 std::ostream & operator<<(std::ostream & s, const scalar<NumericT> & val)
822 template<
class NumericT>
void norm_frobenius_cpu(matrix_base< T > const &vec, T &result)
Computes the Frobenius norm of a vector with final reduction on the CPU.
viennacl::enable_if< viennacl::is_scalar< S1 >::value &&viennacl::is_scalar< S2 >::value &&viennacl::is_any_scalar< ScalarType1 >::value >::type as(S1 &s1, S2 const &s2, ScalarType1 const &alpha, vcl_size_t len_alpha, bool reciprocal_alpha, bool flip_sign_alpha)
Interface for the generic operation s1 = s2 @ alpha, where s1 and s2 are GPU scalars, @ denotes multiplication or division, and alpha is either a GPU or a CPU scalar.
scalar_expression(LHS &lhs, RHS &rhs)
void memory_write(mem_handle &dst_buffer, vcl_size_t dst_offset, vcl_size_t bytes_to_write, const void *ptr, bool async=false)
Writes data from main RAM identified by 'ptr' to the buffer identified by 'dst_buffer'.
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
void inner_prod_cpu(vector_base< T > const &vec1, vector_base< T > const &vec2, T &result)
Computes the inner product of two vectors with the final reduction step on the CPU - dispatcher inter...
void min_cpu(vector_base< T > const &vec, T &result)
Computes the minimum of a vector with final reduction on the CPU.
scalar_expression(LHS &lhs, RHS &rhs)
scalar()
Creates the scalar object, but does not yet allocate memory. Thus, scalar<> can also be a global vari...
scalar(NumericT val, viennacl::context ctx=viennacl::context())
Allocates the memory for the scalar and sets it to the supplied value.
scalar_expression< const self_type, const self_type, op_flip_sign > operator-() const
Sign flip of the scalar. Does not evaluate immediately, but instead returns an expression template ob...
void max_cpu(vector_base< T > const &vec, T &result)
Computes the maximum of a vector with final reduction on the CPU.
handle_type & handle()
Returns the memory handle, non-const version.
scalar_expression(LHS &lhs, RHS &rhs)
Implementations of scalar operations.
scalar(scalar_expression< T1, T2, OP > const &proxy)
Allocates memory for the scalar and sets it to the result of supplied expression. ...
self_type & operator/=(scalar< NumericT > const &other)
Inplace division with a ViennaCL scalar.
void norm_2_cpu(vector_base< T > const &vec, T &result)
Computes the l^2-norm of a vector with final reduction on the CPU - dispatcher interface.
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
self_type operator*(scalar< NumericT > const &other) const
Multiplication of two ViennaCL scalars.
This file provides the forward declarations for the main types used within ViennaCL.
RHS & rhs() const
Returns the left hand side operand.
void memory_read(mem_handle const &src_buffer, vcl_size_t src_offset, vcl_size_t bytes_to_read, void *ptr, bool async=false)
Reads data from a buffer back to main RAM.
self_type & operator-=(scalar< NumericT > const &other)
Inplace subtraction of a ViennaCL scalar.
A proxy for scalar expressions (e.g. from inner vector products)
NumericT value_type
Returns the underlying host scalar type.
A tag class representing the maximum of a vector.
scalar_expression(LHS &lhs, RHS &rhs)
RHS & rhs() const
Returns the left hand side operand.
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
void norm_2_impl(vector_base< T > const &vec, scalar< T > &result)
Computes the l^2-norm of a vector - dispatcher interface.
RHS & rhs() const
Returns the left hand side operand.
void norm_frobenius_impl(matrix_base< T > const &vec, scalar< T > &result)
Computes the Frobenius norm of a matrix - dispatcher interface.
scalar_expression(LHS &lhs, RHS &rhs)
void max_impl(vector_base< T > const &vec, scalar< T > &result)
vcl_size_t index() const
Returns the index of the represented element.
scalar(const scalar &other)
Copy constructor. Allocates new memory for the scalar and copies the value of the supplied scalar...
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
void inner_prod_impl(vector_base< T > const &vec1, vector_base< T > const &vec2, scalar< T > &result)
Computes the inner product of two vectors - dispatcher interface.
self_type operator/(scalar< NumericT > const &other) const
Division of two ViennaCL scalars.
self_type & operator+=(scalar< NumericT > const &other)
Inplace addition of a ViennaCL scalar.
LHS & lhs() const
Returns the left hand side operand.
self_type & operator=(entry_proxy< NumericT > const &other)
Assigns a vector entry.
scalar_expression(LHS &lhs, RHS &rhs)
void norm_1_cpu(vector_base< T > const &vec, T &result)
Computes the l^1-norm of a vector with final reduction on the CPU.
LHS & lhs() const
Returns the left hand side operand.
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
const handle_type & handle() const
Returns the memory handle, const version.
std::istream & operator>>(std::istream &s, const scalar< NumericT > &val)
Allows to directly read a value of a scalar from an input stream.
void norm_inf_impl(vector_base< T > const &vec, scalar< T > &result)
Computes the supremum-norm of a vector.
void norm_1_impl(vector_base< T > const &vec, scalar< T > &result)
Computes the l^1-norm of a vector - dispatcher interface.
RHS & rhs() const
Returns the left hand side operand.
self_type & operator*=(scalar< NumericT > const &other)
Inplace multiplication with a ViennaCL scalar.
LHS & lhs() const
Returns the left hand side operand.
viennacl::result_of::cpu_value_type< DummyType >::type ScalarType
void min_impl(vector_base< T > const &vec, scalar< T > &result)
viennacl::enable_if< viennacl::is_scalar< S1 >::value &&viennacl::is_scalar< S2 >::value &&viennacl::is_scalar< S3 >::value &&viennacl::is_any_scalar< ScalarType1 >::value &&viennacl::is_any_scalar< ScalarType2 >::value >::type asbs(S1 &s1, S2 const &s2, ScalarType1 const &alpha, vcl_size_t len_alpha, bool reciprocal_alpha, bool flip_sign_alpha, S3 const &s3, ScalarType2 const &beta, vcl_size_t len_beta, bool reciprocal_beta, bool flip_sign_beta)
Interface for the generic operation s1 = s2 @ alpha + s3 @ beta, where s1, s2 and s3 are GPU scalars...
LHS & lhs() const
Returns the left hand side operand.
A tag class representing the minimum of a vector.
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Implementations of the OpenCL backend, where all contexts are stored in.
LHS & lhs() const
Returns the left hand side operand.
RHS & rhs() const
Returns the left hand side operand.
handle_type const & handle() const
Returns the memory viennacl::ocl::handle.
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...
A tag class representing the 1-norm of a vector.
void memory_copy(mem_handle const &src_buffer, mem_handle &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' to memory starting at address 'ds...
self_type operator+(scalar< NumericT > const &other)
Addition of two ViennaCL scalars.
viennacl::context context(T const &t)
Returns an ID for the currently active memory domain of an object.
LHS & lhs() const
Returns the left hand side operand.
LHS & lhs() const
Returns the left hand side operand.
viennacl::backend::mem_handle handle_type
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
scalar_expression(LHS &lhs, RHS &rhs)
A tag class representing the Frobenius-norm of a matrix.
void memory_create(mem_handle &handle, vcl_size_t size_in_bytes, viennacl::context const &ctx, const void *host_ptr=NULL)
Creates an array of the specified size. If the second argument is provided, the buffer is initialized...
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Extracts the underlying OpenCL handle from a vector, a matrix, an expression etc. ...
viennacl::backend::mem_handle & handle(T &obj)
Returns the generic memory handle of an object. Non-const version.
A tag class representing inner products of two vectors.
RHS & rhs() const
Returns the left hand side operand.
A proxy class for a single element of a vector or matrix. This proxy should not be noticed by end-use...
A tag class representing the inf-norm of a vector.
LHS & lhs() const
Returns the left hand side operand.
RHS & rhs() const
Returns the left hand side operand.
A tag class representing the 2-norm of a vector.
void norm_inf_cpu(vector_base< T > const &vec, T &result)
Computes the supremum-norm of a vector with final reduction on the CPU.
A collection of compile time type deductions.
Main interface routines for memory management.
scalar_expression(LHS &lhs, RHS &rhs)
RHS & rhs() const
Returns the left hand side operand.
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...