ViennaCL - The Vienna Computing Library  1.6.1
Free open-source GPU-accelerated linear algebra and solver library.
scalar.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_SCALAR_HPP_
2 #define VIENNACL_SCALAR_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2014, 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 PDF manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
25 #include <iostream>
26 
27 #include "viennacl/forwards.h"
32 
33 #ifdef VIENNACL_WITH_OPENCL
34 #include "viennacl/ocl/backend.hpp"
35 #endif
36 
37 namespace viennacl
38 {
46 template<typename LHS, typename RHS, typename OP>
47 class scalar_expression
48 {
49  typedef typename LHS::value_type DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
50 public:
52 
53  scalar_expression(LHS & lhs, RHS & rhs) : lhs_(lhs), rhs_(rhs) {}
54 
56  LHS & lhs() const { return lhs_; }
58  RHS & rhs() const { return rhs_; }
59 
61  operator ScalarType () const
62  {
64  temp = *this;
65  return temp;
66  }
67 
68 private:
69  LHS & lhs_;
70  RHS & rhs_;
71 };
72 
73 
81 template<typename LHS, typename RHS>
83 {
84  //typedef typename LHS::value_type DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
85 public:
87 
88  scalar_expression(LHS & lhs, RHS & rhs) : lhs_(lhs), rhs_(rhs) {}
89 
91  LHS & lhs() const { return lhs_; }
93  RHS & rhs() const { return rhs_; }
94 
96  operator ScalarType () const
97  {
98  ScalarType result;
99  viennacl::linalg::inner_prod_cpu(lhs_, rhs_, result);
100  return result;
101  }
102 
103 private:
104  LHS & lhs_;
105  RHS & rhs_;
106 };
107 
108 
114 template<typename LHS, typename RHS>
115 class scalar_expression<LHS, RHS, op_norm_1>
116 {
117  //typedef typename LHS::value_type DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
118 public:
120 
121  scalar_expression(LHS & lhs, RHS & rhs) : lhs_(lhs), rhs_(rhs) {}
122 
124  LHS & lhs() const { return lhs_; }
126  RHS & rhs() const { return rhs_; }
127 
129  operator ScalarType () const
130  {
131  ScalarType result;
132  viennacl::linalg::norm_1_cpu(lhs_, result);
133  return result;
134  }
135 
136 private:
137  LHS & lhs_;
138  RHS & rhs_;
139 };
140 
146 template<typename LHS, typename RHS>
147 class scalar_expression<LHS, RHS, op_norm_2>
148 {
149  //typedef typename LHS::value_type DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
150 public:
152 
153  scalar_expression(LHS & lhs, RHS & rhs) : lhs_(lhs), rhs_(rhs) {}
154 
156  LHS & lhs() const { return lhs_; }
158  RHS & rhs() const { return rhs_; }
159 
161  operator ScalarType () const
162  {
163  ScalarType result;
164  viennacl::linalg::norm_2_cpu(lhs_, result);
165  return result;
166  }
167 
168 private:
169  LHS & lhs_;
170  RHS & rhs_;
171 };
172 
173 
179 template<typename LHS, typename RHS>
181 {
182  //typedef typename LHS::value_type DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
183 public:
185 
186  scalar_expression(LHS & lhs, RHS & rhs) : lhs_(lhs), rhs_(rhs) {}
187 
189  LHS & lhs() const { return lhs_; }
191  RHS & rhs() const { return rhs_; }
192 
194  operator ScalarType () const
195  {
196  ScalarType result;
197  viennacl::linalg::norm_inf_cpu(lhs_, result);
198  return result;
199  }
200 
201 private:
202  LHS & lhs_;
203  RHS & rhs_;
204 };
205 
211 template<typename LHS, typename RHS>
212 class scalar_expression<LHS, RHS, op_max>
213 {
214  //typedef typename LHS::value_type DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
215 public:
217 
218  scalar_expression(LHS & lhs, RHS & rhs) : lhs_(lhs), rhs_(rhs) {}
219 
221  LHS & lhs() const { return lhs_; }
223  RHS & rhs() const { return rhs_; }
224 
226  operator ScalarType () const
227  {
228  ScalarType result;
229  viennacl::linalg::max_cpu(lhs_, result);
230  return result;
231  }
232 
233 private:
234  LHS & lhs_;
235  RHS & rhs_;
236 };
237 
238 
244 template<typename LHS, typename RHS>
245 class scalar_expression<LHS, RHS, op_min>
246 {
247  //typedef typename LHS::value_type DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
248 public:
250 
251  scalar_expression(LHS & lhs, RHS & rhs) : lhs_(lhs), rhs_(rhs) {}
252 
254  LHS & lhs() const { return lhs_; }
256  RHS & rhs() const { return rhs_; }
257 
259  operator ScalarType () const
260  {
261  ScalarType result;
262  viennacl::linalg::min_cpu(lhs_, result);
263  return result;
264  }
265 
266 private:
267  LHS & lhs_;
268  RHS & rhs_;
269 };
270 
271 
272 
278 template<typename LHS, typename RHS>
280 {
281  //typedef typename LHS::value_type DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
282 public:
284 
285  scalar_expression(LHS & lhs, RHS & rhs) : lhs_(lhs), rhs_(rhs) {}
286 
288  LHS & lhs() const { return lhs_; }
290  RHS & rhs() const { return rhs_; }
291 
293  operator ScalarType () const
294  {
295  ScalarType result;
297  return result;
298  }
299 
300 private:
301  LHS & lhs_;
302  RHS & rhs_;
303 };
304 
305 
306 
307 
315 template<class NumericT>
316 class scalar
317 {
318  typedef scalar<NumericT> self_type;
319 public:
322 
324  typedef NumericT value_type;
325 
327  scalar() {}
328 
331  {
332  viennacl::backend::memory_create(val_, sizeof(NumericT), ctx, &val);
333  }
334 
335 #ifdef VIENNACL_WITH_OPENCL
336 
341  explicit scalar(cl_mem mem, size_type /*size*/)
342  {
344  val_.opencl_handle() = mem;
345  val_.opencl_handle().inc(); //prevents that the user-provided memory is deleted once the vector object is destroyed.
346  }
347 #endif
348 
350  template<typename T1, typename T2, typename OP>
352  {
353  val_.switch_active_handle_id(viennacl::traits::handle(proxy.lhs()).get_active_handle_id());
354  viennacl::backend::memory_create(val_, sizeof(NumericT), viennacl::traits::context(proxy));
355  *this = proxy;
356  }
357 
358  //copy constructor
360  scalar(const scalar & other)
361  {
362  if (other.handle().get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED)
363  {
364  //copy value:
365  val_.switch_active_handle_id(other.handle().get_active_handle_id());
366  viennacl::backend::memory_create(val_, sizeof(NumericT), viennacl::traits::context(other));
367  viennacl::backend::memory_copy(other.handle(), val_, 0, 0, sizeof(NumericT));
368  }
369  }
370 
372  operator NumericT() const
373  {
374  // make sure the scalar contains reasonable data:
375  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized, cannot read!"));
376 
377  NumericT tmp;
378  viennacl::backend::memory_read(val_, 0, sizeof(NumericT), &tmp);
379  return tmp;
380  }
381 
383  self_type & operator= (entry_proxy<NumericT> const & other)
384  {
385  init_if_necessary(viennacl::traits::context(other));
386  viennacl::backend::memory_copy(other.handle(), val_, other.index() * sizeof(NumericT), 0, sizeof(NumericT));
387  return *this;
388  }
389 
391  self_type & operator= (scalar<NumericT> const & other)
392  {
393  init_if_necessary(viennacl::traits::context(other));
394  viennacl::backend::memory_copy(other.handle(), val_, 0, 0, sizeof(NumericT));
395  return *this;
396  }
397 
398  self_type & operator= (float cpu_other)
399  {
400  init_if_necessary(viennacl::context());
401 
402  //copy value:
403  NumericT value = static_cast<NumericT>(cpu_other);
404  viennacl::backend::memory_write(val_, 0, sizeof(NumericT), &value);
405  return *this;
406  }
407 
408  self_type & operator= (double cpu_other)
409  {
410  init_if_necessary(viennacl::context());
411 
412  NumericT value = static_cast<NumericT>(cpu_other);
413  viennacl::backend::memory_write(val_, 0, sizeof(NumericT), &value);
414  return *this;
415  }
416 
417  self_type & operator= (long cpu_other)
418  {
419  init_if_necessary(viennacl::context());
420 
421  NumericT value = static_cast<NumericT>(cpu_other);
422  viennacl::backend::memory_write(val_, 0, sizeof(NumericT), &value);
423  return *this;
424  }
425 
426  self_type & operator= (unsigned long cpu_other)
427  {
428  init_if_necessary(viennacl::context());
429 
430  NumericT value = static_cast<NumericT>(cpu_other);
431  viennacl::backend::memory_write(val_, 0, sizeof(NumericT), &value);
432  return *this;
433  }
434 
435  self_type & operator= (int cpu_other)
436  {
437  init_if_necessary(viennacl::context());
438 
439  NumericT value = static_cast<NumericT>(cpu_other);
440  viennacl::backend::memory_write(val_, 0, sizeof(NumericT), &value);
441  return *this;
442  }
443 
444  self_type & operator= (unsigned int cpu_other)
445  {
446  init_if_necessary(viennacl::context());
447 
448  NumericT value = static_cast<NumericT>(cpu_other);
449  viennacl::backend::memory_write(val_, 0, sizeof(NumericT), &value);
450  return *this;
451  }
452 
454  template<typename T1, typename T2>
456  {
457  init_if_necessary(viennacl::traits::context(proxy));
458 
459  viennacl::linalg::inner_prod_impl(proxy.lhs(), proxy.rhs(), *this);
460  return *this;
461  }
462 
464  template<typename T1, typename T2>
466  {
467  init_if_necessary(viennacl::traits::context(proxy));
468 
469  viennacl::linalg::norm_1_impl(proxy.lhs(), *this);
470  return *this;
471  }
472 
474  template<typename T1, typename T2>
476  {
477  init_if_necessary(viennacl::traits::context(proxy));
478 
479  viennacl::linalg::norm_2_impl(proxy.lhs(), *this);
480  return *this;
481  }
482 
484  template<typename T1, typename T2>
486  {
487  init_if_necessary(viennacl::traits::context(proxy));
488 
489  viennacl::linalg::norm_inf_impl(proxy.lhs(), *this);
490  return *this;
491  }
492 
494  template<typename T1, typename T2>
496  {
497  init_if_necessary(viennacl::traits::context(proxy));
498 
499  viennacl::linalg::max_impl(proxy.lhs(), *this);
500  return *this;
501  }
502 
504  template<typename T1, typename T2>
506  {
507  init_if_necessary(viennacl::traits::context(proxy));
508 
509  viennacl::linalg::min_impl(proxy.lhs(), *this);
510  return *this;
511  }
512 
513 
515  template<typename T1, typename T2>
517  {
518  init_if_necessary(viennacl::traits::context(proxy));
519 
520  viennacl::linalg::norm_frobenius_impl(proxy.lhs(), *this);
521  return *this;
522  }
523 
525  template<typename T1, typename T2>
527  {
528  init_if_necessary(viennacl::traits::context(proxy));
529 
530  viennacl::linalg::as(*this, proxy.lhs(), NumericT(-1.0), 1, false, true);
531  return *this;
532  }
533 
534 
536  self_type & operator += (scalar<NumericT> const & other)
537  {
538  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
539 
540  viennacl::linalg::asbs(*this, // s1 =
541  *this, NumericT(1.0), 1, false, false, // s1 * 1.0
542  other, NumericT(1.0), 1, false, false); // + s2 * 1.0
543  return *this;
544  }
546  self_type & operator += (NumericT other)
547  {
548  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
549 
550  viennacl::linalg::asbs(*this, // s1 =
551  *this, NumericT(1.0), 1, false, false, // s1 * 1.0
552  other, NumericT(1.0), 1, false, false); // + s2 * 1.0
553  return *this;
554  }
555 
556 
558  self_type & operator -= (scalar<NumericT> const & other)
559  {
560  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
561 
562  viennacl::linalg::asbs(*this, // s1 =
563  *this, NumericT(1.0), 1, false, false, // s1 * 1.0
564  other, NumericT(-1.0), 1, false, false); // + s2 * (-1.0)
565  return *this;
566  }
568  self_type & operator -= (NumericT other)
569  {
570  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
571 
572  viennacl::linalg::asbs(*this, // s1 =
573  *this, NumericT(1.0), 1, false, false, // s1 * 1.0
574  other, NumericT(-1.0), 1, false, false); // + s2 * (-1.0)
575  return *this;
576  }
577 
578 
580  self_type & operator *= (scalar<NumericT> const & other)
581  {
582  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
583 
584  viennacl::linalg::as(*this, // s1 =
585  *this, other, 1, false, false); // s1 * s2
586  return *this;
587  }
589  self_type & operator *= (NumericT other)
590  {
591  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
592 
593  viennacl::linalg::as(*this, // s1 =
594  *this, other, 1, false, false); // s1 * s2
595  return *this;
596  }
597 
598 
600 
601  self_type & operator /= (scalar<NumericT> const & other)
602  {
603  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
604 
605  viennacl::linalg::as(*this, // s1 =
606  *this, other, 1, true, false); // s1 / s2
607  return *this;
608  }
610  self_type & operator /= (NumericT other)
611  {
612  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
613 
614  viennacl::linalg::as(*this, // s1 =
615  *this, other, 1, true, false); // s1 / s2
616  return *this;
617  }
618 
619 
621 
622  self_type operator + (scalar<NumericT> const & other)
623  {
624  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
625 
626  self_type result = 0;
627 
628  viennacl::linalg::asbs(result, // result =
629  *this, NumericT(1.0), 1, false, false, // *this * 1.0
630  other, NumericT(1.0), 1, false, false); // + other * 1.0
631 
632  return result;
633  }
635  template<typename T1, typename T2, typename OP>
636  self_type operator + (scalar_expression<T1, T2, OP> const & proxy) const
637  {
638  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
639 
640  self_type result = proxy;
641 
642  viennacl::linalg::asbs(result, // result =
643  *this, NumericT(1.0), 1, false, false, // *this * 1.0
644  result, NumericT(1.0), 1, false, false); // + result * 1.0
645 
646  return result;
647  }
649  self_type operator + (NumericT other)
650  {
651  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
652 
653  self_type result = 0;
654 
655  viennacl::linalg::asbs(result, // result =
656  *this, NumericT(1.0), 1, false, false, // *this * 1.0
657  other, NumericT(1.0), 1, false, false); // + other * 1.0
658 
659  return result;
660  }
661 
662 
664 
667  {
669  }
670 
671 
673  self_type operator - (scalar<NumericT> const & other) const
674  {
675  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
676 
677  self_type result = 0;
678 
679  viennacl::linalg::asbs(result, // result =
680  *this, NumericT(1.0), 1, false, false, // *this * 1.0
681  other, NumericT(-1.0), 1, false, false); // + other * (-1.0)
682 
683  return result;
684  }
686  template<typename T1, typename T2, typename OP>
687  self_type operator - (scalar_expression<T1, T2, OP> const & proxy) const
688  {
689  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
690 
691  self_type result = proxy;
692 
693  viennacl::linalg::asbs(result, // result =
694  *this, NumericT(1.0), 1 , false, false, // *this * 1.0
695  result, NumericT(-1.0), 1, false, false); // + result * (-1.0)
696 
697  return result;
698  }
700  scalar<NumericT> operator - (NumericT other) const
701  {
702  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
703 
704  self_type result = 0;
705 
706  viennacl::linalg::asbs(result, // result =
707  *this, NumericT(1.0), 1, false, false, // *this * 1.0
708  other, NumericT(-1.0), 1, false, false); // + other * (-1.0)
709 
710  return result;
711  }
712 
714 
715  self_type operator * (scalar<NumericT> const & other) const
716  {
717  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
718 
719  scalar<NumericT> result = 0;
720 
721  viennacl::linalg::as(result, // result =
722  *this, other, 1, false, false); // *this * other
723 
724  return result;
725  }
727  template<typename T1, typename T2, typename OP>
728  self_type operator * (scalar_expression<T1, T2, OP> const & proxy) const
729  {
730  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
731 
732  self_type result = proxy;
733 
734  viennacl::linalg::as(result, // result =
735  *this, result, 1, false, false); // *this * proxy
736 
737  return result;
738  }
740  self_type operator * (NumericT other) const
741  {
742  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
743 
744  scalar<NumericT> result = 0;
745 
746  viennacl::linalg::as(result, // result =
747  *this, other, 1, false, false); // *this * other
748 
749  return result;
750  }
751 
753 
754  self_type operator / (scalar<NumericT> const & other) const
755  {
756  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
757 
758  self_type result = 0;
759 
760  viennacl::linalg::as(result, // result =
761  *this, other, 1, true, false); // *this / other
762 
763  return result;
764  }
766  template<typename T1, typename T2, typename OP>
767  self_type operator / (scalar_expression<T1, T2, OP> const & proxy) const
768  {
769  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
770 
771  self_type result = proxy;
772 
773  viennacl::linalg::as(result, // result =
774  *this, result, 1, true, false); // *this / proxy
775 
776  return result;
777  }
779  self_type operator / (NumericT other) const
780  {
781  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
782 
783  self_type result = 0;
784 
785  viennacl::linalg::as(result, // result =
786  *this, other, 1, true, false); // *this / other
787 
788  return result;
789  }
790 
792  handle_type & handle() { return val_; }
793 
795  const handle_type & handle() const { return val_; }
796 
797 private:
798 
799  void init_if_necessary(viennacl::context ctx)
800  {
802  {
803  viennacl::backend::memory_create(val_, sizeof(NumericT), ctx);
804  }
805  }
806 
807  handle_type val_;
808 };
809 
810 
811 //stream operators:
813 template<class NumericT>
814 std::ostream & operator<<(std::ostream & s, const scalar<NumericT> & val)
815 {
816  NumericT temp = val;
817  s << temp;
818  return s;
819 }
820 
822 template<class NumericT>
823 std::istream & operator>>(std::istream & s, const scalar<NumericT> & val)
824 {
825  NumericT temp;
826  s >> temp;
827  val = temp;
828  return s;
829 }
830 
831 } //namespace viennacl
832 
833 #endif
void norm_frobenius_cpu(matrix_base< T > const &vec, T &result)
Computes the Frobenius norm of a vector with final reduction on the CPU.
vcl_size_t index() const
Returns the index of the represented element.
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.
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
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:226
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.
void max_cpu(vector_base< T > const &vec, T &result)
Computes the maximum of a vector with final reduction on the CPU.
self_type & operator/=(scalar< NumericT > const &other)
Inplace division with a ViennaCL scalar.
Definition: scalar.hpp:601
scalar_expression(LHS &lhs, RHS &rhs)
Definition: scalar.hpp:53
Implementations of scalar operations.
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.
scalar()
Creates the scalar object, but does not yet allocate memory. Thus, scalar<> can also be a global vari...
Definition: scalar.hpp:327
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:119
This file provides the forward declarations for the main types used within ViennaCL.
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:290
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
A proxy for scalar expressions (e.g. from inner vector products)
Definition: forwards.h:229
self_type operator*(scalar< NumericT > const &other) const
Multiplication of two ViennaCL scalars.
Definition: scalar.hpp:715
A tag class representing the maximum of a vector.
Definition: forwards.h:209
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:223
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Definition: context.hpp:39
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:216
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:249
void norm_2_impl(vector_base< T > const &vec, scalar< T > &result)
Computes the l^2-norm of a vector - dispatcher interface.
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:29
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:158
void norm_frobenius_impl(matrix_base< T > const &vec, scalar< T > &result)
Computes the Frobenius norm of a matrix - dispatcher interface.
void max_impl(vector_base< T > const &vec, scalar< T > &result)
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:151
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.
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:91
handle_type const & handle() const
Returns the memory viennacl::ocl::handle.
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.
Definition: scalar.hpp:221
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:184
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:283
std::istream & operator>>(std::istream &s, const scalar< NumericT > &val)
Allows to directly read a value of a scalar from an input stream.
Definition: scalar.hpp:823
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.
Definition: scalar.hpp:93
viennacl::backend::mem_handle handle_type
Definition: scalar.hpp:320
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:288
const handle_type & handle() const
Returns the memory handle, const version.
Definition: scalar.hpp:795
viennacl::result_of::cpu_value_type< DummyType >::type ScalarType
Definition: scalar.hpp:51
std::size_t vcl_size_t
Definition: forwards.h:74
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.
Definition: scalar.hpp:156
A tag class representing the minimum of a vector.
Definition: forwards.h:212
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Definition: result_of.hpp:238
Implementations of the OpenCL backend, where all contexts are stored in.
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:254
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:191
self_type & operator=(entry_proxy< NumericT > const &other)
Assigns a vector entry.
Definition: scalar.hpp:383
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:121
self_type operator/(scalar< NumericT > const &other) const
Division of two ViennaCL scalars.
Definition: scalar.hpp:754
A tag class representing the 1-norm of a vector.
Definition: forwards.h:200
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:40
vcl_size_t size_type
Definition: scalar.hpp:321
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:124
scalar(const scalar &other)
Copy constructor. Allocates new memory for the scalar and copies the value of the supplied scalar...
Definition: scalar.hpp:360
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:56
self_type & operator*=(scalar< NumericT > const &other)
Inplace multiplication with a ViennaCL scalar.
Definition: scalar.hpp:580
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
scalar(NumericT val, viennacl::context ctx=viennacl::context())
Allocates the memory for the scalar and sets it to the supplied value.
Definition: scalar.hpp:330
A tag class representing the Frobenius-norm of a matrix.
Definition: forwards.h:216
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
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:86
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
A tag class representing inner products of two vectors.
Definition: forwards.h:197
self_type operator+(scalar< NumericT > const &other)
Addition of two ViennaCL scalars.
Definition: scalar.hpp:622
handle_type & handle()
Returns the memory handle, non-const version.
Definition: scalar.hpp:792
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:126
A proxy class for a single element of a vector or matrix. This proxy should not be noticed by end-use...
Definition: forwards.h:232
A tag class representing the inf-norm of a vector.
Definition: forwards.h:206
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:189
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:58
NumericT value_type
Returns the underlying host scalar type.
Definition: scalar.hpp:324
A tag class representing the 2-norm of a vector.
Definition: forwards.h:203
void norm_inf_cpu(vector_base< T > const &vec, T &result)
Computes the supremum-norm of a vector with final reduction on the CPU.
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:351
A collection of compile time type deductions.
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:666
Main interface routines for memory management.
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:256
self_type & operator-=(scalar< NumericT > const &other)
Inplace subtraction of a ViennaCL scalar.
Definition: scalar.hpp:558
self_type & operator+=(scalar< NumericT > const &other)
Inplace addition of a ViennaCL scalar.
Definition: scalar.hpp:536
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:118