this example demostrates a black-scholes option pricing kernel.
| 29 | |
| 30 | // this example demostrates a black-scholes option pricing kernel. |
| 31 | int main() |
| 32 | { |
| 33 | // number of options |
| 34 | const int N = 4000000; |
| 35 | |
| 36 | // black-scholes parameters |
| 37 | const float risk_free_rate = 0.02f; |
| 38 | const float volatility = 0.30f; |
| 39 | |
| 40 | // get default device and setup context |
| 41 | compute::device gpu = compute::system::default_device(); |
| 42 | compute::context context(gpu); |
| 43 | compute::command_queue queue(context, gpu); |
| 44 | std::cout << "device: " << gpu.name() << std::endl; |
| 45 | |
| 46 | // initialize option data on host |
| 47 | std::vector<float> stock_price_data(N); |
| 48 | std::vector<float> option_strike_data(N); |
| 49 | std::vector<float> option_years_data(N); |
| 50 | |
| 51 | std::srand(5347); |
| 52 | for(int i = 0; i < N; i++){ |
| 53 | stock_price_data[i] = rand_float(5.0f, 30.0f); |
| 54 | option_strike_data[i] = rand_float(1.0f, 100.0f); |
| 55 | option_years_data[i] = rand_float(0.25f, 10.0f); |
| 56 | } |
| 57 | |
| 58 | // create memory buffers on the device |
| 59 | compute::vector<float> call_result(N, context); |
| 60 | compute::vector<float> put_result(N, context); |
| 61 | compute::vector<float> stock_price(N, context); |
| 62 | compute::vector<float> option_strike(N, context); |
| 63 | compute::vector<float> option_years(N, context); |
| 64 | |
| 65 | // copy initial values to the device |
| 66 | compute::copy_n(stock_price_data.begin(), N, stock_price.begin(), queue); |
| 67 | compute::copy_n(option_strike_data.begin(), N, option_strike.begin(), queue); |
| 68 | compute::copy_n(option_years_data.begin(), N, option_years.begin(), queue); |
| 69 | |
| 70 | // source code for black-scholes program |
| 71 | const char source[] = BOOST_COMPUTE_STRINGIZE_SOURCE( |
| 72 | // approximation of the cumulative normal distribution function |
| 73 | static float cnd(float d) |
| 74 | { |
| 75 | const float A1 = 0.319381530f; |
| 76 | const float A2 = -0.356563782f; |
| 77 | const float A3 = 1.781477937f; |
| 78 | const float A4 = -1.821255978f; |
| 79 | const float A5 = 1.330274429f; |
| 80 | const float RSQRT2PI = 0.39894228040143267793994605993438f; |
| 81 | |
| 82 | float K = 1.0f / (1.0f + 0.2316419f * fabs(d)); |
| 83 | float cnd = |
| 84 | RSQRT2PI * exp(-0.5f * d * d) * |
| 85 | (K * (A1 + K * (A2 + K * (A3 + K * (A4 + K * A5))))); |
| 86 | |
| 87 | if(d > 0){ |
| 88 | cnd = 1.0f - cnd; |
nothing calls this directly
no test coverage detected