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,15 +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 <cuda.h>
|
||||
|
||||
#include <cuda_runtime.h>
|
||||
#include <helper_functions.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "binomialOptions_common.h"
|
||||
#include "realtype.h"
|
||||
@@ -61,138 +59,140 @@ 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, int argc, char **argv);
|
||||
extern "C" void binomialOptionsGPU(real *callValue, TOptionData *optionData, int optN, int argc, char **argv);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// 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]);
|
||||
|
||||
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;
|
||||
StopWatchInterface *hTimer = NULL;
|
||||
|
||||
int i;
|
||||
int i;
|
||||
|
||||
sdkCreateTimer(&hTimer);
|
||||
sdkCreateTimer(&hTimer);
|
||||
|
||||
printf("Generating input data...\n");
|
||||
printf("Generating input data...\n");
|
||||
|
||||
// Generate options set
|
||||
srand(123);
|
||||
// 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;
|
||||
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]);
|
||||
}
|
||||
BlackScholesCall(callValueBS[i], optionData[i]);
|
||||
}
|
||||
|
||||
printf("Running GPU binomial tree...\n");
|
||||
printf("Running GPU binomial tree...\n");
|
||||
|
||||
sdkResetTimer(&hTimer);
|
||||
sdkStartTimer(&hTimer);
|
||||
sdkResetTimer(&hTimer);
|
||||
sdkStartTimer(&hTimer);
|
||||
|
||||
binomialOptionsGPU(callValueGPU, optionData, OPT_N, argc, argv);
|
||||
binomialOptionsGPU(callValueGPU, optionData, OPT_N, argc, argv);
|
||||
|
||||
sdkStopTimer(&hTimer);
|
||||
sdkStopTimer(&hTimer);
|
||||
|
||||
gpuTime = sdkGetTimerValue(&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("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");
|
||||
printf("Comparing the results...\n");
|
||||
|
||||
sumDelta = 0;
|
||||
sumRef = 0;
|
||||
printf("GPU binomial vs. Black-Scholes\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");
|
||||
printf("Test passed\n");
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
@@ -32,12 +32,13 @@
|
||||
// Global types
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
typedef struct {
|
||||
float S;
|
||||
float X;
|
||||
float T;
|
||||
float R;
|
||||
float V;
|
||||
typedef struct
|
||||
{
|
||||
float S;
|
||||
float X;
|
||||
float T;
|
||||
float R;
|
||||
float V;
|
||||
} TOptionData;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -25,50 +25,53 @@
|
||||
* 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"
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Polynomial approximation of cumulative normal distribution function
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static double CND(double d) {
|
||||
const double A1 = 0.31938153;
|
||||
const double A2 = -0.356563782;
|
||||
const double A3 = 1.781477937;
|
||||
const double A4 = -1.821255978;
|
||||
const double A5 = 1.330274429;
|
||||
const double RSQRT2PI = 0.39894228040143267793994605993438;
|
||||
static double CND(double d)
|
||||
{
|
||||
const double A1 = 0.31938153;
|
||||
const double A2 = -0.356563782;
|
||||
const double A3 = 1.781477937;
|
||||
const double A4 = -1.821255978;
|
||||
const double A5 = 1.330274429;
|
||||
const double RSQRT2PI = 0.39894228040143267793994605993438;
|
||||
|
||||
double K = 1.0 / (1.0 + 0.2316419 * fabs(d));
|
||||
double K = 1.0 / (1.0 + 0.2316419 * fabs(d));
|
||||
|
||||
double cnd = RSQRT2PI * exp(-0.5 * d * d) *
|
||||
(K * (A1 + K * (A2 + K * (A3 + K * (A4 + K * A5)))));
|
||||
double cnd = RSQRT2PI * exp(-0.5 * d * d) * (K * (A1 + K * (A2 + K * (A3 + K * (A4 + K * A5)))));
|
||||
|
||||
if (d > 0) cnd = 1.0 - cnd;
|
||||
if (d > 0)
|
||||
cnd = 1.0 - cnd;
|
||||
|
||||
return cnd;
|
||||
return cnd;
|
||||
}
|
||||
|
||||
extern "C" void BlackScholesCall(float &callResult, TOptionData optionData) {
|
||||
double S = optionData.S;
|
||||
double X = optionData.X;
|
||||
double T = optionData.T;
|
||||
double R = optionData.R;
|
||||
double V = optionData.V;
|
||||
double sqrtT = sqrt(T);
|
||||
extern "C" void BlackScholesCall(float &callResult, TOptionData optionData)
|
||||
{
|
||||
double S = optionData.S;
|
||||
double X = optionData.X;
|
||||
double T = optionData.T;
|
||||
double R = optionData.R;
|
||||
double V = optionData.V;
|
||||
double sqrtT = sqrt(T);
|
||||
|
||||
double d1 = (log(S / X) + (R + 0.5 * V * V) * T) / (V * sqrtT);
|
||||
double d2 = d1 - V * sqrtT;
|
||||
double d1 = (log(S / X) + (R + 0.5 * V * V) * T) / (V * sqrtT);
|
||||
double d2 = d1 - V * sqrtT;
|
||||
|
||||
double CNDD1 = CND(d1);
|
||||
double CNDD2 = CND(d2);
|
||||
double CNDD1 = CND(d1);
|
||||
double CNDD2 = CND(d2);
|
||||
|
||||
// Calculate Call and Put simultaneously
|
||||
double expRT = exp(-R * T);
|
||||
// Calculate Call and Put simultaneously
|
||||
double expRT = exp(-R * T);
|
||||
|
||||
callResult = (float)(S * CNDD1 - X * expRT * CNDD2);
|
||||
callResult = (float)(S * CNDD1 - X * expRT * CNDD2);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -76,51 +79,54 @@ extern "C" void BlackScholesCall(float &callResult, TOptionData optionData) {
|
||||
// Note that CPU code is for correctness testing only and not for benchmarking.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static double expiryCallValue(double S, double X, double vDt, int i) {
|
||||
double d = S * exp(vDt * (2.0 * i - NUM_STEPS)) - X;
|
||||
return (d > 0) ? d : 0;
|
||||
static double expiryCallValue(double S, double X, double vDt, int i)
|
||||
{
|
||||
double d = S * exp(vDt * (2.0 * i - NUM_STEPS)) - X;
|
||||
return (d > 0) ? d : 0;
|
||||
}
|
||||
|
||||
extern "C" void binomialOptionsCPU(float &callResult, TOptionData optionData) {
|
||||
static double Call[NUM_STEPS + 1];
|
||||
const double S = optionData.S;
|
||||
const double X = optionData.X;
|
||||
const double T = optionData.T;
|
||||
const double R = optionData.R;
|
||||
const double V = optionData.V;
|
||||
extern "C" void binomialOptionsCPU(float &callResult, TOptionData optionData)
|
||||
{
|
||||
static double Call[NUM_STEPS + 1];
|
||||
const double S = optionData.S;
|
||||
const double X = optionData.X;
|
||||
const double T = optionData.T;
|
||||
const double R = optionData.R;
|
||||
const double V = optionData.V;
|
||||
|
||||
const double dt = T / (double)NUM_STEPS;
|
||||
const double vDt = V * sqrt(dt);
|
||||
const double rDt = R * dt;
|
||||
const double dt = T / (double)NUM_STEPS;
|
||||
const double vDt = V * sqrt(dt);
|
||||
const double rDt = R * dt;
|
||||
|
||||
// Per-step interest and discount factors
|
||||
const double If = exp(rDt);
|
||||
const double Df = exp(-rDt);
|
||||
// Per-step interest and discount factors
|
||||
const double If = exp(rDt);
|
||||
const double Df = exp(-rDt);
|
||||
|
||||
// Values and pseudoprobabilities of upward and downward moves
|
||||
const double u = exp(vDt);
|
||||
const double d = exp(-vDt);
|
||||
const double pu = (If - d) / (u - d);
|
||||
const double pd = 1.0 - pu;
|
||||
const double puByDf = pu * Df;
|
||||
const double pdByDf = pd * Df;
|
||||
// Values and pseudoprobabilities of upward and downward moves
|
||||
const double u = exp(vDt);
|
||||
const double d = exp(-vDt);
|
||||
const double pu = (If - d) / (u - d);
|
||||
const double pd = 1.0 - pu;
|
||||
const double puByDf = pu * Df;
|
||||
const double 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.
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
// 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);
|
||||
for (int i = 0; i <= NUM_STEPS; i++)
|
||||
Call[i] = expiryCallValue(S, X, vDt, i);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Walk backwards up binomial tree
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// 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];
|
||||
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 = (float)Call[0];
|
||||
callResult = (float)Call[0];
|
||||
}
|
||||
|
||||
@@ -29,99 +29,104 @@
|
||||
// Global types and parameters
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <cmath>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <cmath>
|
||||
|
||||
// Other helpers
|
||||
#include <helper_cuda.h>
|
||||
#include <nvrtc_helper.h>
|
||||
|
||||
// CUDA runtime
|
||||
#include <cuda_runtime.h>
|
||||
|
||||
#include "binomialOptions_common.h"
|
||||
|
||||
#include "common_gpu_header.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 bool moduleLoaded = false;
|
||||
char *cubin, *kernel_file;
|
||||
size_t cubinSize;
|
||||
CUmodule module;
|
||||
char *cubin, *kernel_file;
|
||||
size_t cubinSize;
|
||||
CUmodule module;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Host-side interface to GPU binomialOptions
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
extern "C" void binomialOptionsGPU(real *callValue, TOptionData *optionData,
|
||||
int optN, int argc, char **argv) {
|
||||
if (!moduleLoaded) {
|
||||
kernel_file = sdkFindFilePath("binomialOptions_kernel.cu", argv[0]);
|
||||
compileFileToCUBIN(kernel_file, argc, argv, &cubin, &cubinSize, 0);
|
||||
module = loadCUBIN(cubin, argc, argv);
|
||||
moduleLoaded = true;
|
||||
}
|
||||
extern "C" void binomialOptionsGPU(real *callValue, TOptionData *optionData, int optN, int argc, char **argv)
|
||||
{
|
||||
if (!moduleLoaded) {
|
||||
kernel_file = sdkFindFilePath("binomialOptions_kernel.cu", argv[0]);
|
||||
compileFileToCUBIN(kernel_file, argc, argv, &cubin, &cubinSize, 0);
|
||||
module = loadCUBIN(cubin, argc, argv);
|
||||
moduleLoaded = true;
|
||||
}
|
||||
|
||||
__TOptionData h_OptionData[MAX_OPTIONS];
|
||||
__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;
|
||||
}
|
||||
|
||||
CUfunction kernel_addr;
|
||||
checkCudaErrors(
|
||||
cuModuleGetFunction(&kernel_addr, module, "binomialOptionsKernel"));
|
||||
CUfunction kernel_addr;
|
||||
checkCudaErrors(cuModuleGetFunction(&kernel_addr, module, "binomialOptionsKernel"));
|
||||
|
||||
CUdeviceptr d_OptionData;
|
||||
checkCudaErrors(
|
||||
cuModuleGetGlobal(&d_OptionData, NULL, module, "d_OptionData"));
|
||||
checkCudaErrors(
|
||||
cuMemcpyHtoD(d_OptionData, h_OptionData, optN * sizeof(__TOptionData)));
|
||||
CUdeviceptr d_OptionData;
|
||||
checkCudaErrors(cuModuleGetGlobal(&d_OptionData, NULL, module, "d_OptionData"));
|
||||
checkCudaErrors(cuMemcpyHtoD(d_OptionData, h_OptionData, optN * sizeof(__TOptionData)));
|
||||
|
||||
dim3 cudaBlockSize(128, 1, 1);
|
||||
dim3 cudaGridSize(optN, 1, 1);
|
||||
dim3 cudaBlockSize(128, 1, 1);
|
||||
dim3 cudaGridSize(optN, 1, 1);
|
||||
|
||||
checkCudaErrors(cuLaunchKernel(kernel_addr, cudaGridSize.x, cudaGridSize.y,
|
||||
cudaGridSize.z, /* grid dim */
|
||||
cudaBlockSize.x, cudaBlockSize.y,
|
||||
cudaBlockSize.z, /* block dim */
|
||||
0, 0, /* shared mem, stream */
|
||||
NULL, /* arguments */
|
||||
0));
|
||||
checkCudaErrors(cuLaunchKernel(kernel_addr,
|
||||
cudaGridSize.x,
|
||||
cudaGridSize.y,
|
||||
cudaGridSize.z, /* grid dim */
|
||||
cudaBlockSize.x,
|
||||
cudaBlockSize.y,
|
||||
cudaBlockSize.z, /* block dim */
|
||||
0,
|
||||
0, /* shared mem, stream */
|
||||
NULL, /* arguments */
|
||||
0));
|
||||
|
||||
checkCudaErrors(cuCtxSynchronize());
|
||||
checkCudaErrors(cuCtxSynchronize());
|
||||
|
||||
CUdeviceptr d_CallValue;
|
||||
checkCudaErrors(cuModuleGetGlobal(&d_CallValue, NULL, module, "d_CallValue"));
|
||||
checkCudaErrors(cuMemcpyDtoH(callValue, d_CallValue, optN * sizeof(real)));
|
||||
CUdeviceptr d_CallValue;
|
||||
checkCudaErrors(cuModuleGetGlobal(&d_CallValue, NULL, module, "d_CallValue"));
|
||||
checkCudaErrors(cuMemcpyDtoH(callValue, d_CallValue, optN * sizeof(real)));
|
||||
}
|
||||
|
||||
@@ -25,20 +25,21 @@
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "common_gpu_header.h"
|
||||
#include "binomialOptions_common.h"
|
||||
#include "common_gpu_header.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];
|
||||
__device__ real d_CallValue[MAX_OPTIONS];
|
||||
__device__ real d_CallValue[MAX_OPTIONS];
|
||||
|
||||
#define THREADBLOCK_SIZE 128
|
||||
#define ELEMS_PER_THREAD (NUM_STEPS / THREADBLOCK_SIZE)
|
||||
@@ -51,57 +52,59 @@ __device__ real d_CallValue[MAX_OPTIONS];
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#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
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// GPU kernel
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
extern "C" __global__ void binomialOptionsKernel() {
|
||||
__shared__ real call_exchange[THREADBLOCK_SIZE + 1];
|
||||
extern "C" __global__ void binomialOptionsKernel()
|
||||
{
|
||||
__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];
|
||||
__syncthreads();
|
||||
call[ELEMS_PER_THREAD] = call_exchange[tid + 1];
|
||||
__syncthreads();
|
||||
for (int i = NUM_STEPS; i > 0; --i) {
|
||||
call_exchange[tid] = call[0];
|
||||
__syncthreads();
|
||||
call[ELEMS_PER_THREAD] = call_exchange[tid + 1];
|
||||
__syncthreads();
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
* is strictly prohibited.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(__COMMON_GPU_HEADER_H)
|
||||
#define __COMMON_GPU_HEADER_H
|
||||
|
||||
@@ -16,16 +16,16 @@
|
||||
// Internal GPU-side constants and data structures
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#define TIME_STEPS 16
|
||||
#define TIME_STEPS 16
|
||||
|
||||
#define CACHE_DELTA (2 * TIME_STEPS)
|
||||
|
||||
#define CACHE_SIZE (256)
|
||||
#define CACHE_SIZE (256)
|
||||
|
||||
#define CACHE_STEP (CACHE_SIZE - CACHE_DELTA)
|
||||
#define CACHE_STEP (CACHE_SIZE - CACHE_DELTA)
|
||||
|
||||
#if NUM_STEPS % CACHE_DELTA
|
||||
#error Bad constants
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
|
||||
// To use double precision uncomment the macro DOUBLE_PRECISION below, default
|
||||
// is single precision.
|
||||
//#define DOUBLE_PRECISION
|
||||
// #define DOUBLE_PRECISION
|
||||
|
||||
#ifndef DOUBLE_PRECISION
|
||||
typedef float real;
|
||||
|
||||
Reference in New Issue
Block a user