mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-25 22:28:17 +08:00
Apply consistent code formatting across the repo. Add clang-format and pre-commit hooks.
This commit is contained in:
@@ -31,14 +31,13 @@
|
||||
* See supplied whitepaper for more explanations.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
#include <helper_functions.h>
|
||||
#include <helper_cuda.h>
|
||||
#include <helper_functions.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "binomialOptions_common.h"
|
||||
#include "realtype.h"
|
||||
@@ -57,132 +56,134 @@ extern "C" void binomialOptionsCPU(real &callResult, TOptionData optionData);
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Process an array of OptN options on GPU
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
extern "C" void binomialOptionsGPU(real *callValue, TOptionData *optionData,
|
||||
int optN);
|
||||
extern "C" void binomialOptionsGPU(real *callValue, TOptionData *optionData, int optN);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Helper function, returning uniformly distributed
|
||||
// random float in [low, high] range
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
real randData(real low, real high) {
|
||||
real t = (real)rand() / (real)RAND_MAX;
|
||||
return ((real)1.0 - t) * low + t * high;
|
||||
real randData(real low, real high)
|
||||
{
|
||||
real t = (real)rand() / (real)RAND_MAX;
|
||||
return ((real)1.0 - t) * low + t * high;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Main program
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int main(int argc, char **argv) {
|
||||
printf("[%s] - Starting...\n", argv[0]);
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
printf("[%s] - Starting...\n", argv[0]);
|
||||
|
||||
int devID = findCudaDevice(argc, (const char **)argv);
|
||||
int devID = findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
const int OPT_N = MAX_OPTIONS;
|
||||
const int OPT_N = MAX_OPTIONS;
|
||||
|
||||
TOptionData optionData[MAX_OPTIONS];
|
||||
real callValueBS[MAX_OPTIONS], callValueGPU[MAX_OPTIONS],
|
||||
callValueCPU[MAX_OPTIONS];
|
||||
TOptionData optionData[MAX_OPTIONS];
|
||||
real callValueBS[MAX_OPTIONS], callValueGPU[MAX_OPTIONS], callValueCPU[MAX_OPTIONS];
|
||||
|
||||
real sumDelta, sumRef, gpuTime, errorVal;
|
||||
real sumDelta, sumRef, gpuTime, errorVal;
|
||||
|
||||
StopWatchInterface *hTimer = NULL;
|
||||
int i;
|
||||
StopWatchInterface *hTimer = NULL;
|
||||
int i;
|
||||
|
||||
sdkCreateTimer(&hTimer);
|
||||
sdkCreateTimer(&hTimer);
|
||||
|
||||
printf("Generating input data...\n");
|
||||
// Generate options set
|
||||
srand(123);
|
||||
printf("Generating input data...\n");
|
||||
// Generate options set
|
||||
srand(123);
|
||||
|
||||
for (i = 0; i < OPT_N; i++) {
|
||||
optionData[i].S = randData(5.0f, 30.0f);
|
||||
optionData[i].X = randData(1.0f, 100.0f);
|
||||
optionData[i].T = randData(0.25f, 10.0f);
|
||||
optionData[i].R = 0.06f;
|
||||
optionData[i].V = 0.10f;
|
||||
BlackScholesCall(callValueBS[i], optionData[i]);
|
||||
}
|
||||
for (i = 0; i < OPT_N; i++) {
|
||||
optionData[i].S = randData(5.0f, 30.0f);
|
||||
optionData[i].X = randData(1.0f, 100.0f);
|
||||
optionData[i].T = randData(0.25f, 10.0f);
|
||||
optionData[i].R = 0.06f;
|
||||
optionData[i].V = 0.10f;
|
||||
BlackScholesCall(callValueBS[i], optionData[i]);
|
||||
}
|
||||
|
||||
printf("Running GPU binomial tree...\n");
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkResetTimer(&hTimer);
|
||||
sdkStartTimer(&hTimer);
|
||||
printf("Running GPU binomial tree...\n");
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkResetTimer(&hTimer);
|
||||
sdkStartTimer(&hTimer);
|
||||
|
||||
binomialOptionsGPU(callValueGPU, optionData, OPT_N);
|
||||
binomialOptionsGPU(callValueGPU, optionData, OPT_N);
|
||||
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkStopTimer(&hTimer);
|
||||
gpuTime = sdkGetTimerValue(&hTimer);
|
||||
printf("Options count : %i \n", OPT_N);
|
||||
printf("Time steps : %i \n", NUM_STEPS);
|
||||
printf("binomialOptionsGPU() time: %f msec\n", gpuTime);
|
||||
printf("Options per second : %f \n", OPT_N / (gpuTime * 0.001));
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkStopTimer(&hTimer);
|
||||
gpuTime = sdkGetTimerValue(&hTimer);
|
||||
printf("Options count : %i \n", OPT_N);
|
||||
printf("Time steps : %i \n", NUM_STEPS);
|
||||
printf("binomialOptionsGPU() time: %f msec\n", gpuTime);
|
||||
printf("Options per second : %f \n", OPT_N / (gpuTime * 0.001));
|
||||
|
||||
printf("Running CPU binomial tree...\n");
|
||||
printf("Running CPU binomial tree...\n");
|
||||
|
||||
for (i = 0; i < OPT_N; i++) {
|
||||
binomialOptionsCPU(callValueCPU[i], optionData[i]);
|
||||
}
|
||||
for (i = 0; i < OPT_N; i++) {
|
||||
binomialOptionsCPU(callValueCPU[i], optionData[i]);
|
||||
}
|
||||
|
||||
printf("Comparing the results...\n");
|
||||
sumDelta = 0;
|
||||
sumRef = 0;
|
||||
printf("GPU binomial vs. Black-Scholes\n");
|
||||
printf("Comparing the results...\n");
|
||||
sumDelta = 0;
|
||||
sumRef = 0;
|
||||
printf("GPU binomial vs. Black-Scholes\n");
|
||||
|
||||
for (i = 0; i < OPT_N; i++) {
|
||||
sumDelta += fabs(callValueBS[i] - callValueGPU[i]);
|
||||
sumRef += fabs(callValueBS[i]);
|
||||
}
|
||||
for (i = 0; i < OPT_N; i++) {
|
||||
sumDelta += fabs(callValueBS[i] - callValueGPU[i]);
|
||||
sumRef += fabs(callValueBS[i]);
|
||||
}
|
||||
|
||||
if (sumRef > 1E-5) {
|
||||
printf("L1 norm: %E\n", (double)(sumDelta / sumRef));
|
||||
} else {
|
||||
printf("Avg. diff: %E\n", (double)(sumDelta / (real)OPT_N));
|
||||
}
|
||||
if (sumRef > 1E-5) {
|
||||
printf("L1 norm: %E\n", (double)(sumDelta / sumRef));
|
||||
}
|
||||
else {
|
||||
printf("Avg. diff: %E\n", (double)(sumDelta / (real)OPT_N));
|
||||
}
|
||||
|
||||
printf("CPU binomial vs. Black-Scholes\n");
|
||||
sumDelta = 0;
|
||||
sumRef = 0;
|
||||
printf("CPU binomial vs. Black-Scholes\n");
|
||||
sumDelta = 0;
|
||||
sumRef = 0;
|
||||
|
||||
for (i = 0; i < OPT_N; i++) {
|
||||
sumDelta += fabs(callValueBS[i] - callValueCPU[i]);
|
||||
sumRef += fabs(callValueBS[i]);
|
||||
}
|
||||
for (i = 0; i < OPT_N; i++) {
|
||||
sumDelta += fabs(callValueBS[i] - callValueCPU[i]);
|
||||
sumRef += fabs(callValueBS[i]);
|
||||
}
|
||||
|
||||
if (sumRef > 1E-5) {
|
||||
printf("L1 norm: %E\n", sumDelta / sumRef);
|
||||
} else {
|
||||
printf("Avg. diff: %E\n", (double)(sumDelta / (real)OPT_N));
|
||||
}
|
||||
if (sumRef > 1E-5) {
|
||||
printf("L1 norm: %E\n", sumDelta / sumRef);
|
||||
}
|
||||
else {
|
||||
printf("Avg. diff: %E\n", (double)(sumDelta / (real)OPT_N));
|
||||
}
|
||||
|
||||
printf("CPU binomial vs. GPU binomial\n");
|
||||
sumDelta = 0;
|
||||
sumRef = 0;
|
||||
printf("CPU binomial vs. GPU binomial\n");
|
||||
sumDelta = 0;
|
||||
sumRef = 0;
|
||||
|
||||
for (i = 0; i < OPT_N; i++) {
|
||||
sumDelta += fabs(callValueGPU[i] - callValueCPU[i]);
|
||||
sumRef += callValueCPU[i];
|
||||
}
|
||||
for (i = 0; i < OPT_N; i++) {
|
||||
sumDelta += fabs(callValueGPU[i] - callValueCPU[i]);
|
||||
sumRef += callValueCPU[i];
|
||||
}
|
||||
|
||||
if (sumRef > 1E-5) {
|
||||
printf("L1 norm: %E\n", errorVal = sumDelta / sumRef);
|
||||
} else {
|
||||
printf("Avg. diff: %E\n", (double)(sumDelta / (real)OPT_N));
|
||||
}
|
||||
if (sumRef > 1E-5) {
|
||||
printf("L1 norm: %E\n", errorVal = sumDelta / sumRef);
|
||||
}
|
||||
else {
|
||||
printf("Avg. diff: %E\n", (double)(sumDelta / (real)OPT_N));
|
||||
}
|
||||
|
||||
printf("Shutting down...\n");
|
||||
printf("Shutting down...\n");
|
||||
|
||||
sdkDeleteTimer(&hTimer);
|
||||
sdkDeleteTimer(&hTimer);
|
||||
|
||||
printf(
|
||||
"\nNOTE: The CUDA Samples are not meant for performance measurements. "
|
||||
"Results may vary when GPU Boost is enabled.\n\n");
|
||||
printf("\nNOTE: The CUDA Samples are not meant for performance measurements. "
|
||||
"Results may vary when GPU Boost is enabled.\n\n");
|
||||
|
||||
if (errorVal > 5e-4) {
|
||||
printf("Test failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (errorVal > 5e-4) {
|
||||
printf("Test failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("Test passed\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
printf("Test passed\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
@@ -33,12 +33,13 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Global types
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
typedef struct {
|
||||
real S;
|
||||
real X;
|
||||
real T;
|
||||
real R;
|
||||
real V;
|
||||
typedef struct
|
||||
{
|
||||
real S;
|
||||
real X;
|
||||
real T;
|
||||
real R;
|
||||
real V;
|
||||
} TOptionData;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -25,96 +25,102 @@
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "binomialOptions_common.h"
|
||||
#include "realtype.h"
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Polynomial approximation of cumulative normal distribution function
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
static real CND(real d) {
|
||||
const real A1 = (real)0.31938153;
|
||||
const real A2 = (real)-0.356563782;
|
||||
const real A3 = (real)1.781477937;
|
||||
const real A4 = (real)-1.821255978;
|
||||
const real A5 = (real)1.330274429;
|
||||
const real RSQRT2PI = (real)0.39894228040143267793994605993438;
|
||||
static real CND(real d)
|
||||
{
|
||||
const real A1 = (real)0.31938153;
|
||||
const real A2 = (real)-0.356563782;
|
||||
const real A3 = (real)1.781477937;
|
||||
const real A4 = (real)-1.821255978;
|
||||
const real A5 = (real)1.330274429;
|
||||
const real RSQRT2PI = (real)0.39894228040143267793994605993438;
|
||||
|
||||
real K = (real)(1.0 / (1.0 + 0.2316419 * (real)fabs(d)));
|
||||
real K = (real)(1.0 / (1.0 + 0.2316419 * (real)fabs(d)));
|
||||
|
||||
real cnd = (real)RSQRT2PI * (real)exp(-0.5 * d * d) *
|
||||
(K * (A1 + K * (A2 + K * (A3 + K * (A4 + K * A5)))));
|
||||
real cnd = (real)RSQRT2PI * (real)exp(-0.5 * d * d) * (K * (A1 + K * (A2 + K * (A3 + K * (A4 + K * A5)))));
|
||||
|
||||
if (d > 0) cnd = (real)1.0 - cnd;
|
||||
if (d > 0)
|
||||
cnd = (real)1.0 - cnd;
|
||||
|
||||
return cnd;
|
||||
return cnd;
|
||||
}
|
||||
|
||||
extern "C" void BlackScholesCall(real &callResult, TOptionData optionData) {
|
||||
real S = optionData.S;
|
||||
real X = optionData.X;
|
||||
real T = optionData.T;
|
||||
real R = optionData.R;
|
||||
real V = optionData.V;
|
||||
extern "C" void BlackScholesCall(real &callResult, TOptionData optionData)
|
||||
{
|
||||
real S = optionData.S;
|
||||
real X = optionData.X;
|
||||
real T = optionData.T;
|
||||
real R = optionData.R;
|
||||
real V = optionData.V;
|
||||
|
||||
real sqrtT = (real)sqrt(T);
|
||||
real d1 = (real)(log(S / X) + (R + (real)0.5 * V * V) * T) / (V * sqrtT);
|
||||
real d2 = d1 - V * sqrtT;
|
||||
real CNDD1 = CND(d1);
|
||||
real CNDD2 = CND(d2);
|
||||
real sqrtT = (real)sqrt(T);
|
||||
real d1 = (real)(log(S / X) + (R + (real)0.5 * V * V) * T) / (V * sqrtT);
|
||||
real d2 = d1 - V * sqrtT;
|
||||
real CNDD1 = CND(d1);
|
||||
real CNDD2 = CND(d2);
|
||||
|
||||
// Calculate Call and Put simultaneously
|
||||
real expRT = (real)exp(-R * T);
|
||||
callResult = (real)(S * CNDD1 - X * expRT * CNDD2);
|
||||
// Calculate Call and Put simultaneously
|
||||
real expRT = (real)exp(-R * T);
|
||||
callResult = (real)(S * CNDD1 - X * expRT * CNDD2);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Process an array of OptN options on CPU
|
||||
// Note that CPU code is for correctness testing only and not for benchmarking.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
static real expiryCallValue(real S, real X, real vDt, int i) {
|
||||
real d = S * (real)exp(vDt * (real)(2 * i - NUM_STEPS)) - X;
|
||||
return (d > (real)0) ? d : (real)0;
|
||||
static real expiryCallValue(real S, real X, real vDt, int i)
|
||||
{
|
||||
real d = S * (real)exp(vDt * (real)(2 * i - NUM_STEPS)) - X;
|
||||
return (d > (real)0) ? d : (real)0;
|
||||
}
|
||||
|
||||
extern "C" void binomialOptionsCPU(real &callResult, TOptionData optionData) {
|
||||
static real Call[NUM_STEPS + 1];
|
||||
extern "C" void binomialOptionsCPU(real &callResult, TOptionData optionData)
|
||||
{
|
||||
static real Call[NUM_STEPS + 1];
|
||||
|
||||
const real S = optionData.S;
|
||||
const real X = optionData.X;
|
||||
const real T = optionData.T;
|
||||
const real R = optionData.R;
|
||||
const real V = optionData.V;
|
||||
const real S = optionData.S;
|
||||
const real X = optionData.X;
|
||||
const real T = optionData.T;
|
||||
const real R = optionData.R;
|
||||
const real V = optionData.V;
|
||||
|
||||
const real dt = T / (real)NUM_STEPS;
|
||||
const real vDt = (real)V * (real)sqrt(dt);
|
||||
const real rDt = R * dt;
|
||||
// Per-step interest and discount factors
|
||||
const real If = (real)exp(rDt);
|
||||
const real Df = (real)exp(-rDt);
|
||||
// Values and pseudoprobabilities of upward and downward moves
|
||||
const real u = (real)exp(vDt);
|
||||
const real d = (real)exp(-vDt);
|
||||
const real pu = (If - d) / (u - d);
|
||||
const real pd = (real)1.0 - pu;
|
||||
const real puByDf = pu * Df;
|
||||
const real pdByDf = pd * Df;
|
||||
const real dt = T / (real)NUM_STEPS;
|
||||
const real vDt = (real)V * (real)sqrt(dt);
|
||||
const real rDt = R * dt;
|
||||
// Per-step interest and discount factors
|
||||
const real If = (real)exp(rDt);
|
||||
const real Df = (real)exp(-rDt);
|
||||
// Values and pseudoprobabilities of upward and downward moves
|
||||
const real u = (real)exp(vDt);
|
||||
const real d = (real)exp(-vDt);
|
||||
const real pu = (If - d) / (u - d);
|
||||
const real pd = (real)1.0 - pu;
|
||||
const real puByDf = pu * Df;
|
||||
const real pdByDf = pd * Df;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
// Compute values at expiration date:
|
||||
// call option value at period end is V(T) = S(T) - X
|
||||
// if S(T) is greater than X, or zero otherwise.
|
||||
// The computation is similar for put options.
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
for (int i = 0; i <= NUM_STEPS; i++) Call[i] = expiryCallValue(S, X, vDt, i);
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
// Compute values at expiration date:
|
||||
// call option value at period end is V(T) = S(T) - X
|
||||
// if S(T) is greater than X, or zero otherwise.
|
||||
// The computation is similar for put options.
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
for (int i = 0; i <= NUM_STEPS; i++)
|
||||
Call[i] = expiryCallValue(S, X, vDt, i);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Walk backwards up binomial tree
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
for (int i = NUM_STEPS; i > 0; i--)
|
||||
for (int j = 0; j <= i - 1; j++)
|
||||
Call[j] = puByDf * Call[j + 1] + pdByDf * Call[j];
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Walk backwards up binomial tree
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
for (int i = NUM_STEPS; i > 0; i--)
|
||||
for (int j = 0; j <= i - 1; j++)
|
||||
Call[j] = puByDf * Call[j + 1] + pdByDf * Call[j];
|
||||
|
||||
callResult = (real)Call[0];
|
||||
callResult = (real)Call[0];
|
||||
}
|
||||
|
||||
@@ -28,40 +28,43 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Global types and parameters
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#include <cooperative_groups.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <cooperative_groups.h>
|
||||
|
||||
namespace cg = cooperative_groups;
|
||||
|
||||
#include <helper_cuda.h>
|
||||
|
||||
#include "binomialOptions_common.h"
|
||||
#include "realtype.h"
|
||||
|
||||
// Preprocessed input option data
|
||||
typedef struct {
|
||||
real S;
|
||||
real X;
|
||||
real vDt;
|
||||
real puByDf;
|
||||
real pdByDf;
|
||||
typedef struct
|
||||
{
|
||||
real S;
|
||||
real X;
|
||||
real vDt;
|
||||
real puByDf;
|
||||
real pdByDf;
|
||||
} __TOptionData;
|
||||
static __constant__ __TOptionData d_OptionData[MAX_OPTIONS];
|
||||
static __device__ real d_CallValue[MAX_OPTIONS];
|
||||
static __device__ real d_CallValue[MAX_OPTIONS];
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Overloaded shortcut functions for different precision modes
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#ifndef DOUBLE_PRECISION
|
||||
__device__ inline float expiryCallValue(float S, float X, float vDt, int i) {
|
||||
float d = S * __expf(vDt * (2.0f * i - NUM_STEPS)) - X;
|
||||
return (d > 0.0F) ? d : 0.0F;
|
||||
__device__ inline float expiryCallValue(float S, float X, float vDt, int i)
|
||||
{
|
||||
float d = S * __expf(vDt * (2.0f * i - NUM_STEPS)) - X;
|
||||
return (d > 0.0F) ? d : 0.0F;
|
||||
}
|
||||
#else
|
||||
__device__ inline double expiryCallValue(double S, double X, double vDt,
|
||||
int i) {
|
||||
double d = S * exp(vDt * (2.0 * i - NUM_STEPS)) - X;
|
||||
return (d > 0.0) ? d : 0.0;
|
||||
__device__ inline double expiryCallValue(double S, double X, double vDt, int i)
|
||||
{
|
||||
double d = S * exp(vDt * (2.0 * i - NUM_STEPS)) - X;
|
||||
return (d > 0.0) ? d : 0.0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -74,84 +77,83 @@ __device__ inline double expiryCallValue(double S, double X, double vDt,
|
||||
#error Bad constants
|
||||
#endif
|
||||
|
||||
__global__ void binomialOptionsKernel() {
|
||||
// Handle to thread block group
|
||||
cg::thread_block cta = cg::this_thread_block();
|
||||
__shared__ real call_exchange[THREADBLOCK_SIZE + 1];
|
||||
__global__ void binomialOptionsKernel()
|
||||
{
|
||||
// Handle to thread block group
|
||||
cg::thread_block cta = cg::this_thread_block();
|
||||
__shared__ real call_exchange[THREADBLOCK_SIZE + 1];
|
||||
|
||||
const int tid = threadIdx.x;
|
||||
const real S = d_OptionData[blockIdx.x].S;
|
||||
const real X = d_OptionData[blockIdx.x].X;
|
||||
const real vDt = d_OptionData[blockIdx.x].vDt;
|
||||
const real puByDf = d_OptionData[blockIdx.x].puByDf;
|
||||
const real pdByDf = d_OptionData[blockIdx.x].pdByDf;
|
||||
const int tid = threadIdx.x;
|
||||
const real S = d_OptionData[blockIdx.x].S;
|
||||
const real X = d_OptionData[blockIdx.x].X;
|
||||
const real vDt = d_OptionData[blockIdx.x].vDt;
|
||||
const real puByDf = d_OptionData[blockIdx.x].puByDf;
|
||||
const real pdByDf = d_OptionData[blockIdx.x].pdByDf;
|
||||
|
||||
real call[ELEMS_PER_THREAD + 1];
|
||||
real call[ELEMS_PER_THREAD + 1];
|
||||
#pragma unroll
|
||||
for (int i = 0; i < ELEMS_PER_THREAD; ++i)
|
||||
call[i] = expiryCallValue(S, X, vDt, tid * ELEMS_PER_THREAD + i);
|
||||
for (int i = 0; i < ELEMS_PER_THREAD; ++i)
|
||||
call[i] = expiryCallValue(S, X, vDt, tid * ELEMS_PER_THREAD + i);
|
||||
|
||||
if (tid == 0)
|
||||
call_exchange[THREADBLOCK_SIZE] = expiryCallValue(S, X, vDt, NUM_STEPS);
|
||||
if (tid == 0)
|
||||
call_exchange[THREADBLOCK_SIZE] = expiryCallValue(S, X, vDt, NUM_STEPS);
|
||||
|
||||
int final_it = max(0, tid * ELEMS_PER_THREAD - 1);
|
||||
int final_it = max(0, tid * ELEMS_PER_THREAD - 1);
|
||||
|
||||
#pragma unroll 16
|
||||
for (int i = NUM_STEPS; i > 0; --i) {
|
||||
call_exchange[tid] = call[0];
|
||||
cg::sync(cta);
|
||||
call[ELEMS_PER_THREAD] = call_exchange[tid + 1];
|
||||
cg::sync(cta);
|
||||
for (int i = NUM_STEPS; i > 0; --i) {
|
||||
call_exchange[tid] = call[0];
|
||||
cg::sync(cta);
|
||||
call[ELEMS_PER_THREAD] = call_exchange[tid + 1];
|
||||
cg::sync(cta);
|
||||
|
||||
if (i > final_it) {
|
||||
if (i > final_it) {
|
||||
#pragma unroll
|
||||
for (int j = 0; j < ELEMS_PER_THREAD; ++j)
|
||||
call[j] = puByDf * call[j + 1] + pdByDf * call[j];
|
||||
for (int j = 0; j < ELEMS_PER_THREAD; ++j)
|
||||
call[j] = puByDf * call[j + 1] + pdByDf * call[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (tid == 0) {
|
||||
d_CallValue[blockIdx.x] = call[0];
|
||||
}
|
||||
if (tid == 0) {
|
||||
d_CallValue[blockIdx.x] = call[0];
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Host-side interface to GPU binomialOptions
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
extern "C" void binomialOptionsGPU(real *callValue, TOptionData *optionData,
|
||||
int optN) {
|
||||
__TOptionData h_OptionData[MAX_OPTIONS];
|
||||
extern "C" void binomialOptionsGPU(real *callValue, TOptionData *optionData, int optN)
|
||||
{
|
||||
__TOptionData h_OptionData[MAX_OPTIONS];
|
||||
|
||||
for (int i = 0; i < optN; i++) {
|
||||
const real T = optionData[i].T;
|
||||
const real R = optionData[i].R;
|
||||
const real V = optionData[i].V;
|
||||
for (int i = 0; i < optN; i++) {
|
||||
const real T = optionData[i].T;
|
||||
const real R = optionData[i].R;
|
||||
const real V = optionData[i].V;
|
||||
|
||||
const real dt = T / (real)NUM_STEPS;
|
||||
const real vDt = V * sqrt(dt);
|
||||
const real rDt = R * dt;
|
||||
// Per-step interest and discount factors
|
||||
const real If = exp(rDt);
|
||||
const real Df = exp(-rDt);
|
||||
// Values and pseudoprobabilities of upward and downward moves
|
||||
const real u = exp(vDt);
|
||||
const real d = exp(-vDt);
|
||||
const real pu = (If - d) / (u - d);
|
||||
const real pd = (real)1.0 - pu;
|
||||
const real puByDf = pu * Df;
|
||||
const real pdByDf = pd * Df;
|
||||
const real dt = T / (real)NUM_STEPS;
|
||||
const real vDt = V * sqrt(dt);
|
||||
const real rDt = R * dt;
|
||||
// Per-step interest and discount factors
|
||||
const real If = exp(rDt);
|
||||
const real Df = exp(-rDt);
|
||||
// Values and pseudoprobabilities of upward and downward moves
|
||||
const real u = exp(vDt);
|
||||
const real d = exp(-vDt);
|
||||
const real pu = (If - d) / (u - d);
|
||||
const real pd = (real)1.0 - pu;
|
||||
const real puByDf = pu * Df;
|
||||
const real pdByDf = pd * Df;
|
||||
|
||||
h_OptionData[i].S = (real)optionData[i].S;
|
||||
h_OptionData[i].X = (real)optionData[i].X;
|
||||
h_OptionData[i].vDt = (real)vDt;
|
||||
h_OptionData[i].puByDf = (real)puByDf;
|
||||
h_OptionData[i].pdByDf = (real)pdByDf;
|
||||
}
|
||||
h_OptionData[i].S = (real)optionData[i].S;
|
||||
h_OptionData[i].X = (real)optionData[i].X;
|
||||
h_OptionData[i].vDt = (real)vDt;
|
||||
h_OptionData[i].puByDf = (real)puByDf;
|
||||
h_OptionData[i].pdByDf = (real)pdByDf;
|
||||
}
|
||||
|
||||
checkCudaErrors(cudaMemcpyToSymbol(d_OptionData, h_OptionData,
|
||||
optN * sizeof(__TOptionData)));
|
||||
binomialOptionsKernel<<<optN, THREADBLOCK_SIZE>>>();
|
||||
getLastCudaError("binomialOptionsKernel() execution failed.\n");
|
||||
checkCudaErrors(
|
||||
cudaMemcpyFromSymbol(callValue, d_CallValue, optN * sizeof(real)));
|
||||
checkCudaErrors(cudaMemcpyToSymbol(d_OptionData, h_OptionData, optN * sizeof(__TOptionData)));
|
||||
binomialOptionsKernel<<<optN, THREADBLOCK_SIZE>>>();
|
||||
getLastCudaError("binomialOptionsKernel() execution failed.\n");
|
||||
checkCudaErrors(cudaMemcpyFromSymbol(callValue, d_CallValue, optN * sizeof(real)));
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
#ifndef REALTYPE_H
|
||||
#define REALTYPE_H
|
||||
|
||||
//#define DOUBLE_PRECISION
|
||||
// #define DOUBLE_PRECISION
|
||||
|
||||
#ifndef DOUBLE_PRECISION
|
||||
typedef float real;
|
||||
|
||||
Reference in New Issue
Block a user