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"
29 #include "viennacl/traits/size.hpp"
33 
34 namespace viennacl
35 {
36 namespace linalg
37 {
38 namespace host_based
39 {
40 template<typename ScalarT1,
41  typename ScalarT2, typename FactorT>
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>
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>
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>
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
Simple enable-if variant that uses the SFINAE pattern.
Definition: enable_if.hpp:30
Generic size and resize functionality for different vector and matrix types.
Extracts the underlying OpenCL start index handle from a vector, a matrix, an expression etc...
Various little tools used here and there in ViennaCL.
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.
This file provides the forward declarations for the main types used within ViennaCL.
Determines row and column increments for matrices and matrix proxies.
viennacl::scalar< int > s2
viennacl::scalar< float > s1
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::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)
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)
std::size_t vcl_size_t
Definition: forwards.h:75
Common routines for single-threaded or OpenMP-enabled execution on CPU.
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Definition: result_of.hpp:271
All the predicates used within ViennaCL. Checks for expressions to be vectors, etc.
Helper struct for checking whether a type is a viennacl::scalar<>
Definition: forwards.h:455
Simple enable-if variant that uses the SFINAE pattern.
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)