1313
1414#include " timer.h"
1515#include " hemi/hemi.h"
16+ #include " hemi/parallel_for.h"
17+ #include " hemi/device_api.h"
1618
1719const float RISKFREE = 0 .02f ;
1820const float VOLATILITY = 0 .30f ;
@@ -42,15 +44,11 @@ float CND(float d)
4244}
4345
4446// Black-Scholes formula for both call and put
45- HEMI_KERNEL (BlackScholes)
46- (float *callResult, float *putResult, const float *stockPrice,
47- const float *optionStrike, const float *optionYears, float Riskfree,
48- float Volatility, int optN)
47+ void BlackScholes (float *callResult, float *putResult, const float *stockPrice,
48+ const float *optionStrike, const float *optionYears, float Riskfree,
49+ float Volatility, int optN)
4950{
50- int offset = hemiGetElementOffset ();
51- int stride = hemiGetElementStride ();
52-
53- for (int opt = offset; opt < optN; opt += stride)
51+ hemi::parallel_for (0 , optN, [=] HEMI_LAMBDA (int opt)
5452 {
5553 float S = stockPrice[opt];
5654 float X = optionStrike[opt];
@@ -68,7 +66,7 @@ HEMI_KERNEL(BlackScholes)
6866 float expRT = expf (- R * T);
6967 callResult[opt] = S * CNDD1 - X * expRT * CNDD2;
7068 putResult[opt] = X * expRT * (1 .0f - CNDD2) - S * (1 .0f - CNDD1);
71- }
69+ });
7270}
7371
7472float RandFloat (float low, float high)
@@ -120,9 +118,6 @@ int main(int argc, char **argv)
120118
121119 initOptions (OPT_N, stockPrice, optionStrike, optionYears);
122120
123- int blockDim = 128 ; // blockDim, gridDim ignored by host code
124- int gridDim = std::min<int >(1024 , (OPT_N + blockDim - 1 ) / blockDim);
125-
126121 printf (" Running %s Version...\n " , HEMI_LOC_STRING);
127122
128123 StartTimer ();
@@ -137,11 +132,11 @@ int main(int argc, char **argv)
137132 d_stockPrice = stockPrice;
138133 d_optionStrike = optionStrike;
139134 d_optionYears = optionYears;
140- #endif
135+ #endif
136+
141137
142- HEMI_KERNEL_LAUNCH (BlackScholes, gridDim, blockDim, 0 , 0 ,
143- d_callResult, d_putResult, d_stockPrice, d_optionStrike,
144- d_optionYears, RISKFREE, VOLATILITY, OPT_N);
138+ BlackScholes (d_callResult, d_putResult, (const float *)d_stockPrice, (const float *)d_optionStrike,
139+ (const float *)d_optionYears, RISKFREE, VOLATILITY, OPT_N);
145140
146141#ifdef HEMI_CUDA_COMPILER
147142 checkCuda ( cudaMemcpy (callResult, d_callResult, OPT_SZ, cudaMemcpyDeviceToHost) );
0 commit comments