Black Scholes using C++ AMP

In the financial industry, Black Scholes is one of the methods used to valuate options. This one is faster than Binomial Options. In this post, I will walk you through a C++ AMP implementation of Black Scholes.

main – Program entry point

Let’s start with main() , where an instance of the blackscholes class is created, an option price is computed, and it is then verified against results of a CPU implementation. The “blackscholes” class implements both the C++ AMP kernel and the CPU implementation. The constructor generates random data and initializes parameters.

blackscholes::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. The computation parameters are captured in separate array and passed to the kernel. This kernel schedules tiles of size BSCHOLES_TILE_SIZE and there is one thread per option price computation. The calculated options are stored in a separate output arrays. This implementation takes advantage of many cores on GPU to run threads in parallel. After the computation completes, the results are copied out to host memory for verification.

blackscholes::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 “blackscholes::blackscholes_CPU”), then the results of CPU and GPU are compared using “blackscholes::sequence_equal”.

blackscholes::cnd_calc

This is an interesting function to mention because of its restrict(amp, cpu) modifier. This function can be called from a CPU function as well as from a GPU kernel. In this sample this function is called from “blackscholes::blackscholes_CPU“ as well as kernel in “blackscholes::execute”.

Download the sample

Please download the attached sample of the Black Scholes that we discussed here and run it on your hardware, and try to understand what the code does and to learn from it. You will need, as always, Visual Studio 11.


BlackScholes.zip