# Parallel Programming in Native Code

Parallel programming using C++ AMP, PPL and Agents libraries.

# Eigen Values

### Eigen Values

The computation of Eigen values is an important problem to solve in linear algebra, statistics, physics, bio metrics and many other fields. In this blog post, I would like to share the Eigen Values sample implemented using C++ AMP. This is the C++ AMP Version of NVIDIA CUDA’s Eigen Values Sample

#### Introduction

Given an n × n square matrix A of real or complex numbers, an eigenvalue λ is a scalar value that satisfies the below relation :

Av = λv

where v is a nonzero n × 1 column vector and it is called an eigenvector and that pair of value and vector is called an Eigen pair.

Several algorithms exist to find the Eigen values. In this sample, we tried to find the eigen values using ‘Lanczos algorithm’. For simplification, we have taken an input that is Trigonal matrix ‘T’ and its eigen values and eigen vectors are found in using QR Algorithm as mentioned in the section ‘Solve for eigenvalues and eigenvectors ‘ of Lanczos algorithm. Please refer to this white paper, which provides more details of the algorithm used in this sample.

#### main – Program entry point

Let’s start with main(), where an instance of the EigenValues class is created to compute Eigen values, and it is then verified against results of a CPU implementation. The “EigenValues” class implements both the C++ AMP kernel and the CPU implementation. The constructor generates the elements of a Trigonal matrix randomly.

#### EigenValues::execute

This is the function where the C++ AMP kernel is used for computation. To start with, the input data is stored in a concurrency::array_view. The computation parameters are captured by value inside the kernel. This kernel schedules tiles of size EIGVALUES_TILE_SIZE and there is one eigen value computation per thread. The calculated options are stored in a separate output array_view created using ‘m_eigenValuesGPU’ vector. This implementation takes advantage of many cores on GPU to run threads in parallel. After the computation completes, the results are automatically synced to the ‘m_eigenValuesGPU’ vector, which is used later for verification.

#### EigenValues::verify

This function validates the results computed on GPU. The same input data is used to calculate results on the CPU (which is implemented in function “EigenValues<T>::ComputeEigenvaluesCPU”), then the results of CPU and GPU are compared using “EigenValues<T>::VerifyResults”.