# Parallel Programming in Native Code

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

# Antenna Radiation Simulation using C++ AMP

### Antenna Radiation Simulation using C++ AMP

In this blog post, we present a sample about antenna radiation pattern simulation. Aside from minor tweaks that we made, this was written for us by EM Photonics who offer C++ AMP consultancy. First I’ll describe the app, then the algorithm, and then the three classes used in the code. Last, I will provide instructions on how to get the sample running on your machine.

This app calculates antenna radiation patterns for simple wire antennas of varying lengths. Below is a sample screenshot of the app when running. Press Ctrl + F5 to start the demo. When it is running, you can press any key on the keyboard to switch computation method between C++ AMP and CPU. On the
title bar you can see the Frames Per Second for the different modes.

The length of the antenna is increased and decreased, leading to varying patterns. Each line in the output display represents a different length antenna; tracing across a line represents the radiation pattern of the antenna as swept over a 180 degree observation arc at a distance from the antenna. Brighter colors represent stronger radiation. A lightweight version of the Method of Moments (MoM) is employed for the calculation. In this method, the length of the antenna is subdivided into small pieces. The antenna is excited by a signal and the goal is to obtain the distribution of currents on the antenna, from which the radiation pattern can be calculated. Each segment of the antenna affects each other segment, and so there are N x N interactions. Mathematically, a matrix can be constructed where each line represents all of the interactions for a single element. This matrix is thus N x N in size, and must be solved.

The solution of the matrix is a large portion of the computation, and is performed with the LAPACK method GETRF for LU Factorization. This is then followed by a series of other calls from the BLAS and LAPACK libraries (TRSM and LASWP) to obtain the final current distribution.

The code presented contains code paths for both - CPU and C++ AMP.

#### antenna_sim class

This class represents an antenna simulation interface. Let’s focus on some of its members.

• antenna_sim_amp<COMPUTE_TYPE> amp_solver - C++ AMP computation agent. All computation is done on GPU using C++ AMP.
• antenna_sim_cpu<COMPUTE_TYPE> cpu_solver – CPU computation agent which uses PPL. All computation is done on CPU using PPL.
• compute_element<COMPUTE_TYPE>* current_solver – pointer to the current agent. It is updated when the computation agent is changed.
• std::unique_ptr<graphics::texture<float,2>>& tex – texture for rendering. To accelerate the rendering, we use two texture buffers created by Direct3D interop.
• std::unique_ptr<graphics::texture<float,2>>& tex2 – the second texture for rendering.
• array<COMPUTE_TYPE> screen_line – in each computation call, the demo updates one line on the screen. When the app is running, the screen scrolls down gradually.
• std::mutex& thread_m - there are two threads running in the demo. The worker thread focuses on computation. The main thread focuses on rendering. The mutex is used to enforce exclusive data access between threads.
• antenna_sim::operator()() – This function is called by worker thread to calculate the signal strength. It calls either C++ AMP GPU computation or PPL CPU computation.
• antenna_sim::data_to_screen() – Fills in the data to the rendering texture .

#### antenna_sim_amp class

This class implements the computation using C++ AMP. Some of its member are:

• antenna_sim_amp::fill_matrix() – Fill in the input matrix data, which is used for AMP LAPACK library.
• antenna_sim_amp::solve() – Call AMP LAPACK library to solve the matrix equation.
• antenna_sim_amp::prep_angles() – Use the result from AMP LAPACK library to calculate the texel information which is used for rendering.

#### antenna_sim_cpu class

The interface of this class is similar to antenna_sim_amp, but all computation is done with a CPU multi-core implementation. Correspondingly, it calls a CPU LAPACK library to solve the matrix equation.

1. The sample Visual Studio project attached to this blog post.
2. The C++ AMP LAPACK library and BLAS library from Codeplex. Unzip them into Antenna project solution folder. Add ampblas.vcxproj and amplapack.vcxproj to Antenna.sln
3. Add reference to a CPU version of LAPACK of your choice. For example, this one.

To compile and run the project, you have to specify these four build macros, %LAPACK_LIB_FILES_32%, %LAPACK_LIB_PATH_32%, %LAPACK_LIB_FILES_64%, %LAPACK_LIB_PATH_64%. For the details of these four build macros, please refer to the C++ AMP LAPACK library readme.txt.

Your feedback is welcome in the comments below or on our MSDN forum.

The attached sample code is released under the MS-RSL license.

Attachment: Antenna.zip
Blog - Comment List MSDN TechNet