public SGrid(CDevice device, int nx, int nr, DateTime today, DateTime[] t_i, TimeSpan dt0, double xval0, double[] xpivot_p, double[] xgridspacing_p, double[] rval_r, int r0) { if (_initialized) throw new System.Exception(); _initialized = true; init(device, nx, nr, today, t_i, dt0, xval0, xpivot_p, xgridspacing_p, rval_r, r0); }
public S1DGrid( CDevice device, int nx, DateTime today, DateTime[] t_i, TimeSpan dt0, double xval0, double[] xpivot_p, double[] xgridspacing_p) : base(device, nx, 1, today, t_i, dt0, xval0, xpivot_p, xgridspacing_p, null, -1) { }
public static double Price(double startVal, double strike, double riskFreeRate, double volatility, double yearsToMaturity, int numCurvePoints, int numSteps, double maxTimeStepDays, int numBatches, int numScenariosPerBatch, int numStates, int seed) { //Grid Setup int numPivotPoints = 7; double[] pivotPoints = new double[numPivotPoints]; double[] gridSpacings = new double[numPivotPoints]; pivotPoints[0] = 1; gridSpacings[0] = 5; pivotPoints[1] = 70; gridSpacings[1] = 2.5; pivotPoints[2] = 90; gridSpacings[2] = 1; pivotPoints[3] = 110; gridSpacings[3] = 2.5; pivotPoints[4] = 140; gridSpacings[4] = 5; pivotPoints[5] = 200; gridSpacings[5] = 7.5; pivotPoints[6] = 300; gridSpacings[6] = 10; //Setup interest rate curve and discount curve DateTime today = DateTime.Today; double daysInYear = 365.25; double daysToMaturity = yearsToMaturity * daysInYear; DateTime[] rateCurveTimes = BuildDates(today, daysToMaturity, numCurvePoints); double[] rates = new double[numCurvePoints]; double[] discountFactors = new double[numCurvePoints]; for (int iPoint = 0; iPoint < numCurvePoints; ++iPoint) { rates[iPoint] = riskFreeRate; if (iPoint == 0) discountFactors[0] = Math.Exp(-rates[iPoint] * (rateCurveTimes[0] - today).Days / daysInYear); else discountFactors[iPoint] = discountFactors[iPoint - 1] * Math.Exp(-rates[iPoint] * (rateCurveTimes[iPoint] - rateCurveTimes[iPoint - 1]).Days / daysInYear); } //Create grid //CDevice device = new CDevice(EFloatingPointPrecision.bit32, EFloatingPointUnit.device, 0); CDevice device = new CDevice(EFloatingPointPrecision.bit64, EFloatingPointUnit.host, 0); S1DGrid grid = new S1DGrid(device, numStates, today, rateCurveTimes, TimeSpan.FromDays(maxTimeStepDays), startVal, pivotPoints, gridSpacings); //Setup drift and vol arrays for geometric brownian motion double[][] drift = new double[numCurvePoints][]; double[][] vol = new double[numCurvePoints][]; for (int iCurvePoint = 0; iCurvePoint < numCurvePoints; ++iCurvePoint) { drift[iCurvePoint] = new double[numStates]; vol[iCurvePoint] = new double[numStates]; for (int iState = 0; iState < numStates; ++iState) { drift[iCurvePoint][iState] = rates[iCurvePoint] * grid.host_d_xval(iState); vol[iCurvePoint][iState] = volatility * grid.host_d_xval(iState); } } //Create model CLVModel model = new CLVModel(grid, "BSTestModel"); model.mkgen(drift, vol); //drift and vol are expressed annually model.set_discount_curve(discountFactors); DateTime[] stepTimes = BuildDates(today, daysToMaturity, numSteps); model.make_mc_plan(numScenariosPerBatch, numBatches, stepTimes); model.exe_mc_plan(); VanillaPayoffEvaluator evaluator = new VanillaPayoffEvaluator(model, strike); //model.device_mc_init(); //model.device_mc_run1f(null, evaluator); //first parameter is not used if (seed != 0) model.host_d_mc_init_seed(seed); else model.host_d_mc_init(); model.host_d_mc_run1f(null, evaluator); //first parameter is not used double payoff = evaluator.GetPayoff(); //do discounting double pv = payoff * discountFactors[discountFactors.Length - 1]; return pv; }
static void Main(string[] args) { Console.WriteLine("running benchmark for stochastic volatility model on the cpu"); int ni = 10; double S0 = 100; int nscen_per_batch = 4096 * 25; int nbatches = 100; TimeSpan dt0 = TimeSpan.FromDays(1); EFloatingPointUnit fpu = EFloatingPointUnit.host; EFloatingPointPrecision fpp = EFloatingPointPrecision.bit64; DateTime today = DateTime.Today; DateTime[] t_k = new DateTime[40]; for (int k = 0; k < 40; k++) t_k[k] = today.AddDays(7 * (k + 1)); DateTime[] t_i = new DateTime[ni]; t_i[0] = today.AddDays(30); t_i[1] = today.AddDays(60); t_i[2] = today.AddDays(90); t_i[3] = today.AddDays(120); t_i[4] = today.AddDays(150); t_i[5] = today.AddDays(180); t_i[6] = today.AddDays(210); t_i[7] = today.AddDays(240); t_i[8] = today.AddDays(270); t_i[9] = today.AddDays(300); double[] xpivot_p = new double[7]; double[] xgridspacing_p = new double[7]; xpivot_p[0] = 1; xgridspacing_p[0] = 5; xpivot_p[1] = 70; xgridspacing_p[1] = 2.5; xpivot_p[2] = 90; xgridspacing_p[2] = 1; xpivot_p[3] = 110; xgridspacing_p[3] = 2.5; xpivot_p[4] = 140; xgridspacing_p[4] = 5; xpivot_p[5] = 200; xgridspacing_p[5] = 7.5; xpivot_p[6] = 300; xgridspacing_p[6] = 10; int nx = 64; int nr = 8; double[] rval_r = new double[nr]; rval_r[0] = 0.55; rval_r[1] = 0.75; rval_r[2] = 0.90; rval_r[3] = 1.0; rval_r[4] = 1.1; rval_r[5] = 1.3; rval_r[6] = 1.6; rval_r[7] = 2.0; int r0 = 4; OPModel.Types.CDevice device = new OPModel.Types.CDevice(fpp, fpu, 0); OPModel.Types.S2DGrid grid = new OPModel.Types.S2DGrid(device, nx, nr, today, t_i, dt0, S0, xpivot_p, xgridspacing_p, rval_r, r0); double vol = .25; double lowbeta = 0.5; double highbeta = 0.5; double volvol = 0.5; double volmrr = 0.5; double volmrl = 3; double jumpsz_minus = -3.0; double jumpsz_plus = 0.0; double[,] taumatrix_ccol = new double[8, 4]; for (int col = 0; col <= 2; col++) { taumatrix_ccol[0, col] = vol; taumatrix_ccol[1, col] = lowbeta; taumatrix_ccol[2, col] = highbeta; taumatrix_ccol[3, col] = volvol; taumatrix_ccol[4, col] = volmrr; taumatrix_ccol[5, col] = volmrl; taumatrix_ccol[6, col] = jumpsz_minus; taumatrix_ccol[7, col] = jumpsz_plus; } double[] ir_i = new double[ni]; double[] df_i = new double[ni]; for (int i = 0; i < ni; i++) { ir_i[i] = 0.05; if (i == 0) df_i[0] = Math.Exp(-ir_i[i] * (t_i[0] - grid.today).Days / 365.25); else df_i[i] = df_i[i - 1] * Math.Exp(-ir_i[i] * (t_i[i] - t_i[i - 1]).Days / 365.25); } CStopWatch sw = new CStopWatch(); CSVModel model = new CSVModel(grid, "DCSV1F", df_i); model.mkgen(taumatrix_ccol, null); model.make_mc_plan(nscen_per_batch, nbatches, t_k); model.reset_flop_counter(); double time = sw.Peek(); sw.Reset(); model.exe_mc_plan(); time = sw.Peek(); double nflops = model.cpu_nflops; double gigaflops_per_second = nflops / (1000000000d * time); Console.WriteLine("blas performance: " + String.Format("{0:0.0}", gigaflops_per_second) + " GF/sec"); CMCEvaluator evaluator = new emptyEvaluator(); double[] payoff_a = new double[nscen_per_batch * model.mcplan.nth]; model.host_d_mc_init(); sw.Reset(); unsafe { model.host_d_mc_run1f(payoff_a, evaluator); } time = sw.Peek(); double nevals = (double)t_k.Length * (double)nbatches * (double)nscen_per_batch; double milion_evals_per_second = nevals / (1000000 * time); Console.WriteLine("mc performance: " + String.Format("{0:0.0}", milion_evals_per_second) + " milion eval/sec"); Console.Read(); }
public SGrid(SGrid grid, bool deep_copy) { if (grid == null || _initialized) throw new System.Exception(); _initialized = true; this._device = grid._device; this._d = grid.d; this._nx = grid.nx; this._nr = grid.nr; this._ni = grid.ni; this._dt0 = grid.dt0; this._x0 = grid._x0; this._r0 = grid._r0; this._y0 = grid._y0; this._today = grid._today; if (deep_copy) { this._t_i = new DateTime[ni]; grid.t_i.CopyTo(this.t_i, 0); this._host_d_xval_y = new double[d]; grid._host_d_xval_y.CopyTo(this._host_d_xval_y, 0); set_invm(grid._host_d_invm); } else { this._t_i = grid.t_i; this._host_d_xval_y = grid._host_d_xval_y; this._host_d_invm = grid._host_d_invm; } }
protected void init(CDevice device, int nx, int nr, DateTime today, DateTime[] t_i, TimeSpan dt0, double xval0, double[] xpivot_p, double[] xgridspacing_p, double[] rval_r, int r0) { this._device = device; this._d = (int)(nx * nr); this._nx = nx; this._nr = nr; this._today = today; if (t_i == null) throw new System.Exception(); this._ni = t_i.Length; this._t_i = new DateTime[ni]; t_i.CopyTo(this.t_i, 0); this._dt0 = dt0; this._host_d_xval_y = new double[d]; this._host_d_rval_y = new double[d]; if (nr > 1) { if (rval_r.Length != nr) throw new System.Exception(); } else { if (rval_r != null) throw new System.Exception(); } this._r0 = r0; mkgrid(ref _host_d_xval_y, ref _host_d_rval_y, ref _x0, r0, ref _y0, xval0, xpivot_p, xgridspacing_p, rval_r); set_invm(pregen()); }
static void Main(string[] args) { Console.WriteLine("running benchmark for local volatility model on the cpu"); int ni = 10; double S0 = 100; int nscen_per_batch = 4096 * 25; int nbatches = 120; TimeSpan dt0 = TimeSpan.FromDays(1); EFloatingPointUnit fpu = EFloatingPointUnit.host; EFloatingPointPrecision fpp = EFloatingPointPrecision.bit64; DateTime today = DateTime.Today; DateTime[] t_k = new DateTime[40]; for (int k = 0; k < 40; k++) t_k[k] = today.AddDays(7 * (k + 1)); DateTime[] t_i = new DateTime[ni]; t_i[0] = today.AddDays(30); t_i[1] = today.AddDays(60); t_i[2] = today.AddDays(90); t_i[3] = today.AddDays(120); t_i[4] = today.AddDays(150); t_i[5] = today.AddDays(180); t_i[6] = today.AddDays(210); t_i[7] = today.AddDays(240); t_i[8] = today.AddDays(270); t_i[9] = today.AddDays(300); double[] xpivot_p = new double[7]; double[] xgridspacing_p = new double[7]; xpivot_p[0] = 1; xgridspacing_p[0] = 5; xpivot_p[1] = 70; xgridspacing_p[1] = 2.5; xpivot_p[2] = 90; xgridspacing_p[2] = 1; xpivot_p[3] = 110; xgridspacing_p[3] = 2.5; xpivot_p[4] = 140; xgridspacing_p[4] = 5; xpivot_p[5] = 200; xgridspacing_p[5] = 7.5; xpivot_p[6] = 300; xgridspacing_p[6] = 10; int nx = 128; OPModel.Types.CDevice device = new OPModel.Types.CDevice(fpp, fpu, 0); OPModel.Types.S1DGrid grid = new OPModel.Types.S1DGrid(device, nx, today, t_i, dt0, S0, xpivot_p, xgridspacing_p); double beta_i; double[] ir_i = new double[ni]; double[] df_i = new double[ni]; double[][] SDrift_i_y = new double[ni][]; double[][] SVol_i_y = new double[ni][]; for (int i = 0; i < ni; i++) { ir_i[i] = 0.05; if (i == 0) df_i[0] = Math.Exp(-ir_i[i] * (t_i[0] - grid.today).Days / 365.25); else df_i[i] = df_i[i - 1] * Math.Exp(-ir_i[i] * (t_i[i] - t_i[i - 1]).Days / 365.25); double Sigma0 = 0.25; beta_i = 1; SDrift_i_y[i] = new double[grid.d]; SVol_i_y[i] = new double[grid.d]; for (int y = 0; y < grid.d; y++) { SDrift_i_y[i][y] = ir_i[i] * grid.host_d_xval(y); SVol_i_y[i][y] = Sigma0 * grid.host_d_xval(grid.y0) * Math.Pow(grid.host_d_xval(y) / grid.host_d_xval(grid.y0), beta_i); } } CStopWatch sw = new CStopWatch(); sw.Reset(); CLVModel model = new CLVModel(grid, "DCLV1F"); model.set_discount_curve(df_i); model.mkgen(SDrift_i_y, SVol_i_y); model.make_mc_plan(nscen_per_batch, nbatches, t_k); model.reset_flop_counter(); double time = sw.Peek(); sw.Reset(); model.exe_mc_plan(); time = sw.Peek(); double nflops = model.cpu_nflops; double gigaflops_per_second = nflops / (1000000000d * time); Console.WriteLine("blas performance: " + String.Format("{0:0.0}", gigaflops_per_second) + " GF/sec"); CMCEvaluator evaluator = new emptyEvaluator(); model.host_d_mc_init(); double[] payoff_a = new double[nscen_per_batch * model.mcplan.nth]; sw.Reset(); unsafe { model.host_d_mc_run1f(payoff_a, evaluator); } time = sw.Peek(); double nevals = (double)t_k.Length * (double)nbatches * (double)nscen_per_batch; double milion_evals_per_second = nevals / (1000000 * time); Console.WriteLine("mc performance: " + String.Format("{0:0.0}", milion_evals_per_second) + " milion eval/sec"); Console.Read(); }
public static void run_benchmark_gpu_sgsv1f(uint dev) { Console.WriteLine("running sgsv1f, device " + dev); int ni = 10; double S0 = 100; int nscen_per_batch = 4096 * 25; int nbatches = 100; TimeSpan dt0 = TimeSpan.FromDays(1); EFloatingPointUnit fpu = EFloatingPointUnit.device; EFloatingPointPrecision fpp = EFloatingPointPrecision.bit32; DateTime today = DateTime.Today; DateTime[] t_k = new DateTime[40]; for (int k = 0; k < 40; k++) t_k[k] = today.AddDays(7 * (k + 1)); DateTime[] t_i = new DateTime[ni]; t_i[0] = today.AddDays(30); t_i[1] = today.AddDays(60); t_i[2] = today.AddDays(90); t_i[3] = today.AddDays(120); t_i[4] = today.AddDays(150); t_i[5] = today.AddDays(180); t_i[6] = today.AddDays(210); t_i[7] = today.AddDays(240); t_i[8] = today.AddDays(270); t_i[9] = today.AddDays(300); double[] xpivot_p = new double[7]; double[] xgridspacing_p = new double[7]; xpivot_p[0] = 1; xgridspacing_p[0] = 5; xpivot_p[1] = 70; xgridspacing_p[1] = 2.5; xpivot_p[2] = 90; xgridspacing_p[2] = 1; xpivot_p[3] = 110; xgridspacing_p[3] = 2.5; xpivot_p[4] = 140; xgridspacing_p[4] = 5; xpivot_p[5] = 200; xgridspacing_p[5] = 7.5; xpivot_p[6] = 300; xgridspacing_p[6] = 10; int nx = 64; int nr = 8; double[] rval_r = new double[nr]; rval_r[0] = 0.55; rval_r[1] = 0.75; rval_r[2] = 0.90; rval_r[3] = 1.0; rval_r[4] = 1.1; rval_r[5] = 1.3; rval_r[6] = 1.6; rval_r[7] = 2.0; int r0 = 4; OPModel.Types.CDevice device = new OPModel.Types.CDevice(fpp, fpu, 0); OPModel.Types.S2DGrid grid = new OPModel.Types.S2DGrid(device, nx, nr, today, t_i, dt0, S0, xpivot_p, xgridspacing_p, rval_r, r0); double vol = .25; double lowbeta = 0.5; double highbeta = 0.5; double volvol = 0.5; double volmrr = 0.5; double volmrl = 3; double jumpsz_minus = -3.0; double jumpsz_plus = 0.0; double[,] taumatrix_ccol = new double[8, 4]; for (int col = 0; col <= 2; col++) { taumatrix_ccol[0, col] = vol; taumatrix_ccol[1, col] = lowbeta; taumatrix_ccol[2, col] = highbeta; taumatrix_ccol[3, col] = volvol; taumatrix_ccol[4, col] = volmrr; taumatrix_ccol[5, col] = volmrl; taumatrix_ccol[6, col] = jumpsz_minus; taumatrix_ccol[7, col] = jumpsz_plus; } double[] ir_i = new double[ni]; double[] df_i = new double[ni]; for (int i = 0; i < ni; i++) { ir_i[i] = 0.05; if (i == 0) df_i[0] = Math.Exp(-ir_i[i] * (t_i[0] - grid.today).Days / 365.25); else df_i[i] = df_i[i - 1] * Math.Exp(-ir_i[i] * (t_i[i] - t_i[i - 1]).Days / 365.25); } CSVModel model = new CSVModel(grid, "SGSV1F", df_i); model.mkgen(taumatrix_ccol, null); model.make_mc_plan(nscen_per_batch, nbatches, t_k); model.device_thread_synchronize(); CStopWatch sw = new CStopWatch(); sw.Reset(); model.reset_flop_counter(); model.exe_mc_plan(); model.device_thread_synchronize(); double time = sw.Peek(); double nflops = model.gpu_nflops; double gigaflops_per_second = nflops / (1000000000d * time); Console.WriteLine("blas performance: " + String.Format("{0:0.0}", gigaflops_per_second) + " GF/sec"); CMCEvaluator evaluator = new emptyEvaluator(); double[] pdf_y = new double[grid.d]; sw.Reset(); if (model.device_mc_init() == 1) { Console.WriteLine("device_mc_init() failed on device " + dev + ". Aborting"); return; } unsafe { model.device_mc_run1f(pdf_y, evaluator); } model.device_thread_synchronize(); time = sw.Peek(); double nevals = (double)t_k.Length * (double)nbatches * (double)nscen_per_batch; double milion_evals_per_second = nevals / (1000000 * time); int status = opcuda_shutdown(); if (status != 0) throw new ExecutionEngineException(); Console.WriteLine("mc performance: " + String.Format("{0:0.0}", milion_evals_per_second) + " milion eval/sec"); }
public static void run_benchmark_gpu_sglv1f(uint dev) { Console.WriteLine("running sglv1f, device " + dev); int ni = 10; double S0 = 100; int nscen_per_batch = 4096 ; int nbatches = 10; TimeSpan dt0 = TimeSpan.FromDays(1); EFloatingPointUnit fpu = EFloatingPointUnit.device; EFloatingPointPrecision fpp = EFloatingPointPrecision.bit32; DateTime today = DateTime.Today; DateTime[] t_k = new DateTime[40]; for (int k = 0; k < 40; k++) t_k[k] = today.AddDays(7 * (k + 1)); DateTime[] t_i = new DateTime[ni]; t_i[0] = today.AddDays(30); t_i[1] = today.AddDays(60); t_i[2] = today.AddDays(90); t_i[3] = today.AddDays(120); t_i[4] = today.AddDays(150); t_i[5] = today.AddDays(180); t_i[6] = today.AddDays(210); t_i[7] = today.AddDays(240); t_i[8] = today.AddDays(270); t_i[9] = today.AddDays(300); double[] xpivot_p = new double[7]; double[] xgridspacing_p = new double[7]; xpivot_p[0] = 1; xgridspacing_p[0] = 5; xpivot_p[1] = 70; xgridspacing_p[1] = 2.5; xpivot_p[2] = 90; xgridspacing_p[2] = 1; xpivot_p[3] = 110; xgridspacing_p[3] = 2.5; xpivot_p[4] = 140; xgridspacing_p[4] = 5; xpivot_p[5] = 200; xgridspacing_p[5] = 7.5; xpivot_p[6] = 300; xgridspacing_p[6] = 10; int nx = 128; OPModel.Types.CDevice device = new OPModel.Types.CDevice(fpp, fpu, dev); OPModel.Types.S1DGrid grid = new OPModel.Types.S1DGrid(device, nx, today, t_i, dt0, S0, xpivot_p, xgridspacing_p); double beta_i; double[] ir_i = new double[ni]; double[] df_i = new double[ni]; double[][] SDrift_i_y = new double[ni][]; double[][] SVol_i_y = new double[ni][]; for (int i = 0; i < ni; i++) { ir_i[i] = 0.05; if (i == 0) df_i[0] = Math.Exp(-ir_i[i] * (t_i[0] - grid.today).Days / 365.25); else df_i[i] = df_i[i - 1] * Math.Exp(-ir_i[i] * (t_i[i] - t_i[i - 1]).Days / 365.25); double Sigma0 = 0.25; beta_i = 1; SDrift_i_y[i] = new double[grid.d]; SVol_i_y[i] = new double[grid.d]; for (int y = 0; y < grid.d; y++) { SDrift_i_y[i][y] = ir_i[i] * grid.host_d_xval(y); SVol_i_y[i][y] = Sigma0 * grid.host_d_xval(grid.y0) * Math.Pow(grid.host_d_xval(y) / grid.host_d_xval(grid.y0), beta_i); } } CStopWatch sw = new CStopWatch(); sw.Reset(); CLVModel model = new CLVModel(grid, "SGLV1F"); model.set_discount_curve(df_i); model.mkgen(SDrift_i_y, SVol_i_y); model.make_mc_plan(nscen_per_batch, nbatches, t_k); model.reset_flop_counter(); sw.Reset(); model.exe_mc_plan(); model.device_thread_synchronize(); double time = sw.Peek(); double nflops = model.gpu_nflops; double gigaflops_per_second = nflops / (1000000000d * time); Console.WriteLine("blas performance: " + String.Format("{0:0.0}", gigaflops_per_second) + " GF/sec"); CMCEvaluator evaluator = new emptyEvaluator(); double[] pdf_y = new double[grid.d]; if (model.device_mc_init() == 1) { Console.WriteLine("device_mc_init() failed on device " + dev + ". Aborting"); return; } sw.Reset(); unsafe { model.device_mc_run1f(pdf_y, evaluator); } model.device_thread_synchronize(); time = sw.Peek(); double nevals = (double)t_k.Length * (double)nbatches * (double)nscen_per_batch; double milion_evals_per_second = nevals / (1000000 * time); int status = opcuda_shutdown(); if (status != 0) throw new ExecutionEngineException(); Console.WriteLine("mc performance: " + String.Format("{0:0.0}", milion_evals_per_second) + " milion eval/sec"); }