mirror of
https://github.com/NVIDIA/cuda-samples.git
synced 2026-10-13 00:08:26 +08:00
CUDA 13.2 samples update (#432)
- Added Python samples for CUDA Python 1.0 release - Renamed top-level `Samples` directory to `cpp` to accommodate Python samples.
This commit is contained in:
18
cpp/5_Domain_Specific/binomialOptions/.vscode/c_cpp_properties.json
vendored
Normal file
18
cpp/5_Domain_Specific/binomialOptions/.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Linux",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**",
|
||||
"${workspaceFolder}/../../../Common"
|
||||
],
|
||||
"defines": [],
|
||||
"compilerPath": "/usr/local/cuda/bin/nvcc",
|
||||
"cStandard": "gnu17",
|
||||
"cppStandard": "gnu++14",
|
||||
"intelliSenseMode": "linux-gcc-x64",
|
||||
"configurationProvider": "ms-vscode.makefile-tools"
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
7
cpp/5_Domain_Specific/binomialOptions/.vscode/extensions.json
vendored
Normal file
7
cpp/5_Domain_Specific/binomialOptions/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"recommendations": [
|
||||
"nvidia.nsight-vscode-edition",
|
||||
"ms-vscode.cpptools",
|
||||
"ms-vscode.makefile-tools"
|
||||
]
|
||||
}
|
||||
38
cpp/5_Domain_Specific/binomialOptions/CMakeLists.txt
Normal file
38
cpp/5_Domain_Specific/binomialOptions/CMakeLists.txt
Normal file
@@ -0,0 +1,38 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/Modules")
|
||||
|
||||
project(binomialOptions LANGUAGES C CXX CUDA)
|
||||
|
||||
find_package(CUDAToolkit REQUIRED)
|
||||
|
||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
set(CMAKE_CUDA_ARCHITECTURES 75 80 86 87 89 90 100 110 120)
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wno-deprecated-gpu-targets")
|
||||
if(ENABLE_CUDA_DEBUG)
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -G") # enable cuda-gdb (may significantly affect performance on some targets)
|
||||
else()
|
||||
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -lineinfo") # add line information to all builds for debug tools (exclusive to -G option)
|
||||
endif()
|
||||
|
||||
# Include directories and libraries
|
||||
include_directories(../../../Common)
|
||||
|
||||
# Source file
|
||||
# Add target for binomialOptions
|
||||
add_executable(binomialOptions binomialOptions.cpp binomialOptions_gold.cpp binomialOptions_kernel.cu)
|
||||
|
||||
target_compile_options(binomialOptions PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda>)
|
||||
|
||||
target_compile_features(binomialOptions PRIVATE cxx_std_17 cuda_std_17)
|
||||
|
||||
set_target_properties(binomialOptions PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
|
||||
|
||||
target_include_directories(binomialOptions PUBLIC
|
||||
${CUDAToolkit_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
# Include installation configuration
|
||||
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/InstallSamples.cmake)
|
||||
setup_samples_install()
|
||||
34
cpp/5_Domain_Specific/binomialOptions/README.md
Normal file
34
cpp/5_Domain_Specific/binomialOptions/README.md
Normal file
@@ -0,0 +1,34 @@
|
||||
# binomialOptions - Binomial Option Pricing
|
||||
|
||||
## Description
|
||||
|
||||
This sample evaluates fair call price for a given set of European options under binomial model.
|
||||
|
||||
## Key Concepts
|
||||
|
||||
Computational Finance
|
||||
|
||||
## Supported SM Architectures
|
||||
|
||||
[SM 5.0 ](https://developer.nvidia.com/cuda-gpus) [SM 5.2 ](https://developer.nvidia.com/cuda-gpus) [SM 5.3 ](https://developer.nvidia.com/cuda-gpus) [SM 6.0 ](https://developer.nvidia.com/cuda-gpus) [SM 6.1 ](https://developer.nvidia.com/cuda-gpus) [SM 7.0 ](https://developer.nvidia.com/cuda-gpus) [SM 7.2 ](https://developer.nvidia.com/cuda-gpus) [SM 7.5 ](https://developer.nvidia.com/cuda-gpus) [SM 8.0 ](https://developer.nvidia.com/cuda-gpus) [SM 8.6 ](https://developer.nvidia.com/cuda-gpus) [SM 8.7 ](https://developer.nvidia.com/cuda-gpus) [SM 8.9 ](https://developer.nvidia.com/cuda-gpus) [SM 9.0 ](https://developer.nvidia.com/cuda-gpus)
|
||||
|
||||
## Supported OSes
|
||||
|
||||
Linux, Windows
|
||||
|
||||
## Supported CPU Architecture
|
||||
|
||||
x86_64, armv7l
|
||||
|
||||
## CUDA APIs involved
|
||||
|
||||
### [CUDA Runtime API](http://docs.nvidia.com/cuda/cuda-runtime-api/index.html)
|
||||
cudaDeviceSynchronize, cudaMemcpyToSymbol, cudaMemcpyFromSymbol
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Download and install the [CUDA Toolkit](https://developer.nvidia.com/cuda-downloads) for your corresponding platform.
|
||||
|
||||
## References (for more details)
|
||||
|
||||
[whitepaper](./doc/binomialOptions.pdf)
|
||||
189
cpp/5_Domain_Specific/binomialOptions/binomialOptions.cpp
Normal file
189
cpp/5_Domain_Specific/binomialOptions/binomialOptions.cpp
Normal file
@@ -0,0 +1,189 @@
|
||||
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This sample evaluates fair call price for a
|
||||
* given set of European options under binomial model.
|
||||
* See supplied whitepaper for more explanations.
|
||||
*/
|
||||
|
||||
#include <cuda_runtime.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"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Black-Scholes formula for binomial tree results validation
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
extern "C" void BlackScholesCall(real &callResult, TOptionData optionData);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Process single option on CPU
|
||||
// Note that CPU code is for correctness testing only and not for benchmarking.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
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);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// 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;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Main program
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
printf("[%s] - Starting...\n", argv[0]);
|
||||
|
||||
int devID = findCudaDevice(argc, (const char **)argv);
|
||||
|
||||
const int OPT_N = MAX_OPTIONS;
|
||||
|
||||
TOptionData optionData[MAX_OPTIONS];
|
||||
real callValueBS[MAX_OPTIONS], callValueGPU[MAX_OPTIONS], callValueCPU[MAX_OPTIONS];
|
||||
|
||||
real sumDelta, sumRef, gpuTime, errorVal;
|
||||
|
||||
StopWatchInterface *hTimer = NULL;
|
||||
int i;
|
||||
|
||||
sdkCreateTimer(&hTimer);
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
printf("Running GPU binomial tree...\n");
|
||||
checkCudaErrors(cudaDeviceSynchronize());
|
||||
sdkResetTimer(&hTimer);
|
||||
sdkStartTimer(&hTimer);
|
||||
|
||||
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));
|
||||
|
||||
printf("Running CPU binomial tree...\n");
|
||||
|
||||
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");
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
printf("Shutting down...\n");
|
||||
|
||||
sdkDeleteTimer(&hTimer);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
printf("Test passed\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef BINOMIALOPTIONS_COMMON_H
|
||||
#define BINOMIALOPTIONS_COMMON_H
|
||||
|
||||
#include "realtype.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Global types
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
typedef struct
|
||||
{
|
||||
real S;
|
||||
real X;
|
||||
real T;
|
||||
real R;
|
||||
real V;
|
||||
} TOptionData;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Global parameters
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Number of time steps
|
||||
#define NUM_STEPS 2048
|
||||
// Max option batch size
|
||||
#define MAX_OPTIONS 1024
|
||||
|
||||
#endif
|
||||
126
cpp/5_Domain_Specific/binomialOptions/binomialOptions_gold.cpp
Normal file
126
cpp/5_Domain_Specific/binomialOptions/binomialOptions_gold.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#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;
|
||||
|
||||
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)))));
|
||||
|
||||
if (d > 0)
|
||||
cnd = (real)1.0 - 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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// 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;
|
||||
}
|
||||
|
||||
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 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);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// 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];
|
||||
}
|
||||
159
cpp/5_Domain_Specific/binomialOptions/binomialOptions_kernel.cu
Normal file
159
cpp/5_Domain_Specific/binomialOptions/binomialOptions_kernel.cu
Normal file
@@ -0,0 +1,159 @@
|
||||
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Global types and parameters
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#include <cooperative_groups.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.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;
|
||||
} __TOptionData;
|
||||
static __constant__ __TOptionData d_OptionData[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;
|
||||
}
|
||||
#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;
|
||||
}
|
||||
#endif
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// GPU kernel
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#define THREADBLOCK_SIZE 128
|
||||
#define ELEMS_PER_THREAD (NUM_STEPS / THREADBLOCK_SIZE)
|
||||
#if NUM_STEPS % THREADBLOCK_SIZE
|
||||
#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];
|
||||
|
||||
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];
|
||||
#pragma unroll
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
if (i > final_it) {
|
||||
#pragma unroll
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Host-side interface to GPU binomialOptions
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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)));
|
||||
}
|
||||
BIN
cpp/5_Domain_Specific/binomialOptions/doc/binomialOptions.doc
Normal file
BIN
cpp/5_Domain_Specific/binomialOptions/doc/binomialOptions.doc
Normal file
Binary file not shown.
BIN
cpp/5_Domain_Specific/binomialOptions/doc/binomialOptions.pdf
Normal file
BIN
cpp/5_Domain_Specific/binomialOptions/doc/binomialOptions.pdf
Normal file
Binary file not shown.
39
cpp/5_Domain_Specific/binomialOptions/realtype.h
Normal file
39
cpp/5_Domain_Specific/binomialOptions/realtype.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
||||
* contributors may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
||||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef REALTYPE_H
|
||||
#define REALTYPE_H
|
||||
|
||||
// #define DOUBLE_PRECISION
|
||||
|
||||
#ifndef DOUBLE_PRECISION
|
||||
typedef float real;
|
||||
#else
|
||||
typedef double real;
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user