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_; }
248 template<
class SCALARTYPE>
251 typedef scalar<SCALARTYPE> self_type;
268 #ifdef VIENNACL_WITH_OPENCL
274 explicit scalar(cl_mem mem, size_type )
277 val_.opencl_handle() = mem;
278 val_.opencl_handle().inc();
283 template <
typename T1,
typename T2,
typename OP>
305 operator SCALARTYPE()
const
336 SCALARTYPE value =
static_cast<SCALARTYPE
>(cpu_other);
345 SCALARTYPE value =
static_cast<SCALARTYPE
>(cpu_other);
354 SCALARTYPE value =
static_cast<SCALARTYPE
>(cpu_other);
363 SCALARTYPE value =
static_cast<SCALARTYPE
>(cpu_other);
372 SCALARTYPE value =
static_cast<SCALARTYPE
>(cpu_other);
381 SCALARTYPE value =
static_cast<SCALARTYPE
>(cpu_other);
387 template <
typename T1,
typename T2>
397 template <
typename T1,
typename T2>
407 template <
typename T1,
typename T2>
417 template <
typename T1,
typename T2>
427 template <
typename T1,
typename T2>
437 template <
typename T1,
typename T2>
453 *
this, SCALARTYPE(1.0), 1,
false,
false,
454 other, SCALARTYPE(1.0), 1,
false,
false);
463 *
this, SCALARTYPE(1.0), 1,
false,
false,
464 other, SCALARTYPE(1.0), 1,
false,
false);
475 *
this, SCALARTYPE(1.0), 1,
false,
false,
476 other, SCALARTYPE(-1.0), 1,
false,
false);
485 *
this, SCALARTYPE(1.0), 1,
false,
false,
486 other, SCALARTYPE(-1.0), 1,
false,
false);
497 *
this, other, 1,
false,
false);
506 *
this, other, 1,
false,
false);
518 *
this, other, 1,
true,
false);
527 *
this, other, 1,
true,
false);
538 self_type result = 0;
541 *
this, SCALARTYPE(1.0), 1,
false,
false,
542 other, SCALARTYPE(1.0), 1,
false,
false);
547 template <
typename T1,
typename T2,
typename OP>
552 self_type result = proxy;
555 *
this, SCALARTYPE(1.0), 1,
false,
false,
556 result, SCALARTYPE(1.0), 1,
false,
false);
565 self_type result = 0;
568 *
this, SCALARTYPE(1.0), 1,
false,
false,
569 other, SCALARTYPE(1.0), 1,
false,
false);
589 self_type result = 0;
592 *
this, SCALARTYPE(1.0), 1,
false,
false,
593 other, SCALARTYPE(-1.0), 1,
false,
false);
598 template <
typename T1,
typename T2,
typename OP>
603 self_type result = proxy;
606 *
this, SCALARTYPE(1.0), 1 ,
false,
false,
607 result, SCALARTYPE(-1.0), 1,
false,
false);
616 self_type result = 0;
619 *
this, SCALARTYPE(1.0), 1,
false,
false,
620 other, SCALARTYPE(-1.0), 1,
false,
false);
634 *
this, other, 1,
false,
false);
639 template <
typename T1,
typename T2,
typename OP>
644 self_type result = proxy;
647 *
this, result, 1,
false,
false);
659 *
this, other, 1,
false,
false);
670 self_type result = 0;
673 *
this, other, 1,
true,
false);
678 template <
typename T1,
typename T2,
typename OP>
683 self_type result = proxy;
686 *
this, result, 1,
true,
false);
695 self_type result = 0;
698 *
this, other, 1,
true,
false);
707 const handle_type &
handle()
const {
return val_; }
725 template<
class SCALARTYPE>
726 std::ostream & operator<<(std::ostream & s, const scalar<SCALARTYPE> & val)
728 SCALARTYPE temp = val;
734 template<
class SCALARTYPE>
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.
Definition: scalar_operations.hpp:65
scalar_expression(LHS &lhs, RHS &rhs)
Definition: scalar.hpp:218
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:216
std::size_t vcl_size_t
Definition: forwards.h:58
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'.
Definition: memory.hpp:220
Definition: forwards.h:478
self_type & operator/=(scalar< SCALARTYPE > const &other)
Inplace division with a ViennaCL scalar.
Definition: scalar.hpp:513
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:184
scalar_expression(LHS &lhs, RHS &rhs)
Definition: scalar.hpp:153
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:156
self_type & operator-=(scalar< SCALARTYPE > const &other)
Inplace subtraction of a ViennaCL scalar.
Definition: scalar.hpp:470
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:119
Implementations of scalar operations.
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.
Definition: vector_operations.hpp:351
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:172
scalar_expression(LHS &lhs, RHS &rhs)
Definition: scalar.hpp:186
self_type & operator=(entry_proxy< SCALARTYPE > const &other)
Assigns a vector entry.
Definition: scalar.hpp:316
scalar_expression(LHS &lhs, RHS &rhs)
Definition: scalar.hpp:88
self_type operator+(scalar< SCALARTYPE > const &other)
Addition of two ViennaCL scalars.
Definition: scalar.hpp:534
SCALARTYPE value_type
Returns the underlying host scalar type.
Definition: scalar.hpp:257
vcl_size_t index() const
Returns the index of the represented element.
Definition: entry_proxy.hpp:137
This file provides the forward declarations for the main types used within ViennaCL.
scalar()
Creates the scalar object, but does not yet allocate memory. Thus, scalar<> can also be a global vari...
Definition: scalar.hpp:260
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.
Definition: memory.hpp:261
scalar(const scalar &other)
Copy constructor. Allocates new memory for the scalar and copies the value of the supplied scalar...
Definition: scalar.hpp:293
std::istream & operator>>(std::istream &s, const scalar< SCALARTYPE > &val)
Allows to directly read a value of a scalar from an input stream.
Definition: scalar.hpp:735
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:191
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Definition: context.hpp:39
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:91
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:29
A proxy class for a single element of a vector or matrix. This proxy should not be noticed by end-use...
Definition: forwards.h:178
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:189
Definition: forwards.h:480
void norm_1_impl(vector_base< T > const &vec, scalar< T > &result)
Computes the l^1-norm of a vector - dispatcher interface.
Definition: vector_operations.hpp:530
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:93
self_type & operator*=(scalar< SCALARTYPE > const &other)
Inplace multiplication with a ViennaCL scalar.
Definition: scalar.hpp:492
void norm_frobenius_cpu(matrix_base< T, F > const &vec, T &result)
Computes the Frobenius norm of a vector with final reduction on the CPU.
Definition: matrix_operations.hpp:325
handle_type const & handle() const
Returns the memory viennacl::ocl::handle.
Definition: entry_proxy.hpp:141
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:56
scalar(scalar_expression< T1, T2, OP > const &proxy)
Allocates memory for the scalar and sets it to the result of supplied expression. ...
Definition: scalar.hpp:284
scalar(SCALARTYPE val, viennacl::context ctx=viennacl::context())
Allocates the memory for the scalar and sets it to the supplied value.
Definition: scalar.hpp:263
void norm_inf_cpu(vector_base< T > const &vec, T &result)
Computes the supremum-norm of a vector with final reduction on the CPU.
Definition: vector_operations.hpp:761
A proxy for scalar expressions (e.g. from inner vector products)
Definition: forwards.h:175
viennacl::result_of::cpu_value_type< DummyType >::type ScalarType
Definition: scalar.hpp:51
handle_type & handle()
Returns the memory handle, non-const version.
Definition: scalar.hpp:704
self_type operator/(scalar< SCALARTYPE > const &other) const
Division of two ViennaCL scalars.
Definition: scalar.hpp:666
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...
Definition: scalar_operations.hpp:114
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.
Definition: vector_operations.hpp:669
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:124
Implementations of the OpenCL backend, where all contexts are stored in.
self_type & operator+=(scalar< SCALARTYPE > const &other)
Inplace addition of a ViennaCL scalar.
Definition: scalar.hpp:448
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:221
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:223
vcl_size_t size_type
Definition: scalar.hpp:254
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...
Definition: scalar.hpp:578
A tag class representing the 1-norm of a vector.
Definition: forwards.h:153
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...
Definition: memory.hpp:140
viennacl::context context(T const &t)
Returns an ID for the currently active memory domain of an object.
Definition: context.hpp:41
self_type operator*(scalar< SCALARTYPE > const &other) const
Multiplication of two ViennaCL scalars.
Definition: scalar.hpp:627
viennacl::backend::mem_handle handle_type
Definition: scalar.hpp:253
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Definition: result_of.hpp:276
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...
Definition: vector_operations.hpp:423
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:86
void norm_2_impl(vector_base< T > const &vec, scalar< T > &result)
Computes the l^2-norm of a vector - dispatcher interface.
Definition: vector_operations.hpp:624
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:62
A tag class representing the Frobenius-norm of a matrix.
Definition: forwards.h:162
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...
Definition: memory.hpp:87
const handle_type & handle() const
Returns the memory handle, const version.
Definition: scalar.hpp:707
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:158
void norm_frobenius_impl(matrix_base< T, F > const &vec, scalar< T > &result)
Computes the Frobenius norm of a matrix - dispatcher interface.
Definition: matrix_operations.hpp:311
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:94
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.
Definition: handle.hpp:41
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:151
A tag class representing inner products of two vectors.
Definition: forwards.h:150
A tag class representing the inf-norm of a vector.
Definition: forwards.h:159
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:58
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:91
A tag class representing the 2-norm of a vector.
Definition: forwards.h:156
A collection of compile time type deductions.
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.
Definition: vector_operations.hpp:577
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:126
Main interface routines for memory management.
void norm_inf_impl(vector_base< T > const &vec, scalar< T > &result)
Computes the supremum-norm of a vector.
Definition: vector_operations.hpp:716
scalar_expression(LHS &lhs, RHS &rhs)
Definition: scalar.hpp:121
scalar_expression(LHS &lhs, RHS &rhs)
Definition: scalar.hpp:53