diff --git a/Makefile b/Makefile index 022a728..e108bd8 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC=gcc -CFLAGS=-Wall -fPIC -O3 -ansi -pedantic-errors +CFLAGS=-Wall -fPIC -O3 -ansi -pedantic-errors -pthread LDFLAGS=-lm PREFIX= /usr/local diff --git a/build/opt-pricer b/build/opt-pricer index d091c94..6ade738 100755 Binary files a/build/opt-pricer and b/build/opt-pricer differ diff --git a/src/gbm_mc.c b/src/gbm_mc.c index b2224a0..9df56ae 100644 --- a/src/gbm_mc.c +++ b/src/gbm_mc.c @@ -3,9 +3,17 @@ #include "gbm_mc.h" #include "utils.h" + #include +#include +#include +#include +#include #include +#define NUM_THREADS 8 + + double gbm_simulation(double spot, double rfr, double vol, double tte, double rand) { /* S_T = S_0e^{[r-\frac{\sigma^2}{2}]t + \sigma\sqrt{t}\xi_i} */ @@ -17,14 +25,17 @@ double gbm_simulation(double spot, double rfr, double vol, double tte, double ra return spot * exp(drift + stoch); } -void gbm(struct Option *opt) + +void *run_simulations(void *opt_ptr) { + int i; double tte, theta_tte, expiry_date, value_date, level, rand, df; double delta_shift = 0, vega_shift = 0, theta_shift = 0, rho_shift = 0; double gamma_shift = 0, base_gamma = 0, upper_gamma = 0, lower_gamma = 0; double price, delta, gamma, vega, theta, rho; double base = 0; - int i; + + struct Option *opt = (struct Option*) opt_ptr; if (opt->sims < 1) opt->sims = 1; @@ -79,4 +90,41 @@ void gbm(struct Option *opt) opt->vega = (vega - price) / 0.01; opt->theta = (theta - price) / (tte - theta_tte); opt->rho = (rho - price); + + return (void *) opt; +} + + +void gbm(struct Option *opt) +{ + int i; + pthread_t *threads; + struct Option *options; + options = malloc(sizeof(struct Option) * NUM_THREADS); + for(i=0; isims = opt->sims / NUM_THREADS; + + for(i=0; ifv += result->fv / NUM_THREADS; + opt->delta += result->delta / NUM_THREADS; + opt->gamma += result->gamma / NUM_THREADS; + opt->vega += result->vega / NUM_THREADS; + opt->theta += result->theta / NUM_THREADS; + opt->rho += result->rho / NUM_THREADS; + opt->sims += result->sims; + } } diff --git a/src/gbm_mc.h b/src/gbm_mc.h index 308e247..99ed8d8 100644 --- a/src/gbm_mc.h +++ b/src/gbm_mc.h @@ -3,9 +3,13 @@ #include + struct Option; + double gbm_simulation(double spot, double rfr, double vol, double tte, double rand); +void *run_simulation(void *opt_ptr); + void gbm(struct Option *opt); #endif