ViennaCL - The Vienna Computing Library: viennacl/linalg/host_based/scalar_operations.hpp Source File

ViennaCL - The Vienna Computing Library 1.7.1 Free open-source GPU-accelerated linear algebra and solver library. |

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
scalar_operations.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_HOST_BASED_SCALAR_OPERATIONS_HPP_
2 #define VIENNACL_LINALG_HOST_BASED_SCALAR_OPERATIONS_HPP_
3
4 /* =========================================================================
5 Copyright (c) 2010-2016, Institute for Microelectronics,
6 Institute for Analysis and Scientific Computing,
7 TU Wien.
8 Portions of this software are copyright by UChicago Argonne, LLC.
9
10 -----------------
11 ViennaCL - The Vienna Computing Library
12 -----------------
13
14 Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15
16 (A list of authors and contributors can be found in the manual)
17
18 License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20
25 #include "viennacl/forwards.h"
26 #include "viennacl/tools/tools.hpp"
27 #include "viennacl/meta/predicate.hpp"
28 #include "viennacl/meta/enable_if.hpp"
29 #include "viennacl/traits/size.hpp"
30 #include "viennacl/traits/start.hpp"
31 #include "viennacl/traits/stride.hpp"
32 #include "viennacl/linalg/host_based/common.hpp"
33
34 namespace viennacl
35 {
36 namespace linalg
37 {
38 namespace host_based
39 {
40 template<typename ScalarT1,
41 typename ScalarT2, typename FactorT>
42 typename viennacl::enable_if< viennacl::is_scalar<ScalarT1>::value
43 && viennacl::is_scalar<ScalarT2>::value
44 && viennacl::is_any_scalar<FactorT>::value
45 >::type
46 as(ScalarT1 & s1,
47 ScalarT2 const & s2, FactorT const & alpha, vcl_size_t /*len_alpha*/, bool reciprocal_alpha, bool flip_sign_alpha)
48 {
49 typedef typename viennacl::result_of::cpu_value_type<ScalarT1>::type value_type;
50
51 value_type * data_s1 = detail::extract_raw_pointer<value_type>(s1);
52 value_type const * data_s2 = detail::extract_raw_pointer<value_type>(s2);
53
54 value_type data_alpha = alpha;
55 if (flip_sign_alpha)
56 data_alpha = -data_alpha;
57 if (reciprocal_alpha)
58 data_alpha = static_cast<value_type>(1) / data_alpha;
59
60 *data_s1 = *data_s2 * data_alpha;
61 }
62
63
64 template<typename ScalarT1,
65 typename ScalarT2, typename FactorT2,
66 typename ScalarT3, typename FactorT3>
67 typename viennacl::enable_if< viennacl::is_scalar<ScalarT1>::value
68 && viennacl::is_scalar<ScalarT2>::value
69 && viennacl::is_scalar<ScalarT3>::value
70 && viennacl::is_any_scalar<FactorT2>::value
71 && viennacl::is_any_scalar<FactorT3>::value
72 >::type
73 asbs(ScalarT1 & s1,
74 ScalarT2 const & s2, FactorT2 const & alpha, vcl_size_t /*len_alpha*/, bool reciprocal_alpha, bool flip_sign_alpha,
75 ScalarT3 const & s3, FactorT3 const & beta, vcl_size_t /*len_beta*/, bool reciprocal_beta, bool flip_sign_beta)
76 {
77 typedef typename viennacl::result_of::cpu_value_type<ScalarT1>::type value_type;
78
79 value_type * data_s1 = detail::extract_raw_pointer<value_type>(s1);
80 value_type const * data_s2 = detail::extract_raw_pointer<value_type>(s2);
81 value_type const * data_s3 = detail::extract_raw_pointer<value_type>(s3);
82
83 value_type data_alpha = alpha;
84 if (flip_sign_alpha)
85 data_alpha = -data_alpha;
86 if (reciprocal_alpha)
87 data_alpha = static_cast<value_type>(1) / data_alpha;
88
89 value_type data_beta = beta;
90 if (flip_sign_beta)
91 data_beta = -data_beta;
92 if (reciprocal_beta)
93 data_beta = static_cast<value_type>(1) / data_beta;
94
95 *data_s1 = *data_s2 * data_alpha + *data_s3 * data_beta;
96 }
97
98
99 template<typename ScalarT1,
100 typename ScalarT2, typename FactorT2,
101 typename ScalarT3, typename FactorT3>
102 typename viennacl::enable_if< viennacl::is_scalar<ScalarT1>::value
103 && viennacl::is_scalar<ScalarT2>::value
104 && viennacl::is_scalar<ScalarT3>::value
105 && viennacl::is_any_scalar<FactorT2>::value
106 && viennacl::is_any_scalar<FactorT3>::value
107 >::type
108 asbs_s(ScalarT1 & s1,
109 ScalarT2 const & s2, FactorT2 const & alpha, vcl_size_t /*len_alpha*/, bool reciprocal_alpha, bool flip_sign_alpha,
110 ScalarT3 const & s3, FactorT3 const & beta, vcl_size_t /*len_beta*/, bool reciprocal_beta, bool flip_sign_beta)
111 {
112 typedef typename viennacl::result_of::cpu_value_type<ScalarT1>::type value_type;
113
114 value_type * data_s1 = detail::extract_raw_pointer<value_type>(s1);
115 value_type const * data_s2 = detail::extract_raw_pointer<value_type>(s2);
116 value_type const * data_s3 = detail::extract_raw_pointer<value_type>(s3);
117
118 value_type data_alpha = alpha;
119 if (flip_sign_alpha)
120 data_alpha = -data_alpha;
121 if (reciprocal_alpha)
122 data_alpha = static_cast<value_type>(1) / data_alpha;
123
124 value_type data_beta = beta;
125 if (flip_sign_beta)
126 data_beta = -data_beta;
127 if (reciprocal_beta)
128 data_beta = static_cast<value_type>(1) / data_beta;
129
130 *data_s1 += *data_s2 * data_alpha + *data_s3 * data_beta;
131 }
132
133
139 template<typename ScalarT1, typename ScalarT2>
140 typename viennacl::enable_if< viennacl::is_scalar<ScalarT1>::value
141 && viennacl::is_scalar<ScalarT2>::value
142 >::type
143 swap(ScalarT1 & s1, ScalarT2 & s2)
144 {
145 typedef typename viennacl::result_of::cpu_value_type<ScalarT1>::type value_type;
146
147 value_type * data_s1 = detail::extract_raw_pointer<value_type>(s1);
148 value_type * data_s2 = detail::extract_raw_pointer<value_type>(s2);
149
150 value_type temp = *data_s2;
151 *data_s2 = *data_s1;
152 *data_s1 = temp;
153 }
154
155
156
157 } //namespace host_based
158 } //namespace linalg
159 } //namespace viennacl
160
161
162 #endif
viennacl::enable_if
Simple enable-if variant that uses the SFINAE pattern.
Definition: enable_if.hpp:30
size.hpp
Generic size and resize functionality for different vector and matrix types.
start.hpp
Extracts the underlying OpenCL start index handle from a vector, a matrix, an expression etc...
tools.hpp
Various little tools used here and there in ViennaCL.
viennacl::linalg::host_based::swap
viennacl::enable_if< viennacl::is_scalar< ScalarT1 >::value &&viennacl::is_scalar< ScalarT2 >::value >::type swap(ScalarT1 &s1, ScalarT2 &s2)
Swaps the contents of two scalars, data is copied.
Definition: scalar_operations.hpp:143
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
stride.hpp
Determines row and column increments for matrices and matrix proxies.
s2
viennacl::scalar< int > s2
Definition: global_variables.cpp:58
s1
viennacl::scalar< float > s1
Definition: global_variables.cpp:57
viennacl::is_any_scalar
Helper struct for checking whether the provided type represents a scalar (either host, from ViennaCL, or a flip-sign proxy)
Definition: forwards.h:469
viennacl::linalg::host_based::asbs
viennacl::enable_if< viennacl::is_scalar< ScalarT1 >::value &&viennacl::is_scalar< ScalarT2 >::value &&viennacl::is_scalar< ScalarT3 >::value &&viennacl::is_any_scalar< FactorT2 >::value &&viennacl::is_any_scalar< FactorT3 >::value >::type asbs(ScalarT1 &s1, ScalarT2 const &s2, FactorT2 const &alpha, vcl_size_t, bool reciprocal_alpha, bool flip_sign_alpha, ScalarT3 const &s3, FactorT3 const &beta, vcl_size_t, bool reciprocal_beta, bool flip_sign_beta)
Definition: scalar_operations.hpp:73
viennacl::linalg::host_based::as
viennacl::enable_if< viennacl::is_scalar< ScalarT1 >::value &&viennacl::is_scalar< ScalarT2 >::value &&viennacl::is_any_scalar< FactorT >::value >::type as(ScalarT1 &s1, ScalarT2 const &s2, FactorT const &alpha, vcl_size_t, bool reciprocal_alpha, bool flip_sign_alpha)
Definition: scalar_operations.hpp:46
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
common.hpp
Common routines for single-threaded or OpenMP-enabled execution on CPU.
viennacl::result_of::cpu_value_type::type
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Definition: result_of.hpp:271
predicate.hpp
All the predicates used within ViennaCL. Checks for expressions to be vectors, etc.
viennacl::is_scalar
Helper struct for checking whether a type is a viennacl::scalar<>
Definition: forwards.h:455
enable_if.hpp
Simple enable-if variant that uses the SFINAE pattern.
viennacl::linalg::host_based::asbs_s
viennacl::enable_if< viennacl::is_scalar< ScalarT1 >::value &&viennacl::is_scalar< ScalarT2 >::value &&viennacl::is_scalar< ScalarT3 >::value &&viennacl::is_any_scalar< FactorT2 >::value &&viennacl::is_any_scalar< FactorT3 >::value >::type asbs_s(ScalarT1 &s1, ScalarT2 const &s2, FactorT2 const &alpha, vcl_size_t, bool reciprocal_alpha, bool flip_sign_alpha, ScalarT3 const &s3, FactorT3 const &beta, vcl_size_t, bool reciprocal_beta, bool flip_sign_beta)
Definition: scalar_operations.hpp:108