1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| static void BlackScholesBodyCPU( float* call, float* put, float Sf, float Xf, float Tf, float Rf, float Vf, unsigned int i ){ double S = Sf, X = Xf, T = Tf, R = Rf, V = Vf;
double sqrtT = sqrt(T); double d1 = (log(S / X) + (R + 0.5 * V * V) * T) / (V * sqrtT); double d2 = d1 - V * sqrtT; double CNDD1 = CND(d1, i); double CNDD2 = CND(d2, i);
double expRT = exp(- R * T); *call = (float)(S * CNDD1 - X * expRT * CNDD2); *put = (float)(X * expRT * (1.0 - CNDD2) - S * (1.0 - CNDD1)); }
__global__ void BlackScholesKernel(uint64_t optionCount, \ float R, \ float V, \ float *d_Call, \ float *d_Put, \ float *d_S, \ float *d_X, \ float *d_T){ for(unsigned int i = 0; i < optionCount; i++){ BlackScholesBodyCPU( &d_Call[i], &d_Put[i], d_S[i], d_X[i], d_T[i], R, V, i ); } }
__global__ void BlackScholesthreads(uint64_t optionCount, \ float R, \ float V, \ float *d_Call, \ float *d_Put, \ float *d_S, \ float *d_X, \ float *d_T){ int threadId = get_thread_id(); int threadsNum = get_group_size(); uint64_t optionCount_p = optionCount/threadsNum; uint64_t extras = optionCount%threadsNum; uint64_t offset; if(threadId < extras){ optionCount_p++; offset=threadId*optionCount_p; }else{ offset=threadId*(optionCount_p+1)-(threadId-extras); } BlackScholesKernel( optionCount_p, R, V, d_Call+offset, d_Put+offset, d_S+offset, d_X+offset, d_T+offset ); }
|