Made program multi-threaded

This commit is contained in:
Kevin
2017-07-30 21:06:53 -04:00
parent b1ca1c9097
commit 0ec0e69bfe
3 changed files with 63 additions and 38 deletions

Binary file not shown.

View File

@@ -34,6 +34,7 @@ void *run_simulations(void *opt_ptr)
double gamma_shift = 0, base_gamma = 0, upper_gamma = 0, lower_gamma = 0;
double price, delta, gamma, vega, theta, rho;
double base = 0;
double max_rand = 0;
struct Option *opt = (struct Option*) opt_ptr;
@@ -45,7 +46,8 @@ void *run_simulations(void *opt_ptr)
theta_tte = tte - 1. / 365;
for (i=0; i<opt->sims; i++) {
rand = gaussrand();
rand = opt->randoms[i];
max_rand = rand > max_rand ? rand : max_rand;
/* Base scenario */
level = gbm_simulation(opt->spot, opt->rfr, opt->vol, tte, rand);
base += max((level - opt->strike) * opt->type, 0);
@@ -97,15 +99,37 @@ void *run_simulations(void *opt_ptr)
void gbm(struct Option *opt)
{
int i;
int i = 0, j = 0;
pthread_t *threads;
struct Option *options;
double **randoms;
options = malloc(sizeof(struct Option) * NUM_THREADS);
randoms = malloc(sizeof(double*) * NUM_THREADS);
for(i=0; i<NUM_THREADS; i++) {
randoms[i] = malloc(sizeof(double) * opt->sims / NUM_THREADS);
}
/* generate array of normal randoms */
for(i=0;;) {
if (j == (opt->sims / NUM_THREADS)) {
j = 0;
i += 1;
}
if (i == NUM_THREADS) {
break;
}
randoms[i][j] = gaussrand();
j++;
}
opt->sims = opt->sims / NUM_THREADS;
for(i=0; i<NUM_THREADS; i++) {
options[i] = *opt;
options[i].expiry_date = opt->expiry_date;
options[i].value_date = opt->value_date;
options[i].randoms = randoms[i];
}
threads = malloc(sizeof(pthread_t) * NUM_THREADS);

View File

@@ -21,6 +21,7 @@ struct Option {
double rfr;
double vol;
long sims;
double *randoms;
/* fv and greeks */
double fv;