# Example 1: Scalar

The scalar type **scalar<T>** with template parameter *T* denoting the underlying CPU floating point type (*float* or *double*) represents a single floating point value on the GPU. **scalar<T>** is designed to behave much like a scalar type on the CPU, but library users have to keep in mind that every operation on **scalar<T>** requires to launch the appropriate compute kernel on the GPU and is thus much slower then the CPU equivalent.

Be aware that operations between objects of type **scalar<T>** (e.g.~additions. comparisons) have large overhead. For every operation, a separate compute kernel launch is required.

Handling scalars in ViennaCL

typedef float ScalarType; //typedef double ScalarType; //use this if your GPU supports double precision // Define a few CPU scalars: ScalarType s1 = 3.1415926; ScalarType s2 = 2.71763; ScalarType s3 = 42.0; // ViennaCL scalars are defined in the same way: viennacl::scalar<ScalarType> vcl_s1; viennacl::scalar<ScalarType> vcl_s2 = 1.0; viennacl::scalar<ScalarType> vcl_s3 = 1.0; // CPU scalars can be transparently assigned to GPU scalars and vice versa: vcl_s1 = s1; s2 = vcl_s2; vcl_s3 = s3; // Operations between GPU scalars work just as for CPU scalars (but are much slower!) s1 += s2; vcl_s1 += vcl_s2; s1 = s2 + s3; vcl_s1 = vcl_s2 + vcl_s3; s1 = s2 + s3 * s2 - s3 / s1; vcl_s1 = vcl_s2 + vcl_s3 * vcl_s2 - vcl_s3 / vcl_s1; // Operations can also be mixed: vcl_s1 = s1 * vcl_s2 + s3 - vcl_s3; // Output stream is overloaded as well: std::cout << "CPU scalar s2: " << s2 << std::endl; std::cout << "GPU scalar vcl_s2: " << vcl_s2 << std::endl;