ViennaCL - The Vienna Computing Library  1.7.0
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-2015, 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 <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 
276 template<typename LHS, typename RHS>
277 class scalar_expression<LHS, RHS, op_sum>
278 {
279  //typedef typename LHS::value_type DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
280 public:
282 
283  scalar_expression(LHS & lhs, RHS & rhs) : lhs_(lhs), rhs_(rhs) {}
284 
286  LHS & lhs() const { return lhs_; }
288  RHS & rhs() const { return rhs_; }
289 
291  operator ScalarType () const
292  {
293  ScalarType result;
294  viennacl::linalg::sum_cpu(lhs_, result);
295  return result;
296  }
297 
298 private:
299  LHS & lhs_;
300  RHS & rhs_;
301 };
302 
303 
309 template<typename LHS, typename RHS>
311 {
312  //typedef typename LHS::value_type DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
313 public:
315 
316  scalar_expression(LHS & lhs, RHS & rhs) : lhs_(lhs), rhs_(rhs) {}
317 
319  LHS & lhs() const { return lhs_; }
321  RHS & rhs() const { return rhs_; }
322 
324  operator ScalarType () const
325  {
326  ScalarType result;
328  return result;
329  }
330 
331 private:
332  LHS & lhs_;
333  RHS & rhs_;
334 };
335 
336 
337 
338 
346 template<class NumericT>
347 class scalar
348 {
349  typedef scalar<NumericT> self_type;
350 public:
353 
356 
358  scalar() {}
359 
362  {
363  viennacl::backend::memory_create(val_, sizeof(NumericT), ctx, &val);
364  }
365 
366 #ifdef VIENNACL_WITH_OPENCL
367 
372  explicit scalar(cl_mem mem, size_type /*size*/)
373  {
375  val_.opencl_handle() = mem;
376  val_.opencl_handle().inc(); //prevents that the user-provided memory is deleted once the vector object is destroyed.
377  }
378 #endif
379 
381  template<typename T1, typename T2, typename OP>
383  {
384  val_.switch_active_handle_id(viennacl::traits::handle(proxy.lhs()).get_active_handle_id());
386  *this = proxy;
387  }
388 
389  //copy constructor
391  scalar(const scalar & other)
392  {
393  if (other.handle().get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED)
394  {
395  //copy value:
396  val_.switch_active_handle_id(other.handle().get_active_handle_id());
398  viennacl::backend::memory_copy(other.handle(), val_, 0, 0, sizeof(NumericT));
399  }
400  }
401 
403  operator NumericT() const
404  {
405  // make sure the scalar contains reasonable data:
406  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized, cannot read!"));
407 
408  NumericT tmp;
409  viennacl::backend::memory_read(val_, 0, sizeof(NumericT), &tmp);
410  return tmp;
411  }
412 
414  self_type & operator= (entry_proxy<NumericT> const & other)
415  {
416  init_if_necessary(viennacl::traits::context(other));
417  viennacl::backend::memory_copy(other.handle(), val_, other.index() * sizeof(NumericT), 0, sizeof(NumericT));
418  return *this;
419  }
420 
422  self_type & operator= (scalar<NumericT> const & other)
423  {
424  init_if_necessary(viennacl::traits::context(other));
425  viennacl::backend::memory_copy(other.handle(), val_, 0, 0, sizeof(NumericT));
426  return *this;
427  }
428 
429  self_type & operator= (float cpu_other)
430  {
431  init_if_necessary(viennacl::context());
432 
433  //copy value:
434  NumericT value = static_cast<NumericT>(cpu_other);
435  viennacl::backend::memory_write(val_, 0, sizeof(NumericT), &value);
436  return *this;
437  }
438 
439  self_type & operator= (double cpu_other)
440  {
441  init_if_necessary(viennacl::context());
442 
443  NumericT value = static_cast<NumericT>(cpu_other);
444  viennacl::backend::memory_write(val_, 0, sizeof(NumericT), &value);
445  return *this;
446  }
447 
448  self_type & operator= (long cpu_other)
449  {
450  init_if_necessary(viennacl::context());
451 
452  NumericT value = static_cast<NumericT>(cpu_other);
453  viennacl::backend::memory_write(val_, 0, sizeof(NumericT), &value);
454  return *this;
455  }
456 
457  self_type & operator= (unsigned long cpu_other)
458  {
459  init_if_necessary(viennacl::context());
460 
461  NumericT value = static_cast<NumericT>(cpu_other);
462  viennacl::backend::memory_write(val_, 0, sizeof(NumericT), &value);
463  return *this;
464  }
465 
466  self_type & operator= (int cpu_other)
467  {
468  init_if_necessary(viennacl::context());
469 
470  NumericT value = static_cast<NumericT>(cpu_other);
471  viennacl::backend::memory_write(val_, 0, sizeof(NumericT), &value);
472  return *this;
473  }
474 
475  self_type & operator= (unsigned int cpu_other)
476  {
477  init_if_necessary(viennacl::context());
478 
479  NumericT value = static_cast<NumericT>(cpu_other);
480  viennacl::backend::memory_write(val_, 0, sizeof(NumericT), &value);
481  return *this;
482  }
483 
485  template<typename T1, typename T2>
487  {
488  init_if_necessary(viennacl::traits::context(proxy));
489 
490  viennacl::linalg::inner_prod_impl(proxy.lhs(), proxy.rhs(), *this);
491  return *this;
492  }
493 
495  template<typename T1, typename T2>
497  {
498  init_if_necessary(viennacl::traits::context(proxy));
499 
500  viennacl::linalg::norm_1_impl(proxy.lhs(), *this);
501  return *this;
502  }
503 
505  template<typename T1, typename T2>
507  {
508  init_if_necessary(viennacl::traits::context(proxy));
509 
510  viennacl::linalg::norm_2_impl(proxy.lhs(), *this);
511  return *this;
512  }
513 
515  template<typename T1, typename T2>
517  {
518  init_if_necessary(viennacl::traits::context(proxy));
519 
520  viennacl::linalg::norm_inf_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::max_impl(proxy.lhs(), *this);
531  return *this;
532  }
533 
535  template<typename T1, typename T2>
537  {
538  init_if_necessary(viennacl::traits::context(proxy));
539 
540  viennacl::linalg::min_impl(proxy.lhs(), *this);
541  return *this;
542  }
543 
545  template<typename T1, typename T2>
547  {
548  init_if_necessary(viennacl::traits::context(proxy));
549 
550  viennacl::linalg::sum_impl(proxy.lhs(), *this);
551  return *this;
552  }
553 
554 
556  template<typename T1, typename T2>
558  {
559  init_if_necessary(viennacl::traits::context(proxy));
560 
561  viennacl::linalg::norm_frobenius_impl(proxy.lhs(), *this);
562  return *this;
563  }
564 
566  template<typename T1, typename T2>
568  {
569  init_if_necessary(viennacl::traits::context(proxy));
570 
571  viennacl::linalg::as(*this, proxy.lhs(), NumericT(-1.0), 1, false, true);
572  return *this;
573  }
574 
575 
577  self_type & operator += (scalar<NumericT> const & other)
578  {
579  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
580 
581  viennacl::linalg::asbs(*this, // s1 =
582  *this, NumericT(1.0), 1, false, false, // s1 * 1.0
583  other, NumericT(1.0), 1, false, false); // + s2 * 1.0
584  return *this;
585  }
587  self_type & operator += (NumericT other)
588  {
589  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
590 
591  viennacl::linalg::asbs(*this, // s1 =
592  *this, NumericT(1.0), 1, false, false, // s1 * 1.0
593  other, NumericT(1.0), 1, false, false); // + s2 * 1.0
594  return *this;
595  }
596 
597 
599  self_type & operator -= (scalar<NumericT> const & other)
600  {
601  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
602 
603  viennacl::linalg::asbs(*this, // s1 =
604  *this, NumericT(1.0), 1, false, false, // s1 * 1.0
605  other, NumericT(-1.0), 1, false, false); // + s2 * (-1.0)
606  return *this;
607  }
609  self_type & operator -= (NumericT other)
610  {
611  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
612 
613  viennacl::linalg::asbs(*this, // s1 =
614  *this, NumericT(1.0), 1, false, false, // s1 * 1.0
615  other, NumericT(-1.0), 1, false, false); // + s2 * (-1.0)
616  return *this;
617  }
618 
619 
621  self_type & operator *= (scalar<NumericT> const & other)
622  {
623  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
624 
625  viennacl::linalg::as(*this, // s1 =
626  *this, other, 1, false, false); // s1 * s2
627  return *this;
628  }
630  self_type & operator *= (NumericT other)
631  {
632  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
633 
634  viennacl::linalg::as(*this, // s1 =
635  *this, other, 1, false, false); // s1 * s2
636  return *this;
637  }
638 
639 
641 
642  self_type & operator /= (scalar<NumericT> const & other)
643  {
644  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
645 
646  viennacl::linalg::as(*this, // s1 =
647  *this, other, 1, true, false); // s1 / s2
648  return *this;
649  }
651  self_type & operator /= (NumericT other)
652  {
653  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
654 
655  viennacl::linalg::as(*this, // s1 =
656  *this, other, 1, true, false); // s1 / s2
657  return *this;
658  }
659 
660 
662 
663  self_type operator + (scalar<NumericT> const & other)
664  {
665  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
666 
667  self_type result = 0;
668 
669  viennacl::linalg::asbs(result, // result =
670  *this, NumericT(1.0), 1, false, false, // *this * 1.0
671  other, NumericT(1.0), 1, false, false); // + other * 1.0
672 
673  return result;
674  }
676  template<typename T1, typename T2, typename OP>
677  self_type operator + (scalar_expression<T1, T2, OP> const & proxy) const
678  {
679  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
680 
681  self_type result = proxy;
682 
683  viennacl::linalg::asbs(result, // result =
684  *this, NumericT(1.0), 1, false, false, // *this * 1.0
685  result, NumericT(1.0), 1, false, false); // + result * 1.0
686 
687  return result;
688  }
690  self_type operator + (NumericT other)
691  {
692  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
693 
694  self_type result = 0;
695 
696  viennacl::linalg::asbs(result, // result =
697  *this, NumericT(1.0), 1, false, false, // *this * 1.0
698  other, NumericT(1.0), 1, false, false); // + other * 1.0
699 
700  return result;
701  }
702 
703 
705 
708  {
710  }
711 
712 
714  self_type operator - (scalar<NumericT> const & other) const
715  {
716  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
717 
718  self_type result = 0;
719 
720  viennacl::linalg::asbs(result, // result =
721  *this, NumericT(1.0), 1, false, false, // *this * 1.0
722  other, NumericT(-1.0), 1, false, false); // + other * (-1.0)
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::asbs(result, // result =
735  *this, NumericT(1.0), 1 , false, false, // *this * 1.0
736  result, NumericT(-1.0), 1, false, false); // + result * (-1.0)
737 
738  return result;
739  }
742  {
743  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
744 
745  self_type result = 0;
746 
747  viennacl::linalg::asbs(result, // result =
748  *this, NumericT(1.0), 1, false, false, // *this * 1.0
749  other, NumericT(-1.0), 1, false, false); // + other * (-1.0)
750 
751  return result;
752  }
753 
755 
756  self_type operator * (scalar<NumericT> const & other) const
757  {
758  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
759 
760  scalar<NumericT> result = 0;
761 
762  viennacl::linalg::as(result, // result =
763  *this, other, 1, false, false); // *this * other
764 
765  return result;
766  }
768  template<typename T1, typename T2, typename OP>
769  self_type operator * (scalar_expression<T1, T2, OP> const & proxy) const
770  {
771  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
772 
773  self_type result = proxy;
774 
775  viennacl::linalg::as(result, // result =
776  *this, result, 1, false, false); // *this * proxy
777 
778  return result;
779  }
781  self_type operator * (NumericT other) const
782  {
783  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
784 
785  scalar<NumericT> result = 0;
786 
787  viennacl::linalg::as(result, // result =
788  *this, other, 1, false, false); // *this * other
789 
790  return result;
791  }
792 
794 
795  self_type operator / (scalar<NumericT> const & other) const
796  {
797  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
798 
799  self_type result = 0;
800 
801  viennacl::linalg::as(result, // result =
802  *this, other, 1, true, false); // *this / other
803 
804  return result;
805  }
807  template<typename T1, typename T2, typename OP>
808  self_type operator / (scalar_expression<T1, T2, OP> const & proxy) const
809  {
810  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
811 
812  self_type result = proxy;
813 
814  viennacl::linalg::as(result, // result =
815  *this, result, 1, true, false); // *this / proxy
816 
817  return result;
818  }
820  self_type operator / (NumericT other) const
821  {
822  assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));
823 
824  self_type result = 0;
825 
826  viennacl::linalg::as(result, // result =
827  *this, other, 1, true, false); // *this / other
828 
829  return result;
830  }
831 
833  handle_type & handle() { return val_; }
834 
836  const handle_type & handle() const { return val_; }
837 
838 private:
839 
840  void init_if_necessary(viennacl::context ctx)
841  {
843  {
844  viennacl::backend::memory_create(val_, sizeof(NumericT), ctx);
845  }
846  }
847 
848  handle_type val_;
849 };
850 
851 
852 //stream operators:
854 template<class NumericT>
855 std::ostream & operator<<(std::ostream & s, const scalar<NumericT> & val)
856 {
857  NumericT temp = val;
858  s << temp;
859  return s;
860 }
861 
863 template<class NumericT>
864 std::istream & operator>>(std::istream & s, const scalar<NumericT> & val)
865 {
866  NumericT temp;
867  s >> temp;
868  val = temp;
869  return s;
870 }
871 
872 } //namespace viennacl
873 
874 #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:227
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...
A tag class representing the summation of a vector.
Definition: forwards.h:116
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:642
scalar_expression(LHS &lhs, RHS &rhs)
Definition: scalar.hpp:53
Implementations of scalar operations.
void sum_impl(vector_base< T > const &vec, scalar< T > &result)
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:358
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:321
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:230
self_type operator*(scalar< NumericT > const &other) const
Multiplication of two ViennaCL scalars.
Definition: scalar.hpp:756
A tag class representing the maximum of a vector.
Definition: forwards.h:210
float NumericT
Definition: bisect.cpp:40
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:34
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:314
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:864
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:351
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:319
const handle_type & handle() const
Returns the memory handle, const version.
Definition: scalar.hpp:836
viennacl::result_of::cpu_value_type< DummyType >::type ScalarType
Definition: scalar.hpp:51
std::size_t vcl_size_t
Definition: forwards.h:75
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:213
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Definition: result_of.hpp:271
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:414
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:795
A tag class representing the 1-norm of a vector.
Definition: forwards.h:201
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
void sum_cpu(vector_base< T > const &vec, T &result)
Computes the sum of a vector with final reduction on the CPU.
vcl_size_t size_type
Definition: scalar.hpp:352
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:391
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:621
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:361
A tag class representing the Frobenius-norm of a matrix.
Definition: forwards.h:217
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
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:281
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:198
self_type operator+(scalar< NumericT > const &other)
Addition of two ViennaCL scalars.
Definition: scalar.hpp:663
handle_type & handle()
Returns the memory handle, non-const version.
Definition: scalar.hpp:833
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:233
A tag class representing the inf-norm of a vector.
Definition: forwards.h:207
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:355
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:288
A tag class representing the 2-norm of a vector.
Definition: forwards.h:204
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:382
A collection of compile time type deductions.
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:286
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:707
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:599
self_type & operator+=(scalar< NumericT > const &other)
Inplace addition of a ViennaCL scalar.
Definition: scalar.hpp:577
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