This file is indexed.

/usr/include/viennacl/scalar.hpp is in libviennacl-dev 1.7.1+dfsg1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
#ifndef VIENNACL_SCALAR_HPP_
#define VIENNACL_SCALAR_HPP_

/* =========================================================================
   Copyright (c) 2010-2016, Institute for Microelectronics,
                            Institute for Analysis and Scientific Computing,
                            TU Wien.
   Portions of this software are copyright by UChicago Argonne, LLC.

                            -----------------
                  ViennaCL - The Vienna Computing Library
                            -----------------

   Project Head:    Karl Rupp                   rupp@iue.tuwien.ac.at

   (A list of authors and contributors can be found in the manual)

   License:         MIT (X11), see file LICENSE in the base directory
============================================================================= */

/** @file viennacl/scalar.hpp
    @brief Implementation of the ViennaCL scalar class
*/

#include <iostream>

#include "viennacl/forwards.h"
#include "viennacl/backend/memory.hpp"
#include "viennacl/meta/result_of.hpp"
#include "viennacl/linalg/scalar_operations.hpp"
#include "viennacl/traits/handle.hpp"

#ifdef VIENNACL_WITH_OPENCL
#include "viennacl/ocl/backend.hpp"
#endif

namespace viennacl
{
/** @brief A proxy for scalar expressions (e.g. from inner vector products)
  *
  * assumption: dim(LHS) >= dim(RHS), where dim(scalar) = 0, dim(vector) = 1 and dim(matrix = 2)
  * @tparam LHS   The left hand side operand
  * @tparam RHS   The right hand side operand
  * @tparam OP    The operation tag
  */
template<typename LHS, typename RHS, typename OP>
class scalar_expression
{
  typedef typename LHS::value_type          DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
public:
  typedef typename viennacl::result_of::cpu_value_type<DummyType>::type    ScalarType;

  scalar_expression(LHS & lhs, RHS & rhs) : lhs_(lhs), rhs_(rhs) {}

  /** @brief Returns the left hand side operand */
  LHS & lhs() const { return lhs_; }
  /** @brief Returns the left hand side operand */
  RHS & rhs() const { return rhs_; }

  /** @brief Conversion operator to a ViennaCL scalar */
  operator ScalarType () const
  {
    viennacl::scalar<ScalarType> temp;
    temp = *this;
    return temp;
  }

private:
  LHS & lhs_;
  RHS & rhs_;
};


/** @brief Specialization of a scalar expression for inner products. Allows for a final reduction on the CPU
  *
  * assumption: dim(LHS) >= dim(RHS), where dim(scalar) = 0, dim(vector) = 1 and dim(matrix = 2)
  * @tparam LHS   The left hand side operand
  * @tparam RHS   The right hand side operand
  * @tparam OP    The operation tag
  */
template<typename LHS, typename RHS>
class scalar_expression<LHS, RHS, op_inner_prod>
{
  //typedef typename LHS::value_type          DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
public:
  typedef typename viennacl::result_of::cpu_value_type<LHS>::type    ScalarType;

  scalar_expression(LHS & lhs, RHS & rhs) : lhs_(lhs), rhs_(rhs) {}

  /** @brief Returns the left hand side operand */
  LHS & lhs() const { return lhs_; }
  /** @brief Returns the left hand side operand */
  RHS & rhs() const { return rhs_; }

  /** @brief Conversion operator to a ViennaCL scalar */
  operator ScalarType () const
  {
    ScalarType result;
    viennacl::linalg::inner_prod_cpu(lhs_, rhs_, result);
    return result;
  }

private:
  LHS & lhs_;
  RHS & rhs_;
};


/** @brief Specialization of a scalar expression for norm_1. Allows for a final reduction on the CPU
  *
  * @tparam LHS   The left hand side operand
  * @tparam RHS   The right hand side operand
  */
template<typename LHS, typename RHS>
class scalar_expression<LHS, RHS, op_norm_1>
{
  //typedef typename LHS::value_type          DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
public:
  typedef typename viennacl::result_of::cpu_value_type<LHS>::type    ScalarType;

  scalar_expression(LHS & lhs, RHS & rhs) : lhs_(lhs), rhs_(rhs) {}

  /** @brief Returns the left hand side operand */
  LHS & lhs() const { return lhs_; }
  /** @brief Returns the left hand side operand */
  RHS & rhs() const { return rhs_; }

  /** @brief Conversion operator to a ViennaCL scalar */
  operator ScalarType () const
  {
    ScalarType result;
    viennacl::linalg::norm_1_cpu(lhs_, result);
    return result;
  }

private:
  LHS & lhs_;
  RHS & rhs_;
};

/** @brief Specialization of a scalar expression for norm_2. Allows for a final reduction on the CPU
  *
  * @tparam LHS   The left hand side operand
  * @tparam RHS   The right hand side operand
  */
template<typename LHS, typename RHS>
class scalar_expression<LHS, RHS, op_norm_2>
{
  //typedef typename LHS::value_type          DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
public:
  typedef typename viennacl::result_of::cpu_value_type<LHS>::type    ScalarType;

  scalar_expression(LHS & lhs, RHS & rhs) : lhs_(lhs), rhs_(rhs) {}

  /** @brief Returns the left hand side operand */
  LHS & lhs() const { return lhs_; }
  /** @brief Returns the left hand side operand */
  RHS & rhs() const { return rhs_; }

  /** @brief Conversion operator to a ViennaCL scalar */
  operator ScalarType () const
  {
    ScalarType result;
    viennacl::linalg::norm_2_cpu(lhs_, result);
    return result;
  }

private:
  LHS & lhs_;
  RHS & rhs_;
};


/** @brief Specialization of a scalar expression for norm_inf. Allows for a final reduction on the CPU
  *
  * @tparam LHS   The left hand side operand
  * @tparam RHS   The right hand side operand
  */
template<typename LHS, typename RHS>
class scalar_expression<LHS, RHS, op_norm_inf>
{
  //typedef typename LHS::value_type          DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
public:
  typedef typename viennacl::result_of::cpu_value_type<LHS>::type    ScalarType;

  scalar_expression(LHS & lhs, RHS & rhs) : lhs_(lhs), rhs_(rhs) {}

  /** @brief Returns the left hand side operand */
  LHS & lhs() const { return lhs_; }
  /** @brief Returns the left hand side operand */
  RHS & rhs() const { return rhs_; }

  /** @brief Conversion operator to a ViennaCL scalar */
  operator ScalarType () const
  {
    ScalarType result;
    viennacl::linalg::norm_inf_cpu(lhs_, result);
    return result;
  }

private:
  LHS & lhs_;
  RHS & rhs_;
};

/** @brief Specialization of a scalar expression for max(). Allows for a final reduction on the CPU
  *
  * @tparam LHS   The left hand side operand
  * @tparam RHS   The right hand side operand
  */
template<typename LHS, typename RHS>
class scalar_expression<LHS, RHS, op_max>
{
  //typedef typename LHS::value_type          DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
public:
  typedef typename viennacl::result_of::cpu_value_type<LHS>::type    ScalarType;

  scalar_expression(LHS & lhs, RHS & rhs) : lhs_(lhs), rhs_(rhs) {}

  /** @brief Returns the left hand side operand */
  LHS & lhs() const { return lhs_; }
  /** @brief Returns the left hand side operand */
  RHS & rhs() const { return rhs_; }

  /** @brief Conversion operator to a ViennaCL scalar */
  operator ScalarType () const
  {
    ScalarType result;
    viennacl::linalg::max_cpu(lhs_, result);
    return result;
  }

private:
  LHS & lhs_;
  RHS & rhs_;
};


/** @brief Specialization of a scalar expression for norm_inf. Allows for a final reduction on the CPU
  *
  * @tparam LHS   The left hand side operand
  * @tparam RHS   The right hand side operand
  */
template<typename LHS, typename RHS>
class scalar_expression<LHS, RHS, op_min>
{
  //typedef typename LHS::value_type          DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
public:
  typedef typename viennacl::result_of::cpu_value_type<LHS>::type    ScalarType;

  scalar_expression(LHS & lhs, RHS & rhs) : lhs_(lhs), rhs_(rhs) {}

  /** @brief Returns the left hand side operand */
  LHS & lhs() const { return lhs_; }
  /** @brief Returns the left hand side operand */
  RHS & rhs() const { return rhs_; }

  /** @brief Conversion operator to a ViennaCL scalar */
  operator ScalarType () const
  {
    ScalarType result;
    viennacl::linalg::min_cpu(lhs_, result);
    return result;
  }

private:
  LHS & lhs_;
  RHS & rhs_;
};

/** @brief Specialization of a scalar expression for norm_inf. Allows for a final reduction on the CPU
  *
  * @tparam LHS   The left hand side operand
  * @tparam RHS   The right hand side operand
  */
template<typename LHS, typename RHS>
class scalar_expression<LHS, RHS, op_sum>
{
  //typedef typename LHS::value_type          DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
public:
  typedef typename viennacl::result_of::cpu_value_type<LHS>::type    ScalarType;

  scalar_expression(LHS & lhs, RHS & rhs) : lhs_(lhs), rhs_(rhs) {}

  /** @brief Returns the left hand side operand */
  LHS & lhs() const { return lhs_; }
  /** @brief Returns the left hand side operand */
  RHS & rhs() const { return rhs_; }

  /** @brief Conversion operator to a ViennaCL scalar */
  operator ScalarType () const
  {
    ScalarType result;
    viennacl::linalg::sum_cpu(lhs_, result);
    return result;
  }

private:
  LHS & lhs_;
  RHS & rhs_;
};


/** @brief Specialization of a scalar expression for norm_frobenius. Allows for a final reduction on the CPU
  *
  * @tparam LHS   The left hand side operand
  * @tparam RHS   The right hand side operand
  */
template<typename LHS, typename RHS>
class scalar_expression<LHS, RHS, op_norm_frobenius>
{
  //typedef typename LHS::value_type          DummyType; //Visual C++ 2005 does not allow to write LHS::value_type::value_type
public:
  typedef typename viennacl::result_of::cpu_value_type<LHS>::type    ScalarType;

  scalar_expression(LHS & lhs, RHS & rhs) : lhs_(lhs), rhs_(rhs) {}

  /** @brief Returns the left hand side operand */
  LHS & lhs() const { return lhs_; }
  /** @brief Returns the left hand side operand */
  RHS & rhs() const { return rhs_; }

  /** @brief Conversion operator to a ViennaCL scalar */
  operator ScalarType () const
  {
    ScalarType result;
    viennacl::linalg::norm_frobenius_cpu(lhs_, result);
    return result;
  }

private:
  LHS & lhs_;
  RHS & rhs_;
};




/** @brief This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type like float or double.
  *
  * Since every read and write operation requires a CPU->GPU or GPU->CPU transfer, this type should be used with care.
  * The advantage of this type is that the GPU command queue can be filled without blocking read operations.
  *
  * @tparam NumericT  Either float or double. Checked at compile time.
  */
template<class NumericT>
class scalar
{
  typedef scalar<NumericT>         self_type;
public:
  typedef viennacl::backend::mem_handle                     handle_type;
  typedef vcl_size_t                                        size_type;

  /** @brief Returns the underlying host scalar type. */
  typedef NumericT   value_type;

  /** @brief Creates the scalar object, but does not yet allocate memory. Thus, scalar<> can also be a global variable (if really necessary). */
  scalar() {}

  /** @brief Allocates the memory for the scalar and sets it to the supplied value. */
  scalar(NumericT val, viennacl::context ctx = viennacl::context())
  {
    viennacl::backend::memory_create(val_, sizeof(NumericT), ctx, &val);
  }

#ifdef VIENNACL_WITH_OPENCL
  /** @brief Wraps an existing memory entry into a scalar
    *
    * @param mem    The OpenCL memory handle
    * @param size   Ignored - Only necessary to avoid ambiguities. Users are advised to set this parameter to '1'.
    */
  explicit scalar(cl_mem mem, size_type /*size*/)
  {
    val_.switch_active_handle_id(viennacl::OPENCL_MEMORY);
    val_.opencl_handle() = mem;
    val_.opencl_handle().inc();  //prevents that the user-provided memory is deleted once the vector object is destroyed.
  }
#endif

  /** @brief Allocates memory for the scalar and sets it to the result of supplied expression. */
  template<typename T1, typename T2, typename OP>
  scalar(scalar_expression<T1, T2, OP> const & proxy)
  {
    val_.switch_active_handle_id(viennacl::traits::handle(proxy.lhs()).get_active_handle_id());
    viennacl::backend::memory_create(val_, sizeof(NumericT), viennacl::traits::context(proxy));
    *this = proxy;
  }

  //copy constructor
  /** @brief Copy constructor. Allocates new memory for the scalar and copies the value of the supplied scalar */
  scalar(const scalar & other)
  {
    if (other.handle().get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED)
    {
      //copy value:
      val_.switch_active_handle_id(other.handle().get_active_handle_id());
      viennacl::backend::memory_create(val_, sizeof(NumericT), viennacl::traits::context(other));
      viennacl::backend::memory_copy(other.handle(), val_, 0, 0, sizeof(NumericT));
    }
  }

  /** @brief Reads the value of the scalar from the GPU and returns the float or double value. */
  operator NumericT() const
  {
    // make sure the scalar contains reasonable data:
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized, cannot read!"));

    NumericT tmp;
    viennacl::backend::memory_read(val_, 0, sizeof(NumericT), &tmp);
    return tmp;
  }

  /** @brief Assigns a vector entry. */
  self_type & operator= (entry_proxy<NumericT> const & other)
  {
    init_if_necessary(viennacl::traits::context(other));
    viennacl::backend::memory_copy(other.handle(), val_, other.index() * sizeof(NumericT), 0, sizeof(NumericT));
    return *this;
  }

  /** @brief Assigns the value from another scalar. */
  self_type & operator= (scalar<NumericT> const & other)
  {
    init_if_necessary(viennacl::traits::context(other));
    viennacl::backend::memory_copy(other.handle(), val_, 0, 0, sizeof(NumericT));
    return *this;
  }

  self_type & operator= (float cpu_other)
  {
    init_if_necessary(viennacl::context());

    //copy value:
    NumericT value = static_cast<NumericT>(cpu_other);
    viennacl::backend::memory_write(val_, 0, sizeof(NumericT), &value);
    return *this;
  }

  self_type & operator= (double cpu_other)
  {
    init_if_necessary(viennacl::context());

    NumericT value = static_cast<NumericT>(cpu_other);
    viennacl::backend::memory_write(val_, 0, sizeof(NumericT), &value);
    return *this;
  }

  self_type & operator= (long cpu_other)
  {
    init_if_necessary(viennacl::context());

    NumericT value = static_cast<NumericT>(cpu_other);
    viennacl::backend::memory_write(val_, 0, sizeof(NumericT), &value);
    return *this;
  }

  self_type & operator= (unsigned long cpu_other)
  {
    init_if_necessary(viennacl::context());

    NumericT value = static_cast<NumericT>(cpu_other);
    viennacl::backend::memory_write(val_, 0, sizeof(NumericT), &value);
    return *this;
  }

  self_type & operator= (int cpu_other)
  {
    init_if_necessary(viennacl::context());

    NumericT value = static_cast<NumericT>(cpu_other);
    viennacl::backend::memory_write(val_, 0, sizeof(NumericT), &value);
    return *this;
  }

  self_type & operator= (unsigned int cpu_other)
  {
    init_if_necessary(viennacl::context());

    NumericT value = static_cast<NumericT>(cpu_other);
    viennacl::backend::memory_write(val_, 0, sizeof(NumericT), &value);
    return *this;
  }

  /** @brief Sets the scalar to the result of supplied inner product expression. */
  template<typename T1, typename T2>
  self_type & operator= (scalar_expression<T1, T2, op_inner_prod> const & proxy)
  {
    init_if_necessary(viennacl::traits::context(proxy));

    viennacl::linalg::inner_prod_impl(proxy.lhs(), proxy.rhs(), *this);
    return *this;
  }

  /** @brief Sets the scalar to the result of supplied norm_1 expression. */
  template<typename T1, typename T2>
  self_type & operator= (scalar_expression<T1, T2, op_norm_1> const & proxy)
  {
    init_if_necessary(viennacl::traits::context(proxy));

    viennacl::linalg::norm_1_impl(proxy.lhs(), *this);
    return *this;
  }

  /** @brief Sets the scalar to the result of supplied norm_2 expression. */
  template<typename T1, typename T2>
  self_type & operator= (scalar_expression<T1, T2, op_norm_2> const & proxy)
  {
    init_if_necessary(viennacl::traits::context(proxy));

    viennacl::linalg::norm_2_impl(proxy.lhs(), *this);
    return *this;
  }

  /** @brief Sets the scalar to the result of supplied norm_inf expression. */
  template<typename T1, typename T2>
  self_type & operator= (scalar_expression<T1, T2, op_norm_inf> const & proxy)
  {
    init_if_necessary(viennacl::traits::context(proxy));

    viennacl::linalg::norm_inf_impl(proxy.lhs(), *this);
    return *this;
  }

  /** @brief Sets the scalar to the result of supplied max expression. */
  template<typename T1, typename T2>
  self_type & operator= (scalar_expression<T1, T2, op_max> const & proxy)
  {
    init_if_necessary(viennacl::traits::context(proxy));

    viennacl::linalg::max_impl(proxy.lhs(), *this);
    return *this;
  }

  /** @brief Sets the scalar to the result of supplied min expression. */
  template<typename T1, typename T2>
  self_type & operator= (scalar_expression<T1, T2, op_min> const & proxy)
  {
    init_if_necessary(viennacl::traits::context(proxy));

    viennacl::linalg::min_impl(proxy.lhs(), *this);
    return *this;
  }

  /** @brief Sets the scalar to the result of supplied sum expression. */
  template<typename T1, typename T2>
  self_type & operator= (scalar_expression<T1, T2, op_sum> const & proxy)
  {
    init_if_necessary(viennacl::traits::context(proxy));

    viennacl::linalg::sum_impl(proxy.lhs(), *this);
    return *this;
  }


  /** @brief Sets the scalar to the result of supplied norm_frobenius expression. */
  template<typename T1, typename T2>
  self_type & operator= (scalar_expression<T1, T2, op_norm_frobenius> const & proxy)
  {
    init_if_necessary(viennacl::traits::context(proxy));

    viennacl::linalg::norm_frobenius_impl(proxy.lhs(), *this);
    return *this;
  }

  /** @brief Sets the scalar to the inverse with respect to addition of the supplied sub-expression */
  template<typename T1, typename T2>
  self_type & operator= (scalar_expression<T1, T2, op_flip_sign> const & proxy)
  {
    init_if_necessary(viennacl::traits::context(proxy));

    viennacl::linalg::as(*this, proxy.lhs(), NumericT(-1.0), 1, false, true);
    return *this;
  }


  /** @brief Inplace addition of a ViennaCL scalar */
  self_type & operator += (scalar<NumericT> const & other)
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    viennacl::linalg::asbs(*this,                                       // s1 =
                           *this, NumericT(1.0), 1, false, false,     //       s1 * 1.0
                           other, NumericT(1.0), 1, false, false);    //     + s2 * 1.0
    return *this;
  }
  /** @brief Inplace addition of a host scalar (float or double) */
  self_type & operator += (NumericT other)
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    viennacl::linalg::asbs(*this,                                       // s1 =
                           *this, NumericT(1.0), 1, false, false,     //       s1 * 1.0
                           other, NumericT(1.0), 1, false, false);    //     + s2 * 1.0
    return *this;
  }


  /** @brief Inplace subtraction of a ViennaCL scalar */
  self_type & operator -= (scalar<NumericT> const & other)
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    viennacl::linalg::asbs(*this,                                       // s1 =
                           *this, NumericT(1.0), 1, false, false,     //       s1 * 1.0
                           other, NumericT(-1.0), 1, false, false);   //     + s2 * (-1.0)
    return *this;
  }
  /** @brief Inplace subtraction of a host scalar (float or double) */
  self_type & operator -= (NumericT other)
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    viennacl::linalg::asbs(*this,                                       // s1 =
                           *this, NumericT(1.0), 1, false, false,     //       s1 * 1.0
                           other, NumericT(-1.0), 1, false, false);   //     + s2 * (-1.0)
    return *this;
  }


  /** @brief Inplace multiplication with a ViennaCL scalar */
  self_type & operator *= (scalar<NumericT> const & other)
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    viennacl::linalg::as(*this,                                       // s1 =
                         *this, other, 1, false, false);              //      s1 * s2
    return *this;
  }
  /** @brief Inplace  multiplication with a host scalar (float or double) */
  self_type & operator *= (NumericT other)
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    viennacl::linalg::as(*this,                                       // s1 =
                         *this, other, 1, false, false);              //      s1 * s2
    return *this;
  }


  //////////////// operator /=    ////////////////////////////
  /** @brief Inplace division with a ViennaCL scalar */
  self_type & operator /= (scalar<NumericT> const & other)
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    viennacl::linalg::as(*this,                                       // s1 =
                         *this, other, 1, true, false);              //      s1 / s2
    return *this;
  }
  /** @brief Inplace division with a host scalar (float or double) */
  self_type & operator /= (NumericT other)
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    viennacl::linalg::as(*this,                                       // s1 =
                         *this, other, 1, true, false);              //      s1 / s2
    return *this;
  }


  //////////////// operator + ////////////////////////////
  /** @brief Addition of two ViennaCL scalars */
  self_type operator + (scalar<NumericT> const & other)
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    self_type result = 0;

    viennacl::linalg::asbs(result,                                       // result =
                           *this, NumericT(1.0), 1, false, false,      //            *this * 1.0
                           other, NumericT(1.0), 1, false, false);     //          + other * 1.0

    return result;
  }
  /** @brief Addition of a ViennaCL scalar with a scalar expression */
  template<typename T1, typename T2, typename OP>
  self_type operator + (scalar_expression<T1, T2, OP> const & proxy) const
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    self_type result = proxy;

    viennacl::linalg::asbs(result,                                       // result =
                           *this, NumericT(1.0), 1, false, false,      //            *this * 1.0
                           result, NumericT(1.0), 1, false, false);     //        + result * 1.0

    return result;
  }
  /** @brief Addition of a ViennaCL scalar with a host scalar (float, double) */
  self_type operator + (NumericT other)
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    self_type result = 0;

    viennacl::linalg::asbs(result,                                       // result =
                           *this, NumericT(1.0), 1, false, false,      //            *this * 1.0
                           other, NumericT(1.0), 1, false, false);     //          + other * 1.0

    return result;
  }


  //////////////// operator - ////////////////////////////

  /** @brief Sign flip of the scalar. Does not evaluate immediately, but instead returns an expression template object */
  scalar_expression<const self_type, const self_type, op_flip_sign> operator-() const
  {
    return scalar_expression<const self_type, const self_type, op_flip_sign>(*this, *this);
  }


  /** @brief Subtraction of two ViennaCL scalars */
  self_type operator - (scalar<NumericT> const & other) const
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    self_type result = 0;

    viennacl::linalg::asbs(result,                                       // result =
                           *this, NumericT(1.0), 1, false, false,      //            *this * 1.0
                           other, NumericT(-1.0), 1, false, false);    //          + other * (-1.0)

    return result;
  }
  /** @brief Subtraction of a ViennaCL scalar from a scalar expression */
  template<typename T1, typename T2, typename OP>
  self_type operator - (scalar_expression<T1, T2, OP> const & proxy) const
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    self_type result = proxy;

    viennacl::linalg::asbs(result,                                       // result =
                           *this, NumericT(1.0), 1 , false, false,    //            *this * 1.0
                           result, NumericT(-1.0), 1, false, false);  //          + result * (-1.0)

    return result;
  }
  /** @brief Subtraction of a host scalar (float, double) from a ViennaCL scalar */
  scalar<NumericT> operator - (NumericT other) const
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    self_type result = 0;

    viennacl::linalg::asbs(result,                                       // result =
                           *this, NumericT(1.0), 1, false, false,      //            *this * 1.0
                           other, NumericT(-1.0), 1, false, false);    //          + other * (-1.0)

    return result;
  }

  //////////////// operator * ////////////////////////////
  /** @brief Multiplication of two ViennaCL scalars */
  self_type operator * (scalar<NumericT> const & other) const
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    scalar<NumericT> result = 0;

    viennacl::linalg::as(result,                                     // result =
                         *this, other, 1, false, false);              //          *this * other

    return result;
  }
  /** @brief Multiplication of a ViennaCL scalar with a scalar expression */
  template<typename T1, typename T2, typename OP>
  self_type operator * (scalar_expression<T1, T2, OP> const & proxy) const
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    self_type result = proxy;

    viennacl::linalg::as(result,                                       // result =
                         *this, result, 1, false, false);              //            *this * proxy

    return result;
  }
  /** @brief Multiplication of a host scalar (float, double) with a ViennaCL scalar */
  self_type operator * (NumericT other) const
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    scalar<NumericT> result = 0;

    viennacl::linalg::as(result,                                     // result =
                         *this, other, 1, false, false);              //          *this * other

    return result;
  }

  //////////////// operator /    ////////////////////////////
  /** @brief Division of two ViennaCL scalars */
  self_type operator / (scalar<NumericT> const & other) const
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    self_type result = 0;

    viennacl::linalg::as(result,                                     // result =
                         *this, other, 1, true, false);              //           *this / other

    return result;
  }
  /** @brief Division of a ViennaCL scalar by a scalar expression */
  template<typename T1, typename T2, typename OP>
  self_type operator / (scalar_expression<T1, T2, OP> const & proxy) const
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    self_type result = proxy;

    viennacl::linalg::as(result,                                     // result =
                         *this, result, 1, true, false);              //          *this / proxy

    return result;
  }
  /** @brief Division of a ViennaCL scalar by a host scalar (float, double)*/
  self_type operator / (NumericT other) const
  {
    assert( val_.get_active_handle_id() != viennacl::MEMORY_NOT_INITIALIZED && bool("Scalar not initialized!"));

    self_type result = 0;

    viennacl::linalg::as(result,                                     // result =
                         *this, other, 1, true, false);              //            *this / other

    return result;
  }

  /** @brief Returns the memory handle, non-const version */
  handle_type & handle() { return val_; }

  /** @brief Returns the memory handle, const version */
  const handle_type & handle() const { return val_; }

private:

  void init_if_necessary(viennacl::context ctx)
  {
    if (val_.get_active_handle_id() == viennacl::MEMORY_NOT_INITIALIZED)
    {
      viennacl::backend::memory_create(val_, sizeof(NumericT), ctx);
    }
  }

  handle_type val_;
};


//stream operators:
/** @brief Allows to directly print the value of a scalar to an output stream */
template<class NumericT>
std::ostream & operator<<(std::ostream & s, const scalar<NumericT> & val)
{
  NumericT temp = val;
  s << temp;
  return s;
}

/** @brief Allows to directly read a value of a scalar from an input stream */
template<class NumericT>
std::istream & operator>>(std::istream & s, const scalar<NumericT> & val)
{
  NumericT temp;
  s >> temp;
  val = temp;
  return s;
}

} //namespace viennacl

#endif