Made program multi-threaded
This commit is contained in:
BIN
build/opt-pricer
BIN
build/opt-pricer
Binary file not shown.
28
src/gbm_mc.c
28
src/gbm_mc.c
@@ -34,6 +34,7 @@ void *run_simulations(void *opt_ptr)
|
|||||||
double gamma_shift = 0, base_gamma = 0, upper_gamma = 0, lower_gamma = 0;
|
double gamma_shift = 0, base_gamma = 0, upper_gamma = 0, lower_gamma = 0;
|
||||||
double price, delta, gamma, vega, theta, rho;
|
double price, delta, gamma, vega, theta, rho;
|
||||||
double base = 0;
|
double base = 0;
|
||||||
|
double max_rand = 0;
|
||||||
|
|
||||||
struct Option *opt = (struct Option*) opt_ptr;
|
struct Option *opt = (struct Option*) opt_ptr;
|
||||||
|
|
||||||
@@ -45,7 +46,8 @@ void *run_simulations(void *opt_ptr)
|
|||||||
theta_tte = tte - 1. / 365;
|
theta_tte = tte - 1. / 365;
|
||||||
|
|
||||||
for (i=0; i<opt->sims; i++) {
|
for (i=0; i<opt->sims; i++) {
|
||||||
rand = gaussrand();
|
rand = opt->randoms[i];
|
||||||
|
max_rand = rand > max_rand ? rand : max_rand;
|
||||||
/* Base scenario */
|
/* Base scenario */
|
||||||
level = gbm_simulation(opt->spot, opt->rfr, opt->vol, tte, rand);
|
level = gbm_simulation(opt->spot, opt->rfr, opt->vol, tte, rand);
|
||||||
base += max((level - opt->strike) * opt->type, 0);
|
base += max((level - opt->strike) * opt->type, 0);
|
||||||
@@ -97,15 +99,37 @@ void *run_simulations(void *opt_ptr)
|
|||||||
|
|
||||||
void gbm(struct Option *opt)
|
void gbm(struct Option *opt)
|
||||||
{
|
{
|
||||||
int i;
|
int i = 0, j = 0;
|
||||||
pthread_t *threads;
|
pthread_t *threads;
|
||||||
struct Option *options;
|
struct Option *options;
|
||||||
|
double **randoms;
|
||||||
options = malloc(sizeof(struct Option) * NUM_THREADS);
|
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;
|
opt->sims = opt->sims / NUM_THREADS;
|
||||||
for(i=0; i<NUM_THREADS; i++) {
|
for(i=0; i<NUM_THREADS; i++) {
|
||||||
options[i] = *opt;
|
options[i] = *opt;
|
||||||
options[i].expiry_date = opt->expiry_date;
|
options[i].expiry_date = opt->expiry_date;
|
||||||
options[i].value_date = opt->value_date;
|
options[i].value_date = opt->value_date;
|
||||||
|
options[i].randoms = randoms[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
threads = malloc(sizeof(pthread_t) * NUM_THREADS);
|
threads = malloc(sizeof(pthread_t) * NUM_THREADS);
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ struct Option {
|
|||||||
double rfr;
|
double rfr;
|
||||||
double vol;
|
double vol;
|
||||||
long sims;
|
long sims;
|
||||||
|
double *randoms;
|
||||||
|
|
||||||
/* fv and greeks */
|
/* fv and greeks */
|
||||||
double fv;
|
double fv;
|
||||||
|
|||||||
Reference in New Issue
Block a user