From aa317fa8b518d024964fbeac81714eb9d1fbe677 Mon Sep 17 00:00:00 2001 From: Roshan Date: Sun, 25 Jan 2015 20:03:27 +0530 Subject: [PATCH] Modify black scholes example to use RNG from library instead of std::rand --- example/black_scholes.cpp | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/example/black_scholes.cpp b/example/black_scholes.cpp index b4b046956..3b9a6d502 100644 --- a/example/black_scholes.cpp +++ b/example/black_scholes.cpp @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include namespace compute = boost::compute; @@ -43,18 +45,6 @@ int main() compute::command_queue queue(context, gpu); std::cout << "device: " << gpu.name() << std::endl; - // initialize option data on host - std::vector stock_price_data(N); - std::vector option_strike_data(N); - std::vector option_years_data(N); - - std::srand(5347); - for(int i = 0; i < N; i++){ - stock_price_data[i] = rand_float(5.0f, 30.0f); - option_strike_data[i] = rand_float(1.0f, 100.0f); - option_years_data[i] = rand_float(0.25f, 10.0f); - } - // create memory buffers on the device compute::vector call_result(N, context); compute::vector put_result(N, context); @@ -62,10 +52,15 @@ int main() compute::vector option_strike(N, context); compute::vector option_years(N, context); - // copy initial values to the device - compute::copy_n(stock_price_data.begin(), N, stock_price.begin(), queue); - compute::copy_n(option_strike_data.begin(), N, option_strike.begin(), queue); - compute::copy_n(option_years_data.begin(), N, option_years.begin(), queue); + // generate option data + compute::default_random_engine engine(queue, 5347); + compute::uniform_real_distribution dist_stock_price(5.0f, 30.0f); + compute::uniform_real_distribution dist_option_strike(1.0f, 100.0f); + compute::uniform_real_distribution dist_option_years(0.25f, 10.0f); + + dist_stock_price.generate(stock_price.begin(), stock_price.end(), engine, queue); + dist_option_strike.generate(option_strike.begin(), option_strike.end(), engine, queue); + dist_option_years.generate(option_years.begin(), option_years.end(), engine, queue); // source code for black-scholes program const char source[] = BOOST_COMPUTE_STRINGIZE_SOURCE(