Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify black scholes example to use internal RNG from library instead of std::rand #418

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions example/black_scholes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <boost/compute/system.hpp>
#include <boost/compute/algorithm/copy_n.hpp>
#include <boost/compute/container/vector.hpp>
#include <boost/compute/random/default_random_engine.hpp>
#include <boost/compute/random/uniform_real_distribution.hpp>
#include <boost/compute/utility/source.hpp>

namespace compute = boost::compute;
Expand Down Expand Up @@ -43,29 +45,22 @@ int main()
compute::command_queue queue(context, gpu);
std::cout << "device: " << gpu.name() << std::endl;

// initialize option data on host
std::vector<float> stock_price_data(N);
std::vector<float> option_strike_data(N);
std::vector<float> 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<float> call_result(N, context);
compute::vector<float> put_result(N, context);
compute::vector<float> stock_price(N, context);
compute::vector<float> option_strike(N, context);
compute::vector<float> 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<float> dist_stock_price(5.0f, 30.0f);
compute::uniform_real_distribution<float> dist_option_strike(1.0f, 100.0f);
compute::uniform_real_distribution<float> 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(
Expand Down