Skip to content

Commit 0032d4b

Browse files
authored
Update black_scholes_sycl.cpp
1 parent ba0dc69 commit 0032d4b

1 file changed

Lines changed: 23 additions & 10 deletions

File tree

Libraries/oneMKL/black_scholes/src/black_scholes_sycl.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ static inline T CNDF_C(T input)
6262
}
6363
#endif // USE_CNDF_C
6464

65-
void BlackScholes::body() {
65+
template<typename DATA_TYPE>
66+
void BlackScholes<DATA_TYPE>::body() {
6667
// this can not be captured to the kernel. So, we need to copy internals of the class to local variables
6768
DATA_TYPE* h_stock_price_local = this->h_stock_price;
6869
DATA_TYPE* h_option_years_local = this->h_option_years;
@@ -100,7 +101,8 @@ void BlackScholes::body() {
100101
});
101102
}
102103

103-
BlackScholes::BlackScholes()
104+
template<typename DATA_TYPE>
105+
BlackScholes<DATA_TYPE>::BlackScholes()
104106
{
105107
black_scholes_queue = new sycl::queue;
106108

@@ -110,9 +112,6 @@ BlackScholes::BlackScholes()
110112
h_option_strike = sycl::malloc_shared<DATA_TYPE>(opt_n, *black_scholes_queue);
111113
h_option_years = sycl::malloc_shared<DATA_TYPE>(opt_n, *black_scholes_queue);
112114

113-
black_scholes_queue->fill(h_call_result, 0.0, opt_n);
114-
black_scholes_queue->fill(h_put_result, 0.0, opt_n);
115-
116115
constexpr int rand_seed = 777;
117116
namespace mkl_rng = oneapi::mkl::rng;
118117
// create random number generator object
@@ -130,7 +129,8 @@ BlackScholes::BlackScholes()
130129
sycl::event::wait({event_1, event_2, event_3});
131130
}
132131

133-
BlackScholes::~BlackScholes()
132+
template<typename DATA_TYPE>
133+
BlackScholes<DATA_TYPE>::~BlackScholes()
134134
{
135135
sycl::free(h_call_result, *black_scholes_queue);
136136
sycl::free(h_put_result, *black_scholes_queue);
@@ -140,7 +140,8 @@ BlackScholes::~BlackScholes()
140140
delete black_scholes_queue;
141141
}
142142

143-
void BlackScholes::run()
143+
template<typename DATA_TYPE>
144+
void BlackScholes<DATA_TYPE>::run()
144145
{
145146
std::printf("%s Precision Black&Scholes Option Pricing version %d.%d running on %s using DPC++, workgroup size %d, sub-group size %d.\n",
146147
sizeof(DATA_TYPE) > 4 ? "Double" : "Single", MAJOR, MINOR, black_scholes_queue->get_device().get_info<sycl::info::device::name>().c_str(), wg_size, sg_size);
@@ -171,9 +172,21 @@ void BlackScholes::run()
171172

172173
int main(int const argc, char const* argv[])
173174
{
174-
BlackScholes test{};
175-
test.run();
176-
test.check();
175+
bool is_fp64 = true;
176+
{
177+
sycl::queue test_queue;
178+
is_fp64 = test_queue.get_device().has(sycl::aspect::fp64);
179+
}
180+
if (is_fp64) {
181+
BlackScholes<double> test{};
182+
test.run();
183+
test.check();
184+
} else {
185+
std::cout<<"Warning: could not find a device with double precision support. Single precision is used."<<std::endl;
186+
BlackScholes<float> test{};
187+
test.run();
188+
test.check();
189+
}
177190

178191
return 0;
179192
}

0 commit comments

Comments
 (0)