forked from ParaGroup/p3arsec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HJM_Securities.cpp
378 lines (312 loc) · 10.9 KB
/
HJM_Securities.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//HJM_Securities.cpp
//Routines to compute various security prices using HJM framework (via Simulation).
//Authors: Mark Broadie, Jatin Dewanwala
//Collaborator: Mikhail Smelyanskiy, Jike Chong, Intel
//Modified by Christian Bienia for the PARSEC Benchmark Suite
//
// FastFlow version by Daniele De Sensi (d.desensi.software@gmail.com)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include "nr_routines.h"
#include "HJM.h"
#include "HJM_Securities.h"
#include "HJM_type.h"
#ifdef ENABLE_THREADS
#include <pthread.h>
#define MAX_THREAD 1024
#ifdef TBB_VERSION
#include "tbb/task_scheduler_init.h"
#include "tbb/blocked_range.h"
#include "tbb/parallel_for.h"
#include "tbb/cache_aligned_allocator.h"
tbb::cache_aligned_allocator<FTYPE> memory_ftype;
tbb::cache_aligned_allocator<parm> memory_parm;
#define TBB_GRAINSIZE 1
#endif // TBB_VERSION
#ifdef ENABLE_NORNIR
#include <instrumenter.hpp>
#include <stdlib.h>
#include <iostream>
std::string getParametersPath(){
return std::string(getenv("PARSECDIR")) + std::string("/parameters.xml");
}
#endif //ENABLE_NORNIR
#ifdef FF_VERSION
#include <ff/parallel_for.hpp>
#endif
#endif //ENABLE_THREADS
#ifdef ENABLE_PARSEC_HOOKS
#include <hooks.h>
#endif
int NUM_TRIALS = DEFAULT_NUM_TRIALS;
int nThreads = 1;
int nSwaptions = 1;
int iN = 11;
//FTYPE dYears = 5.5;
int iFactors = 3;
parm *swaptions;
long seed = 1979; //arbitrary (but constant) default value (birth year of Christian Bienia)
long swaption_seed;
// =================================================
FTYPE *dSumSimSwaptionPrice_global_ptr;
FTYPE *dSumSquareSimSwaptionPrice_global_ptr;
int chunksize;
#ifdef ENABLE_NORNIR
nornir::Instrumenter* instr;
#endif //ENABLE_NORNIR
#ifdef TBB_VERSION
struct Worker {
Worker(){}
void operator()(const tbb::blocked_range<int> &range) const {
FTYPE pdSwaptionPrice[2];
int begin = range.begin();
int end = range.end();
for(int i=begin; i!=end; i++) {
int iSuccess = HJM_Swaption_Blocking(pdSwaptionPrice, swaptions[i].dStrike,
swaptions[i].dCompounding, swaptions[i].dMaturity,
swaptions[i].dTenor, swaptions[i].dPaymentInterval,
swaptions[i].iN, swaptions[i].iFactors, swaptions[i].dYears,
swaptions[i].pdYield, swaptions[i].ppdFactors,
swaption_seed+i, NUM_TRIALS, BLOCK_SIZE, 0);
assert(iSuccess == 1);
swaptions[i].dSimSwaptionMeanPrice = pdSwaptionPrice[0];
swaptions[i].dSimSwaptionStdError = pdSwaptionPrice[1];
}
}
};
#endif //TBB_VERSION
#if !defined(FF_VERSION) && !defined(TBB_VERSION)
void * worker(void *arg){
int tid = *((int *)arg);
FTYPE pdSwaptionPrice[2];
int beg, end, chunksize;
if (tid < (nSwaptions % nThreads)) {
chunksize = nSwaptions/nThreads + 1;
beg = tid * chunksize;
end = (tid+1)*chunksize;
} else {
chunksize = nSwaptions/nThreads;
int offsetThread = nSwaptions % nThreads;
int offset = offsetThread * (chunksize + 1);
beg = offset + (tid - offsetThread) * chunksize;
end = offset + (tid - offsetThread + 1) * chunksize;
}
if(tid == nThreads -1 )
end = nSwaptions;
for(int i=beg; i < end; i++) {
int iSuccess = HJM_Swaption_Blocking(pdSwaptionPrice, swaptions[i].dStrike,
swaptions[i].dCompounding, swaptions[i].dMaturity,
swaptions[i].dTenor, swaptions[i].dPaymentInterval,
swaptions[i].iN, swaptions[i].iFactors, swaptions[i].dYears,
swaptions[i].pdYield, swaptions[i].ppdFactors,
swaption_seed+i, NUM_TRIALS, BLOCK_SIZE, tid);
assert(iSuccess == 1);
swaptions[i].dSimSwaptionMeanPrice = pdSwaptionPrice[0];
swaptions[i].dSimSwaptionStdError = pdSwaptionPrice[1];
}
return NULL;
}
#endif
//print a little help message explaining how to use this program
void print_usage(char *name) {
fprintf(stderr,"Usage: %s OPTION [OPTIONS]...\n", name);
fprintf(stderr,"Options:\n");
fprintf(stderr,"\t-ns [number of swaptions (should be > number of threads]\n");
fprintf(stderr,"\t-sm [number of simulations]\n");
fprintf(stderr,"\t-nt [number of threads]\n");
fprintf(stderr,"\t-sd [random number seed]\n");
}
//Please note: Whenever we type-cast to (int), we add 0.5 to ensure that the value is rounded to the correct number.
//For instance, if X/Y = 0.999 then (int) (X/Y) will equal 0 and not 1 (as (int) rounds down).
//Adding 0.5 ensures that this does not happen. Therefore we use (int) (X/Y + 0.5); instead of (int) (X/Y);
int main(int argc, char *argv[])
{
int iSuccess = 0;
int i,j;
FTYPE **factors=NULL;
#ifdef PARSEC_VERSION
#define __PARSEC_STRING(x) #x
#define __PARSEC_XSTRING(x) __PARSEC_STRING(x)
printf("PARSEC Benchmark Suite Version " __PARSEC_XSTRING(PARSEC_VERSION)"\n");
fflush(NULL);
#else
printf("PARSEC Benchmark Suite\n");
fflush(NULL);
#endif //PARSEC_VERSION
#ifdef ENABLE_PARSEC_HOOKS
__parsec_bench_begin(__parsec_swaptions);
#endif
if(argc == 1)
{
print_usage(argv[0]);
exit(1);
}
for (int j=1; j<argc; j++) {
if (!strcmp("-sm", argv[j])) {NUM_TRIALS = atoi(argv[++j]);}
else if (!strcmp("-nt", argv[j])) {nThreads = atoi(argv[++j]);}
else if (!strcmp("-ns", argv[j])) {nSwaptions = atoi(argv[++j]);}
else if (!strcmp("-sd", argv[j])) {seed = atoi(argv[++j]);}
else {
fprintf(stderr,"Error: Unknown option: %s\n", argv[j]);
print_usage(argv[0]);
exit(1);
}
}
if(nSwaptions < nThreads) {
fprintf(stderr,"Error: Fewer swaptions than threads.\n");
print_usage(argv[0]);
exit(1);
}
printf("Number of Simulations: %d, Number of threads: %d Number of swaptions: %d\n", NUM_TRIALS, nThreads, nSwaptions);
swaption_seed = (long)(2147483647L * RanUnif(&seed));
#ifdef ENABLE_NORNIR
instr = new nornir::Instrumenter(getParametersPath(), nThreads);
#endif //ENABLE_NORNIR
#ifdef ENABLE_THREADS
#ifdef TBB_VERSION
tbb::task_scheduler_init init(nThreads);
#elif !defined(FF_VERSION)
pthread_t *threads;
pthread_attr_t pthread_custom_attr;
threads = (pthread_t *) malloc(nThreads * sizeof(pthread_t));
pthread_attr_init(&pthread_custom_attr);
#endif // TBB_VERSION
if ((nThreads < 1) || (nThreads > MAX_THREAD))
{
fprintf(stderr,"Number of threads must be between 1 and %d.\n", MAX_THREAD);
exit(1);
}
#else
if (nThreads != 1)
{
fprintf(stderr,"Number of threads must be 1 (serial version)\n");
exit(1);
}
#endif //ENABLE_THREADS
// initialize input dataset
factors = dmatrix(0, iFactors-1, 0, iN-2);
//the three rows store vol data for the three factors
factors[0][0]= .01;
factors[0][1]= .01;
factors[0][2]= .01;
factors[0][3]= .01;
factors[0][4]= .01;
factors[0][5]= .01;
factors[0][6]= .01;
factors[0][7]= .01;
factors[0][8]= .01;
factors[0][9]= .01;
factors[1][0]= .009048;
factors[1][1]= .008187;
factors[1][2]= .007408;
factors[1][3]= .006703;
factors[1][4]= .006065;
factors[1][5]= .005488;
factors[1][6]= .004966;
factors[1][7]= .004493;
factors[1][8]= .004066;
factors[1][9]= .003679;
factors[2][0]= .001000;
factors[2][1]= .000750;
factors[2][2]= .000500;
factors[2][3]= .000250;
factors[2][4]= .000000;
factors[2][5]= -.000250;
factors[2][6]= -.000500;
factors[2][7]= -.000750;
factors[2][8]= -.001000;
factors[2][9]= -.001250;
// setting up multiple swaptions
swaptions =
#ifdef TBB_VERSION
(parm *)memory_parm.allocate(sizeof(parm)*nSwaptions, NULL);
#else
(parm *)malloc(sizeof(parm)*nSwaptions);
#endif
int k;
for (i = 0; i < nSwaptions; i++) {
swaptions[i].Id = i;
swaptions[i].iN = iN;
swaptions[i].iFactors = iFactors;
swaptions[i].dYears = 5.0 + ((int)(60*RanUnif(&seed)))*0.25; //5 to 20 years in 3 month intervals
swaptions[i].dStrike = 0.1 + ((int)(49*RanUnif(&seed)))*0.1; //strikes ranging from 0.1 to 5.0 in steps of 0.1
swaptions[i].dCompounding = 0;
swaptions[i].dMaturity = 1.0;
swaptions[i].dTenor = 2.0;
swaptions[i].dPaymentInterval = 1.0;
swaptions[i].pdYield = dvector(0,iN-1);;
swaptions[i].pdYield[0] = .1;
for(j=1;j<=swaptions[i].iN-1;++j)
swaptions[i].pdYield[j] = swaptions[i].pdYield[j-1]+.005;
swaptions[i].ppdFactors = dmatrix(0, swaptions[i].iFactors-1, 0, swaptions[i].iN-2);
for(k=0;k<=swaptions[i].iFactors-1;++k)
for(j=0;j<=swaptions[i].iN-2;++j)
swaptions[i].ppdFactors[k][j] = factors[k][j];
}
// **********Calling the Swaption Pricing Routine*****************
#ifdef ENABLE_PARSEC_HOOKS
__parsec_roi_begin();
#endif
#ifdef ENABLE_THREADS
#ifdef TBB_VERSION
Worker w;
tbb::parallel_for(tbb::blocked_range<int>(0,nSwaptions,TBB_GRAINSIZE),w);
#elif defined(FF_VERSION)
ff::ParallelFor pf;
pf.parallel_for_thid(0, nSwaptions, 1, PARFOR_STATIC(0), [](const long i, const int thid) {
FTYPE pdSwaptionPrice[2];
int iSuccess = HJM_Swaption_Blocking(pdSwaptionPrice, swaptions[i].dStrike,
swaptions[i].dCompounding, swaptions[i].dMaturity,
swaptions[i].dTenor, swaptions[i].dPaymentInterval,
swaptions[i].iN, swaptions[i].iFactors, swaptions[i].dYears,
swaptions[i].pdYield, swaptions[i].ppdFactors,
swaption_seed+i, NUM_TRIALS, BLOCK_SIZE, thid);
assert(iSuccess == 1);
swaptions[i].dSimSwaptionMeanPrice = pdSwaptionPrice[0];
swaptions[i].dSimSwaptionStdError = pdSwaptionPrice[1];
}, nThreads);
#else
int threadIDs[nThreads];
for (i = 0; i < nThreads; i++) {
threadIDs[i] = i;
pthread_create(&threads[i], &pthread_custom_attr, worker, &threadIDs[i]);
}
for (i = 0; i < nThreads; i++) {
pthread_join(threads[i], NULL);
}
free(threads);
#endif // TBB_VERSION
#else
int threadID=0;
worker(&threadID);
#endif //ENABLE_THREADS
#ifdef ENABLE_PARSEC_HOOKS
__parsec_roi_end();
#endif
#ifdef ENABLE_NORNIR
instr->terminate();
std::cout << "riff.time|" << instr->getExecutionTime() << std::endl;
std::cout << "riff.iterations|" << instr->getTotalTasks() << std::endl;
delete instr;
#endif //ENABLE_NORNIR
for (i = 0; i < nSwaptions; i++) {
fprintf(stderr,"Swaption %d: [SwaptionPrice: %.10lf StdError: %.10lf] \n",
i, swaptions[i].dSimSwaptionMeanPrice, swaptions[i].dSimSwaptionStdError);
}
for (i = 0; i < nSwaptions; i++) {
free_dvector(swaptions[i].pdYield, 0, swaptions[i].iN-1);
free_dmatrix(swaptions[i].ppdFactors, 0, swaptions[i].iFactors-1, 0, swaptions[i].iN-2);
}
#ifdef TBB_VERSION
memory_parm.deallocate(swaptions, sizeof(parm));
#else
free(swaptions);
#endif // TBB_VERSION
//***********************************************************
#ifdef ENABLE_PARSEC_HOOKS
__parsec_bench_end();
#endif
return iSuccess;
}