ViennaCL - The Vienna Computing Library: viennacl/scalar.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.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_SCALAR_HPP_
2 #define VIENNACL_SCALAR_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 <iostream>
26
27 #include "viennacl/forwards.h"
28 #include "viennacl/backend/memory.hpp"
29 #include "viennacl/meta/result_of.hpp"
30 #include "viennacl/linalg/scalar_operations.hpp"
31 #include "viennacl/traits/handle.hpp"
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:
51 typedef typename viennacl::result_of::cpu_value_type<DummyType>::type ScalarType;
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 {
63 viennacl::scalar<ScalarType> temp;
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>
82 class scalar_expression<LHS, RHS, op_inner_prod>
83 {
84 //typedef typename LHS::value_type DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
85 public:
86 typedef typename viennacl::result_of::cpu_value_type<LHS>::type ScalarType;
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:
119 typedef typename viennacl::result_of::cpu_value_type<LHS>::type ScalarType;
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:
151 typedef typename viennacl::result_of::cpu_value_type<LHS>::type ScalarType;
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>
180 class scalar_expression<LHS, RHS, op_norm_inf>
181 {
182 //typedef typename LHS::value_type DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
183 public:
184 typedef typename viennacl::result_of::cpu_value_type<LHS>::type ScalarType;
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:
216 typedef typename viennacl::result_of::cpu_value_type<LHS>::type ScalarType;
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:
249 typedef typename viennacl::result_of::cpu_value_type<LHS>::type ScalarType;
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:
281 typedef typename viennacl::result_of::cpu_value_type<LHS>::type ScalarType;
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>
310 class scalar_expression<LHS, RHS, op_norm_frobenius>
311 {
312 //typedef typename LHS::value_type DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
313 public:
314 typedef typename viennacl::result_of::cpu_value_type<LHS>::type ScalarType;
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;
327 viennacl::linalg::norm_frobenius_cpu(lhs_, 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:
351 typedef viennacl::backend::mem_handle handle_type;
352 typedef vcl_size_t size_type;
353
355 typedef NumericT value_type;
356
358 scalar() {}
359
361 scalar(NumericT val, viennacl::context ctx = viennacl::context())
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 {
374 val_.switch_active_handle_id(viennacl::OPENCL_MEMORY);
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>
382 scalar(scalar_expression<T1, T2, OP> const & proxy)
383 {
384 val_.switch_active_handle_id(viennacl::traits::handle(proxy.lhs()).get_active_handle_id());
385 viennacl::backend::memory_create(val_, sizeof(NumericT), viennacl::traits::context(proxy));
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());
397 viennacl::backend::memory_create(val_, sizeof(NumericT), viennacl::traits::context(other));
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>
486 self_type & operator= (scalar_expression<T1, T2, op_inner_prod> const & proxy)
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>
496 self_type & operator= (scalar_expression<T1, T2, op_norm_1> const & proxy)
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>
506 self_type & operator= (scalar_expression<T1, T2, op_norm_2> const & proxy)
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>
516 self_type & operator= (scalar_expression<T1, T2, op_norm_inf> const & proxy)
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>
526 self_type & operator= (scalar_expression<T1, T2, op_max> const & proxy)
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>
536 self_type & operator= (scalar_expression<T1, T2, op_min> const & proxy)
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>
546 self_type & operator= (scalar_expression<T1, T2, op_sum> const & proxy)
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>
557 self_type & operator= (scalar_expression<T1, T2, op_norm_frobenius> const & proxy)
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>
567 self_type & operator= (scalar_expression<T1, T2, op_flip_sign> const & proxy)
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
707 scalar_expression<const self_type, const self_type, op_flip_sign> operator-() const
708 {
709 return scalar_expression<const self_type, const self_type, op_flip_sign>(*this, *this);
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 }
741 scalar<NumericT> operator - (NumericT other) const
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 {
842 if (val_.get_active_handle_id() == viennacl::MEMORY_NOT_INITIALIZED)
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
viennacl::linalg::norm_frobenius_cpu
void norm_frobenius_cpu(matrix_base< T > const &vec, T &result)
Computes the Frobenius norm of a vector with final reduction on the CPU.
Definition: matrix_operations.hpp:399
viennacl::linalg::as
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
viennacl::scalar_expression< LHS, RHS, op_inner_prod >::scalar_expression
scalar_expression(LHS &lhs, RHS &rhs)
Definition: scalar.hpp:88
viennacl::backend::memory_write
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
viennacl::scalar
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:227
viennacl::linalg::inner_prod_cpu
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:459
viennacl::op_sum
A tag class representing the summation of a vector.
Definition: forwards.h:116
viennacl::linalg::min_cpu
void min_cpu(vector_base< T > const &vec, T &result)
Computes the minimum of a vector with final reduction on the CPU.
Definition: vector_operations.hpp:1015
viennacl::scalar_expression< LHS, RHS, op_norm_frobenius >::scalar_expression
scalar_expression(LHS &lhs, RHS &rhs)
Definition: scalar.hpp:316
viennacl::scalar::scalar
scalar()
Creates the scalar object, but does not yet allocate memory. Thus, scalar<> can also be a global vari...
Definition: scalar.hpp:358
viennacl::scalar::scalar
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
viennacl::scalar::operator-
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
viennacl::linalg::max_cpu
void max_cpu(vector_base< T > const &vec, T &result)
Computes the maximum of a vector with final reduction on the CPU.
Definition: vector_operations.hpp:928
viennacl::scalar::handle
handle_type & handle()
Returns the memory handle, non-const version.
Definition: scalar.hpp:833
viennacl::scalar_expression::scalar_expression
scalar_expression(LHS &lhs, RHS &rhs)
Definition: scalar.hpp:53
scalar_operations.hpp
Implementations of scalar operations.
viennacl::scalar::scalar
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
viennacl::linalg::sum_impl
void sum_impl(vector_base< T > const &vec, scalar< T > &result)
viennacl::scalar::operator/=
self_type & operator/=(scalar< NumericT > const &other)
Inplace division with a ViennaCL scalar.
Definition: scalar.hpp:642
viennacl::linalg::norm_2_cpu
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:705
viennacl::OPENCL_MEMORY
Definition: forwards.h:349
viennacl::scalar_expression< LHS, RHS, op_norm_1 >::ScalarType
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:119
viennacl::scalar::operator*
self_type operator*(scalar< NumericT > const &other) const
Multiplication of two ViennaCL scalars.
Definition: scalar.hpp:756
viennacl::scalar::size_type
vcl_size_t size_type
Definition: scalar.hpp:352
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::scalar_expression< LHS, RHS, op_norm_frobenius >::rhs
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:321
viennacl::backend::memory_read
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
viennacl::scalar::operator-=
self_type & operator-=(scalar< NumericT > const &other)
Inplace subtraction of a ViennaCL scalar.
Definition: scalar.hpp:599
viennacl::scalar_expression
A proxy for scalar expressions (e.g. from inner vector products)
Definition: forwards.h:230
viennacl::scalar::value_type
NumericT value_type
Returns the underlying host scalar type.
Definition: scalar.hpp:355
viennacl::op_max
A tag class representing the maximum of a vector.
Definition: forwards.h:210
viennacl::scalar_expression< LHS, RHS, op_min >::scalar_expression
scalar_expression(LHS &lhs, RHS &rhs)
Definition: scalar.hpp:251
NumericT
float NumericT
Definition: bisect.cpp:40
viennacl::scalar_expression< LHS, RHS, op_max >::rhs
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:223
viennacl::context
Represents a generic 'context' similar to an OpenCL context, but is backend-agnostic and thus also su...
Definition: context.hpp:39
viennacl::scalar_expression< LHS, RHS, op_max >::ScalarType
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:216
viennacl::scalar_expression< LHS, RHS, op_min >::ScalarType
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:249
viennacl::linalg::norm_2_impl
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:660
viennacl::scalar_expression< LHS, RHS, op_norm_2 >::rhs
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:158
viennacl::linalg::norm_frobenius_impl
void norm_frobenius_impl(matrix_base< T > const &vec, scalar< T > &result)
Computes the Frobenius norm of a matrix - dispatcher interface.
Definition: matrix_operations.hpp:369
viennacl::scalar_expression< LHS, RHS, op_norm_inf >::scalar_expression
scalar_expression(LHS &lhs, RHS &rhs)
Definition: scalar.hpp:186
viennacl::linalg::max_impl
void max_impl(vector_base< T > const &vec, scalar< T > &result)
viennacl::entry_proxy::index
vcl_size_t index() const
Returns the index of the represented element.
Definition: entry_proxy.hpp:138
viennacl::scalar_expression< LHS, RHS, op_sum >::scalar_expression
scalar_expression(LHS &lhs, RHS &rhs)
Definition: scalar.hpp:283
viennacl::scalar::scalar
scalar(const scalar &other)
Copy constructor. Allocates new memory for the scalar and copies the value of the supplied scalar...
Definition: scalar.hpp:391
viennacl::scalar_expression< LHS, RHS, op_norm_2 >::ScalarType
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:151
viennacl::linalg::inner_prod_impl
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:387
viennacl::scalar::operator/
self_type operator/(scalar< NumericT > const &other) const
Division of two ViennaCL scalars.
Definition: scalar.hpp:795
viennacl::scalar::operator+=
self_type & operator+=(scalar< NumericT > const &other)
Inplace addition of a ViennaCL scalar.
Definition: scalar.hpp:577
viennacl::scalar_expression< LHS, RHS, op_inner_prod >::lhs
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:91
viennacl::scalar::operator=
self_type & operator=(entry_proxy< NumericT > const &other)
Assigns a vector entry.
Definition: scalar.hpp:414
viennacl::scalar_expression< LHS, RHS, op_norm_2 >::scalar_expression
scalar_expression(LHS &lhs, RHS &rhs)
Definition: scalar.hpp:153
viennacl::MEMORY_NOT_INITIALIZED
Definition: forwards.h:347
viennacl::linalg::norm_1_cpu
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:613
viennacl::scalar_expression< LHS, RHS, op_max >::lhs
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:221
viennacl::scalar_expression< LHS, RHS, op_norm_inf >::ScalarType
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:184
viennacl::scalar_expression< LHS, RHS, op_norm_frobenius >::ScalarType
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:314
viennacl::scalar::handle
const handle_type & handle() const
Returns the memory handle, const version.
Definition: scalar.hpp:836
viennacl::operator>>
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
viennacl::linalg::norm_inf_impl
void norm_inf_impl(vector_base< T > const &vec, scalar< T > &result)
Computes the supremum-norm of a vector.
Definition: vector_operations.hpp:752
viennacl::linalg::norm_1_impl
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:566
viennacl::scalar_expression< LHS, RHS, op_inner_prod >::rhs
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:93
viennacl::scalar::operator*=
self_type & operator*=(scalar< NumericT > const &other)
Inplace multiplication with a ViennaCL scalar.
Definition: scalar.hpp:621
viennacl::scalar_expression< LHS, RHS, op_norm_frobenius >::lhs
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:319
viennacl::scalar_expression::ScalarType
viennacl::result_of::cpu_value_type< DummyType >::type ScalarType
Definition: scalar.hpp:51
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::linalg::min_impl
void min_impl(vector_base< T > const &vec, scalar< T > &result)
viennacl::linalg::asbs
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
viennacl::scalar_expression< LHS, RHS, op_norm_2 >::lhs
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:156
viennacl::op_min
A tag class representing the minimum of a vector.
Definition: forwards.h:213
viennacl::result_of::cpu_value_type::type
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Definition: result_of.hpp:271
backend.hpp
Implementations of the OpenCL backend, where all contexts are stored in.
viennacl::scalar_expression< LHS, RHS, op_min >::lhs
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:254
viennacl::scalar_expression< LHS, RHS, op_norm_inf >::rhs
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:191
viennacl::entry_proxy::handle
handle_type const & handle() const
Returns the memory viennacl::ocl::handle.
Definition: entry_proxy.hpp:142
viennacl::backend::mem_handle::switch_active_handle_id
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
viennacl::op_norm_1
A tag class representing the 1-norm of a vector.
Definition: forwards.h:201
viennacl::backend::memory_copy
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::scalar::operator+
self_type operator+(scalar< NumericT > const &other)
Addition of two ViennaCL scalars.
Definition: scalar.hpp:663
viennacl::traits::context
viennacl::context context(T const &t)
Returns an ID for the currently active memory domain of an object.
Definition: context.hpp:40
viennacl::linalg::sum_cpu
void sum_cpu(vector_base< T > const &vec, T &result)
Computes the sum of a vector with final reduction on the CPU.
Definition: vector_operations.hpp:1102
viennacl::scalar_expression< LHS, RHS, op_norm_1 >::lhs
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:124
viennacl::scalar_expression::lhs
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:56
viennacl::scalar::handle_type
viennacl::backend::mem_handle handle_type
Definition: scalar.hpp:351
viennacl::backend::mem_handle
Main abstraction class for multiple memory domains. Represents a buffer in either main RAM...
Definition: mem_handle.hpp:89
viennacl::scalar_expression< LHS, RHS, op_norm_1 >::scalar_expression
scalar_expression(LHS &lhs, RHS &rhs)
Definition: scalar.hpp:121
viennacl::op_norm_frobenius
A tag class representing the Frobenius-norm of a matrix.
Definition: forwards.h:217
viennacl::backend::memory_create
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::scalar_expression< LHS, RHS, op_inner_prod >::ScalarType
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:86
viennacl::scalar_expression< LHS, RHS, op_sum >::ScalarType
viennacl::result_of::cpu_value_type< LHS >::type ScalarType
Definition: scalar.hpp:281
handle.hpp
Extracts the underlying OpenCL handle from a vector, a matrix, an expression etc. ...
viennacl::traits::handle
viennacl::backend::mem_handle & handle(T &obj)
Returns the generic memory handle of an object. Non-const version.
Definition: handle.hpp:41
viennacl::op_inner_prod
A tag class representing inner products of two vectors.
Definition: forwards.h:198
viennacl::scalar_expression< LHS, RHS, op_norm_1 >::rhs
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:126
viennacl::entry_proxy
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
viennacl::op_norm_inf
A tag class representing the inf-norm of a vector.
Definition: forwards.h:207
viennacl::scalar_expression< LHS, RHS, op_norm_inf >::lhs
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:189
viennacl::scalar_expression::rhs
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:58
viennacl::scalar_expression< LHS, RHS, op_sum >::rhs
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:288
viennacl::op_norm_2
A tag class representing the 2-norm of a vector.
Definition: forwards.h:204
viennacl::linalg::norm_inf_cpu
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:797
result_of.hpp
A collection of compile time type deductions.
viennacl::scalar_expression< LHS, RHS, op_sum >::lhs
LHS & lhs() const
Returns the left hand side operand.
Definition: scalar.hpp:286
memory.hpp
Main interface routines for memory management.
viennacl::scalar_expression< LHS, RHS, op_max >::scalar_expression
scalar_expression(LHS &lhs, RHS &rhs)
Definition: scalar.hpp:218
viennacl::scalar_expression< LHS, RHS, op_min >::rhs
RHS & rhs() const
Returns the left hand side operand.
Definition: scalar.hpp:256
viennacl::backend::mem_handle::get_active_handle_id
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