├── Black-Scholes ├── CPU │ ├── Makefile │ ├── blackScholesAnalyticEngine.c │ ├── blackScholesAnalyticEngineKernelsCpu.c │ ├── blackScholesAnalyticEngineKernelsCpu.h │ ├── blackScholesAnalyticEngineStructs.h │ └── errorFunctConsts.h ├── CUDA │ ├── Makefile │ ├── blackScholesAnalyticEngine.cu │ ├── blackScholesAnalyticEngineKernels.cu │ ├── blackScholesAnalyticEngineKernels.cuh │ ├── blackScholesAnalyticEngineKernelsCpu.cu │ ├── blackScholesAnalyticEngineKernelsCpu.cuh │ ├── blackScholesAnalyticEngineStructs.cuh │ └── errorFunctConsts.cuh ├── HMPP-CUDA │ ├── ErrorFunctConstants.h │ ├── Makefile │ ├── blackScholesAnalyticEngine.c │ ├── blackScholesAnalyticEngineKernels.c │ ├── blackScholesAnalyticEngineKernels.h │ ├── blackScholesAnalyticEngineStructs.h │ └── blackScholesEngineHmppCuda.c ├── HMPP-OpenCL │ ├── ErrorFunctConstants.h │ ├── Makefile │ ├── blackScholesAnalyticEngine.c │ ├── blackScholesAnalyticEngineKernels.c │ ├── blackScholesAnalyticEngineKernels.h │ ├── blackScholesAnalyticEngineStructs.h │ └── blackScholesEngineHmppOpenCL.c ├── OpenACC │ ├── ErrorFunctConstants.h │ ├── Makefile │ ├── blackScholesAnalyticEngine.c │ ├── blackScholesAnalyticEngineKernels.c │ ├── blackScholesAnalyticEngineKernels.h │ ├── blackScholesAnalyticEngineStructs.h │ └── blackScholesEngineHmppOpenACC.c ├── OpenCL │ ├── Makefile │ ├── blackScholesAnalyticEngine.c │ ├── blackScholesAnalyticEngineKernels.cl │ ├── blackScholesAnalyticEngineKernels.h │ ├── blackScholesAnalyticEngineKernelsCpu.c │ ├── blackScholesAnalyticEngineKernelsCpu.h │ ├── blackScholesAnalyticEngineStructs.h │ ├── blackScholesStructs.h │ └── errorFunctConsts.h └── OpenMP │ ├── Makefile │ ├── blackScholesAnalyticEngine.c │ ├── blackScholesAnalyticEngineKernelsCpu.c │ ├── blackScholesAnalyticEngineKernelsCpu.h │ ├── blackScholesAnalyticEngineStructs.h │ └── errorFunctConsts.h ├── Bonds ├── CPU │ ├── Makefile │ ├── bondsEngine.c │ ├── bondsKernelsCpu.c │ ├── bondsKernelsCpu.h │ └── bondsStructs.h ├── CUDA │ ├── Makefile │ ├── bondsEngine.cu │ ├── bondsKernelsCpu.cu │ ├── bondsKernelsCpu.cuh │ ├── bondsKernelsGpu.cu │ ├── bondsKernelsGpu.cuh │ └── bondsStructs.cuh ├── HMPP-CUDA │ ├── Makefile │ ├── bondsEngineHmppCuda.c │ ├── bondsKernelsCpu.c │ ├── bondsKernelsCpu.h │ └── bondsStructs.h ├── OpenACC │ ├── Makefile │ ├── bondsEngineOpenACC.c │ ├── bondsKernelsCpu.c │ ├── bondsKernelsCpu.h │ └── bondsStructs.h └── OpenMP │ ├── Makefile │ ├── bondsEngine.c │ ├── bondsKernelsCpu.c │ ├── bondsKernelsCpu.h │ └── bondsStructs.h ├── LICENSE ├── Monte-Carlo ├── CPU │ ├── Makefile │ ├── monteCarloConstants.h │ ├── monteCarloEngine.c │ ├── monteCarloKernelsCpu.c │ ├── monteCarloKernelsCpu.h │ └── monteCarloStructs.h ├── CUDA │ ├── Makefile │ ├── monteCarloConstants.cuh │ ├── monteCarloEngine.cu │ ├── monteCarloKernels.cu │ ├── monteCarloKernels.cuh │ ├── monteCarloKernelsCpu.cu │ ├── monteCarloKernelsCpu.cuh │ └── monteCarloStructs.cuh ├── HMPP-CUDA │ ├── Makefile │ ├── monteCarloConstants.h │ ├── monteCarloEngine.c │ ├── monteCarloEngineHmppCuda.c │ ├── monteCarloKernels.c │ ├── monteCarloKernels.h │ ├── monteCarloStructs.h │ ├── mt19937.c │ └── mt19937.h ├── HMPP-OpenCL │ ├── Makefile │ ├── monteCarloConstants.h │ ├── monteCarloEngine.c │ ├── monteCarloEngineHmppOpenCL.c │ ├── monteCarloKernels.c │ ├── monteCarloKernels.h │ ├── monteCarloStructs.h │ ├── mt19937.c │ └── mt19937.h ├── OpenACC │ ├── Makefile │ ├── monteCarloConstants.h │ ├── monteCarloEngine.c │ ├── monteCarloEngineHmppOpenACC.c │ ├── monteCarloEngineOpenACC.c │ ├── monteCarloKernels.c │ ├── monteCarloKernels.h │ ├── monteCarloStructs.h │ ├── mt19937.c │ └── mt19937.h ├── OpenCL │ ├── Makefile │ ├── monteCarloConstants.h │ ├── monteCarloEngine.c │ ├── monteCarloKernels.cl │ ├── monteCarloKernels.h │ ├── monteCarloKernelsCpu.c │ ├── monteCarloKernelsCpu.h │ ├── monteCarloStructs.h │ ├── mt19937.c │ └── mt19937.h └── OpenMP │ ├── Makefile │ ├── monteCarloConstants.h │ ├── monteCarloEngine.c │ ├── monteCarloKernelsCpu.c │ ├── monteCarloKernelsCpu.h │ └── monteCarloStructs.h ├── README-parameters.txt ├── README.md ├── Repo ├── CPU │ ├── Makefile │ ├── repoEngine.c │ ├── repoKernelsCpu.c │ ├── repoKernelsCpu.h │ └── repoStructs.h ├── CUDA │ ├── Makefile │ ├── repoEngine.cu │ ├── repoKernels.cu │ ├── repoKernels.cuh │ ├── repoKernelsCpu.cu │ ├── repoKernelsCpu.cuh │ └── repoStructs.cuh ├── HMPP-CUDA │ ├── Makefile │ ├── repoEngineHmppCuda.c │ ├── repoKernels.c │ ├── repoKernels.h │ └── repoStructs.h ├── OpenACC │ ├── Makefile │ ├── repoEngineOpenACC.c │ ├── repoKernels.c │ ├── repoKernels.h │ └── repoStructs.h └── OpenMP │ ├── Makefile │ ├── repoEngine.c │ ├── repoKernelsCpu.c │ ├── repoKernelsCpu.h │ └── repoStructs.h └── compileHmppOpenACC.mk /Black-Scholes/CPU/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | g++ -O3 blackScholesAnalyticEngine.c -o blackScholesEngine.exe 3 | clean: 4 | rm -f *~ *.exe 5 | -------------------------------------------------------------------------------- /Black-Scholes/CPU/blackScholesAnalyticEngineKernelsCpu.h: -------------------------------------------------------------------------------- 1 | //blackScholesAnalyticEngineKernelsCpu.cuh 2 | //Scott Grauer-Gray 3 | //Declarations of kernels for running black scholes using the analytic engine 4 | 5 | #ifndef BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_CPU_CUH 6 | #define BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_CPU_CUH 7 | 8 | 9 | //needed for the constants in the error function 10 | #include "errorFunctConsts.h" 11 | #include 12 | 13 | 14 | //device kernel to retrieve the compound factor in interestRate 15 | float interestRateCompoundFactorCpu(float t, yieldTermStruct currYieldTermStruct); 16 | 17 | 18 | //device kernel to retrieve the discount factor in interestRate 19 | float interestRateDiscountFactorCpu(float t, yieldTermStruct currYieldTermStruct); 20 | 21 | 22 | //device function to get the variance of the black volatility function 23 | float getBlackVolBlackVarCpu(blackVolStruct volTS); 24 | 25 | 26 | //device function to get the discount on a dividend yield 27 | float getDiscountOnDividendYieldCpu(float yearFraction, yieldTermStruct dividendYieldTermStruct); 28 | 29 | 30 | //device function to get the discount on the risk free rate 31 | float getDiscountOnRiskFreeRateCpu(float yearFraction, yieldTermStruct riskFreeRateYieldTermStruct); 32 | 33 | 34 | //device kernel to run the error function 35 | float errorFunctCpu(normalDistStruct normDist, float x); 36 | 37 | 38 | //device kernel to run the operator function in cumulative normal distribution 39 | float cumNormDistOpCpu(normalDistStruct normDist, float z); 40 | 41 | 42 | //device kernel to run the gaussian function in the normal distribution 43 | float gaussianFunctNormDistCpu(normalDistStruct normDist, float x); 44 | 45 | 46 | //device kernel to retrieve the derivative in a cumulative normal distribution 47 | float cumNormDistDerivCpu(normalDistStruct normDist, float x); 48 | 49 | 50 | //device function to initialize the cumulative normal distribution structure 51 | void initCumNormDistCpu(normalDistStruct& currCumNormDist); 52 | 53 | 54 | //device function to initialize variable in the black calculator 55 | void initBlackCalcVarsCpu(blackCalcStruct& blackCalculator, payoffStruct payoff); 56 | 57 | 58 | //device function to initialize the black calculator 59 | void initBlackCalculatorCpu(blackCalcStruct& blackCalc, payoffStruct payoff, float forwardPrice, float stdDev, float riskFreeDiscount); 60 | 61 | 62 | //device function to retrieve the output resulting value 63 | float getResultValCpu(blackCalcStruct blackCalculator); 64 | 65 | 66 | //global function to retrieve the output value for an option 67 | void getOutValOptionCpu(optionInputStruct* options, float* outputVals, int optionNum, int numVals); 68 | 69 | #endif //BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_CPU_CUH 70 | -------------------------------------------------------------------------------- /Black-Scholes/CPU/blackScholesAnalyticEngineStructs.h: -------------------------------------------------------------------------------- 1 | //blackScholesAnalyticEngineStructs.cuh 2 | //Scott Grauer-Gray 3 | //Structs for running black scholes using the analytic engine (from quantlib) on the GPU 4 | 5 | #ifndef BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 6 | #define BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 7 | 8 | //define the total number of samples 9 | #define NUM_SAMPLES_BLACK_SCHOLES_ANALYTIC 200000 10 | 11 | //define the thread block size 12 | #define THREAD_BLOCK_SIZE 256 13 | 14 | typedef struct 15 | { 16 | int day; 17 | int month; 18 | int year; 19 | } dateStruct; 20 | 21 | 22 | typedef struct 23 | { 24 | int type; 25 | float strike; 26 | } payoffStruct; 27 | 28 | 29 | typedef struct 30 | { 31 | float typeExercise; 32 | float yearFractionTime; 33 | } exerciseStruct; 34 | 35 | typedef struct 36 | { 37 | float rate; 38 | float freq; 39 | int comp; 40 | } interestRateStruct; 41 | 42 | typedef struct 43 | { 44 | float timeYearFraction; 45 | float forward; 46 | float compounding; 47 | float frequency; 48 | float intRate; 49 | } yieldTermStruct; 50 | 51 | typedef struct 52 | { 53 | float timeYearFraction; 54 | float following; 55 | float volatility; 56 | } blackVolStruct; 57 | 58 | typedef struct 59 | { 60 | float x0; 61 | yieldTermStruct dividendTS; 62 | yieldTermStruct riskFreeTS; 63 | blackVolStruct blackVolTS; 64 | } blackScholesMertStruct; 65 | 66 | typedef struct 67 | { 68 | blackScholesMertStruct process; 69 | float tGrid; 70 | float xGrid; 71 | float dampingSteps; 72 | float schemeDesc; 73 | float localVol; 74 | } engineStruct; 75 | 76 | 77 | typedef struct 78 | { 79 | payoffStruct payoff; 80 | float yearFractionTime; 81 | blackScholesMertStruct pricingEngine; 82 | } optionStruct; 83 | 84 | typedef struct 85 | { 86 | float strike; 87 | float forward; 88 | float stdDev; 89 | float discount; 90 | float variance; 91 | float d1; 92 | float d2; 93 | float alpha; 94 | float beta; 95 | float DalphaDd1; 96 | float DbetaDd2; 97 | float n_d1; 98 | float cum_d1; 99 | float n_d2; 100 | float cum_d2; 101 | float x; 102 | float DxDs; 103 | float DxDstrike; 104 | } blackCalcStruct; 105 | 106 | typedef struct 107 | { 108 | float average; 109 | float sigma; 110 | float denominator; 111 | float derNormalizationFactor; 112 | float normalizationFactor; 113 | } normalDistStruct; 114 | 115 | //define into for each type of option 116 | #define CALL 0 117 | #define PUT 1 118 | 119 | typedef struct 120 | { 121 | int type; 122 | float strike; 123 | float spot; 124 | float q; 125 | float r; 126 | float t; 127 | float vol; 128 | float value; 129 | float tol; 130 | } optionInputStruct; 131 | 132 | #endif //BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 133 | -------------------------------------------------------------------------------- /Black-Scholes/CUDA/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | nvcc -O3 blackScholesAnalyticEngine.cu -o blackScholesEngine.exe 3 | clean: 4 | rm -f *~ *.exe 5 | -------------------------------------------------------------------------------- /Black-Scholes/CUDA/blackScholesAnalyticEngineKernels.cuh: -------------------------------------------------------------------------------- 1 | //blackScholesAnalyticEngineKernels.cuh 2 | //Scott Grauer-Gray 3 | //Kernels for running black scholes using the analytic engine 4 | 5 | #ifndef BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_CUH 6 | #define BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_CUH 7 | 8 | #include 9 | 10 | #include 11 | 12 | #include 13 | #include 14 | 15 | //needed for the structs used on the code 16 | #include "blackScholesAnalyticEngineStructs.cuh" 17 | 18 | //constants used in this code 19 | #define M_1_SQRTPI 0.564189583547756286948 20 | #define M_SQRT_2 0.7071067811865475244008443621048490392848359376887 21 | 22 | 23 | //device kernel to retrieve the compound factor in interestRate 24 | __device__ float interestRateCompoundFactor(float t, yieldTermStruct currYieldTermStruct); 25 | 26 | //device kernel to retrieve the discount factor in interestRate 27 | __device__ float interestRateDiscountFactor(float t, yieldTermStruct currYieldTermStruct); 28 | 29 | 30 | //device function to get the variance of the black volatility function 31 | __device__ float getBlackVolBlackVar(blackVolStruct volTS); 32 | 33 | 34 | //device function to get the discount on a dividend yield 35 | __device__ float getDiscountOnDividendYield(float yearFraction, yieldTermStruct dividendYieldTermStruct); 36 | 37 | 38 | //device function to get the discount on the risk free rate 39 | __device__ float getDiscountOnRiskFreeRate(float yearFraction, yieldTermStruct riskFreeRateYieldTermStruct); 40 | 41 | //device kernel to run the error function 42 | __device__ float errorFunct(normalDistStruct normDist, float x); 43 | 44 | 45 | //device kernel to run the operator function in cumulative normal distribution 46 | __device__ float cumNormDistOp(normalDistStruct normDist, float z); 47 | 48 | 49 | //device kernel to run the gaussian function in the normal distribution 50 | __device__ float gaussianFunctNormDist(normalDistStruct normDist, float x); 51 | 52 | //device kernel to retrieve the derivative in a cumulative normal distribution 53 | __device__ float cumNormDistDeriv(normalDistStruct normDist, float x); 54 | 55 | //device function to initialize the cumulative normal distribution structure 56 | __device__ void initCumNormDist(normalDistStruct* currCumNormDist); 57 | 58 | 59 | //device function to initialize variable in the black calculator 60 | __device__ void initBlackCalcVars(blackCalcStruct* blackCalculator, payoffStruct payoff); 61 | 62 | 63 | //device function to initialize the black calculator 64 | __device__ void initBlackCalculator(blackCalcStruct* blackCalc, payoffStruct payoff, float forwardPrice, float stdDev, float riskFreeDiscount); 65 | 66 | 67 | //device function to retrieve the output resulting value 68 | __device__ float getResultVal(blackCalcStruct* blackCalculator); 69 | 70 | 71 | //global function to retrieve the output value for an option 72 | __global__ void getOutValOption(optionInputStruct* options, float* outputVals, int numVals); 73 | 74 | #endif //BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_CUH 75 | -------------------------------------------------------------------------------- /Black-Scholes/CUDA/blackScholesAnalyticEngineKernelsCpu.cuh: -------------------------------------------------------------------------------- 1 | //blackScholesAnalyticEngineKernelsCpu.cuh 2 | //Scott Grauer-Gray 3 | //Declarations of kernels for running black scholes using the analytic engine 4 | 5 | #ifndef BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_CPU_CUH 6 | #define BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_CPU_CUH 7 | 8 | 9 | //needed for the constants in the error function 10 | #include "errorFunctConsts.cuh" 11 | 12 | 13 | //device kernel to retrieve the compound factor in interestRate 14 | float interestRateCompoundFactorCpu(float t, yieldTermStruct currYieldTermStruct); 15 | 16 | 17 | //device kernel to retrieve the discount factor in interestRate 18 | float interestRateDiscountFactorCpu(float t, yieldTermStruct currYieldTermStruct); 19 | 20 | 21 | //device function to get the variance of the black volatility function 22 | float getBlackVolBlackVarCpu(blackVolStruct volTS); 23 | 24 | 25 | //device function to get the discount on a dividend yield 26 | float getDiscountOnDividendYieldCpu(float yearFraction, yieldTermStruct dividendYieldTermStruct); 27 | 28 | 29 | //device function to get the discount on the risk free rate 30 | float getDiscountOnRiskFreeRateCpu(float yearFraction, yieldTermStruct riskFreeRateYieldTermStruct); 31 | 32 | 33 | //device kernel to run the error function 34 | float errorFunctCpu(normalDistStruct normDist, float x); 35 | 36 | 37 | //device kernel to run the operator function in cumulative normal distribution 38 | float cumNormDistOpCpu(normalDistStruct normDist, float z); 39 | 40 | 41 | //device kernel to run the gaussian function in the normal distribution 42 | float gaussianFunctNormDistCpu(normalDistStruct normDist, float x); 43 | 44 | 45 | //device kernel to retrieve the derivative in a cumulative normal distribution 46 | float cumNormDistDerivCpu(normalDistStruct normDist, float x); 47 | 48 | 49 | //device function to initialize the cumulative normal distribution structure 50 | void initCumNormDistCpu(normalDistStruct& currCumNormDist); 51 | 52 | 53 | //device function to initialize variable in the black calculator 54 | void initBlackCalcVarsCpu(blackCalcStruct& blackCalculator, payoffStruct payoff); 55 | 56 | 57 | //device function to initialize the black calculator 58 | void initBlackCalculatorCpu(blackCalcStruct& blackCalc, payoffStruct payoff, float forwardPrice, float stdDev, float riskFreeDiscount); 59 | 60 | 61 | //device function to retrieve the output resulting value 62 | float getResultValCpu(blackCalcStruct blackCalculator); 63 | 64 | 65 | //global function to retrieve the output value for an option 66 | void getOutValOptionCpu(optionInputStruct* options, float* outputVals, int numVals); 67 | 68 | #endif //BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_CPU_CUH 69 | -------------------------------------------------------------------------------- /Black-Scholes/CUDA/blackScholesAnalyticEngineStructs.cuh: -------------------------------------------------------------------------------- 1 | //blackScholesAnalyticEngineStructs.cuh 2 | //Scott Grauer-Gray 3 | //Structs for running black scholes using the analytic engine (from quantlib) on the GPU 4 | 5 | #ifndef BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 6 | #define BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 7 | 8 | //define the total number of samples 9 | #define NUM_SAMPLES_BLACK_SCHOLES_ANALYTIC 200000 10 | 11 | //define the thread block size 12 | #define THREAD_BLOCK_SIZE 256 13 | 14 | typedef struct 15 | { 16 | int day; 17 | int month; 18 | int year; 19 | } dateStruct; 20 | 21 | 22 | typedef struct 23 | { 24 | int type; 25 | float strike; 26 | } payoffStruct; 27 | 28 | 29 | typedef struct 30 | { 31 | float typeExercise; 32 | float yearFractionTime; 33 | } exerciseStruct; 34 | 35 | typedef struct 36 | { 37 | float rate; 38 | float freq; 39 | int comp; 40 | } interestRateStruct; 41 | 42 | typedef struct 43 | { 44 | float timeYearFraction; 45 | float forward; 46 | float compounding; 47 | float frequency; 48 | float intRate; 49 | } yieldTermStruct; 50 | 51 | typedef struct 52 | { 53 | float timeYearFraction; 54 | float following; 55 | float volatility; 56 | } blackVolStruct; 57 | 58 | typedef struct 59 | { 60 | float x0; 61 | yieldTermStruct dividendTS; 62 | yieldTermStruct riskFreeTS; 63 | blackVolStruct blackVolTS; 64 | } blackScholesMertStruct; 65 | 66 | typedef struct 67 | { 68 | blackScholesMertStruct process; 69 | float tGrid; 70 | float xGrid; 71 | float dampingSteps; 72 | float schemeDesc; 73 | float localVol; 74 | } engineStruct; 75 | 76 | 77 | typedef struct 78 | { 79 | payoffStruct payoff; 80 | float yearFractionTime; 81 | blackScholesMertStruct pricingEngine; 82 | } optionStruct; 83 | 84 | typedef struct 85 | { 86 | float strike; 87 | float forward; 88 | float stdDev; 89 | float discount; 90 | float variance; 91 | float d1; 92 | float d2; 93 | float alpha; 94 | float beta; 95 | float DalphaDd1; 96 | float DbetaDd2; 97 | float n_d1; 98 | float cum_d1; 99 | float n_d2; 100 | float cum_d2; 101 | float x; 102 | float DxDs; 103 | float DxDstrike; 104 | } blackCalcStruct; 105 | 106 | typedef struct 107 | { 108 | float average; 109 | float sigma; 110 | float denominator; 111 | float derNormalizationFactor; 112 | float normalizationFactor; 113 | } normalDistStruct; 114 | 115 | //define into for each type of option 116 | #define CALL 0 117 | #define PUT 1 118 | 119 | typedef struct 120 | { 121 | int type; 122 | float strike; 123 | float spot; 124 | float q; 125 | float r; 126 | float t; 127 | float vol; 128 | float value; 129 | float tol; 130 | } optionInputStruct; 131 | 132 | #endif //BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 133 | -------------------------------------------------------------------------------- /Black-Scholes/HMPP-CUDA/Makefile: -------------------------------------------------------------------------------- 1 | EXE_NAME=blackScholesAnalyticEngine 2 | MAIN_FILE=blackScholesEngineHmppCuda.c 3 | 4 | include ../../compileHmppOpenACC.mk 5 | -------------------------------------------------------------------------------- /Black-Scholes/HMPP-CUDA/blackScholesAnalyticEngineKernels.h: -------------------------------------------------------------------------------- 1 | //blackScholesAnalyticEngineKernelsCpu.cuh 2 | //Scott Grauer-Gray 3 | //Declarations of kernels for running black scholes using the analytic engine 4 | 5 | #ifndef BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_H 6 | #define BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_H 7 | 8 | //needed for structures used in program 9 | #include "blackScholesAnalyticEngineStructs.h" 10 | 11 | //needed for the constants in the error function 12 | #include "ErrorFunctConstants.h" 13 | 14 | //device kernel to retrieve the compound factor in interestRate 15 | float 16 | interestRateCompoundFactor (float t, const yieldTermStruct currYieldTermStruct); 17 | 18 | //device kernel to retrieve the discount factor in interestRate 19 | float 20 | interestRateDiscountFactor (float t, const yieldTermStruct currYieldTermStruct); 21 | 22 | //device function to get the variance of the black volatility function 23 | float 24 | getBlackVolBlackVar (const blackVolStruct volTS); 25 | 26 | //device function to get the discount on a dividend yield 27 | float 28 | getDiscountOnDividendYield (float yearFraction, const yieldTermStruct dividendYieldTermStruct); 29 | 30 | //device function to get the discount on the risk free rate 31 | float 32 | getDiscountOnRiskFreeRate (float yearFraction, const yieldTermStruct riskFreeRateYieldTermStruct); 33 | 34 | //device kernel to run the error function 35 | float 36 | errorFunct (const normalDistStruct normDist, float x); 37 | 38 | //device kernel to run the operator function in cumulative normal distribution 39 | float 40 | cumNormDistOp (const normalDistStruct normDist, float z); 41 | 42 | //device kernel to run the gaussian function in the normal distribution 43 | float 44 | gaussianFunctNormDist (const normalDistStruct normDist, float x); 45 | 46 | //device kernel to retrieve the derivative in a cumulative normal distribution 47 | float 48 | cumNormDistDeriv (const normalDistStruct normDist, float x); 49 | 50 | //device function to initialize the cumulative normal distribution structure 51 | void 52 | initCumNormDist (normalDistStruct* currCumNormDist); 53 | 54 | //device function to initialize variable in the black calculator 55 | void 56 | initBlackCalcVars (blackCalcStruct* blackCalculator, payoffStruct payoff); 57 | 58 | //device function to initialize the black calculator 59 | void 60 | initBlackCalculator (blackCalcStruct* blackCalc, payoffStruct payoff, float forwardPrice, float stdDev, float riskFreeDiscount); 61 | 62 | //device function to retrieve the output resulting value 63 | float 64 | getResultVal (const blackCalcStruct* blackCalculator); 65 | 66 | //global function to retrieve the output value for an option 67 | void 68 | getOutValOption (optionInputStruct* options, float* outputVals, int optionNum); 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /Black-Scholes/HMPP-CUDA/blackScholesAnalyticEngineStructs.h: -------------------------------------------------------------------------------- 1 | //blackScholesAnalyticEngineStructs.cuh 2 | //Scott Grauer-Gray 3 | //Structs for running black scholes using the analytic engine (from quantlib) on the GPU 4 | 5 | #ifndef BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 6 | #define BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 7 | 8 | //define into for each type of option 9 | #define CALL 0 10 | #define PUT 1 11 | 12 | //define the total number of samples 13 | #define NUM_SAMPLES_BLACK_SCHOLES_ANALYTIC 5000000 14 | 15 | //define the thread block size 16 | #define THREAD_BLOCK_SIZE 256 17 | 18 | typedef struct 19 | { 20 | int day; 21 | int month; 22 | int year; 23 | } dateStruct; 24 | 25 | 26 | typedef struct 27 | { 28 | int type; 29 | float strike; 30 | } payoffStruct; 31 | 32 | 33 | typedef struct 34 | { 35 | float typeExercise; 36 | float yearFractionTime; 37 | } exerciseStruct; 38 | 39 | typedef struct 40 | { 41 | float rate; 42 | float freq; 43 | int comp; 44 | } interestRateStruct; 45 | 46 | typedef struct 47 | { 48 | float timeYearFraction; 49 | float forward; 50 | float compounding; 51 | float frequency; 52 | float intRate; 53 | } yieldTermStruct; 54 | 55 | typedef struct 56 | { 57 | float timeYearFraction; 58 | float following; 59 | float volatility; 60 | } blackVolStruct; 61 | 62 | typedef struct 63 | { 64 | float x0; 65 | yieldTermStruct dividendTS; 66 | yieldTermStruct riskFreeTS; 67 | blackVolStruct blackVolTS; 68 | } blackScholesMertStruct; 69 | 70 | typedef struct 71 | { 72 | blackScholesMertStruct process; 73 | float tGrid; 74 | float xGrid; 75 | float dampingSteps; 76 | float schemeDesc; 77 | float localVol; 78 | } engineStruct; 79 | 80 | 81 | typedef struct 82 | { 83 | payoffStruct payoff; 84 | float yearFractionTime; 85 | blackScholesMertStruct pricingEngine; 86 | } optionStruct; 87 | 88 | typedef struct 89 | { 90 | float strike; 91 | float forward; 92 | float stdDev; 93 | float discount; 94 | float variance; 95 | float d1; 96 | float d2; 97 | float alpha; 98 | float beta; 99 | float DalphaDd1; 100 | float DbetaDd2; 101 | float n_d1; 102 | float cum_d1; 103 | float n_d2; 104 | float cum_d2; 105 | float x; 106 | float DxDs; 107 | float DxDstrike; 108 | } blackCalcStruct; 109 | 110 | typedef struct 111 | { 112 | float average; 113 | float sigma; 114 | float denominator; 115 | float derNormalizationFactor; 116 | float normalizationFactor; 117 | } normalDistStruct; 118 | 119 | typedef struct 120 | { 121 | int type; 122 | float strike; 123 | float spot; 124 | float q; 125 | float r; 126 | float t; 127 | float vol; 128 | float value; 129 | float tol; 130 | } optionInputStruct; 131 | 132 | #endif //BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 133 | -------------------------------------------------------------------------------- /Black-Scholes/HMPP-CUDA/blackScholesEngineHmppCuda.c: -------------------------------------------------------------------------------- 1 | //blackScholesEngineHmppCuda.c 2 | //Scott Grauer-Gray 3 | //Main function to call computation for black-scholes using HMPP 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "blackScholesAnalyticEngine.c" 11 | 12 | #define THREAD_BLOCK_SIZE 256 13 | #define NUM_SAMPLES 439058 14 | 15 | int 16 | main (int argc, char** argv) 17 | { 18 | runBlackScholesAnalyticEngine (); 19 | } 20 | -------------------------------------------------------------------------------- /Black-Scholes/HMPP-OpenCL/Makefile: -------------------------------------------------------------------------------- 1 | EXE_NAME=blackScholesAnalyticEngine 2 | MAIN_FILE=blackScholesEngineHmppOpenCL.c 3 | 4 | include ../../compileHmppOpenACC.mk 5 | -------------------------------------------------------------------------------- /Black-Scholes/HMPP-OpenCL/blackScholesAnalyticEngineKernels.h: -------------------------------------------------------------------------------- 1 | //blackScholesAnalyticEngineKernels.h 2 | //Scott Grauer-Gray 3 | //Declarations of kernels for running black scholes using the analytic engine 4 | 5 | #ifndef BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_H 6 | #define BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_H 7 | 8 | //needed for the constants in the error function 9 | #include "ErrorFunctConstants.h" 10 | 11 | //device kernel to retrieve the compound factor in interestRate 12 | float 13 | interestRateCompoundFactor (float t, const yieldTermStruct currYieldTermStruct); 14 | 15 | //device kernel to retrieve the discount factor in interestRate 16 | float 17 | interestRateDiscountFactor (float t, const yieldTermStruct currYieldTermStruct); 18 | 19 | //device function to get the variance of the black volatility function 20 | float 21 | getBlackVolBlackVar (const blackVolStruct volTS); 22 | 23 | //device function to get the discount on a dividend yield 24 | float 25 | getDiscountOnDividendYield (float yearFraction, const yieldTermStruct dividendYieldTermStruct); 26 | 27 | //device function to get the discount on the risk free rate 28 | float 29 | getDiscountOnRiskFreeRate (float yearFraction, const yieldTermStruct riskFreeRateYieldTermStruct); 30 | 31 | //device kernel to run the error function 32 | float 33 | errorFunct (const normalDistStruct normDist, float x); 34 | 35 | //device kernel to run the operator function in cumulative normal distribution 36 | float 37 | cumNormDistOp (const normalDistStruct normDist, float z); 38 | 39 | //device kernel to run the gaussian function in the normal distribution 40 | float 41 | gaussianFunctNormDist (const normalDistStruct normDist, float x); 42 | 43 | //device kernel to retrieve the derivative in a cumulative normal distribution 44 | float 45 | cumNormDistDeriv (const normalDistStruct normDist, float x); 46 | 47 | //device function to initialize the cumulative normal distribution structure 48 | void 49 | initCumNormDist (normalDistStruct* currCumNormDist); 50 | 51 | //device function to initialize variable in the black calculator 52 | void 53 | initBlackCalcVars (blackCalcStruct* blackCalculator, payoffStruct payoff); 54 | 55 | //device function to initialize the black calculator 56 | void 57 | initBlackCalculator (blackCalcStruct* blackCalc, payoffStruct payoff, float forwardPrice, float stdDev, float riskFreeDiscount); 58 | 59 | //device function to retrieve the output resulting value 60 | float 61 | getResultVal (const blackCalcStruct blackCalculator); 62 | 63 | //global function to retrieve the output value for an option 64 | void 65 | getOutValOption (optionInputStruct* options, float* outputVals, int optionNum); 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /Black-Scholes/HMPP-OpenCL/blackScholesAnalyticEngineStructs.h: -------------------------------------------------------------------------------- 1 | //blackScholesAnalyticEngineStructs.cuh 2 | //Scott Grauer-Gray 3 | //Structs for running black scholes using the analytic engine (from quantlib) on the GPU 4 | 5 | #ifndef BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 6 | #define BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 7 | 8 | //define into for each type of option 9 | #define CALL 0 10 | #define PUT 1 11 | 12 | //define the total number of samples 13 | #define NUM_SAMPLES_BLACK_SCHOLES_ANALYTIC 5000000 14 | 15 | //define the thread block size 16 | #define THREAD_BLOCK_SIZE 256 17 | 18 | typedef struct 19 | { 20 | int day; 21 | int month; 22 | int year; 23 | } dateStruct; 24 | 25 | 26 | typedef struct 27 | { 28 | int type; 29 | float strike; 30 | } payoffStruct; 31 | 32 | 33 | typedef struct 34 | { 35 | float typeExercise; 36 | float yearFractionTime; 37 | } exerciseStruct; 38 | 39 | typedef struct 40 | { 41 | float rate; 42 | float freq; 43 | int comp; 44 | } interestRateStruct; 45 | 46 | typedef struct 47 | { 48 | float timeYearFraction; 49 | float forward; 50 | float compounding; 51 | float frequency; 52 | float intRate; 53 | } yieldTermStruct; 54 | 55 | typedef struct 56 | { 57 | float timeYearFraction; 58 | float following; 59 | float volatility; 60 | } blackVolStruct; 61 | 62 | typedef struct 63 | { 64 | float x0; 65 | yieldTermStruct dividendTS; 66 | yieldTermStruct riskFreeTS; 67 | blackVolStruct blackVolTS; 68 | } blackScholesMertStruct; 69 | 70 | typedef struct 71 | { 72 | blackScholesMertStruct process; 73 | float tGrid; 74 | float xGrid; 75 | float dampingSteps; 76 | float schemeDesc; 77 | float localVol; 78 | } engineStruct; 79 | 80 | 81 | typedef struct 82 | { 83 | payoffStruct payoff; 84 | float yearFractionTime; 85 | blackScholesMertStruct pricingEngine; 86 | } optionStruct; 87 | 88 | typedef struct 89 | { 90 | float strike; 91 | float forward; 92 | float stdDev; 93 | float discount; 94 | float variance; 95 | float d1; 96 | float d2; 97 | float alpha; 98 | float beta; 99 | float DalphaDd1; 100 | float DbetaDd2; 101 | float n_d1; 102 | float cum_d1; 103 | float n_d2; 104 | float cum_d2; 105 | float x; 106 | float DxDs; 107 | float DxDstrike; 108 | } blackCalcStruct; 109 | 110 | typedef struct 111 | { 112 | float average; 113 | float sigma; 114 | float denominator; 115 | float derNormalizationFactor; 116 | float normalizationFactor; 117 | } normalDistStruct; 118 | 119 | typedef struct 120 | { 121 | int type; 122 | float strike; 123 | float spot; 124 | float q; 125 | float r; 126 | float t; 127 | float vol; 128 | float value; 129 | float tol; 130 | } optionInputStruct; 131 | 132 | #endif //BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 133 | -------------------------------------------------------------------------------- /Black-Scholes/HMPP-OpenCL/blackScholesEngineHmppOpenCL.c: -------------------------------------------------------------------------------- 1 | //blackScholesEngineHmppOpenCL.c 2 | //Scott Grauer-Gray 3 | //Main function to call computation for black-scholes using HMPP 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "blackScholesAnalyticEngine.c" 11 | 12 | #define THREAD_BLOCK_SIZE 256 13 | #define NUM_SAMPLES 439058 14 | 15 | int 16 | main (int argc, char** argv) 17 | { 18 | runBlackScholesAnalyticEngine (); 19 | } 20 | -------------------------------------------------------------------------------- /Black-Scholes/OpenACC/Makefile: -------------------------------------------------------------------------------- 1 | EXE_NAME=blackScholesAnalyticEngine 2 | MAIN_FILE=blackScholesEngineHmppOpenACC.c 3 | 4 | include ../../compileHmppOpenACC.mk 5 | -------------------------------------------------------------------------------- /Black-Scholes/OpenACC/blackScholesAnalyticEngineKernels.h: -------------------------------------------------------------------------------- 1 | //blackScholesAnalyticEngineKernelsCpu.cuh 2 | //Scott Grauer-Gray 3 | //Declarations of kernels for running black scholes using the analytic engine 4 | 5 | #ifndef BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_H 6 | #define BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_H 7 | 8 | //needed for the constants in the error function 9 | #include "ErrorFunctConstants.h" 10 | 11 | //device kernel to retrieve the compound factor in interestRate 12 | float 13 | interestRateCompoundFactor (float t, const yieldTermStruct currYieldTermStruct); 14 | 15 | //device kernel to retrieve the discount factor in interestRate 16 | float 17 | interestRateDiscountFactor (float t, const yieldTermStruct currYieldTermStruct); 18 | 19 | //device function to get the variance of the black volatility function 20 | float 21 | getBlackVolBlackVar (const blackVolStruct volTS); 22 | 23 | //device function to get the discount on a dividend yield 24 | float 25 | getDiscountOnDividendYield (float yearFraction, const yieldTermStruct dividendYieldTermStruct); 26 | 27 | //device function to get the discount on the risk free rate 28 | float 29 | getDiscountOnRiskFreeRate (float yearFraction, const yieldTermStruct riskFreeRateYieldTermStruct); 30 | 31 | //device kernel to run the error function 32 | float 33 | errorFunct (const normalDistStruct normDist, float x); 34 | 35 | //device kernel to run the operator function in cumulative normal distribution 36 | float 37 | cumNormDistOp (const normalDistStruct normDist, float z); 38 | 39 | //device kernel to run the gaussian function in the normal distribution 40 | float 41 | gaussianFunctNormDist (const normalDistStruct normDist, float x); 42 | 43 | //device kernel to retrieve the derivative in a cumulative normal distribution 44 | float 45 | cumNormDistDeriv (const normalDistStruct normDist, float x); 46 | 47 | //device function to initialize the cumulative normal distribution structure 48 | void 49 | initCumNormDist (normalDistStruct* currCumNormDist); 50 | 51 | //device function to initialize variable in the black calculator 52 | void 53 | initBlackCalcVars (blackCalcStruct* blackCalculator, payoffStruct payoff); 54 | 55 | //device function to initialize the black calculator 56 | blackCalcStruct 57 | initBlackCalculator (payoffStruct payoff, float forwardPrice, float stdDev, float riskFreeDiscount); 58 | 59 | //device function to retrieve the output resulting value 60 | float 61 | getResultVal (blackCalcStruct blackCalculator); 62 | 63 | //global function to retrieve the output value for an option 64 | void 65 | getOutValOption (optionInputStruct* options, float* outputVals, int optionNum); 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /Black-Scholes/OpenACC/blackScholesAnalyticEngineStructs.h: -------------------------------------------------------------------------------- 1 | //blackScholesAnalyticEngineStructs.cuh 2 | //Scott Grauer-Gray 3 | //Structs for running black scholes using the analytic engine (from quantlib) on the GPU 4 | 5 | #ifndef BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 6 | #define BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 7 | 8 | //define into for each type of option 9 | #define CALL 0 10 | #define PUT 1 11 | 12 | //define the total number of samples 13 | #define NUM_SAMPLES_BLACK_SCHOLES_ANALYTIC 5000000 14 | 15 | //define the thread block size 16 | #define THREAD_BLOCK_SIZE 256 17 | 18 | typedef struct 19 | { 20 | int day; 21 | int month; 22 | int year; 23 | } dateStruct; 24 | 25 | 26 | typedef struct 27 | { 28 | int type; 29 | float strike; 30 | } payoffStruct; 31 | 32 | 33 | typedef struct 34 | { 35 | float typeExercise; 36 | float yearFractionTime; 37 | } exerciseStruct; 38 | 39 | typedef struct 40 | { 41 | float rate; 42 | float freq; 43 | int comp; 44 | } interestRateStruct; 45 | 46 | typedef struct 47 | { 48 | float timeYearFraction; 49 | float forward; 50 | float compounding; 51 | float frequency; 52 | float intRate; 53 | } yieldTermStruct; 54 | 55 | typedef struct 56 | { 57 | float timeYearFraction; 58 | float following; 59 | float volatility; 60 | } blackVolStruct; 61 | 62 | typedef struct 63 | { 64 | float x0; 65 | yieldTermStruct dividendTS; 66 | yieldTermStruct riskFreeTS; 67 | blackVolStruct blackVolTS; 68 | } blackScholesMertStruct; 69 | 70 | typedef struct 71 | { 72 | blackScholesMertStruct process; 73 | float tGrid; 74 | float xGrid; 75 | float dampingSteps; 76 | float schemeDesc; 77 | float localVol; 78 | } engineStruct; 79 | 80 | 81 | typedef struct 82 | { 83 | payoffStruct payoff; 84 | float yearFractionTime; 85 | blackScholesMertStruct pricingEngine; 86 | } optionStruct; 87 | 88 | typedef struct 89 | { 90 | float strike; 91 | float forward; 92 | float stdDev; 93 | float discount; 94 | float variance; 95 | float d1; 96 | float d2; 97 | float alpha; 98 | float beta; 99 | float DalphaDd1; 100 | float DbetaDd2; 101 | float n_d1; 102 | float cum_d1; 103 | float n_d2; 104 | float cum_d2; 105 | float x; 106 | float DxDs; 107 | float DxDstrike; 108 | } blackCalcStruct; 109 | 110 | typedef struct 111 | { 112 | float average; 113 | float sigma; 114 | float denominator; 115 | float derNormalizationFactor; 116 | float normalizationFactor; 117 | } normalDistStruct; 118 | 119 | typedef struct 120 | { 121 | int type; 122 | float strike; 123 | float spot; 124 | float q; 125 | float r; 126 | float t; 127 | float vol; 128 | float value; 129 | float tol; 130 | } optionInputStruct; 131 | 132 | #endif //BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 133 | -------------------------------------------------------------------------------- /Black-Scholes/OpenACC/blackScholesEngineHmppOpenACC.c: -------------------------------------------------------------------------------- 1 | //blackScholesEngineHmppOpenACC.c 2 | //Scott Grauer-Gray 3 | //Main function to call computation for black-scholes using OpenACC 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "blackScholesAnalyticEngine.c" 12 | 13 | #define THREAD_BLOCK_SIZE 256 14 | #define NUM_SAMPLES 439058 15 | 16 | int 17 | main (int argc, char** argv) 18 | { 19 | runBlackScholesAnalyticEngine (); 20 | } 21 | -------------------------------------------------------------------------------- /Black-Scholes/OpenCL/Makefile: -------------------------------------------------------------------------------- 1 | INCLUDE=-I/usr/local/cuda-5.0/include 2 | #LIBPATH=-L${OpenCL_SDK}/OpenCL/common/lib -L${OpenCL_SDK}/shared/lib 3 | LIB=-lOpenCL -lm 4 | all: 5 | g++ -O3 ${INCLUDE} ${LIBPATH} ${LIB} blackScholesAnalyticEngine.c -o blackScholesAnalyticEngine.exe 6 | 7 | clean: 8 | rm -f *~ *.exe 9 | -------------------------------------------------------------------------------- /Black-Scholes/OpenCL/blackScholesAnalyticEngineKernels.h: -------------------------------------------------------------------------------- 1 | //blackScholesAnalyticEngineKernels.cuh 2 | //Scott Grauer-Gray 3 | //Kernels for running black scholes using the analytic engine 4 | 5 | #ifndef BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_CUH 6 | #define BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_CUH 7 | 8 | #include 9 | #include 10 | 11 | //needed for the structs used on the code 12 | #include "blackScholesAnalyticEngineStructs.h" 13 | 14 | 15 | 16 | //device kernel to retrieve the compound factor in interestRate 17 | dataType interestRateCompoundFactor(dataType t, yieldTermStruct currYieldTermStruct); 18 | 19 | 20 | //device kernel to retrieve the discount factor in interestRate 21 | dataType interestRateDiscountFactor(dataType t, yieldTermStruct currYieldTermStruct); 22 | 23 | 24 | 25 | //device function to get the variance of the black volatility function 26 | dataType getBlackVolBlackVar(blackVolStruct volTS); 27 | 28 | 29 | //device function to get the discount on a dividend yield 30 | dataType getDiscountOnDividendYield(dataType yearFraction, yieldTermStruct dividendYieldTermStruct); 31 | 32 | 33 | //device function to get the discount on the risk free rate 34 | dataType getDiscountOnRiskFreeRate(dataType yearFraction, yieldTermStruct riskFreeRateYieldTermStruct); 35 | 36 | 37 | //device kernel to run the error function 38 | dataType errorFunct(normalDistStruct normDist, dataType x); 39 | 40 | 41 | //device kernel to run the operator function in cumulative normal distribution 42 | dataType cumNormDistOp(normalDistStruct normDist, dataType z); 43 | 44 | 45 | //device kernel to run the gaussian function in the normal distribution 46 | dataType gaussianFunctNormDist(normalDistStruct normDist, dataType x); 47 | 48 | 49 | //device kernel to retrieve the derivative in a cumulative normal distribution 50 | dataType cumNormDistDeriv(normalDistStruct normDist, dataType x); 51 | 52 | 53 | //device function to initialize the cumulative normal distribution structure 54 | void initCumNormDist(normalDistStruct& currCumNormDist); 55 | 56 | 57 | //device function to initialize variable in the black calculator 58 | void initBlackCalcVars(blackCalcStruct& blackCalculator, payoffStruct payoff); 59 | 60 | 61 | //device function to initialize the black calculator 62 | void initBlackCalculator(blackCalcStruct& blackCalc, payoffStruct payoff, dataType forwardPrice, dataType stdDev, dataType riskFreeDiscount); 63 | 64 | 65 | //device function to retrieve the output resulting value 66 | dataType getResultVal(blackCalcStruct blackCalculator); 67 | 68 | 69 | //global function to retrieve the output value for an option 70 | __kernel void getOutValOption(__global optionInputStruct* options, __global dataType* outputVals, int numVals); 71 | 72 | #endif //BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_CUH 73 | -------------------------------------------------------------------------------- /Black-Scholes/OpenCL/blackScholesAnalyticEngineKernelsCpu.h: -------------------------------------------------------------------------------- 1 | //blackScholesAnalyticEngineKernelsCpu.cuh 2 | //Scott Grauer-Gray 3 | //Declarations of kernels for running black scholes using the analytic engine 4 | 5 | #ifndef BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_CPU_CUH 6 | #define BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_CPU_CUH 7 | 8 | #include "blackScholesAnalyticEngineStructs.h" 9 | 10 | //needed for the constants in the error function 11 | #include "errorFunctConsts.h" 12 | 13 | 14 | //device kernel to retrieve the compound factor in interestRate 15 | dataType interestRateCompoundFactorCpu(dataType t, yieldTermStruct currYieldTermStruct); 16 | 17 | 18 | //device kernel to retrieve the discount factor in interestRate 19 | dataType interestRateDiscountFactorCpu(dataType t, yieldTermStruct currYieldTermStruct); 20 | 21 | 22 | //device function to get the variance of the black volatility function 23 | dataType getBlackVolBlackVarCpu(blackVolStruct volTS); 24 | 25 | 26 | //device function to get the discount on a dividend yield 27 | dataType getDiscountOnDividendYieldCpu(dataType yearFraction, yieldTermStruct dividendYieldTermStruct); 28 | 29 | 30 | //device function to get the discount on the risk free rate 31 | dataType getDiscountOnRiskFreeRateCpu(dataType yearFraction, yieldTermStruct riskFreeRateYieldTermStruct); 32 | 33 | 34 | //device kernel to run the error function 35 | dataType errorFunctCpu(normalDistStruct normDist, dataType x); 36 | 37 | 38 | //device kernel to run the operator function in cumulative normal distribution 39 | dataType cumNormDistOpCpu(normalDistStruct normDist, dataType z); 40 | 41 | 42 | //device kernel to run the gaussian function in the normal distribution 43 | dataType gaussianFunctNormDistCpu(normalDistStruct normDist, dataType x); 44 | 45 | 46 | //device kernel to retrieve the derivative in a cumulative normal distribution 47 | dataType cumNormDistDerivCpu(normalDistStruct normDist, dataType x); 48 | 49 | 50 | //device function to initialize the cumulative normal distribution structure 51 | void initCumNormDistCpu(normalDistStruct& currCumNormDist); 52 | 53 | 54 | //device function to initialize variable in the black calculator 55 | void initBlackCalcVarsCpu(blackCalcStruct& blackCalculator, payoffStruct payoff); 56 | 57 | 58 | //device function to initialize the black calculator 59 | void initBlackCalculatorCpu(blackCalcStruct& blackCalc, payoffStruct payoff, dataType forwardPrice, dataType stdDev, dataType riskFreeDiscount); 60 | 61 | 62 | //device function to retrieve the output resulting value 63 | dataType getResultValCpu(blackCalcStruct blackCalculator); 64 | 65 | 66 | //global function to retrieve the output value for an option 67 | void getOutValOptionCpu(optionInputStruct* options, dataType* outputVals, int numVals); 68 | 69 | #endif //BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_CPU_CUH 70 | -------------------------------------------------------------------------------- /Black-Scholes/OpenCL/blackScholesAnalyticEngineStructs.h: -------------------------------------------------------------------------------- 1 | //blackScholesAnalyticEngineStructs.h 2 | //Scott Grauer-Gray 3 | //Structs for running black scholes using the analytic engine (from quantlib) on the GPU 4 | 5 | #ifndef BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 6 | #define BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 7 | 8 | //define the total number of samples 9 | #define NUM_SAMPLES_BLACK_SCHOLES_ANALYTIC 200000 10 | 11 | //define the thread block size 12 | #define THREAD_BLOCK_SIZE 256 13 | 14 | typedef float dataType; 15 | 16 | typedef struct 17 | { 18 | int day; 19 | int month; 20 | int year; 21 | } dateStruct; 22 | 23 | 24 | typedef struct 25 | { 26 | int type; 27 | dataType strike; 28 | } payoffStruct; 29 | 30 | 31 | typedef struct 32 | { 33 | dataType typeExercise; 34 | dataType yearFractionTime; 35 | } exerciseStruct; 36 | 37 | typedef struct 38 | { 39 | dataType rate; 40 | dataType freq; 41 | int comp; 42 | } interestRateStruct; 43 | 44 | typedef struct 45 | { 46 | dataType timeYearFraction; 47 | dataType forward; 48 | dataType compounding; 49 | dataType frequency; 50 | dataType intRate; 51 | } yieldTermStruct; 52 | 53 | typedef struct 54 | { 55 | dataType timeYearFraction; 56 | dataType following; 57 | dataType volatility; 58 | } blackVolStruct; 59 | 60 | typedef struct 61 | { 62 | dataType x0; 63 | yieldTermStruct dividendTS; 64 | yieldTermStruct riskFreeTS; 65 | blackVolStruct blackVolTS; 66 | } blackScholesMertStruct; 67 | 68 | typedef struct 69 | { 70 | blackScholesMertStruct process; 71 | dataType tGrid; 72 | dataType xGrid; 73 | dataType dampingSteps; 74 | dataType schemeDesc; 75 | dataType localVol; 76 | } engineStruct; 77 | 78 | 79 | typedef struct 80 | { 81 | payoffStruct payoff; 82 | dataType yearFractionTime; 83 | blackScholesMertStruct pricingEngine; 84 | } optionStruct; 85 | 86 | typedef struct 87 | { 88 | dataType strike; 89 | dataType forward; 90 | dataType stdDev; 91 | dataType discount; 92 | dataType variance; 93 | dataType d1; 94 | dataType d2; 95 | dataType alpha; 96 | dataType beta; 97 | dataType DalphaDd1; 98 | dataType DbetaDd2; 99 | dataType n_d1; 100 | dataType cum_d1; 101 | dataType n_d2; 102 | dataType cum_d2; 103 | dataType x; 104 | dataType DxDs; 105 | dataType DxDstrike; 106 | } blackCalcStruct; 107 | 108 | typedef struct 109 | { 110 | dataType average; 111 | dataType sigma; 112 | dataType denominator; 113 | dataType derNormalizationFactor; 114 | dataType normalizationFactor; 115 | } normalDistStruct; 116 | 117 | //define into for each type of option 118 | #define CALL 0 119 | #define PUT 1 120 | 121 | typedef struct 122 | { 123 | int type; 124 | dataType strike; 125 | dataType spot; 126 | dataType q; 127 | dataType r; 128 | dataType t; 129 | dataType vol; 130 | dataType value; 131 | dataType tol; 132 | } optionInputStruct; 133 | 134 | #endif //BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 135 | -------------------------------------------------------------------------------- /Black-Scholes/OpenCL/blackScholesStructs.h: -------------------------------------------------------------------------------- 1 | //blackScholesStructs.cuh 2 | //Scott Grauer-Gray 3 | //Structs for processing black scholes options 4 | 5 | #ifndef BLACK_SCHOLES_STRUCTS_CUH 6 | #define BLACK_SCHOLES_STRUCTS_CUH 7 | 8 | typedef float dataType; 9 | 10 | typedef struct 11 | { 12 | int day; 13 | int month; 14 | int year; 15 | } dateStruct; 16 | 17 | 18 | typedef struct 19 | { 20 | dataType type; 21 | dataType strike; 22 | } payoffStruct; 23 | 24 | 25 | typedef struct 26 | { 27 | dataType typeExercise; 28 | dataType date; 29 | } exerciseStruct; 30 | 31 | typedef struct 32 | { 33 | dataType rate; 34 | dataType freq; 35 | int comp; 36 | } interestRateStruct; 37 | 38 | typedef struct 39 | { 40 | dataType refDate; 41 | dataType calDate; 42 | dataType forward; 43 | dataType compounding; 44 | dataType frequency; 45 | interestRateStruct intRate; 46 | } yieldTermStruct; 47 | 48 | typedef struct 49 | { 50 | dataType refDate; 51 | dataType calDate; 52 | dataType following; 53 | dataType volatility; 54 | } blackVolStruct; 55 | 56 | typedef struct 57 | { 58 | dataType x0; 59 | yieldTermStruct dividendTS; 60 | yieldTermStruct riskFreeTS; 61 | blackVolStruct blackVolTS; 62 | } blackScholesMertStruct; 63 | 64 | typedef struct 65 | { 66 | blackScholesMertStruct process; 67 | dataType tGrid; 68 | dataType xGrid; 69 | dataType dampingSteps; 70 | dataType schemeDesc; 71 | dataType localVol; 72 | } engineStruct; 73 | 74 | 75 | typedef struct 76 | { 77 | payoffStruct payoff; 78 | exerciseStruct exercise; 79 | engineStruct pricingEngine; 80 | } optionStruct; 81 | 82 | typedef struct 83 | { 84 | int direction; 85 | dataType* lower; 86 | dataType* diag; 87 | dataType* upper; 88 | dataType* i0; 89 | dataType* i2; 90 | dataType* reverseIndex; 91 | } tripleBandLinearOpStruct; 92 | 93 | typedef struct 94 | { 95 | } solverStruct; 96 | 97 | #endif //BLACK_SCHOLES_STRUCTS_CUH 98 | -------------------------------------------------------------------------------- /Black-Scholes/OpenMP/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | g++ -O3 -march=native -fopenmp blackScholesAnalyticEngine.c -o blackScholesEngine.exe 3 | clean: 4 | rm -f *~ *.exe 5 | -------------------------------------------------------------------------------- /Black-Scholes/OpenMP/blackScholesAnalyticEngineKernelsCpu.h: -------------------------------------------------------------------------------- 1 | //blackScholesAnalyticEngineKernelsCpu.cuh 2 | //Scott Grauer-Gray 3 | //Declarations of kernels for running black scholes using the analytic engine 4 | 5 | #ifndef BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_CPU_CUH 6 | #define BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_CPU_CUH 7 | 8 | 9 | //needed for the constants in the error function 10 | #include "errorFunctConsts.h" 11 | #include 12 | #include 13 | 14 | 15 | //device kernel to retrieve the compound factor in interestRate 16 | float interestRateCompoundFactorCpu(float t, yieldTermStruct currYieldTermStruct); 17 | 18 | 19 | //device kernel to retrieve the discount factor in interestRate 20 | float interestRateDiscountFactorCpu(float t, yieldTermStruct currYieldTermStruct); 21 | 22 | 23 | //device function to get the variance of the black volatility function 24 | float getBlackVolBlackVarCpu(blackVolStruct volTS); 25 | 26 | 27 | //device function to get the discount on a dividend yield 28 | float getDiscountOnDividendYieldCpu(float yearFraction, yieldTermStruct dividendYieldTermStruct); 29 | 30 | 31 | //device function to get the discount on the risk free rate 32 | float getDiscountOnRiskFreeRateCpu(float yearFraction, yieldTermStruct riskFreeRateYieldTermStruct); 33 | 34 | 35 | //device kernel to run the error function 36 | float errorFunctCpu(normalDistStruct normDist, float x); 37 | 38 | 39 | //device kernel to run the operator function in cumulative normal distribution 40 | float cumNormDistOpCpu(normalDistStruct normDist, float z); 41 | 42 | 43 | //device kernel to run the gaussian function in the normal distribution 44 | float gaussianFunctNormDistCpu(normalDistStruct normDist, float x); 45 | 46 | 47 | //device kernel to retrieve the derivative in a cumulative normal distribution 48 | float cumNormDistDerivCpu(normalDistStruct normDist, float x); 49 | 50 | 51 | //device function to initialize the cumulative normal distribution structure 52 | void initCumNormDistCpu(normalDistStruct& currCumNormDist); 53 | 54 | 55 | //device function to initialize variable in the black calculator 56 | void initBlackCalcVarsCpu(blackCalcStruct& blackCalculator, payoffStruct payoff); 57 | 58 | 59 | //device function to initialize the black calculator 60 | void initBlackCalculatorCpu(blackCalcStruct& blackCalc, payoffStruct payoff, float forwardPrice, float stdDev, float riskFreeDiscount); 61 | 62 | 63 | //device function to retrieve the output resulting value 64 | float getResultValCpu(blackCalcStruct blackCalculator); 65 | 66 | 67 | //global function to retrieve the output value for an option 68 | void getOutValOptionCpu(optionInputStruct* options, float* outputVals, int numVals); 69 | 70 | #endif //BLACK_SCHOLES_ANALYTIC_ENGINE_KERNELS_CPU_CUH 71 | -------------------------------------------------------------------------------- /Black-Scholes/OpenMP/blackScholesAnalyticEngineStructs.h: -------------------------------------------------------------------------------- 1 | //blackScholesAnalyticEngineStructs.cuh 2 | //Scott Grauer-Gray 3 | //Structs for running black scholes using the analytic engine (from quantlib) on the GPU 4 | 5 | #ifndef BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 6 | #define BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 7 | 8 | //define the total number of samples 9 | #define NUM_SAMPLES_BLACK_SCHOLES_ANALYTIC 200000 10 | 11 | //define the thread block size 12 | #define THREAD_BLOCK_SIZE 256 13 | 14 | typedef struct 15 | { 16 | int day; 17 | int month; 18 | int year; 19 | } dateStruct; 20 | 21 | 22 | typedef struct 23 | { 24 | int type; 25 | float strike; 26 | } payoffStruct; 27 | 28 | 29 | typedef struct 30 | { 31 | float typeExercise; 32 | float yearFractionTime; 33 | } exerciseStruct; 34 | 35 | typedef struct 36 | { 37 | float rate; 38 | float freq; 39 | int comp; 40 | } interestRateStruct; 41 | 42 | typedef struct 43 | { 44 | float timeYearFraction; 45 | float forward; 46 | float compounding; 47 | float frequency; 48 | float intRate; 49 | } yieldTermStruct; 50 | 51 | typedef struct 52 | { 53 | float timeYearFraction; 54 | float following; 55 | float volatility; 56 | } blackVolStruct; 57 | 58 | typedef struct 59 | { 60 | float x0; 61 | yieldTermStruct dividendTS; 62 | yieldTermStruct riskFreeTS; 63 | blackVolStruct blackVolTS; 64 | } blackScholesMertStruct; 65 | 66 | typedef struct 67 | { 68 | blackScholesMertStruct process; 69 | float tGrid; 70 | float xGrid; 71 | float dampingSteps; 72 | float schemeDesc; 73 | float localVol; 74 | } engineStruct; 75 | 76 | 77 | typedef struct 78 | { 79 | payoffStruct payoff; 80 | float yearFractionTime; 81 | blackScholesMertStruct pricingEngine; 82 | } optionStruct; 83 | 84 | typedef struct 85 | { 86 | float strike; 87 | float forward; 88 | float stdDev; 89 | float discount; 90 | float variance; 91 | float d1; 92 | float d2; 93 | float alpha; 94 | float beta; 95 | float DalphaDd1; 96 | float DbetaDd2; 97 | float n_d1; 98 | float cum_d1; 99 | float n_d2; 100 | float cum_d2; 101 | float x; 102 | float DxDs; 103 | float DxDstrike; 104 | } blackCalcStruct; 105 | 106 | typedef struct 107 | { 108 | float average; 109 | float sigma; 110 | float denominator; 111 | float derNormalizationFactor; 112 | float normalizationFactor; 113 | } normalDistStruct; 114 | 115 | //define into for each type of option 116 | #define CALL 0 117 | #define PUT 1 118 | 119 | typedef struct 120 | { 121 | int type; 122 | float strike; 123 | float spot; 124 | float q; 125 | float r; 126 | float t; 127 | float vol; 128 | float value; 129 | float tol; 130 | } optionInputStruct; 131 | 132 | #endif //BLACK_SCHOLES_ANALYTIC_ENGINE_STRUCTS_CUH 133 | -------------------------------------------------------------------------------- /Bonds/CPU/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | g++ -O3 bondsEngine.c -o bondsEngine.exe 3 | clean: 4 | rm -f *~ *.exe 5 | -------------------------------------------------------------------------------- /Bonds/CPU/bondsStructs.h: -------------------------------------------------------------------------------- 1 | //bondsStructs.cuh 2 | //Scott Grauer-Gray 3 | //Structs for running the bonds application 4 | 5 | #ifndef BONDS_STRUCTS_CUH 6 | #define BONDS_STRUCTS_CUH 7 | 8 | typedef double dataType; 9 | 10 | #include 11 | #include 12 | 13 | #define SIMPLE_INTEREST 0 14 | #define COMPOUNDED_INTEREST 1 15 | #define CONTINUOUS_INTEREST 2 16 | #define SIMPLE_THEN_COMPOUNDED_INTEREST 3 17 | 18 | #define ANNUAL_FREQ 1 19 | #define SEMIANNUAL_FREQ 2 20 | 21 | #define USE_EXACT_DAY 0 22 | #define USE_SERIAL_NUMS 1 23 | 24 | #define QL_EPSILON_GPU 0.000000000000000001f 25 | 26 | #define COMPUTE_AMOUNT -1 27 | 28 | 29 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 30 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 31 | 32 | #define ACCURACY 1.0e-8 33 | 34 | 35 | typedef struct 36 | { 37 | int month; 38 | int day; 39 | int year; 40 | int dateSerialNum; 41 | } bondsDateStruct; 42 | 43 | 44 | typedef struct 45 | { 46 | bondsDateStruct startDate; 47 | bondsDateStruct maturityDate; 48 | float rate; 49 | } bondStruct; 50 | 51 | 52 | 53 | typedef struct 54 | { 55 | dataType rate; 56 | dataType freq; 57 | int comp; 58 | int dayCounter; 59 | } intRateStruct; 60 | 61 | 62 | typedef struct 63 | { 64 | dataType forward; 65 | dataType compounding; 66 | dataType frequency; 67 | intRateStruct intRate; 68 | bondsDateStruct refDate; 69 | bondsDateStruct calDate; 70 | int dayCounter; 71 | } bondsYieldTermStruct; 72 | 73 | 74 | typedef struct 75 | { 76 | bondsDateStruct paymentDate; 77 | bondsDateStruct accrualStartDate; 78 | bondsDateStruct accrualEndDate; 79 | dataType amount; 80 | } couponStruct; 81 | 82 | 83 | typedef struct 84 | { 85 | couponStruct* legs; 86 | intRateStruct intRate; 87 | int nominal; 88 | int dayCounter; 89 | } cashFlowsStruct; 90 | 91 | 92 | typedef struct 93 | { 94 | dataType* dirtyPrice; 95 | dataType* accruedAmountCurrDate; 96 | dataType* cleanPrice; 97 | dataType* bondForwardVal; 98 | } resultsStruct; 99 | 100 | 101 | typedef struct 102 | { 103 | bondsYieldTermStruct* discountCurve; 104 | bondsYieldTermStruct* repoCurve; 105 | bondsDateStruct* currDate; 106 | bondsDateStruct* maturityDate; 107 | dataType* bondCleanPrice; 108 | bondStruct* bond; 109 | dataType* dummyStrike; 110 | } inArgsStruct; 111 | 112 | 113 | typedef struct 114 | { 115 | dataType npv; 116 | int dayCounter; 117 | int comp; 118 | dataType freq; 119 | bool includecurrDateFlows; 120 | bondsDateStruct currDate; 121 | bondsDateStruct npvDate; 122 | 123 | } irrFinderStruct; 124 | 125 | 126 | typedef struct 127 | { 128 | dataType root_; 129 | dataType xMin_; 130 | dataType xMax_; 131 | dataType fxMin_; 132 | dataType fxMax_; 133 | int maxEvaluations_; 134 | int evaluationNumber_; 135 | dataType lowerBound_; 136 | dataType upperBound_; 137 | bool lowerBoundEnforced_; 138 | bool upperBoundEnforced_; 139 | } solverStruct; 140 | 141 | 142 | 143 | #endif //BONDS_STRUCTS_CUH 144 | -------------------------------------------------------------------------------- /Bonds/CUDA/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | nvcc -O3 bondsEngine.cu -o bondsEngine.exe 3 | clean: 4 | rm -f *~ *.exe 5 | -------------------------------------------------------------------------------- /Bonds/CUDA/bondsStructs.cuh: -------------------------------------------------------------------------------- 1 | //bondsStructs.cuh 2 | //Scott Grauer-Gray 3 | //Structs for running the bonds application 4 | 5 | #ifndef BONDS_STRUCTS_CUH 6 | #define BONDS_STRUCTS_CUH 7 | 8 | typedef double dataType; 9 | 10 | #include 11 | #include 12 | 13 | #define SIMPLE_INTEREST 0 14 | #define COMPOUNDED_INTEREST 1 15 | #define CONTINUOUS_INTEREST 2 16 | #define SIMPLE_THEN_COMPOUNDED_INTEREST 3 17 | 18 | #define ANNUAL_FREQ 1 19 | #define SEMIANNUAL_FREQ 2 20 | 21 | #define USE_EXACT_DAY 0 22 | #define USE_SERIAL_NUMS 1 23 | 24 | #define QL_EPSILON_GPU 0.000000000000000001f 25 | 26 | #define COMPUTE_AMOUNT -1 27 | 28 | 29 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 30 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 31 | 32 | #define ACCURACY 1.0e-8 33 | 34 | 35 | typedef struct 36 | { 37 | int month; 38 | int day; 39 | int year; 40 | int dateSerialNum; 41 | } bondsDateStruct; 42 | 43 | 44 | typedef struct 45 | { 46 | bondsDateStruct startDate; 47 | bondsDateStruct maturityDate; 48 | float rate; 49 | } bondStruct; 50 | 51 | 52 | 53 | typedef struct 54 | { 55 | dataType rate; 56 | dataType freq; 57 | int comp; 58 | int dayCounter; 59 | } intRateStruct; 60 | 61 | 62 | typedef struct 63 | { 64 | dataType forward; 65 | dataType compounding; 66 | dataType frequency; 67 | intRateStruct intRate; 68 | bondsDateStruct refDate; 69 | bondsDateStruct calDate; 70 | int dayCounter; 71 | } bondsYieldTermStruct; 72 | 73 | 74 | typedef struct 75 | { 76 | bondsDateStruct paymentDate; 77 | bondsDateStruct accrualStartDate; 78 | bondsDateStruct accrualEndDate; 79 | dataType amount; 80 | } couponStruct; 81 | 82 | 83 | typedef struct 84 | { 85 | couponStruct* legs; 86 | intRateStruct intRate; 87 | int nominal; 88 | int dayCounter; 89 | } cashFlowsStruct; 90 | 91 | 92 | typedef struct 93 | { 94 | dataType* dirtyPrice; 95 | dataType* accruedAmountCurrDate; 96 | dataType* cleanPrice; 97 | dataType* bondForwardVal; 98 | } resultsStruct; 99 | 100 | 101 | typedef struct 102 | { 103 | bondsYieldTermStruct* discountCurve; 104 | bondsYieldTermStruct* repoCurve; 105 | bondsDateStruct* currDate; 106 | bondsDateStruct* maturityDate; 107 | dataType* bondCleanPrice; 108 | bondStruct* bond; 109 | dataType* dummyStrike; 110 | } inArgsStruct; 111 | 112 | 113 | typedef struct 114 | { 115 | dataType npv; 116 | int dayCounter; 117 | int comp; 118 | dataType freq; 119 | bool includecurrDateFlows; 120 | bondsDateStruct currDate; 121 | bondsDateStruct npvDate; 122 | 123 | } irrFinderStruct; 124 | 125 | 126 | typedef struct 127 | { 128 | dataType root_; 129 | dataType xMin_; 130 | dataType xMax_; 131 | dataType fxMin_; 132 | dataType fxMax_; 133 | int maxEvaluations_; 134 | int evaluationNumber_; 135 | dataType lowerBound_; 136 | dataType upperBound_; 137 | bool lowerBoundEnforced_; 138 | bool upperBoundEnforced_; 139 | } solverStruct; 140 | 141 | 142 | 143 | #endif //BONDS_STRUCTS_CUH 144 | -------------------------------------------------------------------------------- /Bonds/HMPP-CUDA/Makefile: -------------------------------------------------------------------------------- 1 | EXE_NAME=bondsEngine 2 | MAIN_FILE=bondsEngineHmppCuda.c 3 | 4 | include ../../compileHmppOpenACC.mk 5 | -------------------------------------------------------------------------------- /Bonds/HMPP-CUDA/bondsStructs.h: -------------------------------------------------------------------------------- 1 | //bondsStructs.cuh 2 | //Scott Grauer-Gray 3 | //July 6, 2012 4 | //Structs for running the bonds engine 5 | 6 | #ifndef BONDS_STRUCTS_CUH 7 | #define BONDS_STRUCTS_CUH 8 | 9 | typedef double dataType; 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #define SIMPLE_INTEREST 0 16 | #define COMPOUNDED_INTEREST 1 17 | #define CONTINUOUS_INTEREST 2 18 | #define SIMPLE_THEN_COMPOUNDED_INTEREST 3 19 | 20 | #define ANNUAL_FREQ 1 21 | #define SEMIANNUAL_FREQ 2 22 | 23 | #define USE_EXACT_DAY 0 24 | #define USE_SERIAL_NUMS 1 25 | 26 | #define QL_EPSILON_GPU 0.000000000000000001f 27 | 28 | #define COMPUTE_AMOUNT -1 29 | 30 | 31 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 32 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 33 | 34 | #define ACCURACY 1.0e-8 35 | 36 | 37 | typedef struct 38 | { 39 | int month; 40 | int day; 41 | int year; 42 | int dateSerialNum; 43 | } bondsDateStruct; 44 | 45 | 46 | typedef struct 47 | { 48 | bondsDateStruct startDate; 49 | bondsDateStruct maturityDate; 50 | float rate; 51 | } bondStruct; 52 | 53 | 54 | 55 | 56 | typedef struct 57 | { 58 | dataType rate; 59 | dataType freq; 60 | int comp; 61 | int dayCounter; 62 | } intRateStruct; 63 | 64 | 65 | typedef struct 66 | { 67 | dataType forward; 68 | dataType compounding; 69 | dataType frequency; 70 | intRateStruct intRate; 71 | bondsDateStruct refDate; 72 | bondsDateStruct calDate; 73 | int dayCounter; 74 | } bondsYieldTermStruct; 75 | 76 | 77 | typedef struct 78 | { 79 | bondsDateStruct paymentDate; 80 | bondsDateStruct accrualStartDate; 81 | bondsDateStruct accrualEndDate; 82 | 83 | dataType amount; 84 | } couponStruct; 85 | 86 | 87 | typedef struct 88 | { 89 | couponStruct* legs; 90 | intRateStruct intRate; 91 | int nominal; 92 | int dayCounter; 93 | } cashFlowsStruct; 94 | 95 | 96 | typedef struct 97 | { 98 | dataType* dirtyPrice; 99 | dataType* accruedAmountCurrDate; 100 | dataType* cleanPrice; 101 | dataType* bondForwardVal; 102 | } resultsStruct; 103 | 104 | 105 | typedef struct 106 | { 107 | bondsYieldTermStruct* discountCurve; 108 | bondsYieldTermStruct* repoCurve; 109 | bondsDateStruct* currDate; 110 | bondsDateStruct* maturityDate; 111 | dataType* bondCleanPrice; 112 | bondStruct* bond; 113 | dataType* dummyStrike; 114 | } inArgsStruct; 115 | 116 | 117 | typedef struct 118 | { 119 | dataType npv; 120 | int dayCounter; 121 | int comp; 122 | dataType freq; 123 | bool includecurrDateFlows; 124 | bondsDateStruct currDate; 125 | bondsDateStruct npvDate; 126 | 127 | } irrFinderStruct; 128 | 129 | 130 | typedef struct 131 | { 132 | dataType root_; 133 | dataType xMin_; 134 | dataType xMax_; 135 | dataType fxMin_; 136 | dataType fxMax_; 137 | int maxEvaluations_; 138 | int evaluationNumber_; 139 | dataType lowerBound_; 140 | dataType upperBound_; 141 | bool lowerBoundEnforced_; 142 | bool upperBoundEnforced_; 143 | } solverStruct; 144 | 145 | 146 | 147 | #endif //REPO_STRUCTS_CUH 148 | -------------------------------------------------------------------------------- /Bonds/OpenACC/Makefile: -------------------------------------------------------------------------------- 1 | EXE_NAME=bondsOpenAcc 2 | MAIN_FILE=bondsEngineOpenACC.c 3 | 4 | include ../../compileHmppOpenACC.mk 5 | -------------------------------------------------------------------------------- /Bonds/OpenACC/bondsStructs.h: -------------------------------------------------------------------------------- 1 | //bondsStructs.cuh 2 | //Scott Grauer-Gray 3 | //July 6, 2012 4 | //Structs for running the bonds engine 5 | 6 | #ifndef BONDS_STRUCTS_CUH 7 | #define BONDS_STRUCTS_CUH 8 | 9 | typedef double dataType; 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #define SIMPLE_INTEREST 0 16 | #define COMPOUNDED_INTEREST 1 17 | #define CONTINUOUS_INTEREST 2 18 | #define SIMPLE_THEN_COMPOUNDED_INTEREST 3 19 | 20 | #define ANNUAL_FREQ 1 21 | #define SEMIANNUAL_FREQ 2 22 | 23 | #define USE_EXACT_DAY 0 24 | #define USE_SERIAL_NUMS 1 25 | 26 | #define QL_EPSILON_GPU 0.000000000000000001f 27 | 28 | #define COMPUTE_AMOUNT -1 29 | 30 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 31 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 32 | 33 | #define ACCURACY 1.0e-8 34 | 35 | 36 | typedef struct 37 | { 38 | int month; 39 | int day; 40 | int year; 41 | int dateSerialNum; 42 | } bondsDateStruct; 43 | 44 | 45 | typedef struct 46 | { 47 | bondsDateStruct startDate; 48 | bondsDateStruct maturityDate; 49 | float rate; 50 | } bondStruct; 51 | 52 | 53 | typedef struct 54 | { 55 | dataType rate; 56 | dataType freq; 57 | int comp; 58 | int dayCounter; 59 | } intRateStruct; 60 | 61 | 62 | typedef struct 63 | { 64 | dataType forward; 65 | dataType compounding; 66 | dataType frequency; 67 | intRateStruct intRate; 68 | bondsDateStruct refDate; 69 | bondsDateStruct calDate; 70 | int dayCounter; 71 | } bondsYieldTermStruct; 72 | 73 | 74 | typedef struct 75 | { 76 | bondsDateStruct paymentDate; 77 | bondsDateStruct accrualStartDate; 78 | bondsDateStruct accrualEndDate; 79 | dataType amount; 80 | } couponStruct; 81 | 82 | 83 | typedef struct 84 | { 85 | couponStruct* legs; 86 | intRateStruct intRate; 87 | int nominal; 88 | int dayCounter; 89 | } cashFlowsStruct; 90 | 91 | 92 | typedef struct 93 | { 94 | dataType* dirtyPrice; 95 | dataType* accruedAmountCurrDate; 96 | dataType* cleanPrice; 97 | dataType* bondForwardVal; 98 | } resultsStruct; 99 | 100 | 101 | typedef struct 102 | { 103 | bondsYieldTermStruct* discountCurve; 104 | bondsYieldTermStruct* repoCurve; 105 | bondsDateStruct* currDate; 106 | bondsDateStruct* maturityDate; 107 | dataType* bondCleanPrice; 108 | bondStruct* bond; 109 | dataType* dummyStrike; 110 | } inArgsStruct; 111 | 112 | 113 | typedef struct 114 | { 115 | dataType npv; 116 | int dayCounter; 117 | int comp; 118 | dataType freq; 119 | bool includecurrDateFlows; 120 | bondsDateStruct currDate; 121 | bondsDateStruct npvDate; 122 | } irrFinderStruct; 123 | 124 | 125 | typedef struct 126 | { 127 | dataType root_; 128 | dataType xMin_; 129 | dataType xMax_; 130 | dataType fxMin_; 131 | dataType fxMax_; 132 | int maxEvaluations_; 133 | int evaluationNumber_; 134 | dataType lowerBound_; 135 | dataType upperBound_; 136 | bool lowerBoundEnforced_; 137 | bool upperBoundEnforced_; 138 | } solverStruct; 139 | 140 | 141 | 142 | #endif //REPO_STRUCTS_CUH 143 | -------------------------------------------------------------------------------- /Bonds/OpenMP/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | g++ -O3 -march=native -fopenmp bondsEngine.c -o bondsEngine.exe 3 | clean: 4 | rm -f *~ *.exe 5 | -------------------------------------------------------------------------------- /Bonds/OpenMP/bondsStructs.h: -------------------------------------------------------------------------------- 1 | //bondsStructs.cuh 2 | //Scott Grauer-Gray 3 | //Structs for running the bonds application 4 | 5 | #ifndef BONDS_STRUCTS_CUH 6 | #define BONDS_STRUCTS_CUH 7 | 8 | typedef double dataType; 9 | 10 | #include 11 | #include 12 | 13 | #define SIMPLE_INTEREST 0 14 | #define COMPOUNDED_INTEREST 1 15 | #define CONTINUOUS_INTEREST 2 16 | #define SIMPLE_THEN_COMPOUNDED_INTEREST 3 17 | 18 | #define ANNUAL_FREQ 1 19 | #define SEMIANNUAL_FREQ 2 20 | 21 | #define USE_EXACT_DAY 0 22 | #define USE_SERIAL_NUMS 1 23 | 24 | #define QL_EPSILON_GPU 0.000000000000000001f 25 | 26 | #define COMPUTE_AMOUNT -1 27 | 28 | 29 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 30 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 31 | 32 | #define ACCURACY 1.0e-8 33 | 34 | 35 | typedef struct 36 | { 37 | int month; 38 | int day; 39 | int year; 40 | int dateSerialNum; 41 | } bondsDateStruct; 42 | 43 | 44 | typedef struct 45 | { 46 | bondsDateStruct startDate; 47 | bondsDateStruct maturityDate; 48 | float rate; 49 | } bondStruct; 50 | 51 | 52 | 53 | typedef struct 54 | { 55 | dataType rate; 56 | dataType freq; 57 | int comp; 58 | int dayCounter; 59 | } intRateStruct; 60 | 61 | 62 | typedef struct 63 | { 64 | dataType forward; 65 | dataType compounding; 66 | dataType frequency; 67 | intRateStruct intRate; 68 | bondsDateStruct refDate; 69 | bondsDateStruct calDate; 70 | int dayCounter; 71 | } bondsYieldTermStruct; 72 | 73 | 74 | typedef struct 75 | { 76 | bondsDateStruct paymentDate; 77 | bondsDateStruct accrualStartDate; 78 | bondsDateStruct accrualEndDate; 79 | dataType amount; 80 | } couponStruct; 81 | 82 | 83 | typedef struct 84 | { 85 | couponStruct* legs; 86 | intRateStruct intRate; 87 | int nominal; 88 | int dayCounter; 89 | } cashFlowsStruct; 90 | 91 | 92 | typedef struct 93 | { 94 | dataType* dirtyPrice; 95 | dataType* accruedAmountCurrDate; 96 | dataType* cleanPrice; 97 | dataType* bondForwardVal; 98 | } resultsStruct; 99 | 100 | 101 | typedef struct 102 | { 103 | bondsYieldTermStruct* discountCurve; 104 | bondsYieldTermStruct* repoCurve; 105 | bondsDateStruct* currDate; 106 | bondsDateStruct* maturityDate; 107 | dataType* bondCleanPrice; 108 | bondStruct* bond; 109 | dataType* dummyStrike; 110 | } inArgsStruct; 111 | 112 | 113 | typedef struct 114 | { 115 | dataType npv; 116 | int dayCounter; 117 | int comp; 118 | dataType freq; 119 | bool includecurrDateFlows; 120 | bondsDateStruct currDate; 121 | bondsDateStruct npvDate; 122 | 123 | } irrFinderStruct; 124 | 125 | 126 | typedef struct 127 | { 128 | dataType root_; 129 | dataType xMin_; 130 | dataType xMax_; 131 | dataType fxMin_; 132 | dataType fxMax_; 133 | int maxEvaluations_; 134 | int evaluationNumber_; 135 | dataType lowerBound_; 136 | dataType upperBound_; 137 | bool lowerBoundEnforced_; 138 | bool upperBoundEnforced_; 139 | } solverStruct; 140 | 141 | 142 | 143 | #endif //BONDS_STRUCTS_CUH 144 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2013-2014 University of Delaware. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, 7 | this list of conditions and the following disclaimer. 8 | 9 | 2. Redistributions in binary form must reproduce the above copyright notice, 10 | this list of conditions and the following disclaimer in the documentation 11 | and/or other materials provided with the distribution. 12 | 13 | 3. Neither the name of the copyright holder nor the names of its contributors 14 | may be used to endorse or promote products derived from this software without 15 | specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 21 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 23 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 24 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | 28 | FinanceBench is based in part on QuantLib. QuantLib is 29 | Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl 30 | Copyright (C) 2003, 2004, 2005, 2006, 2007 StatPro Italia srl 31 | Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Ferdinando Ametrano 32 | 33 | Copyright (C) 2001, 2002, 2003 Nicolas Di Césaré 34 | Copyright (C) 2001, 2002, 2003 Sadruddin Rejeb 35 | 36 | Copyright (C) 2002, 2003, 2004 Decillion Pty(Ltd) 37 | 38 | Copyright (C) 2003, 2004, 2007 Neil Firth 39 | Copyright (C) 2003, 2004 Roman Gitlin 40 | Copyright (C) 2003 Niels Elken Sønderby 41 | Copyright (C) 2003 Kawanishi Tomoya 42 | 43 | Copyright (C) 2004 FIMAT Group 44 | Copyright (C) 2004 M-Dimension Consulting Inc. 45 | Copyright (C) 2004 Mike Parker 46 | Copyright (C) 2004 Walter Penschke 47 | Copyright (C) 2004 Gianni Piolanti 48 | Copyright (C) 2004, 2005, 2006, 2007 Klaus Spanderen 49 | Copyright (C) 2004 Jeff Yu 50 | 51 | Copyright (C) 2005, 2006 Toyin Akin 52 | Copyright (C) 2005 Sercan Atalik 53 | Copyright (C) 2005, 2006 Theo Boafo 54 | Copyright (C) 2005, 2006 Piter Dias 55 | Copyright (C) 2005 Gary Kennedy 56 | Copyright (C) 2005, 2006, 2007 Joseph Wang 57 | Copyright (C) 2005 Charles Whitmore 58 | 59 | Copyright (C) 2006, 2007 Banca Profilo S.p.A. 60 | Copyright (C) 2006, 2007 Marco Bianchetti 61 | Copyright (C) 2006 Yiping Chen 62 | Copyright (C) 2006, 2007 Warren Chou 63 | Copyright (C) 2006, 2007 Cristina Duminuco 64 | Copyright (C) 2006, 2007 Giorgio Facchinetti 65 | Copyright (C) 2006, 2007 Chiara Fornarola 66 | Copyright (C) 2006 Silvia Frasson 67 | Copyright (C) 2006 Richard Gould 68 | Copyright (C) 2006, 2007 Mark Joshi 69 | Copyright (C) 2006 Allen Kuo 70 | Copyright (C) 2006 Roland Lichters 71 | Copyright (C) 2006, 2007 Katiuscia Manzoni 72 | Copyright (C) 2006, 2007 Mario Pucci 73 | Copyright (C) 2006 François du Vignaud 74 | 75 | Copyright (C) 2007 Affine Group Limited 76 | 77 | QuantLib includes code taken from Peter Jäckel's book "Monte Carlo 78 | Methods in Finance". 79 | 80 | QuantLib includes software developed by the University of Chicago, 81 | as Operator of Argonne National Laboratory. 82 | 83 | 84 | Redistribution and use in source and binary forms, with or without 85 | modification, are permitted provided that the following conditions are met: 86 | 87 | Redistributions of source code must retain the above copyright notice, 88 | this list of conditions and the following disclaimer. 89 | 90 | Redistributions in binary form must reproduce the above copyright notice, 91 | this list of conditions and the following disclaimer in the documentation 92 | and/or other materials provided with the distribution. 93 | 94 | Neither the names of the copyright holders nor the names of the QuantLib 95 | Group and its contributors may be used to endorse or promote products 96 | derived from this software without specific prior written permission. 97 | 98 | 99 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 100 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 101 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 102 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE 103 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 104 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 105 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 106 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 107 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 108 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 109 | -------------------------------------------------------------------------------- /Monte-Carlo/CPU/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | g++ -O3 monteCarloEngine.c -o monteCarloEngine.exe 3 | clean: 4 | rm -f *~ *.exe 5 | -------------------------------------------------------------------------------- /Monte-Carlo/CPU/monteCarloConstants.h: -------------------------------------------------------------------------------- 1 | //monteCarloConstants.cuh 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //Constants for running monte carlo on the GPU/CPU 5 | 6 | #ifndef MONTE_CARLO_CONSTANTS_CUH 7 | #define MONTE_CARLO_CONSTANTS_CUH 8 | 9 | #define DEFAULT_SEQ_VAL 1.0f 10 | #define DEFAULT_SEQ_WEIGHT 1.0f 11 | 12 | #define RISK_VAL 0.06f 13 | #define DIV_VAL 0.0f 14 | #define VOLT_VAL 0.200f 15 | #define UNDERLYING_VAL 30.0f 16 | #define STRIKE_VAL 40.0f 17 | #define DISCOUNT_VAL 0.94176453358424872f 18 | #define START_PATH_VAL 1.0f 19 | #define DEFAULT_SEQ_VAL 1.0f 20 | #define DEFAULT_SEQ_WEIGHT 1.0f 21 | #define SEQUENCE_LENGTH 250 22 | 23 | //define the thread block size 24 | #define THREAD_BLOCK_SIZE 256 25 | 26 | //define the number of samples 27 | #define NUM_SAMPLES 400000 28 | 29 | //number of options 30 | #define NUM_OPTIONS 1 31 | 32 | //number of threads to use for processing each option 33 | #define NUM_THREADS_PER_OPTION NUM_SAMPLES 34 | 35 | #endif //MONTE_CARLO_CONSTANTS_CUH 36 | -------------------------------------------------------------------------------- /Monte-Carlo/CPU/monteCarloEngine.c: -------------------------------------------------------------------------------- 1 | //monteCarloEngine.cu 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //Function for running Monte Carlo on the GPU 5 | 6 | //needed for the monte carlo CPU kernels 7 | #include "monteCarloKernelsCpu.c" 8 | 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #define RISK_VAL 0.06f 16 | #define DIV_VAL 0.0f 17 | #define VOLT_VAL 0.200f 18 | #define UNDERLYING_VAL 30.0f 19 | #define STRIKE_VAL 40.0f 20 | #define DISCOUNT_VAL 0.94176453358424872f 21 | 22 | 23 | 24 | //run monte carlo... 25 | void runMonteCarlo() 26 | { 27 | //int nSamplesArray[] = {100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000} 28 | 29 | int numSamples = 400000; 30 | 31 | //for (int numTime=0; numTime < 12; numTime++) 32 | { 33 | //int numSamples = nSamplesArray[numTime]; 34 | 35 | printf("Number of Samples: %d\n\n", numSamples); 36 | 37 | //declare and initialize the struct used for the option 38 | monteCarloOptionStruct optionStruct; 39 | optionStruct.riskVal = RISK_VAL; 40 | optionStruct.divVal = DIV_VAL; 41 | optionStruct.voltVal = VOLT_VAL; 42 | optionStruct.underlyingVal = UNDERLYING_VAL; 43 | optionStruct.strikeVal = STRIKE_VAL; 44 | optionStruct.discountVal = DISCOUNT_VAL; 45 | 46 | 47 | //declare pointers for data on CPU 48 | dataType* samplePrices; 49 | dataType* sampleWeights; 50 | dataType* times; 51 | monteCarloOptionStruct* optionStructs; 52 | 53 | //allocate space for data on CPU 54 | samplePrices = (dataType*)malloc(NUM_OPTIONS*numSamples*sizeof(dataType)); 55 | sampleWeights = (dataType*)malloc(NUM_OPTIONS*numSamples*sizeof(dataType)); 56 | times = (dataType*)malloc(NUM_OPTIONS*numSamples*sizeof(dataType)); 57 | optionStructs = (monteCarloOptionStruct*)malloc(NUM_OPTIONS*sizeof(monteCarloOptionStruct)); 58 | 59 | long seconds, useconds; 60 | dataType mtimeCpu; 61 | struct timeval start; 62 | struct timeval end; 63 | 64 | for (int optNum = 0; optNum < NUM_OPTIONS; optNum++) 65 | { 66 | optionStructs[optNum] = optionStruct; 67 | } 68 | 69 | //initialize values for data on CPU 70 | //declare pointers for data on GPU 71 | dataType* samplePricesGpu; 72 | dataType* sampleWeightsGpu; 73 | dataType* timesGpu; 74 | monteCarloOptionStruct* optionStructsGpu; 75 | 76 | srand(time(NULL)); 77 | 78 | /* initialize random seed: */ 79 | srand ( rand()); 80 | 81 | //free memory space on the CPU 82 | free(samplePrices); 83 | free(sampleWeights); 84 | free(times); 85 | 86 | 87 | //declare pointers for data on CPU 88 | dataType* samplePricesCpu; 89 | dataType* sampleWeightsCpu; 90 | dataType* timesCpu; 91 | 92 | //allocate space for data on CPU 93 | samplePricesCpu = (dataType*)malloc(numSamples*sizeof(dataType)); 94 | sampleWeightsCpu = (dataType*)malloc(numSamples*sizeof(dataType)); 95 | timesCpu = (dataType*)malloc(numSamples*sizeof(dataType)); 96 | 97 | 98 | gettimeofday(&start, NULL); 99 | 100 | monteCarloGpuKernelCpu(samplePricesCpu, sampleWeightsCpu, timesCpu, (1.0f / (dataType)SEQUENCE_LENGTH), optionStructs, numSamples); 101 | 102 | 103 | gettimeofday(&end, NULL); 104 | 105 | seconds = end.tv_sec - start.tv_sec; 106 | useconds = end.tv_usec - start.tv_usec; 107 | 108 | mtimeCpu = ((seconds) * 1000 + ((dataType)useconds)/1000.0) + 0.5; 109 | printf("Run on CPU\n"); 110 | printf("Processing time on CPU: %f (ms)\n", mtimeCpu); 111 | 112 | //retrieve the average price 113 | dataType cumPrice = 0.0f; 114 | 115 | //add all the computed prices together 116 | for (int numSamp = 0; numSamp < numSamples; numSamp++) 117 | { 118 | cumPrice += samplePricesCpu[numSamp]; 119 | } 120 | 121 | dataType avgPrice = cumPrice / numSamples; 122 | printf("Average Price (CPU computation): %f\n\n", avgPrice); 123 | 124 | //free memory space on the CPU 125 | free(samplePricesCpu); 126 | free(sampleWeightsCpu); 127 | free(timesCpu); 128 | free(optionStructs); 129 | 130 | } 131 | } 132 | 133 | //////////////////////////////////////////////////////////////////////////////// 134 | // Program main 135 | //////////////////////////////////////////////////////////////////////////////// 136 | int 137 | main( int argc, char** argv) 138 | { 139 | runMonteCarlo(); 140 | 141 | char c; 142 | c = getchar(); 143 | printf("%c\n", c); 144 | } 145 | -------------------------------------------------------------------------------- /Monte-Carlo/CPU/monteCarloKernelsCpu.c: -------------------------------------------------------------------------------- 1 | //monteCarloKernelsCpu.cu 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //monte carlo kernels run on the CPU 5 | 6 | 7 | #include "monteCarloKernelsCpu.h" 8 | 9 | 10 | //function to set up the random states 11 | void setup_kernelCpu( ) 12 | { 13 | 14 | srand(time(NULL)); 15 | } 16 | 17 | 18 | //function to compute the inverse normal distribution 19 | float compInverseNormDistCpu(float x) 20 | { 21 | float z; 22 | //if (x < x_low_ || x_high_ < x) { 23 | //z = tail_value(x); 24 | //} else { 25 | 26 | z = x - 0.5; 27 | float r = z*z; 28 | z = (((((A_1*r+A_2)*r+A_3)*r+A_4)*r+A_5)*r+A_6)*z / 29 | (((((B_1*r+B_2)*r+B_3)*r+B_4)*r+B_5)*r+1.0); 30 | 31 | return z; 32 | } 33 | 34 | float interestRateCompoundFactCpu(float t, float rate) 35 | { 36 | //assuming "continuous" option 37 | return exp(rate*t); 38 | } 39 | 40 | float interestRateDiscountFactCpu(float t, float rate) 41 | { 42 | return 1.0/interestRateCompoundFactCpu(t, rate); 43 | } 44 | 45 | float flatForwardDiscountImplCpu(float t, float rate) 46 | { 47 | return interestRateDiscountFactCpu(t, rate); 48 | } 49 | 50 | float yieldTermStructDiscountCpu(float t, float rate) 51 | { 52 | return flatForwardDiscountImplCpu(t, rate); 53 | } 54 | 55 | float interestRateImpliedRateCpu(float compound, float t) 56 | { 57 | float r = log(compound) / t; 58 | return r; 59 | } 60 | 61 | 62 | float yieldTermStructForwardRateCpu(float t1, float t2, float rate) 63 | { 64 | float compound = interestRateDiscountFactCpu(t1, rate) / interestRateDiscountFactCpu(t2, rate); 65 | return interestRateImpliedRateCpu(compound, t2 - t1); 66 | } 67 | 68 | float processDriftCpu(float t, float x, monteCarloOptionStruct optionStruct) 69 | { 70 | float sigma = processDiffCpu(t, x, optionStruct); 71 | float t1 = t + 0.0001; 72 | 73 | return yieldTermStructForwardRateCpu(t, t1, optionStruct.riskVal) - yieldTermStructForwardRateCpu(t, t1, optionStruct.divVal) - (0.5 * sigma * sigma); 74 | } 75 | 76 | float discretizationDriftCpu(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 77 | { 78 | return processDriftCpu(t0, x0, optionStruct) * dt; 79 | } 80 | 81 | float localVoltLocVolCpu(float t, float underlyingLevel, monteCarloOptionStruct optionStruct) 82 | { 83 | return optionStruct.voltVal; 84 | } 85 | 86 | float processDiffCpu(float t, float x, monteCarloOptionStruct optionStruct) 87 | { 88 | return localVoltLocVolCpu(t, x, optionStruct); 89 | } 90 | 91 | float discDiffCpu(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 92 | { 93 | return processDiffCpu(t0, x0, optionStruct) * sqrt(dt); 94 | } 95 | 96 | float stdDeviationCpu(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 97 | { 98 | return discDiffCpu(t0, x0, dt, optionStruct); 99 | } 100 | 101 | float applyCpu(float x0, float dx) 102 | { 103 | return (x0 * exp(dx)); 104 | } 105 | 106 | float discDriftCpu(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 107 | { 108 | return processDriftCpu(t0, x0, optionStruct) * dt; 109 | } 110 | 111 | float processEvolveCpu(float t0, float x0, float dt, float dw, monteCarloOptionStruct optionStruct) 112 | { 113 | return applyCpu(x0, discDriftCpu(t0, x0, dt, optionStruct) + stdDeviationCpu(t0, x0, dt, optionStruct) *dw); 114 | } 115 | 116 | //retrieve the current sequence 117 | void getSequenceCpu(float* sequence, float sampleNum) 118 | { 119 | for (size_t iInSeq = 0; iInSeq < SEQUENCE_LENGTH; iInSeq++) 120 | { 121 | sequence[iInSeq] = DEFAULT_SEQ_VAL; 122 | } 123 | } 124 | 125 | 126 | float getProcessValX0Cpu(monteCarloOptionStruct optionStruct) 127 | { 128 | return optionStruct.underlyingVal; 129 | } 130 | 131 | 132 | void getPathCpu(float* path, size_t sampleNum, float dt, monteCarloOptionStruct optionStruct) 133 | { 134 | path[0] = getProcessValX0Cpu(optionStruct); 135 | 136 | for (size_t i=1; i 13 | #include 14 | #include 15 | #include 16 | 17 | #define A_1 -39.696830286653757 18 | #define A_2 220.94609842452050 19 | #define A_3 -275.92851044696869 20 | #define A_4 138.35775186726900 21 | #define A_5 -30.664798066147160 22 | #define A_6 2.5066282774592392 23 | #define B_1 -54.476098798224058 24 | #define B_2 161.58583685804089 25 | #define B_3 -155.69897985988661 26 | #define B_4 66.801311887719720 27 | #define B_5 -13.280681552885721 28 | 29 | 30 | //function to set up the random states 31 | void setup_kernelCpu(); 32 | 33 | //function to compute the inverse normal distribution 34 | dataType compInverseNormDistCpu(dataType x) ; 35 | 36 | dataType interestRateCompoundFactCpu(dataType t, dataType rate); 37 | 38 | dataType interestRateDiscountFactCpu(dataType t, dataType rate); 39 | 40 | dataType flatForwardDiscountImplCpu(dataType t, dataType rate); 41 | 42 | dataType yieldTermStructDiscountCpu(dataType t, dataType rate); 43 | 44 | dataType interestRateImpliedRateCpu(dataType compound, dataType t); 45 | 46 | dataType yieldTermStructForwardRateCpu(dataType t1, dataType t2, dataType rate); 47 | 48 | dataType processDriftCpu(dataType t, dataType x, monteCarloOptionStruct optionStruct); 49 | 50 | dataType discretizationDriftCpu(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct); 51 | 52 | dataType localVoltLocVolCpu(dataType t, dataType underlyingLevel, monteCarloOptionStruct optionStruct); 53 | 54 | dataType processDiffCpu(dataType t, dataType x, monteCarloOptionStruct optionStruct); 55 | 56 | dataType discDiffCpu(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct); 57 | 58 | dataType stdDeviationCpu(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct); 59 | 60 | dataType applyCpu(dataType x0, dataType dx); 61 | 62 | dataType discDriftCpu(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct); 63 | 64 | dataType processEvolveCpu(dataType t0, dataType x0, dataType dt, dataType dw, monteCarloOptionStruct optionStruct); 65 | 66 | //retrieve the current sequence 67 | void getSequenceCpu(dataType* sequence, dataType sampleNum); 68 | 69 | 70 | dataType getProcessValX0Cpu(monteCarloOptionStruct optionStruct); 71 | 72 | void getPathCpu(dataType* path, size_t sampleNum, dataType dt, int numSample, monteCarloOptionStruct optionStruct); 73 | 74 | 75 | dataType getPriceCpu(dataType val); 76 | 77 | //initialize the path 78 | void initializePathCpu(dataType* path); 79 | 80 | void monteCarloGpuKernelCpu(dataType* samplePrices, dataType* sampleWeights, dataType* times, dataType dt, monteCarloOptionStruct* optionStructs, int numSamples); 81 | 82 | 83 | 84 | #endif //MONTE_CARLO_KERNELS_CPU_CUH 85 | -------------------------------------------------------------------------------- /Monte-Carlo/CPU/monteCarloStructs.h: -------------------------------------------------------------------------------- 1 | //monteCarloStructs.cuh 2 | //Scott Grauer-Gray 3 | //May 24, 2012 4 | //Header file with structures used in monte carlo computation 5 | 6 | #ifndef MONTE_CARLO_STRUCTS_CUH 7 | #define MONTE_CARLO_STRUCTS_CUH 8 | 9 | typedef float dataType; 10 | 11 | //struct for defining what's in an option 12 | typedef struct 13 | { 14 | dataType riskVal; 15 | dataType divVal; 16 | dataType voltVal; 17 | dataType underlyingVal; 18 | dataType strikeVal; 19 | dataType discountVal; 20 | } monteCarloOptionStruct; 21 | 22 | #endif //MONTE_CARLO_STRUCTS_CUH 23 | -------------------------------------------------------------------------------- /Monte-Carlo/CUDA/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | nvcc -O3 monteCarloEngine.cu -o monteCarloEngine.exe 3 | clean: 4 | rm -f *~ *.exe 5 | -------------------------------------------------------------------------------- /Monte-Carlo/CUDA/monteCarloConstants.cuh: -------------------------------------------------------------------------------- 1 | //monteCarloConstants.cuh 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //Constants for running monte carlo on the GPU/CPU 5 | 6 | #ifndef MONTE_CARLO_CONSTANTS_CUH 7 | #define MONTE_CARLO_CONSTANTS_CUH 8 | 9 | #define DEFAULT_SEQ_VAL 1.0f 10 | #define DEFAULT_SEQ_WEIGHT 1.0f 11 | 12 | #define RISK_VAL 0.06f 13 | #define DIV_VAL 0.0f 14 | #define VOLT_VAL 0.200f 15 | #define UNDERLYING_VAL 30.0f 16 | #define STRIKE_VAL 40.0f 17 | #define DISCOUNT_VAL 0.94176453358424872f 18 | #define START_PATH_VAL 1.0f 19 | #define DEFAULT_SEQ_VAL 1.0f 20 | #define DEFAULT_SEQ_WEIGHT 1.0f 21 | #define SEQUENCE_LENGTH 250 22 | 23 | //define the thread block size 24 | #define THREAD_BLOCK_SIZE 256 25 | 26 | //define the number of samples 27 | #define NUM_SAMPLES 400000 28 | 29 | //number of options 30 | #define NUM_OPTIONS 1 31 | 32 | //number of threads to use for processing each option 33 | #define NUM_THREADS_PER_OPTION NUM_SAMPLES 34 | 35 | #endif //MONTE_CARLO_CONSTANTS_CUH 36 | -------------------------------------------------------------------------------- /Monte-Carlo/CUDA/monteCarloKernels.cuh: -------------------------------------------------------------------------------- 1 | //monteCarloKernels.cuh 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //Kernel headers for running monte carlo on the GPU 5 | 6 | #ifndef MONTE_CARLO_KERNELS_CUH 7 | #define MONTE_CARLO_KERNELS_CUH 8 | 9 | //needed for constants related to monte carlo 10 | #include "monteCarloConstants.cuh" 11 | 12 | //needed for structs related to monte carlo 13 | #include "monteCarloStructs.cuh" 14 | 15 | //needed for curand 16 | #include 17 | 18 | 19 | //function to set up the random states 20 | __global__ void setup_kernel ( curandState * state, int seedVal, int numSamples); 21 | 22 | 23 | //function to compute the inverse normal distribution 24 | __device__ dataType compInverseNormDist(dataType x) ; 25 | 26 | 27 | __device__ dataType interestRateCompoundFact(dataType t, dataType rate); 28 | 29 | 30 | __device__ dataType interestRateDiscountFact(dataType t, dataType rate); 31 | 32 | 33 | __device__ dataType flatForwardDiscountImpl(dataType t, dataType rate); 34 | 35 | 36 | __device__ dataType yieldTermStructDiscount(dataType t, dataType rate); 37 | 38 | 39 | __device__ dataType interestRateImpliedRate(dataType compound, dataType t); 40 | 41 | 42 | __device__ dataType yieldTermStructForwardRate(dataType t1, dataType t2, dataType rate); 43 | 44 | 45 | __device__ dataType processDrift(dataType t, dataType x, monteCarloOptionStruct optionStruct); 46 | 47 | 48 | __device__ dataType discretizationDrift(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct); 49 | 50 | 51 | __device__ dataType localVoltLocVol(dataType t, dataType underlyingLevel, monteCarloOptionStruct optionStruct); 52 | 53 | 54 | __device__ dataType processDiff(dataType t, dataType x, monteCarloOptionStruct optionStruct); 55 | 56 | 57 | __device__ dataType discDiff(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct); 58 | 59 | 60 | __device__ dataType stdDeviation(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct); 61 | 62 | 63 | __device__ dataType apply(dataType x0, dataType dx); 64 | 65 | 66 | __device__ dataType discDrift(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct); 67 | 68 | 69 | __device__ dataType processEvolve(dataType t0, dataType x0, dataType dt, dataType dw, monteCarloOptionStruct optionStruct); 70 | 71 | 72 | //retrieve the current sequence 73 | __device__ void getSequence(dataType* sequence, dataType sampleNum); 74 | 75 | 76 | __device__ dataType getProcessValX0(monteCarloOptionStruct optionStruct); 77 | 78 | 79 | __device__ void getPath(dataType* path, size_t sampleNum, dataType dt, curandState * state, monteCarloOptionStruct optionStruct); 80 | 81 | 82 | __device__ dataType getPrice(dataType val); 83 | 84 | 85 | //initialize the path 86 | __device__ void initializePath(dataType* path); 87 | 88 | 89 | __global__ void monteCarloGpuKernel(dataType* samplePrices, dataType* sampleWeights, dataType* times, dataType dt, curandState * state, monteCarloOptionStruct* optionStructs, int numSamples); 90 | 91 | 92 | 93 | 94 | #endif //MONTE_CARLO_KERNELS_CUH 95 | -------------------------------------------------------------------------------- /Monte-Carlo/CUDA/monteCarloKernelsCpu.cu: -------------------------------------------------------------------------------- 1 | //monteCarloKernelsCpu.cu 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //monte carlo kernels run on the CPU 5 | 6 | 7 | #include "monteCarloKernelsCpu.cuh" 8 | 9 | 10 | //function to set up the random states 11 | void setup_kernelCpu( ) 12 | { 13 | 14 | srand(time(NULL)); 15 | } 16 | 17 | 18 | //function to compute the inverse normal distribution 19 | float compInverseNormDistCpu(float x) 20 | { 21 | float z; 22 | //if (x < x_low_ || x_high_ < x) { 23 | //z = tail_value(x); 24 | //} else { 25 | 26 | z = x - 0.5; 27 | float r = z*z; 28 | z = (((((A_1*r+A_2)*r+A_3)*r+A_4)*r+A_5)*r+A_6)*z / 29 | (((((B_1*r+B_2)*r+B_3)*r+B_4)*r+B_5)*r+1.0); 30 | 31 | return z; 32 | } 33 | 34 | float interestRateCompoundFactCpu(float t, float rate) 35 | { 36 | //assuming "continuous" option 37 | return exp(rate*t); 38 | } 39 | 40 | float interestRateDiscountFactCpu(float t, float rate) 41 | { 42 | return 1.0/interestRateCompoundFactCpu(t, rate); 43 | } 44 | 45 | float flatForwardDiscountImplCpu(float t, float rate) 46 | { 47 | return interestRateDiscountFactCpu(t, rate); 48 | } 49 | 50 | float yieldTermStructDiscountCpu(float t, float rate) 51 | { 52 | return flatForwardDiscountImplCpu(t, rate); 53 | } 54 | 55 | float interestRateImpliedRateCpu(float compound, float t) 56 | { 57 | float r = log(compound) / t; 58 | return r; 59 | } 60 | 61 | 62 | float yieldTermStructForwardRateCpu(float t1, float t2, float rate) 63 | { 64 | float compound = interestRateDiscountFactCpu(t1, rate) / interestRateDiscountFactCpu(t2, rate); 65 | return interestRateImpliedRateCpu(compound, t2 - t1); 66 | } 67 | 68 | float processDriftCpu(float t, float x, monteCarloOptionStruct optionStruct) 69 | { 70 | float sigma = processDiffCpu(t, x, optionStruct); 71 | float t1 = t + 0.0001; 72 | 73 | return yieldTermStructForwardRateCpu(t, t1, optionStruct.riskVal) - yieldTermStructForwardRateCpu(t, t1, optionStruct.divVal) - (0.5 * sigma * sigma); 74 | } 75 | 76 | float discretizationDriftCpu(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 77 | { 78 | return processDriftCpu(t0, x0, optionStruct) * dt; 79 | } 80 | 81 | float localVoltLocVolCpu(float t, float underlyingLevel, monteCarloOptionStruct optionStruct) 82 | { 83 | return optionStruct.voltVal; 84 | } 85 | 86 | float processDiffCpu(float t, float x, monteCarloOptionStruct optionStruct) 87 | { 88 | return localVoltLocVolCpu(t, x, optionStruct); 89 | } 90 | 91 | float discDiffCpu(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 92 | { 93 | return processDiffCpu(t0, x0, optionStruct) * sqrt(dt); 94 | } 95 | 96 | float stdDeviationCpu(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 97 | { 98 | return discDiffCpu(t0, x0, dt, optionStruct); 99 | } 100 | 101 | float applyCpu(float x0, float dx) 102 | { 103 | return (x0 * exp(dx)); 104 | } 105 | 106 | float discDriftCpu(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 107 | { 108 | return processDriftCpu(t0, x0, optionStruct) * dt; 109 | } 110 | 111 | float processEvolveCpu(float t0, float x0, float dt, float dw, monteCarloOptionStruct optionStruct) 112 | { 113 | return applyCpu(x0, discDriftCpu(t0, x0, dt, optionStruct) + stdDeviationCpu(t0, x0, dt, optionStruct) *dw); 114 | } 115 | 116 | //retrieve the current sequence 117 | void getSequenceCpu(float* sequence, float sampleNum) 118 | { 119 | for (size_t iInSeq = 0; iInSeq < SEQUENCE_LENGTH; iInSeq++) 120 | { 121 | sequence[iInSeq] = DEFAULT_SEQ_VAL; 122 | } 123 | } 124 | 125 | 126 | float getProcessValX0Cpu(monteCarloOptionStruct optionStruct) 127 | { 128 | return optionStruct.underlyingVal; 129 | } 130 | 131 | 132 | void getPathCpu(float* path, size_t sampleNum, float dt, monteCarloOptionStruct optionStruct) 133 | { 134 | path[0] = getProcessValX0Cpu(optionStruct); 135 | 136 | for (size_t i=1; i 7 | #include 8 | #include 9 | #include 10 | 11 | #include "monteCarloKernels.c" 12 | #include "mt19937.c" 13 | 14 | 15 | #pragma hmpp samples codelet, target=CUDA, args[samplePrices;sampleWeights].io=out 16 | void monteCarloKernel (int n, float* samplePrices, float* sampleWeights, float dt, mt19937state* randStates, monteCarloOptionStruct optionStruct) 17 | { 18 | int i; 19 | 20 | //, global (n, samplePrices, sampleWeights, dt, randStates, optionStructs) 21 | #pragma hmppcg parallel 22 | for (i = 0; i < n; ++i){ 23 | 24 | float path[SEQUENCE_LENGTH]; 25 | initializePath (path); 26 | 27 | int optionStructNum = 0; 28 | 29 | getPath (path, i, dt, randStates, optionStruct); 30 | float price = getPrice (path [SEQUENCE_LENGTH-1]); 31 | 32 | samplePrices [i] = price; 33 | sampleWeights [i] = DEFAULT_SEQ_WEIGHT; 34 | } 35 | } 36 | 37 | 38 | //run monte carlo... 39 | void runMonteCarlo () 40 | { 41 | //declare and initialize the struct used for the option 42 | monteCarloOptionStruct optionStruct; 43 | optionStruct.riskVal = RISK_VAL; 44 | optionStruct.divVal = DIV_VAL; 45 | optionStruct.voltVal = VOLT_VAL; 46 | optionStruct.underlyingVal = UNDERLYING_VAL; 47 | optionStruct.strikeVal = STRIKE_VAL; 48 | optionStruct.discountVal = DISCOUNT_VAL; 49 | 50 | struct timeval begin,end; 51 | mt19937state* randStates = (mt19937state*) malloc (NUM_SAMPLES * sizeof (mt19937state)); 52 | 53 | // Accelerator Version 54 | { 55 | //declare pointers for data on CPU 56 | float* samplePrices = (float*) malloc (NUM_SAMPLES * sizeof (float)); 57 | float* sampleWeights = (float*) malloc (NUM_SAMPLES * sizeof (float)); 58 | 59 | int numSamps = NUM_SAMPLES; 60 | printf("Number of Samples: %d\n", numSamps); 61 | int sampNum; 62 | for (sampNum = 0; sampNum < numSamps; sampNum++) 63 | { 64 | initializeMersenneState (&(randStates[sampNum]), rand ()); 65 | } 66 | #pragma hmpp samples allocate, args[samplePrices;sampleWeights;randStates].size={numSamps} 67 | 68 | #pragma hmpp samples advancedload, args[randStates] 69 | gettimeofday(&begin,NULL); 70 | 71 | #pragma hmpp samples callsite 72 | monteCarloKernel (NUM_SAMPLES, samplePrices, sampleWeights, (1.0f / (float)SEQUENCE_LENGTH), randStates, optionStruct); 73 | #pragma hmpp samples release 74 | gettimeofday(&end,NULL); 75 | 76 | //retrieve the average price 77 | float cumPrice = 0.0f; 78 | 79 | //add all the computed prices together 80 | int numSamp; 81 | for (numSamp = 0; numSamp < NUM_SAMPLES; numSamp++) 82 | { 83 | cumPrice += samplePrices [numSamp]; 84 | } 85 | 86 | float avgPrice = cumPrice / NUM_SAMPLES; 87 | printf("Average price on GPU: %f\n", avgPrice); 88 | printf("Runtime on GPU: %f ms\n", (((end.tv_sec - begin.tv_sec) * 1e6 + (end.tv_usec - begin.tv_usec)) / 1000.0f)); 89 | 90 | free(samplePrices); 91 | free(sampleWeights); 92 | } 93 | 94 | 95 | // CPU Version 96 | { 97 | //declare pointers for data on CPU 98 | float* samplePricesCpu; 99 | float* sampleWeightsCpu; 100 | float* timesCpu; 101 | 102 | //allocate space for data on CPU 103 | samplePricesCpu = (float*) malloc (NUM_SAMPLES * sizeof (float)); 104 | sampleWeightsCpu = (float*) malloc (NUM_SAMPLES * sizeof (float)); 105 | timesCpu = (float*) malloc (NUM_SAMPLES * sizeof (float)); 106 | 107 | int sampNum; 108 | for (sampNum = 0; sampNum < NUM_SAMPLES; sampNum++) 109 | { 110 | initializeMersenneState (&randStates [sampNum], rand ()); 111 | } 112 | 113 | gettimeofday(&begin,NULL); 114 | //run on CPU 115 | monteCarloKernel (NUM_SAMPLES, samplePricesCpu, sampleWeightsCpu, (1.0f / (float)SEQUENCE_LENGTH), randStates, optionStruct); 116 | gettimeofday(&end,NULL); 117 | 118 | //retrieve the average price 119 | float cumPrice = 0.0f; 120 | 121 | //add all the computed prices together 122 | int numSamp; 123 | for (numSamp = 0; numSamp < NUM_SAMPLES; numSamp++) 124 | { 125 | cumPrice += samplePricesCpu[numSamp]; 126 | } 127 | 128 | float avgPrice = cumPrice / NUM_SAMPLES; 129 | printf("Average price on CPU: %f\n", avgPrice); 130 | printf("Runtime on CPU: %f ms\n", (((end.tv_sec - begin.tv_sec) * 1e6 + (end.tv_usec - begin.tv_usec)) / 1000.0f)); 131 | 132 | //free memory space on the CPU 133 | free(samplePricesCpu); 134 | free(sampleWeightsCpu); 135 | free(timesCpu); 136 | free(randStates); 137 | } 138 | } 139 | 140 | -------------------------------------------------------------------------------- /Monte-Carlo/HMPP-CUDA/monteCarloEngineHmppCuda.c: -------------------------------------------------------------------------------- 1 | //monteCarloEngineHmppCuda.c 2 | //Scott Grauer-Gray 3 | //Main function to drive monte-carlo on the gpu using HMPP 4 | 5 | // includes, system 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "monteCarloEngine.c" 12 | 13 | 14 | //////////////////////////////////////////////////////////////////////////////// 15 | // Program main 16 | //////////////////////////////////////////////////////////////////////////////// 17 | int 18 | main( int argc, char** argv) 19 | { 20 | srand (time (NULL)); 21 | 22 | runMonteCarlo(); 23 | 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /Monte-Carlo/HMPP-CUDA/monteCarloKernels.c: -------------------------------------------------------------------------------- 1 | //monteCarloKernels.cu 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //GPU Kernels for running monte carlo 5 | 6 | 7 | #include "monteCarloKernels.h" 8 | 9 | //function to compute the inverse normal distribution 10 | float compInverseNormDist(float x) 11 | { 12 | float z = x - 0.5; 13 | float r = z*z; 14 | z = (((((A_1 * r + A_2) * r+A_3) * r+A_4) * r + A_5) * r + A_6) * z / 15 | (((((B_1 * r + B_2) * r+B_3) * r+B_4) * r + B_5) * r + 1.0); 16 | return z; 17 | } 18 | 19 | float interestRateCompoundFact(float t, float rate) 20 | { 21 | //assuming "continuous" option 22 | return exp(rate*t); 23 | } 24 | 25 | float interestRateDiscountFact(float t, float rate) 26 | { 27 | return 1.0/interestRateCompoundFact(t, rate); 28 | } 29 | 30 | float flatForwardDiscountImpl(float t, float rate) 31 | { 32 | return interestRateDiscountFact(t, rate); 33 | } 34 | 35 | float yieldTermStructDiscount(float t, float rate) 36 | { 37 | return flatForwardDiscountImpl(t, rate); 38 | } 39 | 40 | float interestRateImpliedRate(float compound, float t) 41 | { 42 | float r = log(compound) / t; 43 | return r; 44 | } 45 | 46 | 47 | float yieldTermStructForwardRate(float t1, float t2, float rate) 48 | { 49 | float compound = interestRateDiscountFact(t1, rate) / interestRateDiscountFact(t2, rate); 50 | return interestRateImpliedRate(compound, t2 - t1); 51 | } 52 | 53 | float processDrift(float t, float x, monteCarloOptionStruct optionStruct) 54 | { 55 | float sigma = processDiff(t, x, optionStruct); 56 | float t1 = t + 0.0001; 57 | 58 | return yieldTermStructForwardRate(t, t1, optionStruct.riskVal) - yieldTermStructForwardRate(t, t1, optionStruct.divVal) - (0.5 * sigma * sigma); 59 | } 60 | 61 | float discretizationDrift(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 62 | { 63 | return processDrift(t0, x0, optionStruct) * dt; 64 | } 65 | 66 | float localVoltLocVol(float t, float underlyingLevel, monteCarloOptionStruct optionStruct) 67 | { 68 | return optionStruct.voltVal; 69 | } 70 | 71 | float processDiff(float t, float x, monteCarloOptionStruct optionStruct) 72 | { 73 | return localVoltLocVol(t, x, optionStruct); 74 | } 75 | 76 | float discDiff(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 77 | { 78 | return processDiff(t0, x0, optionStruct) * sqrt(dt); 79 | } 80 | 81 | float stdDeviation(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 82 | { 83 | return discDiff(t0, x0, dt, optionStruct); 84 | } 85 | 86 | float apply(float x0, float dx) 87 | { 88 | return (x0 * exp(dx)); 89 | } 90 | 91 | float discDrift(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 92 | { 93 | return processDrift(t0, x0, optionStruct) * dt; 94 | } 95 | 96 | float processEvolve(float t0, float x0, float dt, float dw, monteCarloOptionStruct optionStruct) 97 | { 98 | return apply(x0, discDrift(t0, x0, dt, optionStruct) + stdDeviation(t0, x0, dt, optionStruct) *dw); 99 | } 100 | 101 | //retrieve the current sequence 102 | void getSequence(float* sequence, float sampleNum) 103 | { 104 | size_t iInSeq; 105 | 106 | #pragma hmppcg noParallel 107 | for (iInSeq = 0; iInSeq < SEQUENCE_LENGTH; iInSeq++) 108 | { 109 | sequence [iInSeq] = DEFAULT_SEQ_VAL; 110 | } 111 | } 112 | 113 | 114 | float getProcessValX0(monteCarloOptionStruct optionStruct) 115 | { 116 | return optionStruct.underlyingVal; 117 | } 118 | 119 | void getPath(float* path, size_t sampleNum, float dt, mt19937state* state, monteCarloOptionStruct optionStruct) 120 | { 121 | float weight = DEFAULT_SEQ_WEIGHT; 122 | path [0] = getProcessValX0(optionStruct); 123 | size_t i; 124 | 125 | #pragma hmppcg noParallel 126 | for (i = 1; i < SEQUENCE_LENGTH; i++) 127 | { 128 | float t = i * dt; 129 | float randVal = generateRandFloat (&(state [sampleNum])); 130 | float inverseCumRandVal = compInverseNormDist (randVal); 131 | path [i] = processEvolve(t, path [i - 1], dt, inverseCumRandVal, optionStruct); 132 | } 133 | } 134 | 135 | 136 | float getPrice (float val) 137 | { 138 | float v = STRIKE_VAL - val; 139 | if (v < 0) 140 | return 0.0f; 141 | return v * DISCOUNT_VAL; 142 | } 143 | 144 | void initializePath(float* path) 145 | { 146 | int i; 147 | 148 | #pragma hmppcg noParallel 149 | for (i = 0; i < SEQUENCE_LENGTH; i++) 150 | { 151 | path [i] = START_PATH_VAL; 152 | } 153 | } 154 | 155 | -------------------------------------------------------------------------------- /Monte-Carlo/HMPP-CUDA/monteCarloKernels.h: -------------------------------------------------------------------------------- 1 | //monteCarloKernels.cuh 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //Kernel headers for running monte carlo on the GPU 5 | 6 | #ifndef MONTE_CARLO_KERNELS_H 7 | #define MONTE_CARLO_KERNELS_H 8 | 9 | #include 10 | 11 | //needed for constants related to monte carlo 12 | #include "monteCarloConstants.h" 13 | 14 | //needed for structs related to monte carlo 15 | #include "monteCarloStructs.h" 16 | 17 | //needed for RNG 18 | #include "mt19937.h" 19 | 20 | //function to set up the random states 21 | 22 | float compInverseNormDist (float x) ; 23 | 24 | float interestRateCompoundFact (float t, float rate); 25 | 26 | float interestRateDiscountFact (float t, float rate); 27 | 28 | float flatForwardDiscountImpl (float t, float rate); 29 | 30 | float yieldTermStructDiscount (float t, float rate); 31 | 32 | float interestRateImpliedRate (float compound, float t); 33 | 34 | float yieldTermStructForwardRate (float t1, float t2, float rate); 35 | 36 | float processDrift (float t, float x, monteCarloOptionStruct optionStruct); 37 | 38 | float discretizationDrift (float t0, float x0, float dt, monteCarloOptionStruct optionStruct); 39 | 40 | float localVoltLocVol (float t, float underlyingLevel, monteCarloOptionStruct optionStruct); 41 | 42 | float processDiff (float t, float x, monteCarloOptionStruct optionStruct); 43 | 44 | float discDiff (float t0, float x0, float dt, monteCarloOptionStruct optionStruct); 45 | 46 | float stdDeviation (float t0, float x0, float dt, monteCarloOptionStruct optionStruct); 47 | 48 | float apply (float x0, float dx); 49 | 50 | float discDrift (float t0, float x0, float dt, monteCarloOptionStruct optionStruct); 51 | 52 | float processEvolve (float t0, float x0, float dt, float dw, monteCarloOptionStruct optionStruct); 53 | 54 | //retrieve the current sequence 55 | void getSequence (float* sequence, float sampleNum); 56 | 57 | float getProcessValX0 (monteCarloOptionStruct optionStruct); 58 | 59 | void getPath (float* path, size_t sampleNum, float dt, mt19937state* state, monteCarloOptionStruct optionStruct); 60 | 61 | float getPrice (float val); 62 | 63 | //initialize the path 64 | void initializePath (float* path); 65 | 66 | #endif //MONTE_CARLO_KERNELS_H 67 | -------------------------------------------------------------------------------- /Monte-Carlo/HMPP-CUDA/monteCarloStructs.h: -------------------------------------------------------------------------------- 1 | //monteCarloStructs.cuh 2 | //Scott Grauer-Gray 3 | //May 24, 2012 4 | //Header file with structures used in monte carlo computation 5 | 6 | #ifndef MONTE_CARLO_STRUCTS_H 7 | #define MONTE_CARLO_STRUCTS_H 8 | 9 | //struct for defining what's in an option 10 | typedef struct 11 | { 12 | float riskVal; 13 | float divVal; 14 | float voltVal; 15 | float underlyingVal; 16 | float strikeVal; 17 | float discountVal; 18 | } monteCarloOptionStruct; 19 | 20 | #endif //MONTE_CARLO_STRUCTS_CUH 21 | -------------------------------------------------------------------------------- /Monte-Carlo/HMPP-CUDA/mt19937.c: -------------------------------------------------------------------------------- 1 | /* A C-program for MT19937: Real number version (1999/10/28) */ 2 | /* genrand() generates one pseudorandom real number (double) */ 3 | /* which is uniformly distributed on [0,1]-interval, for each */ 4 | /* call. sgenrand(seed) sets initial values to the working area */ 5 | /* of 624 words. Before genrand(), sgenrand(seed) must be */ 6 | /* called once. (seed is any 32-bit integer.) */ 7 | /* Integer generator is obtained by modifying two lines. */ 8 | /* Coded by Takuji Nishimura, considering the suggestions by */ 9 | /* Topher Cooper and Marc Rieffel in July-Aug. 1997. */ 10 | 11 | /* This library is free software; you can redistribute it and/or */ 12 | /* modify it under the terms of the GNU Library General Public */ 13 | /* License as published by the Free Software Foundation; either */ 14 | /* version 2 of the License, or (at your option) any later */ 15 | /* version. */ 16 | /* This library is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ 19 | /* See the GNU Library General Public License for more details. */ 20 | /* You should have received a copy of the GNU Library General */ 21 | /* Public License along with this library; if not, write to the */ 22 | /* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA */ 23 | /* 02111-1307 USA */ 24 | 25 | /* Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura. */ 26 | /* Any feedback is very welcome. For any question, comments, */ 27 | /* see http://www.math.keio.ac.jp/matumoto/emt.html or email */ 28 | /* matumoto@math.keio.ac.jp */ 29 | 30 | /* REFERENCE */ 31 | /* M. Matsumoto and T. Nishimura, */ 32 | /* "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform */ 33 | /* Pseudo-Random Number Generator", */ 34 | /* ACM Transactions on Modeling and Computer Simulation, */ 35 | /* Vol. 8, No. 1, January 1998, pp 3--30. */ 36 | 37 | #ifndef MT_19937_C 38 | #define MT_19937_C 39 | 40 | #include 41 | 42 | #include "mt19937.h" 43 | 44 | void 45 | initializeMersenneState (mt19937state* m, unsigned long seed) 46 | { 47 | m->seed = seed; 48 | m->mti = -1; 49 | } 50 | 51 | unsigned long 52 | generateRandInt (mt19937state* m) 53 | { 54 | if (m->mti == -1) 55 | { 56 | int i; 57 | #pragma hmppcg noParallel 58 | for (i = 0; i < __N__; i++) 59 | { 60 | m->mt[i] = m->seed & 0xffff0000; 61 | m->seed = 69069 * m->seed + 1; 62 | m->mt[i] |= (m->seed & 0xffff0000) >> 16; 63 | m->seed = 69069 * m->seed + 1; 64 | } 65 | m->mti = __N__; 66 | } 67 | 68 | unsigned long y; 69 | unsigned long mag01 [2]; 70 | mag01 [0] = 0x0; 71 | mag01 [1] = MATRIX_A; 72 | 73 | /* mag01[x] = x * MATRIX_A for x=0,1 */ 74 | 75 | if (m->mti >= __N__) 76 | { /* generate N words at one time */ 77 | int kk = 0; 78 | #pragma hmppcg noParallel 79 | while (kk < __N__ - __M__) 80 | { 81 | y = (m->mt [kk] & UPPER_MASK) | (m->mt [kk + 1] & LOWER_MASK); 82 | m->mt [kk] = m->mt [kk + __M__] ^ (y >> 1) ^ mag01 [y & 0x1]; 83 | ++kk; 84 | } 85 | #pragma hmppcg noParallel 86 | while (kk < __N__ - 1) 87 | { 88 | y = (m->mt [kk] & UPPER_MASK) | (m->mt [kk + 1] & LOWER_MASK); 89 | m->mt [kk] = m->mt [kk + (__M__ - __N__)] ^ (y >> 1) ^ mag01 [y & 0x1]; 90 | ++kk; 91 | } 92 | 93 | y = (m->mt [__N__ - 1] & UPPER_MASK) | (m->mt [0] & LOWER_MASK); 94 | m->mt [__N__ - 1] = m->mt [__M__ - 1] ^ (y >> 1) ^ mag01 [y & 0x1]; 95 | 96 | m->mti = 0; 97 | } 98 | 99 | y = m->mt [m->mti++]; 100 | y ^= TEMPERING_SHIFT_U(y); 101 | y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B; 102 | y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C; 103 | y ^= TEMPERING_SHIFT_L(y); 104 | return y; 105 | } 106 | 107 | float 108 | generateRandFloat (mt19937state* m) 109 | { 110 | return ( ((float) generateRandInt (m)) / UINT_MAX); 111 | } 112 | 113 | float 114 | generateRandDouble (mt19937state* m) 115 | { 116 | return ( ((double) generateRandInt (m)) / UINT_MAX); 117 | } 118 | 119 | #endif 120 | -------------------------------------------------------------------------------- /Monte-Carlo/HMPP-CUDA/mt19937.h: -------------------------------------------------------------------------------- 1 | /* A C-program for MT19937: Real number version (1999/10/28) */ 2 | /* genrand() generates one pseudorandom real number (double) */ 3 | /* which is uniformly distributed on [0,1]-interval, for each */ 4 | /* call. sgenrand(seed) sets initial values to the working area */ 5 | /* of 624 words. Before genrand(), sgenrand(seed) must be */ 6 | /* called once. (seed is any 32-bit integer.) */ 7 | /* Integer generator is obtained by modifying two lines. */ 8 | /* Coded by Takuji Nishimura, considering the suggestions by */ 9 | /* Topher Cooper and Marc Rieffel in July-Aug. 1997. */ 10 | 11 | /* This library is free software; you can redistribute it and/or */ 12 | /* modify it under the terms of the GNU Library General Public */ 13 | /* License as published by the Free Software Foundation; either */ 14 | /* version 2 of the License, or (at your option) any later */ 15 | /* version. */ 16 | /* This library is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ 19 | /* See the GNU Library General Public License for more details. */ 20 | /* You should have received a copy of the GNU Library General */ 21 | /* Public License along with this library; if not, write to the */ 22 | /* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA */ 23 | /* 02111-1307 USA */ 24 | 25 | /* Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura. */ 26 | /* Any feedback is very welcome. For any question, comments, */ 27 | /* see http://www.math.keio.ac.jp/matumoto/emt.html or email */ 28 | /* matumoto@math.keio.ac.jp */ 29 | 30 | /* REFERENCE */ 31 | /* M. Matsumoto and T. Nishimura, */ 32 | /* "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform */ 33 | /* Pseudo-Random Number Generator", */ 34 | /* ACM Transactions on Modeling and Computer Simulation, */ 35 | /* Vol. 8, No. 1, January 1998, pp 3--30. */ 36 | 37 | #ifndef MT_19937_H 38 | #define MT_19937_H 39 | 40 | /* Period parameters */ 41 | #define __N__ 624 42 | #define __M__ 397 43 | #define MATRIX_A 0x9908b0df /* constant vector a */ 44 | #define UPPER_MASK 0x80000000 /* most significant w-r bits */ 45 | #define LOWER_MASK 0x7fffffff /* least significant r bits */ 46 | 47 | /* Tempering parameters */ 48 | #define TEMPERING_MASK_B 0x9d2c5680 49 | #define TEMPERING_MASK_C 0xefc60000 50 | #define TEMPERING_SHIFT_U(y) (y >> 11) 51 | #define TEMPERING_SHIFT_S(y) (y << 7) 52 | #define TEMPERING_SHIFT_T(y) (y << 15) 53 | #define TEMPERING_SHIFT_L(y) (y >> 18) 54 | 55 | typedef struct mt19937_stateStruct 56 | { 57 | unsigned long mt [__N__]; 58 | int mti; 59 | unsigned long seed; 60 | unsigned int init; 61 | } mt19937state; 62 | 63 | mt19937state* 64 | GetMersenneState (); 65 | 66 | void 67 | initializeMersenneState (mt19937state* m, unsigned long seed); 68 | 69 | unsigned long 70 | generateRandInt (mt19937state* m); 71 | 72 | float 73 | generateRandFloat (mt19937state* m); 74 | 75 | float 76 | generateRandDouble (mt19937state* m); 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /Monte-Carlo/HMPP-OpenCL/Makefile: -------------------------------------------------------------------------------- 1 | EXE_NAME=monteCarloEngine 2 | MAIN_FILE=monteCarloEngineHmppOpenCL.c 3 | 4 | include ../../compileHmppOpenACC.mk 5 | -------------------------------------------------------------------------------- /Monte-Carlo/HMPP-OpenCL/monteCarloConstants.h: -------------------------------------------------------------------------------- 1 | //monteCarloConstants.cuh 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //Constants for running monte carlo on the GPU/CPU 5 | 6 | #ifndef MONTE_CARLO_CONSTANTS_H 7 | #define MONTE_CARLO_CONSTANTS_H 8 | 9 | #define DEFAULT_SEQ_VAL 1.0f 10 | #define DEFAULT_SEQ_WEIGHT 1.0f 11 | 12 | 13 | #define RISK_VAL 0.06f 14 | #define DIV_VAL 0.0f 15 | #define VOLT_VAL 0.200f 16 | #define UNDERLYING_VAL 30.0f 17 | #define STRIKE_VAL 40.0f 18 | #define DISCOUNT_VAL 0.94176453358424872f 19 | #define START_PATH_VAL 1.0f 20 | #define DEFAULT_SEQ_VAL 1.0f 21 | #define DEFAULT_SEQ_WEIGHT 1.0f 22 | #define SEQUENCE_LENGTH 250 23 | 24 | 25 | #define A_1 -39.696830286653757 26 | #define A_2 220.94609842452050 27 | #define A_3 -275.92851044696869 28 | #define A_4 138.35775186726900 29 | #define A_5 -30.664798066147160 30 | #define A_6 2.5066282774592392 31 | #define B_1 -54.476098798224058 32 | #define B_2 161.58583685804089 33 | #define B_3 -155.69897985988661 34 | #define B_4 66.801311887719720 35 | #define B_5 -13.280681552885721 36 | 37 | 38 | //define the thread block size 39 | #define THREAD_BLOCK_SIZE 256 40 | 41 | //define the number of samples 42 | #define NUM_SAMPLES 400000 43 | 44 | //number of options 45 | #define NUM_OPTIONS 1 46 | 47 | //number of threads to use for processing each option 48 | #define NUM_THREADS_PER_OPTION NUM_SAMPLES 49 | 50 | #endif //MONTE_CARLO_CONSTANTS_CUH 51 | 52 | -------------------------------------------------------------------------------- /Monte-Carlo/HMPP-OpenCL/monteCarloEngine.c: -------------------------------------------------------------------------------- 1 | //monteCarloEngine.cu 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //Function for running Monte Carlo on the GPU 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "monteCarloKernels.c" 12 | #include "mt19937.c" 13 | 14 | 15 | #pragma hmpp samples codelet, target=OpenCL, args[samplePrices;sampleWeights].io=out 16 | void monteCarloKernel (int n, float* samplePrices, float* sampleWeights, float dt, mt19937state* randStates, monteCarloOptionStruct optionStruct) 17 | { 18 | int sampNum; 19 | 20 | #pragma hmppcg parallel 21 | for (sampNum = 0; sampNum < n; ++sampNum) 22 | { 23 | 24 | float path[SEQUENCE_LENGTH]; 25 | int i; 26 | 27 | #pragma hmppcg noParallel 28 | for (i = 0; i < SEQUENCE_LENGTH; i++) 29 | { 30 | path [i] = START_PATH_VAL; 31 | } 32 | 33 | int optionStructNum = 0; 34 | 35 | float weight = DEFAULT_SEQ_WEIGHT; 36 | path [0] = getProcessValX0(optionStruct); 37 | size_t ii; 38 | 39 | #pragma hmppcg noParallel 40 | for (ii = 1; ii < SEQUENCE_LENGTH; ii++) 41 | { 42 | float t = ii * dt; 43 | 44 | float randVal = generateRandFloat(randStates, sampNum); 45 | float inverseCumRandVal = compInverseNormDist (randVal); 46 | path [ii] = processEvolve(t, path [ii - 1], dt, inverseCumRandVal, optionStruct); 47 | } 48 | 49 | float price = getPrice (path [SEQUENCE_LENGTH-1]); 50 | 51 | samplePrices [sampNum] = price; 52 | sampleWeights [sampNum] = DEFAULT_SEQ_WEIGHT; 53 | } 54 | } 55 | 56 | 57 | //run monte carlo... 58 | void runMonteCarlo () 59 | { 60 | //declare and initialize the struct used for the option 61 | monteCarloOptionStruct optionStruct; 62 | optionStruct.riskVal = RISK_VAL; 63 | optionStruct.divVal = DIV_VAL; 64 | optionStruct.voltVal = VOLT_VAL; 65 | optionStruct.underlyingVal = UNDERLYING_VAL; 66 | optionStruct.strikeVal = STRIKE_VAL; 67 | optionStruct.discountVal = DISCOUNT_VAL; 68 | 69 | struct timeval begin,end; 70 | mt19937state* randStates = (mt19937state*) malloc (NUM_SAMPLES * sizeof (mt19937state)); 71 | 72 | // Accelerator Version 73 | { 74 | //declare pointers for data on CPU 75 | float* samplePrices = (float*) malloc (NUM_SAMPLES * sizeof (float)); 76 | float* sampleWeights = (float*) malloc (NUM_SAMPLES * sizeof (float)); 77 | 78 | int numSamps = NUM_SAMPLES; 79 | printf("Number of Samples: %d\n", numSamps); 80 | int sampNum; 81 | for (sampNum = 0; sampNum < numSamps; sampNum++) 82 | { 83 | initializeMersenneState (&(randStates[sampNum]), rand ()); 84 | } 85 | 86 | #pragma hmpp samples allocate, args[samplePrices;sampleWeights;randStates].size={numSamps} 87 | 88 | #pragma hmpp samples advancedload, args[randStates] 89 | 90 | gettimeofday(&begin,NULL); 91 | 92 | //run Monte-Carlo on GPU using HMPP 93 | #pragma hmpp samples callsite 94 | monteCarloKernel (NUM_SAMPLES, samplePrices, sampleWeights, (1.0f / (float)SEQUENCE_LENGTH), randStates, optionStruct); 95 | 96 | #pragma hmpp samples release 97 | gettimeofday(&end,NULL); 98 | 99 | //retrieve the average price 100 | float cumPrice = 0.0f; 101 | 102 | //add all the computed prices together 103 | int numSamp; 104 | for (numSamp = 0; numSamp < NUM_SAMPLES; numSamp++) 105 | { 106 | cumPrice += samplePrices [numSamp]; 107 | } 108 | 109 | float avgPrice = cumPrice / NUM_SAMPLES; 110 | printf("Average price on GPU: %f\n", avgPrice); 111 | printf("Runtime on GPU: %f ms\n", (((end.tv_sec - begin.tv_sec) * 1e6 + (end.tv_usec - begin.tv_usec)) / 1000.0f)); 112 | 113 | free(samplePrices); 114 | free(sampleWeights); 115 | } 116 | 117 | 118 | // CPU Version 119 | { 120 | //declare pointers for data on CPU 121 | float* samplePricesCpu; 122 | float* sampleWeightsCpu; 123 | float* timesCpu; 124 | 125 | //allocate space for data on CPU 126 | samplePricesCpu = (float*) malloc (NUM_SAMPLES * sizeof (float)); 127 | sampleWeightsCpu = (float*) malloc (NUM_SAMPLES * sizeof (float)); 128 | timesCpu = (float*) malloc (NUM_SAMPLES * sizeof (float)); 129 | 130 | int sampNum; 131 | for (sampNum = 0; sampNum < NUM_SAMPLES; sampNum++) 132 | { 133 | initializeMersenneState (&randStates [sampNum], rand ()); 134 | } 135 | 136 | gettimeofday(&begin,NULL); 137 | monteCarloKernel (NUM_SAMPLES, samplePricesCpu, sampleWeightsCpu, (1.0f / (float)SEQUENCE_LENGTH), randStates, optionStruct); 138 | //monteCarloGpuKernelCpu (samplePricesCpu, sampleWeightsCpu, timesCpu, (1.0f / (float)SEQUENCE_LENGTH), optionStructs); 139 | gettimeofday(&end,NULL); 140 | 141 | //retrieve the average price 142 | float cumPrice = 0.0f; 143 | 144 | //add all the computed prices together 145 | int numSamp; 146 | for (numSamp = 0; numSamp < NUM_SAMPLES; numSamp++) 147 | { 148 | cumPrice += samplePricesCpu[numSamp]; 149 | } 150 | 151 | float avgPrice = cumPrice / NUM_SAMPLES; 152 | printf("Average price on CPU: %f\n", avgPrice); 153 | printf("Runtime on CPU: %f ms\n", (((end.tv_sec - begin.tv_sec) * 1e6 + (end.tv_usec - begin.tv_usec)) / 1000.0f)); 154 | 155 | //free memory space on the CPU 156 | free(samplePricesCpu); 157 | free(sampleWeightsCpu); 158 | free(timesCpu); 159 | free(randStates); 160 | } 161 | } 162 | 163 | -------------------------------------------------------------------------------- /Monte-Carlo/HMPP-OpenCL/monteCarloEngineHmppOpenCL.c: -------------------------------------------------------------------------------- 1 | //monteCarloEngineHmppOpenCL.c 2 | //Scott Grauer-Gray 3 | //Main function to drive monte-carlo on the gpu using HMPP 4 | 5 | // includes, system 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "monteCarloEngine.c" 12 | 13 | 14 | //////////////////////////////////////////////////////////////////////////////// 15 | // Program main 16 | //////////////////////////////////////////////////////////////////////////////// 17 | int 18 | main( int argc, char** argv) 19 | { 20 | srand (time (NULL)); 21 | 22 | runMonteCarlo(); 23 | 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Monte-Carlo/HMPP-OpenCL/monteCarloKernels.c: -------------------------------------------------------------------------------- 1 | //monteCarloKernels.c 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //GPU Kernels for running monte carlo 5 | 6 | #include "monteCarloKernels.h" 7 | 8 | //function to compute the inverse normal distribution 9 | float compInverseNormDist(float x) 10 | { 11 | float z = x - 0.5; 12 | float r = z*z; 13 | z = (((((A_1 * r + A_2) * r+A_3) * r+A_4) * r + A_5) * r + A_6) * z / 14 | (((((B_1 * r + B_2) * r+B_3) * r+B_4) * r + B_5) * r + 1.0); 15 | return z; 16 | } 17 | 18 | float interestRateCompoundFact(float t, float rate) 19 | { 20 | //assuming "continuous" option 21 | return exp(rate*t); 22 | } 23 | 24 | float interestRateDiscountFact(float t, float rate) 25 | { 26 | return 1.0/interestRateCompoundFact(t, rate); 27 | } 28 | 29 | float flatForwardDiscountImpl(float t, float rate) 30 | { 31 | return interestRateDiscountFact(t, rate); 32 | } 33 | 34 | float yieldTermStructDiscount(float t, float rate) 35 | { 36 | return flatForwardDiscountImpl(t, rate); 37 | } 38 | 39 | float interestRateImpliedRate(float compound, float t) 40 | { 41 | float r = log(compound) / t; 42 | return r; 43 | } 44 | 45 | 46 | float yieldTermStructForwardRate(float t1, float t2, float rate) 47 | { 48 | float compound = interestRateDiscountFact(t1, rate) / interestRateDiscountFact(t2, rate); 49 | return interestRateImpliedRate(compound, t2 - t1); 50 | } 51 | 52 | float processDrift(float t, float x, monteCarloOptionStruct optionStruct) 53 | { 54 | float sigma = processDiff(t, x, optionStruct); 55 | float t1 = t + 0.0001; 56 | 57 | return yieldTermStructForwardRate(t, t1, optionStruct.riskVal) - yieldTermStructForwardRate(t, t1, optionStruct.divVal) - (0.5 * sigma * sigma); 58 | } 59 | 60 | float discretizationDrift(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 61 | { 62 | return processDrift(t0, x0, optionStruct) * dt; 63 | } 64 | 65 | float localVoltLocVol(float t, float underlyingLevel, monteCarloOptionStruct optionStruct) 66 | { 67 | return optionStruct.voltVal; 68 | } 69 | 70 | float processDiff(float t, float x, monteCarloOptionStruct optionStruct) 71 | { 72 | return localVoltLocVol(t, x, optionStruct); 73 | } 74 | 75 | float discDiff(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 76 | { 77 | return processDiff(t0, x0, optionStruct) * sqrt(dt); 78 | } 79 | 80 | float stdDeviation(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 81 | { 82 | return discDiff(t0, x0, dt, optionStruct); 83 | } 84 | 85 | float apply(float x0, float dx) 86 | { 87 | return (x0 * exp(dx)); 88 | } 89 | 90 | float discDrift(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 91 | { 92 | return processDrift(t0, x0, optionStruct) * dt; 93 | } 94 | 95 | float processEvolve(float t0, float x0, float dt, float dw, monteCarloOptionStruct optionStruct) 96 | { 97 | return apply(x0, discDrift(t0, x0, dt, optionStruct) + stdDeviation(t0, x0, dt, optionStruct) *dw); 98 | } 99 | 100 | //retrieve the current sequence 101 | void getSequence(float* sequence, float sampleNum) 102 | { 103 | size_t iInSeq; 104 | 105 | #pragma hmppcg noParallel 106 | for (iInSeq = 0; iInSeq < SEQUENCE_LENGTH; iInSeq++) 107 | { 108 | sequence [iInSeq] = DEFAULT_SEQ_VAL; 109 | } 110 | } 111 | 112 | 113 | float getProcessValX0(monteCarloOptionStruct optionStruct) 114 | { 115 | return optionStruct.underlyingVal; 116 | } 117 | 118 | 119 | 120 | float getPrice (float val) 121 | { 122 | float v = STRIKE_VAL - val; 123 | if (v < 0) 124 | return 0.0f; 125 | return v * DISCOUNT_VAL; 126 | } 127 | 128 | 129 | void initializePath(float* path) 130 | { 131 | int i; 132 | 133 | #pragma hmppcg noParallel 134 | for (i = 0; i < SEQUENCE_LENGTH; i++) 135 | { 136 | path [i] = START_PATH_VAL; 137 | } 138 | } 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /Monte-Carlo/HMPP-OpenCL/monteCarloKernels.h: -------------------------------------------------------------------------------- 1 | //monteCarloKernels.cuh 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //Kernel headers for running monte carlo on the GPU 5 | 6 | #ifndef MONTE_CARLO_KERNELS_H 7 | #define MONTE_CARLO_KERNELS_H 8 | 9 | #include 10 | 11 | //needed for constants related to monte carlo 12 | #include "monteCarloConstants.h" 13 | 14 | //needed for structs related to monte carlo 15 | #include "monteCarloStructs.h" 16 | 17 | //needed for RNG 18 | #include "mt19937.h" 19 | 20 | //function to set up the random states 21 | 22 | float compInverseNormDist (float x) ; 23 | 24 | float interestRateCompoundFact (float t, float rate); 25 | 26 | float interestRateDiscountFact (float t, float rate); 27 | 28 | float flatForwardDiscountImpl (float t, float rate); 29 | 30 | float yieldTermStructDiscount (float t, float rate);// bool extrapolate) 31 | 32 | float interestRateImpliedRate (float compound, float t); 33 | 34 | float yieldTermStructForwardRate (float t1, float t2, float rate); //, float comp, float freq)//, bool extrapolate) 35 | 36 | float processDrift (float t, float x, monteCarloOptionStruct optionStruct); 37 | 38 | float discretizationDrift (float t0, float x0, float dt, monteCarloOptionStruct optionStruct); 39 | 40 | float localVoltLocVol (float t, float underlyingLevel, monteCarloOptionStruct optionStruct); 41 | 42 | float processDiff (float t, float x, monteCarloOptionStruct optionStruct); 43 | 44 | float discDiff (float t0, float x0, float dt, monteCarloOptionStruct optionStruct); 45 | 46 | float stdDeviation (float t0, float x0, float dt, monteCarloOptionStruct optionStruct); 47 | 48 | float apply (float x0, float dx); 49 | 50 | float discDrift (float t0, float x0, float dt, monteCarloOptionStruct optionStruct); 51 | 52 | float processEvolve (float t0, float x0, float dt, float dw, monteCarloOptionStruct optionStruct); 53 | 54 | //retrieve the current sequence 55 | void getSequence (float* sequence, float sampleNum); 56 | 57 | float getProcessValX0 (monteCarloOptionStruct optionStruct); 58 | 59 | void getPath (float* path, size_t sampleNum, float dt, mt19937state* state, monteCarloOptionStruct optionStruct); 60 | 61 | float getPrice (float val); 62 | 63 | //initialize the path 64 | void initializePath (float* path); 65 | 66 | #endif //MONTE_CARLO_KERNELS_H 67 | -------------------------------------------------------------------------------- /Monte-Carlo/HMPP-OpenCL/monteCarloStructs.h: -------------------------------------------------------------------------------- 1 | //monteCarloStructs.h 2 | //Scott Grauer-Gray 3 | //Header file with structures used in monte carlo computation 4 | 5 | #ifndef MONTE_CARLO_STRUCTS_H 6 | #define MONTE_CARLO_STRUCTS_H 7 | 8 | //struct for defining what's in an option 9 | typedef struct 10 | { 11 | float riskVal; 12 | float divVal; 13 | float voltVal; 14 | float underlyingVal; 15 | float strikeVal; 16 | float discountVal; 17 | } monteCarloOptionStruct; 18 | 19 | #endif //MONTE_CARLO_STRUCTS_CUH 20 | -------------------------------------------------------------------------------- /Monte-Carlo/HMPP-OpenCL/mt19937.c: -------------------------------------------------------------------------------- 1 | /* A C-program for MT19937: Real number version (1999/10/28) */ 2 | /* genrand() generates one pseudorandom real number (double) */ 3 | /* which is uniformly distributed on [0,1]-interval, for each */ 4 | /* call. sgenrand(seed) sets initial values to the working area */ 5 | /* of 624 words. Before genrand(), sgenrand(seed) must be */ 6 | /* called once. (seed is any 32-bit integer.) */ 7 | /* Integer generator is obtained by modifying two lines. */ 8 | /* Coded by Takuji Nishimura, considering the suggestions by */ 9 | /* Topher Cooper and Marc Rieffel in July-Aug. 1997. */ 10 | 11 | /* This library is free software; you can redistribute it and/or */ 12 | /* modify it under the terms of the GNU Library General Public */ 13 | /* License as published by the Free Software Foundation; either */ 14 | /* version 2 of the License, or (at your option) any later */ 15 | /* version. */ 16 | /* This library is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ 19 | /* See the GNU Library General Public License for more details. */ 20 | /* You should have received a copy of the GNU Library General */ 21 | /* Public License along with this library; if not, write to the */ 22 | /* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA */ 23 | /* 02111-1307 USA */ 24 | 25 | /* Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura. */ 26 | /* Any feedback is very welcome. For any question, comments, */ 27 | /* see http://www.math.keio.ac.jp/matumoto/emt.html or email */ 28 | /* matumoto@math.keio.ac.jp */ 29 | 30 | /* REFERENCE */ 31 | /* M. Matsumoto and T. Nishimura, */ 32 | /* "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform */ 33 | /* Pseudo-Random Number Generator", */ 34 | /* ACM Transactions on Modeling and Computer Simulation, */ 35 | /* Vol. 8, No. 1, January 1998, pp 3--30. */ 36 | 37 | #ifndef MT_19937_C 38 | #define MT_19937_C 39 | 40 | #include 41 | 42 | #include "mt19937.h" 43 | 44 | void 45 | initializeMersenneState (mt19937state* m, unsigned long seed) 46 | { 47 | m->seed = seed; 48 | m->mti = -1; 49 | } 50 | 51 | unsigned long 52 | generateRandInt (mt19937state* m, int sampleNum) 53 | { 54 | if (m[sampleNum].mti == -1) 55 | { 56 | int i; 57 | #pragma hmppcg noParallel 58 | for (i = 0; i < __N__; i++) 59 | { 60 | m[sampleNum].mt[i] = m[sampleNum].seed & 0xffff0000; 61 | m[sampleNum].seed = 69069 * m[sampleNum].seed + 1; 62 | m[sampleNum].mt[i] |= (m[sampleNum].seed & 0xffff0000) >> 16; 63 | m[sampleNum].seed = 69069 * m[sampleNum].seed + 1; 64 | } 65 | m[sampleNum].mti = __N__; 66 | } 67 | 68 | unsigned long y; 69 | unsigned long mag01 [2]; 70 | mag01 [0] = 0x0; 71 | mag01 [1] = MATRIX_A; 72 | 73 | /* mag01[x] = x * MATRIX_A for x=0,1 */ 74 | 75 | if (m[sampleNum].mti >= __N__) 76 | { /* generate N words at one time */ 77 | int kk = 0; 78 | #pragma hmppcg noParallel 79 | while (kk < __N__ - __M__) 80 | { 81 | y = (m[sampleNum].mt [kk] & UPPER_MASK) | (m[sampleNum].mt [kk + 1] & LOWER_MASK); 82 | m[sampleNum].mt [kk] = m[sampleNum].mt [kk + __M__] ^ (y >> 1) ^ mag01 [y & 0x1]; 83 | ++kk; 84 | } 85 | #pragma hmppcg noParallel 86 | while (kk < __N__ - 1) 87 | { 88 | y = (m[sampleNum].mt [kk] & UPPER_MASK) | (m[sampleNum].mt [kk + 1] & LOWER_MASK); 89 | m[sampleNum].mt [kk] = m[sampleNum].mt [kk + (__M__ - __N__)] ^ (y >> 1) ^ mag01 [y & 0x1]; 90 | ++kk; 91 | } 92 | 93 | y = (m[sampleNum].mt [__N__ - 1] & UPPER_MASK) | (m[sampleNum].mt [0] & LOWER_MASK); 94 | m[sampleNum].mt [__N__ - 1] = m[sampleNum].mt [__M__ - 1] ^ (y >> 1) ^ mag01 [y & 0x1]; 95 | 96 | m[sampleNum].mti = 0; 97 | } 98 | 99 | y = m[sampleNum].mt [m[sampleNum].mti++]; 100 | y ^= TEMPERING_SHIFT_U(y); 101 | y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B; 102 | y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C; 103 | y ^= TEMPERING_SHIFT_L(y); 104 | return y; 105 | } 106 | 107 | float 108 | generateRandFloat (mt19937state* m, int sampleNum) 109 | { 110 | return ( ((float) generateRandInt (m, sampleNum)) / UINT_MAX); 111 | } 112 | 113 | float 114 | generateRandDouble (mt19937state* m, int sampleNum) 115 | { 116 | return ( ((double) generateRandInt (m, sampleNum)) / UINT_MAX); 117 | } 118 | 119 | #endif 120 | -------------------------------------------------------------------------------- /Monte-Carlo/HMPP-OpenCL/mt19937.h: -------------------------------------------------------------------------------- 1 | /* A C-program for MT19937: Real number version (1999/10/28) */ 2 | /* genrand() generates one pseudorandom real number (double) */ 3 | /* which is uniformly distributed on [0,1]-interval, for each */ 4 | /* call. sgenrand(seed) sets initial values to the working area */ 5 | /* of 624 words. Before genrand(), sgenrand(seed) must be */ 6 | /* called once. (seed is any 32-bit integer.) */ 7 | /* Integer generator is obtained by modifying two lines. */ 8 | /* Coded by Takuji Nishimura, considering the suggestions by */ 9 | /* Topher Cooper and Marc Rieffel in July-Aug. 1997. */ 10 | 11 | /* This library is free software; you can redistribute it and/or */ 12 | /* modify it under the terms of the GNU Library General Public */ 13 | /* License as published by the Free Software Foundation; either */ 14 | /* version 2 of the License, or (at your option) any later */ 15 | /* version. */ 16 | /* This library is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ 19 | /* See the GNU Library General Public License for more details. */ 20 | /* You should have received a copy of the GNU Library General */ 21 | /* Public License along with this library; if not, write to the */ 22 | /* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA */ 23 | /* 02111-1307 USA */ 24 | 25 | /* Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura. */ 26 | /* Any feedback is very welcome. For any question, comments, */ 27 | /* see http://www.math.keio.ac.jp/matumoto/emt.html or email */ 28 | /* matumoto@math.keio.ac.jp */ 29 | 30 | /* REFERENCE */ 31 | /* M. Matsumoto and T. Nishimura, */ 32 | /* "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform */ 33 | /* Pseudo-Random Number Generator", */ 34 | /* ACM Transactions on Modeling and Computer Simulation, */ 35 | /* Vol. 8, No. 1, January 1998, pp 3--30. */ 36 | 37 | #ifndef MT_19937_H 38 | #define MT_19937_H 39 | 40 | /* Period parameters */ 41 | #define __N__ 624 42 | #define __M__ 397 43 | #define MATRIX_A 0x9908b0df /* constant vector a */ 44 | #define UPPER_MASK 0x80000000 /* most significant w-r bits */ 45 | #define LOWER_MASK 0x7fffffff /* least significant r bits */ 46 | 47 | /* Tempering parameters */ 48 | #define TEMPERING_MASK_B 0x9d2c5680 49 | #define TEMPERING_MASK_C 0xefc60000 50 | #define TEMPERING_SHIFT_U(y) (y >> 11) 51 | #define TEMPERING_SHIFT_S(y) (y << 7) 52 | #define TEMPERING_SHIFT_T(y) (y << 15) 53 | #define TEMPERING_SHIFT_L(y) (y >> 18) 54 | 55 | typedef struct mt19937_stateStruct 56 | { 57 | unsigned long mt [__N__]; 58 | int mti; 59 | unsigned long seed; 60 | unsigned int init; 61 | } mt19937state; 62 | 63 | mt19937state* 64 | GetMersenneState (); 65 | 66 | void 67 | initializeMersenneState (mt19937state* m, unsigned long seed); 68 | 69 | unsigned long 70 | generateRandInt (mt19937state* m, int sampleNum); 71 | 72 | float 73 | generateRandFloat (mt19937state* m, int sampleNum); 74 | 75 | float 76 | generateRandDouble (mt19937state* m, int sampleNum); 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenACC/Makefile: -------------------------------------------------------------------------------- 1 | EXE_NAME=monteCarloEngine 2 | MAIN_FILE=monteCarloEngineOpenACC.c 3 | 4 | include ../../compileHmppOpenACC.mk 5 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenACC/monteCarloConstants.h: -------------------------------------------------------------------------------- 1 | //monteCarloConstants.cuh 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //Constants for running monte carlo on the GPU/CPU 5 | 6 | #ifndef MONTE_CARLO_CONSTANTS_H 7 | #define MONTE_CARLO_CONSTANTS_H 8 | 9 | #define DEFAULT_SEQ_VAL 1.0f 10 | #define DEFAULT_SEQ_WEIGHT 1.0f 11 | 12 | 13 | #define RISK_VAL 0.06f 14 | #define DIV_VAL 0.0f 15 | #define VOLT_VAL 0.200f 16 | #define UNDERLYING_VAL 30.0f 17 | #define STRIKE_VAL 40.0f 18 | #define DISCOUNT_VAL 0.94176453358424872f 19 | #define START_PATH_VAL 1.0f 20 | #define DEFAULT_SEQ_VAL 1.0f 21 | #define DEFAULT_SEQ_WEIGHT 1.0f 22 | #define SEQUENCE_LENGTH 250 23 | 24 | 25 | #define A_1 -39.696830286653757 26 | #define A_2 220.94609842452050 27 | #define A_3 -275.92851044696869 28 | #define A_4 138.35775186726900 29 | #define A_5 -30.664798066147160 30 | #define A_6 2.5066282774592392 31 | #define B_1 -54.476098798224058 32 | #define B_2 161.58583685804089 33 | #define B_3 -155.69897985988661 34 | #define B_4 66.801311887719720 35 | #define B_5 -13.280681552885721 36 | 37 | 38 | //define the thread block size 39 | #define THREAD_BLOCK_SIZE 256 40 | 41 | //define the number of samples 42 | #define NUM_SAMPLES 400000 43 | 44 | //number of options 45 | #define NUM_OPTIONS 1 46 | 47 | //number of threads to use for processing each option 48 | #define NUM_THREADS_PER_OPTION NUM_SAMPLES 49 | 50 | #endif //MONTE_CARLO_CONSTANTS_CUH 51 | 52 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenACC/monteCarloEngine.c: -------------------------------------------------------------------------------- 1 | //monteCarloEngine.cu 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //Function for running Monte Carlo on the GPU 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "monteCarloKernels.c" 12 | #include "mt19937.c" 13 | 14 | 15 | void monteCarloKernelCpu(int n, float* samplePrices, float* sampleWeights, float dt, mt19937state* randStates, monteCarloOptionStruct optionStruct) 16 | { 17 | int i; 18 | 19 | for (i = 0; i < n; ++i) 20 | { 21 | 22 | float path[SEQUENCE_LENGTH]; 23 | initializePath (path); 24 | 25 | int optionStructNum = 0; 26 | 27 | getPath (path, i, dt, randStates, optionStruct); 28 | float price = getPrice (path [SEQUENCE_LENGTH-1]); 29 | 30 | samplePrices [i] = price; 31 | sampleWeights [i] = DEFAULT_SEQ_WEIGHT; 32 | } 33 | } 34 | 35 | void monteCarloKernel (int n, float* samplePrices, float* sampleWeights, float dt, mt19937state* randStates, monteCarloOptionStruct optionStruct) 36 | { 37 | int i; 38 | 39 | #pragma acc data copyin (randStates[0:n]), copyout (samplePrices[0:n], sampleWeights[0:n]) 40 | { 41 | #pragma acc kernels pcopyin (randStates[0:n]), pcopyout (samplePrices[0:n], sampleWeights[0:n]) 42 | { 43 | #pragma acc loop independent 44 | for (i = 0; i < n; ++i) 45 | { 46 | 47 | float path[SEQUENCE_LENGTH]; 48 | initializePath (path); 49 | 50 | int optionStructNum = 0; 51 | 52 | getPath (path, i, dt, randStates, optionStruct); 53 | float price = getPrice (path [SEQUENCE_LENGTH-1]); 54 | 55 | samplePrices [i] = price; 56 | sampleWeights [i] = DEFAULT_SEQ_WEIGHT; 57 | } 58 | } 59 | } 60 | } 61 | 62 | 63 | //run monte carlo... 64 | void runMonteCarlo () 65 | { 66 | //declare and initialize the struct used for the option 67 | monteCarloOptionStruct optionStruct; 68 | optionStruct.riskVal = RISK_VAL; 69 | optionStruct.divVal = DIV_VAL; 70 | optionStruct.voltVal = VOLT_VAL; 71 | optionStruct.underlyingVal = UNDERLYING_VAL; 72 | optionStruct.strikeVal = STRIKE_VAL; 73 | optionStruct.discountVal = DISCOUNT_VAL; 74 | 75 | struct timeval begin,end; 76 | mt19937state* randStates = (mt19937state*) malloc (NUM_SAMPLES * sizeof (mt19937state)); 77 | 78 | // Accelerator Version 79 | { 80 | //declare pointers for data on CPU 81 | float* samplePrices = (float*) malloc (NUM_SAMPLES * sizeof (float)); 82 | float* sampleWeights = (float*) malloc (NUM_SAMPLES * sizeof (float)); 83 | 84 | int numSamps = NUM_SAMPLES; 85 | printf("Number of Samples: %d\n", numSamps); 86 | int sampNum; 87 | for (sampNum = 0; sampNum < numSamps; sampNum++) 88 | { 89 | initializeMersenneState (&(randStates[sampNum]), rand ()); 90 | } 91 | 92 | gettimeofday(&begin,NULL); 93 | 94 | //run monte-carlo on GPU using OpenACC 95 | monteCarloKernel (NUM_SAMPLES, samplePrices, sampleWeights, (1.0f / (float)SEQUENCE_LENGTH), randStates, optionStruct); 96 | 97 | gettimeofday(&end,NULL); 98 | 99 | //retrieve the average price 100 | float cumPrice = 0.0f; 101 | 102 | //add all the computed prices together 103 | int numSamp; 104 | for (numSamp = 0; numSamp < NUM_SAMPLES; numSamp++) 105 | { 106 | cumPrice += samplePrices [numSamp]; 107 | } 108 | 109 | float avgPrice = cumPrice / NUM_SAMPLES; 110 | printf("Average price on GPU: %f\n", avgPrice); 111 | printf("Runtime on GPU: %f ms\n", (((end.tv_sec - begin.tv_sec) * 1e6 + (end.tv_usec - begin.tv_usec)) / 1000.0f)); 112 | 113 | free(samplePrices); 114 | free(sampleWeights); 115 | } 116 | 117 | 118 | // CPU Version 119 | { 120 | //declare pointers for data on CPU 121 | float* samplePricesCpu; 122 | float* sampleWeightsCpu; 123 | 124 | //allocate space for data on CPU 125 | samplePricesCpu = (float*) malloc (NUM_SAMPLES * sizeof (float)); 126 | sampleWeightsCpu = (float*) malloc (NUM_SAMPLES * sizeof (float)); 127 | 128 | int sampNum; 129 | for (sampNum = 0; sampNum < NUM_SAMPLES; sampNum++) 130 | { 131 | initializeMersenneState (&randStates [sampNum], rand ()); 132 | } 133 | 134 | gettimeofday(&begin,NULL); 135 | monteCarloKernelCpu (NUM_SAMPLES, samplePricesCpu, sampleWeightsCpu, (1.0f / (float)SEQUENCE_LENGTH), randStates, optionStruct); 136 | gettimeofday(&end,NULL); 137 | 138 | //retrieve the average price 139 | float cumPrice = 0.0f; 140 | 141 | //add all the computed prices together 142 | int numSamp; 143 | for (numSamp = 0; numSamp < NUM_SAMPLES; numSamp++) 144 | { 145 | cumPrice += samplePricesCpu[numSamp]; 146 | } 147 | 148 | float avgPrice = cumPrice / NUM_SAMPLES; 149 | printf("Average price on CPU: %f\n", avgPrice); 150 | printf("Runtime on CPU: %f ms\n", (((end.tv_sec - begin.tv_sec) * 1e6 + (end.tv_usec - begin.tv_usec)) / 1000.0f)); 151 | 152 | //free memory space on the CPU 153 | free(samplePricesCpu); 154 | free(sampleWeightsCpu); 155 | } 156 | 157 | free(randStates); 158 | } 159 | 160 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenACC/monteCarloEngineHmppOpenACC.c: -------------------------------------------------------------------------------- 1 | //monteCarloEngineHmppOpenACC.c 2 | //Scott Grauer-Gray 3 | //Main function for running monte-carlo using OpenACC 4 | 5 | // includes, system 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "monteCarloEngine.c" 12 | 13 | 14 | int 15 | main( int argc, char** argv) 16 | { 17 | srand (time (NULL)); 18 | 19 | runMonteCarlo(); 20 | 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenACC/monteCarloEngineOpenACC.c: -------------------------------------------------------------------------------- 1 | //monteCarloEngineOpenACC.c 2 | //Scott Grauer-Gray 3 | //Main function to drive monte-carlo on the gpu using OpenACC 4 | 5 | // includes, system 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "monteCarloEngine.c" 12 | 13 | 14 | //////////////////////////////////////////////////////////////////////////////// 15 | // Program main 16 | //////////////////////////////////////////////////////////////////////////////// 17 | int 18 | main( int argc, char** argv) 19 | { 20 | srand (time (NULL)); 21 | 22 | runMonteCarlo(); 23 | 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenACC/monteCarloKernels.c: -------------------------------------------------------------------------------- 1 | //monteCarloKernels.c 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //GPU Kernels for running monte carlo 5 | 6 | 7 | #include "monteCarloKernels.h" 8 | 9 | //function to compute the inverse normal distribution 10 | float compInverseNormDist(float x) 11 | { 12 | float z = x - 0.5; 13 | float r = z*z; 14 | z = (((((A_1 * r + A_2) * r+A_3) * r+A_4) * r + A_5) * r + A_6) * z / 15 | (((((B_1 * r + B_2) * r+B_3) * r+B_4) * r + B_5) * r + 1.0); 16 | return z; 17 | } 18 | 19 | float interestRateCompoundFact(float t, float rate) 20 | { 21 | //assuming "continuous" option 22 | return exp(rate*t); 23 | } 24 | 25 | float interestRateDiscountFact(float t, float rate) 26 | { 27 | return 1.0/interestRateCompoundFact(t, rate); 28 | } 29 | 30 | float flatForwardDiscountImpl(float t, float rate) 31 | { 32 | return interestRateDiscountFact(t, rate); 33 | } 34 | 35 | float yieldTermStructDiscount(float t, float rate) 36 | { 37 | return flatForwardDiscountImpl(t, rate); 38 | } 39 | 40 | float interestRateImpliedRate(float compound, float t) 41 | { 42 | float r = log(compound) / t; 43 | return r; 44 | } 45 | 46 | 47 | float yieldTermStructForwardRate(float t1, float t2, float rate) 48 | { 49 | float compound = interestRateDiscountFact(t1, rate) / interestRateDiscountFact(t2, rate); 50 | return interestRateImpliedRate(compound, t2 - t1); 51 | } 52 | 53 | float processDrift(float t, float x, monteCarloOptionStruct optionStruct) 54 | { 55 | float sigma = processDiff(t, x, optionStruct); 56 | float t1 = t + 0.0001; 57 | 58 | return yieldTermStructForwardRate(t, t1, optionStruct.riskVal) - yieldTermStructForwardRate(t, t1, optionStruct.divVal) - (0.5 * sigma * sigma); 59 | } 60 | 61 | float discretizationDrift(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 62 | { 63 | return processDrift(t0, x0, optionStruct) * dt; 64 | } 65 | 66 | float localVoltLocVol(float t, float underlyingLevel, monteCarloOptionStruct optionStruct) 67 | { 68 | return optionStruct.voltVal; 69 | } 70 | 71 | float processDiff(float t, float x, monteCarloOptionStruct optionStruct) 72 | { 73 | return localVoltLocVol(t, x, optionStruct); 74 | } 75 | 76 | float discDiff(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 77 | { 78 | return processDiff(t0, x0, optionStruct) * sqrt(dt); 79 | } 80 | 81 | float stdDeviation(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 82 | { 83 | return discDiff(t0, x0, dt, optionStruct); 84 | } 85 | 86 | float apply(float x0, float dx) 87 | { 88 | return (x0 * exp(dx)); 89 | } 90 | 91 | float discDrift(float t0, float x0, float dt, monteCarloOptionStruct optionStruct) 92 | { 93 | return processDrift(t0, x0, optionStruct) * dt; 94 | } 95 | 96 | float processEvolve(float t0, float x0, float dt, float dw, monteCarloOptionStruct optionStruct) 97 | { 98 | return apply(x0, discDrift(t0, x0, dt, optionStruct) + stdDeviation(t0, x0, dt, optionStruct) *dw); 99 | } 100 | 101 | //retrieve the current sequence 102 | void getSequence(float* sequence, float sampleNum) 103 | { 104 | size_t iInSeq; 105 | 106 | #pragma hmppcg noParallel 107 | for (iInSeq = 0; iInSeq < SEQUENCE_LENGTH; iInSeq++) 108 | { 109 | sequence [iInSeq] = DEFAULT_SEQ_VAL; 110 | } 111 | } 112 | 113 | 114 | float getProcessValX0(monteCarloOptionStruct optionStruct) 115 | { 116 | return optionStruct.underlyingVal; 117 | } 118 | 119 | void getPath(float* path, size_t sampleNum, float dt, mt19937state* state, monteCarloOptionStruct optionStruct) 120 | { 121 | float weight = DEFAULT_SEQ_WEIGHT; 122 | path [0] = getProcessValX0(optionStruct); 123 | size_t i; 124 | 125 | #pragma hmppcg noParallel 126 | for (i = 1; i < SEQUENCE_LENGTH; i++) 127 | { 128 | float t = i * dt; 129 | float randVal = generateRandFloat (&(state [sampleNum])); 130 | float inverseCumRandVal = compInverseNormDist (randVal); 131 | path [i] = processEvolve(t, path [i - 1], dt, inverseCumRandVal, optionStruct); 132 | } 133 | } 134 | 135 | 136 | float getPrice (float val) 137 | { 138 | float v = STRIKE_VAL - val; 139 | if (v < 0) 140 | return 0.0f; 141 | return v * DISCOUNT_VAL; 142 | } 143 | 144 | void initializePath(float* path) 145 | { 146 | int i; 147 | 148 | #pragma hmppcg noParallel 149 | for (i = 0; i < SEQUENCE_LENGTH; i++) 150 | { 151 | path [i] = START_PATH_VAL; 152 | } 153 | } 154 | 155 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenACC/monteCarloKernels.h: -------------------------------------------------------------------------------- 1 | //monteCarloKernels.h 2 | //Scott Grauer-Gray 3 | //Kernel headers for running monte carlo on the GPU 4 | 5 | #ifndef MONTE_CARLO_KERNELS_H 6 | #define MONTE_CARLO_KERNELS_H 7 | 8 | #include 9 | 10 | //needed for constants related to monte carlo 11 | #include "monteCarloConstants.h" 12 | 13 | //needed for structs related to monte carlo 14 | #include "monteCarloStructs.h" 15 | 16 | //needed for RNG 17 | #include "mt19937.h" 18 | 19 | //function to set up the random states 20 | 21 | float compInverseNormDist (float x) ; 22 | 23 | float interestRateCompoundFact (float t, float rate); 24 | 25 | float interestRateDiscountFact (float t, float rate); 26 | 27 | float flatForwardDiscountImpl (float t, float rate); 28 | 29 | float yieldTermStructDiscount (float t, float rate); 30 | 31 | float interestRateImpliedRate (float compound, float t); 32 | 33 | float yieldTermStructForwardRate (float t1, float t2, float rate); 34 | 35 | float processDrift (float t, float x, monteCarloOptionStruct optionStruct); 36 | 37 | float discretizationDrift (float t0, float x0, float dt, monteCarloOptionStruct optionStruct); 38 | 39 | float localVoltLocVol (float t, float underlyingLevel, monteCarloOptionStruct optionStruct); 40 | 41 | float processDiff (float t, float x, monteCarloOptionStruct optionStruct); 42 | 43 | float discDiff (float t0, float x0, float dt, monteCarloOptionStruct optionStruct); 44 | 45 | float stdDeviation (float t0, float x0, float dt, monteCarloOptionStruct optionStruct); 46 | 47 | float apply (float x0, float dx); 48 | 49 | float discDrift (float t0, float x0, float dt, monteCarloOptionStruct optionStruct); 50 | 51 | float processEvolve (float t0, float x0, float dt, float dw, monteCarloOptionStruct optionStruct); 52 | 53 | //retrieve the current sequence 54 | void getSequence (float* sequence, float sampleNum); 55 | 56 | float getProcessValX0 (monteCarloOptionStruct optionStruct); 57 | 58 | void getPath (float* path, size_t sampleNum, float dt, mt19937state* state, monteCarloOptionStruct optionStruct); 59 | 60 | float getPrice (float val); 61 | 62 | //initialize the path 63 | void initializePath (float* path); 64 | 65 | #endif //MONTE_CARLO_KERNELS_H 66 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenACC/monteCarloStructs.h: -------------------------------------------------------------------------------- 1 | //monteCarloStructs.h 2 | //Scott Grauer-Gray 3 | //May 24, 2012 4 | //Header file with structures used in monte carlo computation 5 | 6 | #ifndef MONTE_CARLO_STRUCTS_H 7 | #define MONTE_CARLO_STRUCTS_H 8 | 9 | //struct for defining what's in an option 10 | typedef struct 11 | { 12 | float riskVal; 13 | float divVal; 14 | float voltVal; 15 | float underlyingVal; 16 | float strikeVal; 17 | float discountVal; 18 | } monteCarloOptionStruct; 19 | 20 | #endif //MONTE_CARLO_STRUCTS_CUH 21 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenACC/mt19937.c: -------------------------------------------------------------------------------- 1 | /* A C-program for MT19937: Real number version (1999/10/28) */ 2 | /* genrand() generates one pseudorandom real number (double) */ 3 | /* which is uniformly distributed on [0,1]-interval, for each */ 4 | /* call. sgenrand(seed) sets initial values to the working area */ 5 | /* of 624 words. Before genrand(), sgenrand(seed) must be */ 6 | /* called once. (seed is any 32-bit integer.) */ 7 | /* Integer generator is obtained by modifying two lines. */ 8 | /* Coded by Takuji Nishimura, considering the suggestions by */ 9 | /* Topher Cooper and Marc Rieffel in July-Aug. 1997. */ 10 | 11 | /* This library is free software; you can redistribute it and/or */ 12 | /* modify it under the terms of the GNU Library General Public */ 13 | /* License as published by the Free Software Foundation; either */ 14 | /* version 2 of the License, or (at your option) any later */ 15 | /* version. */ 16 | /* This library is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ 19 | /* See the GNU Library General Public License for more details. */ 20 | /* You should have received a copy of the GNU Library General */ 21 | /* Public License along with this library; if not, write to the */ 22 | /* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA */ 23 | /* 02111-1307 USA */ 24 | 25 | /* Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura. */ 26 | /* Any feedback is very welcome. For any question, comments, */ 27 | /* see http://www.math.keio.ac.jp/matumoto/emt.html or email */ 28 | /* matumoto@math.keio.ac.jp */ 29 | 30 | /* REFERENCE */ 31 | /* M. Matsumoto and T. Nishimura, */ 32 | /* "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform */ 33 | /* Pseudo-Random Number Generator", */ 34 | /* ACM Transactions on Modeling and Computer Simulation, */ 35 | /* Vol. 8, No. 1, January 1998, pp 3--30. */ 36 | 37 | #ifndef MT_19937_C 38 | #define MT_19937_C 39 | 40 | #include 41 | 42 | #include "mt19937.h" 43 | 44 | void 45 | initializeMersenneState (mt19937state* m, unsigned long seed) 46 | { 47 | m->seed = seed; 48 | m->mti = -1; 49 | } 50 | 51 | unsigned long 52 | generateRandInt (mt19937state* m) 53 | { 54 | if (m->mti == -1) 55 | { 56 | int i; 57 | #pragma hmppcg noParallel 58 | for (i = 0; i < __N__; i++) 59 | { 60 | m->mt[i] = m->seed & 0xffff0000; 61 | m->seed = 69069 * m->seed + 1; 62 | m->mt[i] |= (m->seed & 0xffff0000) >> 16; 63 | m->seed = 69069 * m->seed + 1; 64 | } 65 | m->mti = __N__; 66 | } 67 | 68 | unsigned long y; 69 | unsigned long mag01 [2]; 70 | mag01 [0] = 0x0; 71 | mag01 [1] = MATRIX_A; 72 | 73 | /* mag01[x] = x * MATRIX_A for x=0,1 */ 74 | 75 | if (m->mti >= __N__) 76 | { /* generate N words at one time */ 77 | int kk = 0; 78 | #pragma hmppcg noParallel 79 | while (kk < __N__ - __M__) 80 | { 81 | y = (m->mt [kk] & UPPER_MASK) | (m->mt [kk + 1] & LOWER_MASK); 82 | m->mt [kk] = m->mt [kk + __M__] ^ (y >> 1) ^ mag01 [y & 0x1]; 83 | ++kk; 84 | } 85 | #pragma hmppcg noParallel 86 | while (kk < __N__ - 1) 87 | { 88 | y = (m->mt [kk] & UPPER_MASK) | (m->mt [kk + 1] & LOWER_MASK); 89 | m->mt [kk] = m->mt [kk + (__M__ - __N__)] ^ (y >> 1) ^ mag01 [y & 0x1]; 90 | ++kk; 91 | } 92 | 93 | y = (m->mt [__N__ - 1] & UPPER_MASK) | (m->mt [0] & LOWER_MASK); 94 | m->mt [__N__ - 1] = m->mt [__M__ - 1] ^ (y >> 1) ^ mag01 [y & 0x1]; 95 | 96 | m->mti = 0; 97 | } 98 | 99 | y = m->mt [m->mti++]; 100 | y ^= TEMPERING_SHIFT_U(y); 101 | y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B; 102 | y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C; 103 | y ^= TEMPERING_SHIFT_L(y); 104 | return y; 105 | } 106 | 107 | float 108 | generateRandFloat (mt19937state* m) 109 | { 110 | return ( ((float) generateRandInt (m)) / UINT_MAX); 111 | } 112 | 113 | float 114 | generateRandDouble (mt19937state* m) 115 | { 116 | return ( ((double) generateRandInt (m)) / UINT_MAX); 117 | } 118 | 119 | #endif 120 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenACC/mt19937.h: -------------------------------------------------------------------------------- 1 | /* A C-program for MT19937: Real number version (1999/10/28) */ 2 | /* genrand() generates one pseudorandom real number (double) */ 3 | /* which is uniformly distributed on [0,1]-interval, for each */ 4 | /* call. sgenrand(seed) sets initial values to the working area */ 5 | /* of 624 words. Before genrand(), sgenrand(seed) must be */ 6 | /* called once. (seed is any 32-bit integer.) */ 7 | /* Integer generator is obtained by modifying two lines. */ 8 | /* Coded by Takuji Nishimura, considering the suggestions by */ 9 | /* Topher Cooper and Marc Rieffel in July-Aug. 1997. */ 10 | 11 | /* This library is free software; you can redistribute it and/or */ 12 | /* modify it under the terms of the GNU Library General Public */ 13 | /* License as published by the Free Software Foundation; either */ 14 | /* version 2 of the License, or (at your option) any later */ 15 | /* version. */ 16 | /* This library is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ 19 | /* See the GNU Library General Public License for more details. */ 20 | /* You should have received a copy of the GNU Library General */ 21 | /* Public License along with this library; if not, write to the */ 22 | /* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA */ 23 | /* 02111-1307 USA */ 24 | 25 | /* Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura. */ 26 | /* Any feedback is very welcome. For any question, comments, */ 27 | /* see http://www.math.keio.ac.jp/matumoto/emt.html or email */ 28 | /* matumoto@math.keio.ac.jp */ 29 | 30 | /* REFERENCE */ 31 | /* M. Matsumoto and T. Nishimura, */ 32 | /* "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform */ 33 | /* Pseudo-Random Number Generator", */ 34 | /* ACM Transactions on Modeling and Computer Simulation, */ 35 | /* Vol. 8, No. 1, January 1998, pp 3--30. */ 36 | 37 | #ifndef MT_19937_H 38 | #define MT_19937_H 39 | 40 | /* Period parameters */ 41 | #define __N__ 624 42 | #define __M__ 397 43 | #define MATRIX_A 0x9908b0df /* constant vector a */ 44 | #define UPPER_MASK 0x80000000 /* most significant w-r bits */ 45 | #define LOWER_MASK 0x7fffffff /* least significant r bits */ 46 | 47 | /* Tempering parameters */ 48 | #define TEMPERING_MASK_B 0x9d2c5680 49 | #define TEMPERING_MASK_C 0xefc60000 50 | #define TEMPERING_SHIFT_U(y) (y >> 11) 51 | #define TEMPERING_SHIFT_S(y) (y << 7) 52 | #define TEMPERING_SHIFT_T(y) (y << 15) 53 | #define TEMPERING_SHIFT_L(y) (y >> 18) 54 | 55 | typedef struct mt19937_stateStruct 56 | { 57 | unsigned long mt [__N__]; 58 | int mti; 59 | unsigned long seed; 60 | unsigned int init; 61 | } mt19937state; 62 | 63 | mt19937state* 64 | GetMersenneState (); 65 | 66 | void 67 | initializeMersenneState (mt19937state* m, unsigned long seed); 68 | 69 | unsigned long 70 | generateRandInt (mt19937state* m); 71 | 72 | float 73 | generateRandFloat (mt19937state* m); 74 | 75 | float 76 | generateRandDouble (mt19937state* m); 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenCL/Makefile: -------------------------------------------------------------------------------- 1 | INCLUDE=-I/usr/local/cuda-5.0/include 2 | #LIBPATH=-L${OpenCL_SDK}/OpenCL/common/lib -L${OpenCL_SDK}/shared/lib 3 | LIB=-lOpenCL -lm 4 | all: 5 | g++ -O3 ${INCLUDE} ${LIBPATH} ${LIB} monteCarloEngine.c -o monteCarloEngine.exe 6 | 7 | clean: 8 | rm -f *~ *.exe 9 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenCL/monteCarloConstants.h: -------------------------------------------------------------------------------- 1 | //monteCarloConstants.cuh 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //Constants for running monte carlo on the GPU/CPU 5 | 6 | #ifndef MONTE_CARLO_CONSTANTS_CUH 7 | #define MONTE_CARLO_CONSTANTS_CUH 8 | 9 | #define DEFAULT_SEQ_VAL 1.0f 10 | #define DEFAULT_SEQ_WEIGHT 1.0f 11 | 12 | 13 | #define RISK_VAL 0.06f 14 | #define DIV_VAL 0.0f 15 | #define VOLT_VAL 0.200f 16 | #define UNDERLYING_VAL 30.0f 17 | #define STRIKE_VAL 40.0f 18 | #define DISCOUNT_VAL 0.94176453358424872f 19 | #define START_PATH_VAL 1.0f 20 | #define DEFAULT_SEQ_VAL 1.0f 21 | #define DEFAULT_SEQ_WEIGHT 1.0f 22 | #define SEQUENCE_LENGTH 250 23 | 24 | 25 | //define the thread block size 26 | #define THREAD_BLOCK_SIZE 256 27 | 28 | //define the number of samples 29 | #define NUM_SAMPLES 400000 30 | 31 | //number of options 32 | #define NUM_OPTIONS 1 33 | 34 | //number of threads to use for processing each option 35 | #define NUM_THREADS_PER_OPTION NUM_SAMPLES 36 | 37 | 38 | #endif //MONTE_CARLO_CONSTANTS_CUH 39 | 40 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenCL/monteCarloKernels.h: -------------------------------------------------------------------------------- 1 | //monteCarloKernels.cuh 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //Kernel headers for running monte carlo on the GPU 5 | 6 | #ifndef MONTE_CARLO_KERNELS_CUH 7 | #define MONTE_CARLO_KERNELS_CUH 8 | 9 | //needed for constants related to monte carlo 10 | #include "monteCarloConstants.h" 11 | 12 | #include "mt19937.h" 13 | 14 | //needed for structs related to monte carlo 15 | #include "monteCarloStructs.h" 16 | 17 | 18 | //function to compute the inverse normal distribution 19 | dataType compInverseNormDist(dataType x) ; 20 | 21 | 22 | dataType interestRateCompoundFact(dataType t, dataType rate); 23 | 24 | 25 | dataType interestRateDiscountFact(dataType t, dataType rate); 26 | 27 | 28 | dataType flatForwardDiscountImpl(dataType t, dataType rate); 29 | 30 | 31 | dataType yieldTermStructDiscount(dataType t, dataType rate); 32 | 33 | 34 | dataType interestRateImpliedRate(dataType compound, dataType t); 35 | 36 | 37 | dataType yieldTermStructForwardRate(dataType t1, dataType t2, dataType rate); 38 | 39 | 40 | dataType processDrift(dataType t, dataType x, monteCarloOptionStruct optionStruct); 41 | 42 | 43 | dataType discretizationDrift(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct); 44 | 45 | 46 | dataType localVoltLocVol(dataType t, dataType underlyingLevel, monteCarloOptionStruct optionStruct); 47 | 48 | 49 | dataType processDiff(dataType t, dataType x, monteCarloOptionStruct optionStruct); 50 | 51 | 52 | dataType discDiff(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct); 53 | 54 | 55 | dataType stdDeviation(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct); 56 | 57 | 58 | dataType apply(dataType x0, dataType dx); 59 | 60 | 61 | dataType discDrift(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct); 62 | 63 | 64 | dataType processEvolve(dataType t0, dataType x0, dataType dt, dataType dw, monteCarloOptionStruct optionStruct); 65 | 66 | 67 | //retrieve the current sequence 68 | void getSequence(dataType* sequence, dataType sampleNum); 69 | 70 | 71 | dataType getProcessValX0(monteCarloOptionStruct optionStruct); 72 | 73 | 74 | void getPath(dataType* path, size_t sampleNum, dataType dt, __global mt19937state* state, monteCarloOptionStruct optionStruct); 75 | 76 | 77 | dataType getPrice(dataType val); 78 | 79 | 80 | //initialize the path 81 | void initializePath(dataType* path); 82 | 83 | 84 | __kernel void monteCarloGpuKernel(__global dataType* samplePrices, __global dataType* sampleWeights, __global dataType* times, dataType dt, __global mt19937state* randStates, __global monteCarloOptionStruct* optionStructs, int numSamples); 85 | 86 | 87 | 88 | 89 | #endif //MONTE_CARLO_KERNELS_CUH 90 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenCL/monteCarloKernelsCpu.c: -------------------------------------------------------------------------------- 1 | //monteCarloKernelsCpu.cu 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //monte carlo kernels run on the CPU 5 | 6 | 7 | #include "monteCarloKernelsCpu.h" 8 | #define A_1 -39.696830286653757 9 | #define A_2 220.94609842452050 10 | #define A_3 -275.92851044696869 11 | #define A_4 138.35775186726900 12 | #define A_5 -30.664798066147160 13 | #define A_6 2.5066282774592392 14 | #define B_1 -54.476098798224058 15 | #define B_2 161.58583685804089 16 | #define B_3 -155.69897985988661 17 | #define B_4 66.801311887719720 18 | #define B_5 -13.280681552885721 19 | 20 | //function to set up the random states 21 | void setup_kernelCpu( ) 22 | { 23 | 24 | srand(time(NULL)); 25 | } 26 | 27 | 28 | //function to compute the inverse normal distribution 29 | dataType compInverseNormDistCpu(dataType x) 30 | { 31 | dataType z; 32 | //if (x < x_low_ || x_high_ < x) { 33 | //z = tail_value(x); 34 | //} else { 35 | 36 | z = x - 0.5; 37 | dataType r = z*z; 38 | z = (((((A_1*r+A_2)*r+A_3)*r+A_4)*r+A_5)*r+A_6)*z / 39 | (((((B_1*r+B_2)*r+B_3)*r+B_4)*r+B_5)*r+1.0); 40 | 41 | return z; 42 | } 43 | 44 | dataType interestRateCompoundFactCpu(dataType t, dataType rate) 45 | { 46 | //assuming "continuous" option 47 | return exp(rate*t); 48 | } 49 | 50 | dataType interestRateDiscountFactCpu(dataType t, dataType rate) 51 | { 52 | return 1.0/interestRateCompoundFactCpu(t, rate); 53 | } 54 | 55 | dataType flatForwardDiscountImplCpu(dataType t, dataType rate) 56 | { 57 | return interestRateDiscountFactCpu(t, rate); 58 | } 59 | 60 | dataType yieldTermStructDiscountCpu(dataType t, dataType rate) 61 | { 62 | return flatForwardDiscountImplCpu(t, rate); 63 | } 64 | 65 | dataType interestRateImpliedRateCpu(dataType compound, dataType t) 66 | { 67 | dataType r = log(compound) / t; 68 | return r; 69 | } 70 | 71 | 72 | dataType yieldTermStructForwardRateCpu(dataType t1, dataType t2, dataType rate) 73 | { 74 | dataType compound = interestRateDiscountFactCpu(t1, rate) / interestRateDiscountFactCpu(t2, rate); 75 | return interestRateImpliedRateCpu(compound, t2 - t1); 76 | } 77 | 78 | dataType processDriftCpu(dataType t, dataType x, monteCarloOptionStruct optionStruct) 79 | { 80 | dataType sigma = processDiffCpu(t, x, optionStruct); 81 | dataType t1 = t + 0.0001; 82 | 83 | return yieldTermStructForwardRateCpu(t, t1, optionStruct.riskVal) - yieldTermStructForwardRateCpu(t, t1, optionStruct.divVal) - (0.5 * sigma * sigma); 84 | } 85 | 86 | dataType discretizationDriftCpu(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct) 87 | { 88 | return processDriftCpu(t0, x0, optionStruct) * dt; 89 | } 90 | 91 | dataType localVoltLocVolCpu(dataType t, dataType underlyingLevel, monteCarloOptionStruct optionStruct) 92 | { 93 | return optionStruct.voltVal; 94 | } 95 | 96 | dataType processDiffCpu(dataType t, dataType x, monteCarloOptionStruct optionStruct) 97 | { 98 | return localVoltLocVolCpu(t, x, optionStruct); 99 | } 100 | 101 | dataType discDiffCpu(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct) 102 | { 103 | return processDiffCpu(t0, x0, optionStruct) * sqrt(dt); 104 | } 105 | 106 | dataType stdDeviationCpu(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct) 107 | { 108 | return discDiffCpu(t0, x0, dt, optionStruct); 109 | } 110 | 111 | dataType applyCpu(dataType x0, dataType dx) 112 | { 113 | return (x0 * exp(dx)); 114 | } 115 | 116 | dataType discDriftCpu(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct) 117 | { 118 | return processDriftCpu(t0, x0, optionStruct) * dt; 119 | } 120 | 121 | dataType processEvolveCpu(dataType t0, dataType x0, dataType dt, dataType dw, monteCarloOptionStruct optionStruct) 122 | { 123 | return applyCpu(x0, discDriftCpu(t0, x0, dt, optionStruct) + stdDeviationCpu(t0, x0, dt, optionStruct) *dw); 124 | } 125 | 126 | //retrieve the current sequence 127 | void getSequenceCpu(dataType* sequence, dataType sampleNum) 128 | { 129 | for (size_t iInSeq = 0; iInSeq < SEQUENCE_LENGTH; iInSeq++) 130 | { 131 | sequence[iInSeq] = DEFAULT_SEQ_VAL; 132 | } 133 | } 134 | 135 | 136 | dataType getProcessValX0Cpu(monteCarloOptionStruct optionStruct) 137 | { 138 | return optionStruct.underlyingVal; 139 | } 140 | 141 | 142 | void getPathCpu(dataType* path, size_t sampleNum, dataType dt, monteCarloOptionStruct optionStruct) 143 | { 144 | dataType weight = DEFAULT_SEQ_WEIGHT; 145 | 146 | path[0] = getProcessValX0Cpu(optionStruct); 147 | 148 | for (size_t i=1; i (b)) ? (a) : (b)) 13 | 14 | //struct for defining what's in an option 15 | typedef struct 16 | { 17 | float riskVal; 18 | float divVal; 19 | float voltVal; 20 | float underlyingVal; 21 | float strikeVal; 22 | float discountVal; 23 | } monteCarloOptionStruct; 24 | 25 | #endif //MONTE_CARLO_STRUCTS_CUH 26 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenCL/mt19937.c: -------------------------------------------------------------------------------- 1 | /* A C-program for MT19937: Real number version (1999/10/28) */ 2 | /* genrand() generates one pseudorandom real number (double) */ 3 | /* which is uniformly distributed on [0,1]-interval, for each */ 4 | /* call. sgenrand(seed) sets initial values to the working area */ 5 | /* of 624 words. Before genrand(), sgenrand(seed) must be */ 6 | /* called once. (seed is any 32-bit integer.) */ 7 | /* Integer generator is obtained by modifying two lines. */ 8 | /* Coded by Takuji Nishimura, considering the suggestions by */ 9 | /* Topher Cooper and Marc Rieffel in July-Aug. 1997. */ 10 | 11 | /* This library is free software; you can redistribute it and/or */ 12 | /* modify it under the terms of the GNU Library General Public */ 13 | /* License as published by the Free Software Foundation; either */ 14 | /* version 2 of the License, or (at your option) any later */ 15 | /* version. */ 16 | /* This library is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ 19 | /* See the GNU Library General Public License for more details. */ 20 | /* You should have received a copy of the GNU Library General */ 21 | /* Public License along with this library; if not, write to the */ 22 | /* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA */ 23 | /* 02111-1307 USA */ 24 | 25 | /* Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura. */ 26 | /* Any feedback is very welcome. For any question, comments, */ 27 | /* see http://www.math.keio.ac.jp/matumoto/emt.html or email */ 28 | /* matumoto@math.keio.ac.jp */ 29 | 30 | /* REFERENCE */ 31 | /* M. Matsumoto and T. Nishimura, */ 32 | /* "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform */ 33 | /* Pseudo-Random Number Generator", */ 34 | /* ACM Transactions on Modeling and Computer Simulation, */ 35 | /* Vol. 8, No. 1, January 1998, pp 3--30. */ 36 | 37 | #ifndef MT_19937_C 38 | #define MT_19937_C 39 | 40 | #include "mt19937.h" 41 | 42 | void 43 | initializeMersenneState (mt19937state* m, unsigned long seed) 44 | { 45 | int i; 46 | 47 | for (i = 0; i < __N__; i++) 48 | { 49 | m->mt[i] = seed & 0xffff0000; 50 | seed = 69069 * seed + 1; 51 | m->mt[i] |= (seed & 0xffff0000) >> 16; 52 | seed = 69069 * seed + 1; 53 | } 54 | m->mti = __N__; 55 | } 56 | 57 | unsigned long 58 | generateRandInt (mt19937state* m) 59 | { 60 | unsigned long y; 61 | unsigned long mag01 [2]; 62 | mag01 [0] = 0x0; 63 | mag01 [1] = MATRIX_A; 64 | 65 | /* mag01[x] = x * MATRIX_A for x=0,1 */ 66 | 67 | if (m->mti >= __N__) 68 | { /* generate N words at one time */ 69 | int kk = 0; 70 | #pragma hmppcg noParallel 71 | while (kk < __N__ - __M__) 72 | { 73 | y = (m->mt [kk] & UPPER_MASK) | (m->mt [kk + 1] & LOWER_MASK); 74 | m->mt [kk] = m->mt [kk + __M__] ^ (y >> 1) ^ mag01 [y & 0x1]; 75 | ++kk; 76 | } 77 | #pragma hmppcg noParallel 78 | while (kk < __N__ - 1) 79 | { 80 | y = (m->mt [kk] & UPPER_MASK) | (m->mt [kk + 1] & LOWER_MASK); 81 | m->mt [kk] = m->mt [kk + (__M__ - __N__)] ^ (y >> 1) ^ mag01 [y & 0x1]; 82 | ++kk; 83 | } 84 | 85 | y = (m->mt [__N__ - 1] & UPPER_MASK) | (m->mt [0] & LOWER_MASK); 86 | m->mt [__N__ - 1] = m->mt [__M__ - 1] ^ (y >> 1) ^ mag01 [y & 0x1]; 87 | 88 | m->mti = 0; 89 | } 90 | 91 | y = m->mt [m->mti++]; 92 | y ^= TEMPERING_SHIFT_U(y); 93 | y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B; 94 | y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C; 95 | y ^= TEMPERING_SHIFT_L(y); 96 | return y; 97 | } 98 | 99 | float 100 | generateRandFloat (mt19937state* m) 101 | { 102 | return ( ((float) generateRandInt (m)) * 2.3283064370807974e-10 ); 103 | } 104 | 105 | float 106 | generateRandDouble (mt19937state* m) 107 | { 108 | return ( ((double) generateRandInt (m)) * 2.3283064370807974e-10 ); 109 | } 110 | 111 | #endif 112 | 113 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenCL/mt19937.h: -------------------------------------------------------------------------------- 1 | /* A C-program for MT19937: Real number version (1999/10/28) */ 2 | /* genrand() generates one pseudorandom real number (double) */ 3 | /* which is uniformly distributed on [0,1]-interval, for each */ 4 | /* call. sgenrand(seed) sets initial values to the working area */ 5 | /* of 624 words. Before genrand(), sgenrand(seed) must be */ 6 | /* called once. (seed is any 32-bit integer.) */ 7 | /* Integer generator is obtained by modifying two lines. */ 8 | /* Coded by Takuji Nishimura, considering the suggestions by */ 9 | /* Topher Cooper and Marc Rieffel in July-Aug. 1997. */ 10 | 11 | /* This library is free software; you can redistribute it and/or */ 12 | /* modify it under the terms of the GNU Library General Public */ 13 | /* License as published by the Free Software Foundation; either */ 14 | /* version 2 of the License, or (at your option) any later */ 15 | /* version. */ 16 | /* This library is distributed in the hope that it will be useful, */ 17 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 18 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ 19 | /* See the GNU Library General Public License for more details. */ 20 | /* You should have received a copy of the GNU Library General */ 21 | /* Public License along with this library; if not, write to the */ 22 | /* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA */ 23 | /* 02111-1307 USA */ 24 | 25 | /* Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura. */ 26 | /* Any feedback is very welcome. For any question, comments, */ 27 | /* see http://www.math.keio.ac.jp/matumoto/emt.html or email */ 28 | /* matumoto@math.keio.ac.jp */ 29 | 30 | /* REFERENCE */ 31 | /* M. Matsumoto and T. Nishimura, */ 32 | /* "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform */ 33 | /* Pseudo-Random Number Generator", */ 34 | /* ACM Transactions on Modeling and Computer Simulation, */ 35 | /* Vol. 8, No. 1, January 1998, pp 3--30. */ 36 | 37 | #ifndef MT_19937_H 38 | #define MT_19937_H 39 | 40 | /* Period parameters */ 41 | #define __N__ 624 42 | #define __M__ 397 43 | #define MATRIX_A 0x9908b0df /* constant vector a */ 44 | #define UPPER_MASK 0x80000000 /* most significant w-r bits */ 45 | #define LOWER_MASK 0x7fffffff /* least significant r bits */ 46 | 47 | /* Tempering parameters */ 48 | #define TEMPERING_MASK_B 0x9d2c5680 49 | #define TEMPERING_MASK_C 0xefc60000 50 | #define TEMPERING_SHIFT_U(y) (y >> 11) 51 | #define TEMPERING_SHIFT_S(y) (y << 7) 52 | #define TEMPERING_SHIFT_T(y) (y << 15) 53 | #define TEMPERING_SHIFT_L(y) (y >> 18) 54 | 55 | typedef struct mt19937_stateStruct 56 | { 57 | unsigned long mt [__N__]; 58 | int mti; 59 | } mt19937state; 60 | 61 | mt19937state* 62 | GetMersenneState (); 63 | 64 | void 65 | initializeMersenneState (mt19937state* m, unsigned long seed); 66 | 67 | unsigned long 68 | generateRandInt (mt19937state* m); 69 | 70 | float 71 | generateRandFloat (mt19937state* m); 72 | 73 | float 74 | generateRandDouble (mt19937state* m); 75 | 76 | #endif 77 | 78 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenMP/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | g++ -O3 -march=native -fopenmp monteCarloEngine.c -o monteCarloEngine.exe 3 | clean: 4 | rm -f *~ *.exe 5 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenMP/monteCarloConstants.h: -------------------------------------------------------------------------------- 1 | //monteCarloConstants.cuh 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //Constants for running monte carlo on the GPU/CPU 5 | 6 | #ifndef MONTE_CARLO_CONSTANTS_CUH 7 | #define MONTE_CARLO_CONSTANTS_CUH 8 | 9 | #define DEFAULT_SEQ_VAL 1.0f 10 | #define DEFAULT_SEQ_WEIGHT 1.0f 11 | 12 | #define RISK_VAL 0.06f 13 | #define DIV_VAL 0.0f 14 | #define VOLT_VAL 0.200f 15 | #define UNDERLYING_VAL 30.0f 16 | #define STRIKE_VAL 40.0f 17 | #define DISCOUNT_VAL 0.94176453358424872f 18 | #define START_PATH_VAL 1.0f 19 | #define DEFAULT_SEQ_VAL 1.0f 20 | #define DEFAULT_SEQ_WEIGHT 1.0f 21 | #define SEQUENCE_LENGTH 250 22 | 23 | //define the thread block size 24 | #define THREAD_BLOCK_SIZE 256 25 | 26 | //define the number of samples 27 | #define NUM_SAMPLES 400000 28 | 29 | //number of options 30 | #define NUM_OPTIONS 1 31 | 32 | //number of threads to use for processing each option 33 | #define NUM_THREADS_PER_OPTION NUM_SAMPLES 34 | 35 | #endif //MONTE_CARLO_CONSTANTS_CUH 36 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenMP/monteCarloEngine.c: -------------------------------------------------------------------------------- 1 | //monteCarloEngine.cu 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //Function for running Monte Carlo on the GPU 5 | 6 | //needed for the monte carlo CPU kernels 7 | #include "monteCarloKernelsCpu.c" 8 | 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #define RISK_VAL 0.06f 16 | #define DIV_VAL 0.0f 17 | #define VOLT_VAL 0.200f 18 | #define UNDERLYING_VAL 30.0f 19 | #define STRIKE_VAL 40.0f 20 | #define DISCOUNT_VAL 0.94176453358424872f 21 | 22 | 23 | 24 | //run monte carlo... 25 | void runMonteCarlo() 26 | { 27 | //int nSamplesArray[] = {100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000} 28 | 29 | int numSamples = 400000; 30 | 31 | //for (int numTime=0; numTime < 12; numTime++) 32 | { 33 | //int numSamples = nSamplesArray[numTime]; 34 | 35 | printf("Number of Samples: %d\n\n", numSamples); 36 | 37 | //declare and initialize the struct used for the option 38 | monteCarloOptionStruct optionStruct; 39 | optionStruct.riskVal = RISK_VAL; 40 | optionStruct.divVal = DIV_VAL; 41 | optionStruct.voltVal = VOLT_VAL; 42 | optionStruct.underlyingVal = UNDERLYING_VAL; 43 | optionStruct.strikeVal = STRIKE_VAL; 44 | optionStruct.discountVal = DISCOUNT_VAL; 45 | 46 | 47 | //declare pointers for data on CPU 48 | dataType* samplePrices; 49 | dataType* sampleWeights; 50 | dataType* times; 51 | monteCarloOptionStruct* optionStructs; 52 | 53 | //allocate space for data on CPU 54 | samplePrices = (dataType*)malloc(NUM_OPTIONS*numSamples*sizeof(dataType)); 55 | sampleWeights = (dataType*)malloc(NUM_OPTIONS*numSamples*sizeof(dataType)); 56 | times = (dataType*)malloc(NUM_OPTIONS*numSamples*sizeof(dataType)); 57 | optionStructs = (monteCarloOptionStruct*)malloc(NUM_OPTIONS*sizeof(monteCarloOptionStruct)); 58 | 59 | long seconds, useconds; 60 | dataType mtimeCpu, mtimeOpenMP; 61 | struct timeval start; 62 | struct timeval end; 63 | 64 | for (int optNum = 0; optNum < NUM_OPTIONS; optNum++) 65 | { 66 | optionStructs[optNum] = optionStruct; 67 | } 68 | 69 | //initialize values for data on CPU 70 | //declare pointers for data on GPU 71 | dataType* samplePricesGpu; 72 | dataType* sampleWeightsGpu; 73 | dataType* timesGpu; 74 | monteCarloOptionStruct* optionStructsGpu; 75 | 76 | srand(time(NULL)); 77 | 78 | /* initialize random seed: */ 79 | srand ( rand()); 80 | 81 | //free memory space on the CPU 82 | free(samplePrices); 83 | free(sampleWeights); 84 | free(times); 85 | 86 | 87 | //declare pointers for data on CPU 88 | dataType* samplePricesCpu; 89 | dataType* sampleWeightsCpu; 90 | dataType* timesCpu; 91 | 92 | //allocate space for data on CPU 93 | samplePricesCpu = (dataType*)malloc(numSamples*sizeof(dataType)); 94 | sampleWeightsCpu = (dataType*)malloc(numSamples*sizeof(dataType)); 95 | timesCpu = (dataType*)malloc(numSamples*sizeof(dataType)); 96 | 97 | 98 | gettimeofday(&start, NULL); 99 | monteCarloGpuKernelCpuOpenMP(samplePricesCpu, sampleWeightsCpu, timesCpu, (1.0f / (dataType)SEQUENCE_LENGTH), optionStructs, numSamples); 100 | gettimeofday(&end, NULL); 101 | 102 | seconds = end.tv_sec - start.tv_sec; 103 | useconds = end.tv_usec - start.tv_usec; 104 | 105 | mtimeOpenMP = ((seconds) * 1000 + ((dataType)useconds)/1000.0); 106 | printf("Run on CPU using OpenMP\n"); 107 | printf("Processing time on CPU using OpenMP: %f (ms)\n", mtimeOpenMP); 108 | 109 | //add all the computed prices together 110 | dataType cumPrice = 0.0f; 111 | for (int numSamp = 0; numSamp < numSamples; numSamp++) 112 | { 113 | cumPrice += samplePricesCpu[numSamp]; 114 | } 115 | 116 | dataType avgPrice = cumPrice / numSamples; 117 | printf("Average Price (CPU computation): %f\n\n", avgPrice); 118 | 119 | gettimeofday(&start, NULL); 120 | monteCarloGpuKernelCpu(samplePricesCpu, sampleWeightsCpu, timesCpu, (1.0f / (dataType)SEQUENCE_LENGTH), optionStructs, numSamples); 121 | gettimeofday(&end, NULL); 122 | 123 | seconds = end.tv_sec - start.tv_sec; 124 | useconds = end.tv_usec - start.tv_usec; 125 | 126 | mtimeCpu = ((seconds) * 1000 + ((dataType)useconds)/1000.0); 127 | printf("Run on CPU\n"); 128 | printf("Processing time on CPU: %f (ms)\n", mtimeCpu); 129 | 130 | 131 | //retrieve the average price 132 | 133 | cumPrice = 0.0f; 134 | 135 | //add all the computed prices together 136 | for (int numSamp = 0; numSamp < numSamples; numSamp++) 137 | { 138 | cumPrice += samplePricesCpu[numSamp]; 139 | } 140 | 141 | avgPrice = cumPrice / numSamples; 142 | printf("Average Price (CPU computation): %f\n\n", avgPrice); 143 | 144 | printf("Speedup Using OpenMP: %f\n", mtimeCpu / mtimeOpenMP); 145 | 146 | //free memory space on the CPU 147 | free(samplePricesCpu); 148 | free(sampleWeightsCpu); 149 | free(timesCpu); 150 | free(optionStructs); 151 | 152 | } 153 | } 154 | 155 | //////////////////////////////////////////////////////////////////////////////// 156 | // Program main 157 | //////////////////////////////////////////////////////////////////////////////// 158 | int 159 | main( int argc, char** argv) 160 | { 161 | runMonteCarlo(); 162 | 163 | char c; 164 | c = getchar(); 165 | printf("%c\n", c); 166 | } 167 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenMP/monteCarloKernelsCpu.h: -------------------------------------------------------------------------------- 1 | //monteCarloKernelsCpu.cuh 2 | //Scott Grauer-Gray 3 | //May 10, 2012 4 | //Headers for monte carlo function on the CPU 5 | 6 | #ifndef MONTE_CARLO_KERNELS_CPU_CUH 7 | #define MONTE_CARLO_KERNELS_CPU_CUH 8 | 9 | //needed for constants related to monte carlo 10 | #include "monteCarloConstants.h" 11 | #include "monteCarloStructs.h" 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #define A_1 -39.696830286653757 18 | #define A_2 220.94609842452050 19 | #define A_3 -275.92851044696869 20 | #define A_4 138.35775186726900 21 | #define A_5 -30.664798066147160 22 | #define A_6 2.5066282774592392 23 | #define B_1 -54.476098798224058 24 | #define B_2 161.58583685804089 25 | #define B_3 -155.69897985988661 26 | #define B_4 66.801311887719720 27 | #define B_5 -13.280681552885721 28 | 29 | 30 | //function to set up the random states 31 | void setup_kernelCpu(); 32 | 33 | //function to compute the inverse normal distribution 34 | dataType compInverseNormDistCpu(dataType x) ; 35 | 36 | dataType interestRateCompoundFactCpu(dataType t, dataType rate); 37 | 38 | dataType interestRateDiscountFactCpu(dataType t, dataType rate); 39 | 40 | dataType flatForwardDiscountImplCpu(dataType t, dataType rate); 41 | 42 | dataType yieldTermStructDiscountCpu(dataType t, dataType rate); 43 | 44 | dataType interestRateImpliedRateCpu(dataType compound, dataType t); 45 | 46 | dataType yieldTermStructForwardRateCpu(dataType t1, dataType t2, dataType rate); 47 | 48 | dataType processDriftCpu(dataType t, dataType x, monteCarloOptionStruct optionStruct); 49 | 50 | dataType discretizationDriftCpu(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct); 51 | 52 | dataType localVoltLocVolCpu(dataType t, dataType underlyingLevel, monteCarloOptionStruct optionStruct); 53 | 54 | dataType processDiffCpu(dataType t, dataType x, monteCarloOptionStruct optionStruct); 55 | 56 | dataType discDiffCpu(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct); 57 | 58 | dataType stdDeviationCpu(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct); 59 | 60 | dataType applyCpu(dataType x0, dataType dx); 61 | 62 | dataType discDriftCpu(dataType t0, dataType x0, dataType dt, monteCarloOptionStruct optionStruct); 63 | 64 | dataType processEvolveCpu(dataType t0, dataType x0, dataType dt, dataType dw, monteCarloOptionStruct optionStruct); 65 | 66 | //retrieve the current sequence 67 | void getSequenceCpu(dataType* sequence, dataType sampleNum); 68 | 69 | 70 | dataType getProcessValX0Cpu(monteCarloOptionStruct optionStruct); 71 | 72 | void getPathCpu(dataType* path, size_t sampleNum, dataType dt, int numSample, monteCarloOptionStruct optionStruct); 73 | 74 | 75 | dataType getPriceCpu(dataType val); 76 | 77 | //initialize the path 78 | void initializePathCpu(dataType* path); 79 | 80 | void monteCarloGpuKernelCpuOpenMP(float* samplePrices, float* sampleWeights, float* times, float dt, monteCarloOptionStruct* optionStructs, int numSamples); 81 | 82 | void monteCarloGpuKernelCpu(dataType* samplePrices, dataType* sampleWeights, dataType* times, dataType dt, monteCarloOptionStruct* optionStructs, int numSamples); 83 | 84 | 85 | 86 | #endif //MONTE_CARLO_KERNELS_CPU_CUH 87 | -------------------------------------------------------------------------------- /Monte-Carlo/OpenMP/monteCarloStructs.h: -------------------------------------------------------------------------------- 1 | //monteCarloStructs.cuh 2 | //Scott Grauer-Gray 3 | //May 24, 2012 4 | //Header file with structures used in monte carlo computation 5 | 6 | #ifndef MONTE_CARLO_STRUCTS_CUH 7 | #define MONTE_CARLO_STRUCTS_CUH 8 | 9 | typedef float dataType; 10 | 11 | //struct for defining what's in an option 12 | typedef struct 13 | { 14 | dataType riskVal; 15 | dataType divVal; 16 | dataType voltVal; 17 | dataType underlyingVal; 18 | dataType strikeVal; 19 | dataType discountVal; 20 | } monteCarloOptionStruct; 21 | 22 | #endif //MONTE_CARLO_STRUCTS_CUH 23 | -------------------------------------------------------------------------------- /README-parameters.txt: -------------------------------------------------------------------------------- 1 | README-parameters 2 | 3 | 4 | 5 | Black-Scholes: 6 | 7 | Parameters in sample code: 8 | Cycles through a set of 37 different options, with 19 CALL options and 18 PUT options 9 | Each option has hard-coded values for the strike, spot, q, r, t, and vol values, as well as the expected output value and tolerances when checking for an error 10 | 5,000,000 total samples are run in parallel in the sample code 11 | 12 | 13 | 14 | 15 | Monte-Carlo: 16 | 17 | Parameters in sample code: 18 | The parameters for this code are in the monteCarloConstants.cuh file and can be adjusted there. 19 | The sample code is run with 400,000 samples in parallel. 20 | 21 | 22 | 23 | 24 | Bonds: 25 | 26 | Parameters in sample code: 27 | 28 | Bond issue date and bond dated date: 29 | Day: random integer value between 1 and 28 30 | Month: random integer value between 1 and 12 31 | Year: value of 1998 or 1999, set randomly 32 | 33 | Bond maturity date: 34 | Day: random integer value between 1 and 28 35 | Month: random integer value between 1 and 12 36 | Year: value of 2000 or 2001, set randomly 37 | 38 | Bond rate: 39 | Random float value between 0.03 and 0.13 40 | 41 | Other parameters are common in all bonds, see code for parameters 42 | 43 | 1000000 bonds (with parameters given above varying across bonds) are run in the sample code 44 | 45 | 46 | 47 | Repo: 48 | 49 | Parameters in sample code: 50 | 51 | Repo Settlement Date: 52 | Day: random integer value between 1 and 28 53 | Month: value of 1, 2, or 3, set randomly 54 | Year: value of 1999 or 2000, set randomly 55 | 56 | Repo Delivery Date: 57 | Day: random integer value between 1 and 28 58 | Month: value of 9, 10, or 11, set randomly 59 | Year: value of 2000 or 2001, set randomly 60 | 61 | Bond issue date and bond dated date: 62 | Day: random integer value between 1 and 28 63 | Month: random integer value between 1 and 12 64 | Year: value of 1997 or 1998, set randomly 65 | 66 | Bond maturity date: 67 | Day: random integer value between 1 and 28 68 | Month: random integer value between 1 and 12 69 | Year: value of 2002 or 2003, set randomly 70 | 71 | Bond rate: 72 | Random float value between 0.03 and 0.13 73 | 74 | Other parameters are common in all repos, see code for parameters 75 | 76 | 1000000 repos (with parameters given above varying across repos) are run in the sample code 77 | 78 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | FinanceBench 2 | ------------ 3 | 4 | * Scott Grauer-Gray 5 | * Will Killian 6 | * Robert Searles 7 | * John Cavazos 8 | 9 | This contains codes for Black-Scholes, Monte-Carlo, Bonds, and Repo financial applications which can be run on the CPU and GPU. All the codes are described in the following paper and any work that uses these codes should cite this paper: 10 | 11 | "Accelerating Financial Applications on the GPU" which was presented at the Sixth Workshop on General Purpose Processing Using GPUs (GPGPU 6) 12 | 13 | 14 | README-parameters.txt contains information on the parameters in each application. 15 | 16 | Directions (tested for Linux-based systems w/ gcc/g++ compiler, NVIDIA GPU, and CAPS Compiler for HMPP/OpenACC): 17 | 18 | For CUDA/OpenCL codes (CUDA available for each application, OpenCL available for Black-Scholes 19 | and Monte-Carlo): 20 | 21 | 1. Set the PATH and LD_LIBRARY_PATH environment variables to point to the appropriate locations for CUDA/OpenCL. 22 | 2. Navigate to CUDA/OpenCL folder in target application. 23 | 3. Run Makefile (by using "make" command). 24 | 4. Application executable should be create and can be run. 25 | 26 | For HMPP/OpenACC Codes: 27 | 28 | 1. Set the PATH and LD_LIBRARY_PATH environment variables to point to the appropriate locations for CUDA/OpenCL. 29 | 2. Set the environment variables needed for the HMPP/OpenACC compilation environment. 30 | 3. Navigate to HMPP/OpenACC folder in target application. 31 | 4. Run Makefile (by using "make" command). 32 | 5. Application executable should be create and can be run. 33 | 34 | For CPU/OpenMP Codes (assuming using g++ compiler): 35 | 36 | 1. Run Makefile (by using "make" command). 37 | 2. Application executable should be create and can be run. 38 | -------------------------------------------------------------------------------- /Repo/CPU/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | g++ -O3 repoEngine.c -o repoEngine.exe 3 | clean: 4 | rm -f *~ *.exe 5 | -------------------------------------------------------------------------------- /Repo/CPU/repoStructs.h: -------------------------------------------------------------------------------- 1 | //repoStructs.cuh 2 | //Scott Grauer-Gray 3 | //July 6, 2012 4 | //Structs for running the repo engine 5 | 6 | #ifndef REPO_STRUCTS_CUH 7 | #define REPO_STRUCTS_CUH 8 | 9 | typedef double dataType; 10 | 11 | #include 12 | #include 13 | 14 | #define SIMPLE_INTEREST 0 15 | #define COMPOUNDED_INTEREST 1 16 | #define CONTINUOUS_INTEREST 2 17 | #define SIMPLE_THEN_COMPOUNDED_INTEREST 3 18 | 19 | #define ANNUAL_FREQ 1 20 | #define SEMIANNUAL_FREQ 2 21 | 22 | #define USE_EXACT_DAY 0 23 | #define USE_SERIAL_NUMS 1 24 | 25 | #define QL_EPSILON_GPU 0.000000000000000001f 26 | 27 | #define COMPUTE_AMOUNT -1 28 | 29 | 30 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 31 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 32 | 33 | #define ACCURACY 1.0e-8 34 | 35 | 36 | typedef struct 37 | { 38 | int month; 39 | int day; 40 | int year; 41 | int dateSerialNum; 42 | } repoDateStruct; 43 | 44 | 45 | typedef struct 46 | { 47 | repoDateStruct startDate; 48 | repoDateStruct maturityDate; 49 | float rate; 50 | } bondStruct; 51 | 52 | 53 | typedef struct 54 | { 55 | dataType rate; 56 | dataType freq; 57 | int comp; 58 | int dayCounter; 59 | } intRateStruct; 60 | 61 | 62 | typedef struct 63 | { 64 | dataType forward; 65 | dataType compounding; 66 | dataType frequency; 67 | intRateStruct intRate; 68 | repoDateStruct refDate; 69 | repoDateStruct calDate; 70 | int dayCounter; 71 | } repoYieldTermStruct; 72 | 73 | 74 | typedef struct 75 | { 76 | repoDateStruct paymentDate; 77 | repoDateStruct accrualStartDate; 78 | repoDateStruct accrualEndDate; 79 | dataType amount; 80 | } couponStruct; 81 | 82 | 83 | typedef struct 84 | { 85 | couponStruct* legs; 86 | intRateStruct intRate; 87 | int nominal; 88 | int dayCounter; 89 | } cashFlowsStruct; 90 | 91 | 92 | typedef struct 93 | { 94 | dataType* dirtyPrice; 95 | dataType* accruedAmountSettlement; 96 | dataType* accruedAmountDeliveryDate; 97 | dataType* cleanPrice; 98 | dataType* forwardSpotIncome; 99 | dataType* underlyingBondFwd; 100 | dataType* repoNpv; 101 | dataType* repoCleanForwardPrice; 102 | dataType* repoDirtyForwardPrice; 103 | dataType* repoImpliedYield; 104 | dataType* marketRepoRate; 105 | dataType* bondForwardVal; 106 | } resultsStruct; 107 | 108 | 109 | typedef struct 110 | { 111 | repoYieldTermStruct* discountCurve; 112 | repoYieldTermStruct* repoCurve; 113 | repoDateStruct* settlementDate; 114 | repoDateStruct* deliveryDate; 115 | repoDateStruct* maturityDate; 116 | repoDateStruct* repoDeliveryDate; 117 | dataType* bondCleanPrice; 118 | bondStruct* bond; 119 | dataType* dummyStrike; 120 | } inArgsStruct; 121 | 122 | 123 | typedef struct 124 | { 125 | dataType npv; 126 | int dayCounter; 127 | int comp; 128 | dataType freq; 129 | bool includeSettlementDateFlows; 130 | repoDateStruct settlementDate; 131 | repoDateStruct npvDate; 132 | } irrFinderStruct; 133 | 134 | 135 | typedef struct 136 | { 137 | dataType root_; 138 | dataType xMin_; 139 | dataType xMax_; 140 | dataType fxMin_; 141 | dataType fxMax_; 142 | int maxEvaluations_; 143 | int evaluationNumber_; 144 | dataType lowerBound_; 145 | dataType upperBound_; 146 | bool lowerBoundEnforced_; 147 | bool upperBoundEnforced_; 148 | } solverStruct; 149 | 150 | 151 | 152 | #endif //REPO_STRUCTS_CUH 153 | -------------------------------------------------------------------------------- /Repo/CUDA/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | nvcc -O3 repoEngine.cu -o repoEngine.exe 3 | clean: 4 | rm -f *~ *.exe 5 | -------------------------------------------------------------------------------- /Repo/CUDA/repoStructs.cuh: -------------------------------------------------------------------------------- 1 | //repoStructs.cuh 2 | //Scott Grauer-Gray 3 | //July 6, 2012 4 | //Structs for running the repo engine 5 | 6 | #ifndef REPO_STRUCTS_CUH 7 | #define REPO_STRUCTS_CUH 8 | 9 | typedef double dataType; 10 | 11 | #include 12 | #include 13 | 14 | #define SIMPLE_INTEREST 0 15 | #define COMPOUNDED_INTEREST 1 16 | #define CONTINUOUS_INTEREST 2 17 | #define SIMPLE_THEN_COMPOUNDED_INTEREST 3 18 | 19 | #define ANNUAL_FREQ 1 20 | #define SEMIANNUAL_FREQ 2 21 | 22 | #define USE_EXACT_DAY 0 23 | #define USE_SERIAL_NUMS 1 24 | 25 | #define QL_EPSILON_GPU 0.000000000000000001f 26 | 27 | #define COMPUTE_AMOUNT -1 28 | 29 | 30 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 31 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 32 | 33 | #define ACCURACY 1.0e-8 34 | 35 | 36 | typedef struct 37 | { 38 | int month; 39 | int day; 40 | int year; 41 | int dateSerialNum; 42 | } repoDateStruct; 43 | 44 | 45 | typedef struct 46 | { 47 | repoDateStruct startDate; 48 | repoDateStruct maturityDate; 49 | float rate; 50 | } bondStruct; 51 | 52 | 53 | typedef struct 54 | { 55 | dataType rate; 56 | dataType freq; 57 | int comp; 58 | int dayCounter; 59 | } intRateStruct; 60 | 61 | 62 | typedef struct 63 | { 64 | dataType forward; 65 | dataType compounding; 66 | dataType frequency; 67 | intRateStruct intRate; 68 | repoDateStruct refDate; 69 | repoDateStruct calDate; 70 | int dayCounter; 71 | } repoYieldTermStruct; 72 | 73 | 74 | typedef struct 75 | { 76 | repoDateStruct paymentDate; 77 | repoDateStruct accrualStartDate; 78 | repoDateStruct accrualEndDate; 79 | dataType amount; 80 | } couponStruct; 81 | 82 | 83 | typedef struct 84 | { 85 | couponStruct* legs; 86 | intRateStruct intRate; 87 | int nominal; 88 | int dayCounter; 89 | } cashFlowsStruct; 90 | 91 | 92 | typedef struct 93 | { 94 | dataType* dirtyPrice; 95 | dataType* accruedAmountSettlement; 96 | dataType* accruedAmountDeliveryDate; 97 | dataType* cleanPrice; 98 | dataType* forwardSpotIncome; 99 | dataType* underlyingBondFwd; 100 | dataType* repoNpv; 101 | dataType* repoCleanForwardPrice; 102 | dataType* repoDirtyForwardPrice; 103 | dataType* repoImpliedYield; 104 | dataType* marketRepoRate; 105 | dataType* bondForwardVal; 106 | } resultsStruct; 107 | 108 | 109 | typedef struct 110 | { 111 | repoYieldTermStruct* discountCurve; 112 | repoYieldTermStruct* repoCurve; 113 | repoDateStruct* settlementDate; 114 | repoDateStruct* deliveryDate; 115 | repoDateStruct* maturityDate; 116 | repoDateStruct* repoDeliveryDate; 117 | dataType* bondCleanPrice; 118 | bondStruct* bond; 119 | dataType* dummyStrike; 120 | } inArgsStruct; 121 | 122 | 123 | typedef struct 124 | { 125 | dataType npv; 126 | int dayCounter; 127 | int comp; 128 | dataType freq; 129 | bool includeSettlementDateFlows; 130 | repoDateStruct settlementDate; 131 | repoDateStruct npvDate; 132 | } irrFinderStruct; 133 | 134 | 135 | typedef struct 136 | { 137 | dataType root_; 138 | dataType xMin_; 139 | dataType xMax_; 140 | dataType fxMin_; 141 | dataType fxMax_; 142 | int maxEvaluations_; 143 | int evaluationNumber_; 144 | dataType lowerBound_; 145 | dataType upperBound_; 146 | bool lowerBoundEnforced_; 147 | bool upperBoundEnforced_; 148 | } solverStruct; 149 | 150 | 151 | 152 | #endif //REPO_STRUCTS_CUH 153 | -------------------------------------------------------------------------------- /Repo/HMPP-CUDA/Makefile: -------------------------------------------------------------------------------- 1 | EXE_NAME=repoEngine 2 | MAIN_FILE=repoEngineHmppCuda.c 3 | 4 | include ../../compileHmppOpenACC.mk 5 | -------------------------------------------------------------------------------- /Repo/HMPP-CUDA/repoStructs.h: -------------------------------------------------------------------------------- 1 | //repoStructs.h 2 | //Scott Grauer-Gray 3 | //Structs for running the repo engine 4 | 5 | #ifndef REPO_STRUCTS_CUH 6 | #define REPO_STRUCTS_CUH 7 | 8 | typedef int bool; 9 | #define true 1 10 | #define false 0 11 | 12 | typedef double dataType; 13 | 14 | #include 15 | #include 16 | 17 | #define SIMPLE_INTEREST 0 18 | #define COMPOUNDED_INTEREST 1 19 | #define CONTINUOUS_INTEREST 2 20 | #define SIMPLE_THEN_COMPOUNDED_INTEREST 3 21 | 22 | #define ANNUAL_FREQ 1 23 | #define SEMIANNUAL_FREQ 2 24 | 25 | #define USE_EXACT_DAY 0 26 | #define USE_SERIAL_NUMS 1 27 | 28 | #define QL_EPSILON_GPU 0.000000000000000001f 29 | 30 | 31 | #define COMPUTE_AMOUNT -1 32 | 33 | #define NUM_REPOS 1000000 34 | 35 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 36 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 37 | 38 | #define ACCURACY 1.0e-8 39 | 40 | 41 | typedef struct 42 | { 43 | int month; 44 | int day; 45 | int year; 46 | int dateSerialNum; 47 | } repoDateStruct; 48 | 49 | 50 | typedef struct 51 | { 52 | repoDateStruct startDate; 53 | repoDateStruct maturityDate; 54 | float rate; 55 | } bondStruct; 56 | 57 | 58 | typedef struct 59 | { 60 | dataType rate; 61 | dataType freq; 62 | int comp; 63 | int dayCounter; 64 | } intRateStruct; 65 | 66 | 67 | typedef struct 68 | { 69 | dataType forward; 70 | dataType compounding; 71 | dataType frequency; 72 | intRateStruct intRate; 73 | repoDateStruct refDate; 74 | repoDateStruct calDate; 75 | int dayCounter; 76 | } repoYieldTermStruct; 77 | 78 | 79 | typedef struct 80 | { 81 | repoDateStruct paymentDate; 82 | repoDateStruct accrualStartDate; 83 | repoDateStruct accrualEndDate; 84 | dataType amount; 85 | } couponStruct; 86 | 87 | 88 | typedef struct 89 | { 90 | couponStruct* legs; 91 | intRateStruct intRate; 92 | int nominal; 93 | int dayCounter; 94 | } cashFlowsStruct; 95 | 96 | 97 | typedef struct 98 | { 99 | dataType* dirtyPrice; 100 | dataType* accruedAmountSettlement; 101 | dataType* accruedAmountDeliveryDate; 102 | dataType* cleanPrice; 103 | dataType* forwardSpotIncome; 104 | dataType* underlyingBondFwd; 105 | dataType* repoNpv; 106 | dataType* repoCleanForwardPrice; 107 | dataType* repoDirtyForwardPrice; 108 | dataType* repoImpliedYield; 109 | dataType* marketRepoRate; 110 | dataType* bondForwardVal; 111 | } resultsStruct; 112 | 113 | 114 | typedef struct 115 | { 116 | repoYieldTermStruct* discountCurve; 117 | repoYieldTermStruct* repoCurve; 118 | repoDateStruct* settlementDate; 119 | repoDateStruct* deliveryDate; 120 | repoDateStruct* maturityDate; 121 | repoDateStruct* repoDeliveryDate; 122 | dataType* bondCleanPrice; 123 | bondStruct* bond; 124 | dataType* dummyStrike; 125 | } inArgsStruct; 126 | 127 | 128 | typedef struct 129 | { 130 | dataType npv; 131 | int dayCounter; 132 | int comp; 133 | dataType freq; 134 | bool includeSettlementDateFlows; 135 | repoDateStruct settlementDate; 136 | repoDateStruct npvDate; 137 | } irrFinderStruct; 138 | 139 | 140 | typedef struct 141 | { 142 | dataType root_; 143 | dataType xMin_; 144 | dataType xMax_; 145 | dataType fxMin_; 146 | dataType fxMax_; 147 | int maxEvaluations_; 148 | int evaluationNumber_; 149 | dataType lowerBound_; 150 | dataType upperBound_; 151 | bool lowerBoundEnforced_; 152 | bool upperBoundEnforced_; 153 | } solverStruct; 154 | 155 | 156 | 157 | #endif //REPO_STRUCTS_CUH 158 | -------------------------------------------------------------------------------- /Repo/OpenACC/Makefile: -------------------------------------------------------------------------------- 1 | EXE_NAME=repoEngine 2 | MAIN_FILE=repoEngineOpenACC.c 3 | 4 | include ../../compileHmppOpenACC.mk 5 | -------------------------------------------------------------------------------- /Repo/OpenACC/repoStructs.h: -------------------------------------------------------------------------------- 1 | //repoStructs.cuh 2 | //Scott Grauer-Gray 3 | //July 6, 2012 4 | //Structs for running the repo engine 5 | 6 | #ifndef REPO_STRUCTS_CUH 7 | #define REPO_STRUCTS_CUH 8 | 9 | typedef int bool; 10 | #define true 1 11 | #define false 0 12 | 13 | typedef double dataType; 14 | 15 | #include 16 | #include 17 | 18 | #define SIMPLE_INTEREST 0 19 | #define COMPOUNDED_INTEREST 1 20 | #define CONTINUOUS_INTEREST 2 21 | #define SIMPLE_THEN_COMPOUNDED_INTEREST 3 22 | 23 | #define ANNUAL_FREQ 1 24 | #define SEMIANNUAL_FREQ 2 25 | 26 | #define USE_EXACT_DAY 0 27 | #define USE_SERIAL_NUMS 1 28 | 29 | #define QL_EPSILON_GPU 0.000000000000000001f 30 | 31 | 32 | #define COMPUTE_AMOUNT -1 33 | 34 | #define NUM_REPOS 1000000 35 | 36 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 37 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 38 | 39 | #define ACCURACY 1.0e-8 40 | 41 | 42 | typedef struct 43 | { 44 | int month; 45 | int day; 46 | int year; 47 | int dateSerialNum; 48 | } repoDateStruct; 49 | 50 | 51 | typedef struct 52 | { 53 | repoDateStruct startDate; 54 | repoDateStruct maturityDate; 55 | float rate; 56 | } bondStruct; 57 | 58 | 59 | typedef struct 60 | { 61 | dataType rate; 62 | dataType freq; 63 | int comp; 64 | int dayCounter; 65 | } intRateStruct; 66 | 67 | 68 | typedef struct 69 | { 70 | dataType forward; 71 | dataType compounding; 72 | dataType frequency; 73 | intRateStruct intRate; 74 | repoDateStruct refDate; 75 | repoDateStruct calDate; 76 | int dayCounter; 77 | } repoYieldTermStruct; 78 | 79 | 80 | typedef struct 81 | { 82 | repoDateStruct paymentDate; 83 | repoDateStruct accrualStartDate; 84 | repoDateStruct accrualEndDate; 85 | dataType amount; 86 | } couponStruct; 87 | 88 | 89 | typedef struct 90 | { 91 | couponStruct* legs; 92 | intRateStruct intRate; 93 | int nominal; 94 | int dayCounter; 95 | } cashFlowsStruct; 96 | 97 | 98 | typedef struct 99 | { 100 | dataType* dirtyPrice; 101 | dataType* accruedAmountSettlement; 102 | dataType* accruedAmountDeliveryDate; 103 | dataType* cleanPrice; 104 | dataType* forwardSpotIncome; 105 | dataType* underlyingBondFwd; 106 | dataType* repoNpv; 107 | dataType* repoCleanForwardPrice; 108 | dataType* repoDirtyForwardPrice; 109 | dataType* repoImpliedYield; 110 | dataType* marketRepoRate; 111 | dataType* bondForwardVal; 112 | } resultsStruct; 113 | 114 | 115 | typedef struct 116 | { 117 | repoYieldTermStruct* discountCurve; 118 | repoYieldTermStruct* repoCurve; 119 | repoDateStruct* settlementDate; 120 | repoDateStruct* deliveryDate; 121 | repoDateStruct* maturityDate; 122 | repoDateStruct* repoDeliveryDate; 123 | dataType* bondCleanPrice; 124 | bondStruct* bond; 125 | dataType* dummyStrike; 126 | } inArgsStruct; 127 | 128 | 129 | typedef struct 130 | { 131 | dataType npv; 132 | int dayCounter; 133 | int comp; 134 | dataType freq; 135 | bool includeSettlementDateFlows; 136 | repoDateStruct settlementDate; 137 | repoDateStruct npvDate; 138 | } irrFinderStruct; 139 | 140 | 141 | typedef struct 142 | { 143 | dataType root_; 144 | dataType xMin_; 145 | dataType xMax_; 146 | dataType fxMin_; 147 | dataType fxMax_; 148 | int maxEvaluations_; 149 | int evaluationNumber_; 150 | dataType lowerBound_; 151 | dataType upperBound_; 152 | bool lowerBoundEnforced_; 153 | bool upperBoundEnforced_; 154 | } solverStruct; 155 | 156 | 157 | 158 | #endif //REPO_STRUCTS_CUH 159 | -------------------------------------------------------------------------------- /Repo/OpenMP/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | g++ -O3 -march=native -fopenmp repoEngine.c -o repoEngine.exe 3 | clean: 4 | rm -f *~ *.exe 5 | -------------------------------------------------------------------------------- /Repo/OpenMP/repoStructs.h: -------------------------------------------------------------------------------- 1 | //repoStructs.cuh 2 | //Scott Grauer-Gray 3 | //July 6, 2012 4 | //Structs for running the repo engine 5 | 6 | #ifndef REPO_STRUCTS_CUH 7 | #define REPO_STRUCTS_CUH 8 | 9 | typedef double dataType; 10 | 11 | #include 12 | #include 13 | 14 | #define SIMPLE_INTEREST 0 15 | #define COMPOUNDED_INTEREST 1 16 | #define CONTINUOUS_INTEREST 2 17 | #define SIMPLE_THEN_COMPOUNDED_INTEREST 3 18 | 19 | #define ANNUAL_FREQ 1 20 | #define SEMIANNUAL_FREQ 2 21 | 22 | #define USE_EXACT_DAY 0 23 | #define USE_SERIAL_NUMS 1 24 | 25 | #define QL_EPSILON_GPU 0.000000000000000001f 26 | 27 | #define COMPUTE_AMOUNT -1 28 | 29 | 30 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 31 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 32 | 33 | #define ACCURACY 1.0e-8 34 | 35 | 36 | typedef struct 37 | { 38 | int month; 39 | int day; 40 | int year; 41 | int dateSerialNum; 42 | } repoDateStruct; 43 | 44 | 45 | typedef struct 46 | { 47 | repoDateStruct startDate; 48 | repoDateStruct maturityDate; 49 | float rate; 50 | } bondStruct; 51 | 52 | 53 | typedef struct 54 | { 55 | dataType rate; 56 | dataType freq; 57 | int comp; 58 | int dayCounter; 59 | } intRateStruct; 60 | 61 | 62 | typedef struct 63 | { 64 | dataType forward; 65 | dataType compounding; 66 | dataType frequency; 67 | intRateStruct intRate; 68 | repoDateStruct refDate; 69 | repoDateStruct calDate; 70 | int dayCounter; 71 | } repoYieldTermStruct; 72 | 73 | 74 | typedef struct 75 | { 76 | repoDateStruct paymentDate; 77 | repoDateStruct accrualStartDate; 78 | repoDateStruct accrualEndDate; 79 | dataType amount; 80 | } couponStruct; 81 | 82 | 83 | typedef struct 84 | { 85 | couponStruct* legs; 86 | intRateStruct intRate; 87 | int nominal; 88 | int dayCounter; 89 | } cashFlowsStruct; 90 | 91 | 92 | typedef struct 93 | { 94 | dataType* dirtyPrice; 95 | dataType* accruedAmountSettlement; 96 | dataType* accruedAmountDeliveryDate; 97 | dataType* cleanPrice; 98 | dataType* forwardSpotIncome; 99 | dataType* underlyingBondFwd; 100 | dataType* repoNpv; 101 | dataType* repoCleanForwardPrice; 102 | dataType* repoDirtyForwardPrice; 103 | dataType* repoImpliedYield; 104 | dataType* marketRepoRate; 105 | dataType* bondForwardVal; 106 | } resultsStruct; 107 | 108 | 109 | typedef struct 110 | { 111 | repoYieldTermStruct* discountCurve; 112 | repoYieldTermStruct* repoCurve; 113 | repoDateStruct* settlementDate; 114 | repoDateStruct* deliveryDate; 115 | repoDateStruct* maturityDate; 116 | repoDateStruct* repoDeliveryDate; 117 | dataType* bondCleanPrice; 118 | bondStruct* bond; 119 | dataType* dummyStrike; 120 | } inArgsStruct; 121 | 122 | 123 | typedef struct 124 | { 125 | dataType npv; 126 | int dayCounter; 127 | int comp; 128 | dataType freq; 129 | bool includeSettlementDateFlows; 130 | repoDateStruct settlementDate; 131 | repoDateStruct npvDate; 132 | } irrFinderStruct; 133 | 134 | 135 | typedef struct 136 | { 137 | dataType root_; 138 | dataType xMin_; 139 | dataType xMax_; 140 | dataType fxMin_; 141 | dataType fxMax_; 142 | int maxEvaluations_; 143 | int evaluationNumber_; 144 | dataType lowerBound_; 145 | dataType upperBound_; 146 | bool lowerBoundEnforced_; 147 | bool upperBoundEnforced_; 148 | } solverStruct; 149 | 150 | 151 | 152 | #endif //REPO_STRUCTS_CUH 153 | -------------------------------------------------------------------------------- /compileHmppOpenACC.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2008 - 2010 CAPS entreprise. All rights reserved. 3 | # 4 | ATI_DISPLAY = DISPLAY=:0.0 5 | 6 | GCC = gcc 7 | ICC = icc 8 | HMPP = hmpp #--nvcc-options -arch,sm_10 9 | 10 | # if running on GPU of compute capacity 1.0, put the following at the end of the previous line 11 | # --nvcc-options -arch,sm_10 12 | 13 | CFLAGS = -O2 -p -w 14 | CPPFLAGS = #-I../../../../common 15 | LDFLAGS = 16 | HMPPFLAGS = --codelet-required 17 | 18 | VPATH = ../../../../common 19 | 20 | CLEANFILES = *.exe *.so *.so.* *.cu *.cu.* *.linkinfo 21 | 22 | BIN = $(EXE_NAME) 23 | 24 | ## choose your compiler here ## 25 | CC = $(GCC) 26 | 27 | #ifeq($(CC),$(GCC)) 28 | FLAGS = $(GCC_FLAGS) 29 | #else 30 | #FLAGS = $(ICC_FLAGS) 31 | #endif 32 | 33 | .DEFAULT: all 34 | 35 | all: 36 | $(HMPP) $(HMPPFLAGS) $(DFLAGS) $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $(EXE_NAME).exe $(MAIN_FILE) 37 | 38 | #exe: $(BIN).exe 39 | 40 | #run: exe 41 | # $(ATI_DISPLAY) ./$(BIN).exe 42 | 43 | #verbose: 44 | # $(MAKE) $(BIN).exe DFLAGS="-k -d" 45 | # $(MAKE) run HMPP_VERBOSITY=9 46 | # $(GPROF) ./$(BIN).exe 47 | 48 | #%.exe: %.c $(DEPS) 49 | # $(HMPP) $(HMPPFLAGS) $(DFLAGS) $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $(EXE_NAME).exe $(MAIN_FILE) 50 | 51 | #%.o: %.c 52 | # $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -c $< -o $@ 53 | 54 | clean: 55 | rm -f $(CLEANFILES) 56 | 57 | help: 58 | @echo "#" 59 | @echo "# Copyright 2008 - 2010 CAPS entreprise. All rights reserved." 60 | @echo "#" 61 | @echo "" 62 | @echo "Targets available are :" 63 | @echo "" 64 | @echo " make all : compiles and runs the application" 65 | @echo "" 66 | @echo " make exe : compiles the application" 67 | @echo "" 68 | @echo " make run : runs the execution" 69 | @echo "" 70 | @echo " make verbose : compiles and launches the execution with the higher level of verbosity" 71 | @echo "" 72 | @echo " make clean : deletes files generated by make" 73 | @echo "" 74 | --------------------------------------------------------------------------------