-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathmcBase.h
989 lines (792 loc) · 27 KB
/
mcBase.h
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
/*
Written by Antoine Savine in 2018
This code is the strict IP of Antoine Savine
License to use and alter this code for personal and commercial applications
is freely granted to any person or company who purchased a copy of the book
Modern Computational Finance: AAD and Parallel Simulations
Antoine Savine
Wiley, 2018
As long as this comment is preserved at the top of the file
*/
#pragma once
// Main file in the simulation library
// All the base classes and template algorithms
// See chapters 6, 7, 12 and 14
#include "AAD.h"
#include <vector>
#include <memory>
#include <algorithm>
#include <numeric>
#include <sstream>
#include <iomanip>
using namespace std;
#include "matrix.h"
#include "ThreadPool.h"
using Time = double;
extern Time systemTime;
// Scenarios
// =========
// SampleDef = definition
// of what data must be simulated
struct SampleDef
{
// need numeraire?
bool numeraire = true;
struct RateDef
{
Time start;
Time end;
string curve;
RateDef(const Time s, const Time e, const string& c) :
start(s), end(e), curve(c) {};
};
vector<Time> discountMats;
vector<RateDef> liborDefs;
// multi-asset: forwardMats[a] = maturities for asset a
vector<vector<Time>> forwardMats;
};
// Sample = simulated value
// of data on a given event date
template <class T>
struct Sample
{
T numeraire;
vector<T> discounts;
vector<T> libors;
// multi-asset: forwardMats[a][t] = forward for asset a, maturity t
vector<vector<T>> forwards;
// Allocate given SampleDef
void allocate(const SampleDef& data)
{
discounts.resize(data.discountMats.size());
libors.resize(data.liborDefs.size());
forwards.resize(data.forwardMats.size());
for (size_t a = 0; a < forwards.size(); ++a) forwards[a].resize(data.forwardMats[a].size());
}
// Initialize defaults
void initialize()
{
numeraire = T(1.0);
fill(discounts.begin(), discounts.end(), T(1.0));
fill(libors.begin(), libors.end(), T(0.0));
for (auto& forward: forwards) fill(forward.begin(), forward.end(), T(100.0));
}
};
template <class T>
using Scenario = vector<Sample<T>>;
template <class T>
inline void allocatePath(const vector<SampleDef>& defline, Scenario<T>& path)
{
path.resize(defline.size());
for (size_t i = 0; i < defline.size(); ++i)
{
path[i].allocate(defline[i]);
}
}
template <class T>
inline void initializePath(Scenario<T>& path)
{
for (auto& scen : path) scen.initialize();
}
// Products
// ========
template <class T>
class Product
{
inline static const vector<string> defaultAssetNames = { "spot" };
public:
// Access to the product timeline
// along with the sample definitions (defline)
virtual const vector<Time>& timeline() const = 0;
virtual const vector<SampleDef>& defline() const = 0;
// Number and names of underlying assets, default = 1 and "spot"
virtual const size_t numAssets() const { return 1; }
virtual const vector<string>& assetNames() const { return defaultAssetNames; }
// Labels of all payoffs in the product
virtual const vector<string>& payoffLabels() const = 0;
// Compute payoffs given a path (on the product timeline)
virtual void payoffs(
// path, one entry per time step (on the product timeline)
const Scenario<T>& path,
// pre-allocated space for resulting payoffs
vector<T>& payoffs)
const = 0;
virtual unique_ptr<Product<T>> clone() const = 0;
virtual ~Product() {}
};
// Models
// ======
template <class T>
class Model
{
inline static const vector<string> defaultAssetNames = { "spot" };
public:
// Number and names of underlying assets, default = 1 and "spot"
virtual const size_t numAssets() const { return 1; }
virtual const vector<string>& assetNames() const { return defaultAssetNames; }
// Initialize with product timeline
virtual void allocate(
const vector<Time>& prdTimeline,
const vector<SampleDef>& prdDefline)
= 0;
virtual void init(
const vector<Time>& prdTimeline,
const vector<SampleDef>& prdDefline)
= 0;
// Access to the MC dimension
virtual size_t simDim() const = 0;
// Generate a path consuming a vector[simDim()] of independent Gaussians
// return results in a pre-allocated scenario
virtual void generatePath(
const vector<double>& gaussVec,
Scenario<T>& path)
const = 0;
virtual unique_ptr<Model<T>> clone() const = 0;
virtual ~Model() {}
// Access to all the model parameters and what they mean
virtual const vector<T*>& parameters() = 0;
virtual const vector<string>& parameterLabels() const = 0;
// Number of parameters
size_t numParams() const
{
return const_cast<Model*>(this)->parameters().size();
}
// Put parameters on tape, only valid for T = Number
void putParametersOnTape()
{
putParametersOnTapeT<T>();
}
private:
// If T not Number : do nothing
template<class U>
void putParametersOnTapeT()
{
}
// If T = Number : put on tape
template <>
void putParametersOnTapeT<Number>()
{
for (Number* param : parameters()) param->putOnTape();
}
};
// Random number generators
// ========================
class RNG
{
public:
// Initialise with dimension simDim
virtual void init(const size_t simDim) = 0;
// Compute the next vector[simDim] of independent Uniforms or Gaussians
// The vector is filled by the function and must be pre-allocated
virtual void nextU(vector<double>& uVec) = 0;
virtual void nextG(vector<double>& gaussVec) = 0;
virtual unique_ptr<RNG> clone() const = 0;
virtual ~RNG() {}
// Skip ahead
virtual void skipTo(const unsigned b) = 0;
};
// Template algorithms
// ===================
// Check compatibility of model and product
// At the moment, only check that assets are the samein both cases
// May be easily extended in the future
template <class T>
inline bool checkCompatiblity(
const Product<T>& prd,
const Model<T>& mdl)
{
return prd.assetNames() == mdl.assetNames();
}
// Serial valuation, chapter 6
// MC simulator: free function that conducts simulations
// and returns a matrix (as vector of vectors) of payoffs
// (0..nPath-1 , 0..nPay-1)
inline vector<vector<double>> mcSimul(
const Product<double>& prd,
const Model<double>& mdl,
const RNG& rng,
const size_t nPath)
{
if (!checkCompatiblity(prd, mdl)) throw runtime_error("Model and product are not compatible");
// Work with copies of the model and RNG
// which are modified when we set up the simulation
// Copies are OK at high level
auto cMdl = mdl.clone();
auto cRng = rng.clone();
// Allocate results
const size_t nPay = prd.payoffLabels().size();
vector<vector<double>> results(nPath, vector<double>(nPay));
// Init the simulation timeline
cMdl->allocate(prd.timeline(), prd.defline());
cMdl->init(prd.timeline(), prd.defline());
// Init the RNG
cRng->init(cMdl->simDim());
// Allocate Gaussian vector
vector<double> gaussVec(cMdl->simDim());
// Allocate path
Scenario<double> path;
allocatePath(prd.defline(), path);
initializePath(path);
// Iterate through paths
for (size_t i = 0; i<nPath; i++)
{
// Next Gaussian vector, dimension D
cRng->nextG(gaussVec);
// Generate path, consume Gaussian vector
cMdl->generatePath(gaussVec, path);
// Compute result
prd.payoffs(path, results[i]);
}
return results; // C++11: move
}
// Parallel valuation, chapter 7
#define BATCHSIZE size_t{64}
// Parallel equivalent of mcSimul()
inline vector<vector<double>> mcParallelSimul(
const Product<double>& prd,
const Model<double>& mdl,
const RNG& rng,
const size_t nPath)
{
if (!checkCompatiblity(prd, mdl)) throw runtime_error("Model and product are not compatible");
auto cMdl = mdl.clone();
const size_t nPay = prd.payoffLabels().size();
vector<vector<double>> results(nPath, vector<double>(nPay));
cMdl->allocate(prd.timeline(), prd.defline());
cMdl->init(prd.timeline(), prd.defline());
// Allocate space for Gaussian vectors and paths,
// one for each thread
ThreadPool *pool = ThreadPool::getInstance();
const size_t nThread = pool->numThreads();
vector<vector<double>> gaussVecs(nThread+1); // +1 for main
vector<Scenario<double>> paths(nThread+1);
for (auto& vec : gaussVecs) vec.resize(cMdl->simDim());
for (auto& path : paths)
{
allocatePath(prd.defline(), path);
initializePath(path);
}
// One RNG per thread
vector<unique_ptr<RNG>> rngs(nThread + 1);
for (auto& random : rngs)
{
random = rng.clone();
random->init(cMdl->simDim());
}
// Reserve memory for futures
vector<TaskHandle> futures;
futures.reserve(nPath / BATCHSIZE + 1);
// Start
// Same as mcSimul() except we send tasks to the pool
// instead of executing them
size_t firstPath = 0;
size_t pathsLeft = nPath;
while (pathsLeft > 0)
{
size_t pathsInTask = min<size_t>(pathsLeft, BATCHSIZE);
futures.push_back( pool->spawnTask ( [&, firstPath, pathsInTask]()
{
// Inside the parallel task,
// pick the right pre-allocated vectors
const size_t threadNum = pool->threadNum();
vector<double>& gaussVec = gaussVecs[threadNum];
Scenario<double>& path = paths[threadNum];
// Get a RNG and position it correctly
auto& random = rngs[threadNum];
random->skipTo(firstPath);
// And conduct the simulations, exactly same as sequential
for (size_t i = 0; i < pathsInTask; i++)
{
// Next Gaussian vector, dimension D
random->nextG(gaussVec);
// Path
cMdl->generatePath(gaussVec, path);
// Payoff
prd.payoffs(path, results[firstPath + i]);
}
// Remember tasks must return bool
return true;
}));
pathsLeft -= pathsInTask;
firstPath += pathsInTask;
}
// Wait and help
for (auto& future : futures) pool->activeWait(future);
return results; // C++11: move
}
// AAD instrumentation of mcSimul(), chapter 12
// returns the following results:
struct AADSimulResults
{
AADSimulResults(const size_t nPath, const size_t nPay, const size_t nParam) :
payoffs(nPath, vector<double>(nPay)),
aggregated(nPath),
risks(nParam)
{}
// matrix(0..nPath - 1, 0..nPay - 1) of payoffs, same as mcSimul()
vector<vector<double>> payoffs;
// vector(0..nPath) of aggregated payoffs
vector<double> aggregated;
// vector(0..nParam - 1) of risk sensitivities
// of aggregated payoff, averaged over paths
vector<double> risks;
};
// Default aggregator = 1st payoff = payoff[0]
const auto defaultAggregator = [](const vector<Number>& v) {return v[0]; };
template<class F = decltype(defaultAggregator)>
inline AADSimulResults
mcSimulAAD(
const Product<Number>& prd,
const Model<Number>& mdl,
const RNG& rng,
const size_t nPath,
const F& aggFun = defaultAggregator)
{
if (!checkCompatiblity(prd, mdl)) throw runtime_error("Model and product are not compatible");
// Work with copies of the model and RNG
// which are modified when we set up the simulation
// Copies are OK at high level
auto cMdl = mdl.clone();
auto cRng = rng.clone();
// Allocate path and model
Scenario<Number> path;
allocatePath(prd.defline(), path);
cMdl->allocate(prd.timeline(), prd.defline());
// Dimensions
const size_t nPay = prd.payoffLabels().size();
const vector<Number*>& params = cMdl->parameters();
const size_t nParam = params.size();
// AAD - 1
// Access to tape
Tape& tape = *Number::tape;
// Clear and initialise tape
tape.clear();
auto resetter = setNumResultsForAAD();
// Put parameters on tape
// note that also initializes all adjoints
cMdl->putParametersOnTape();
// Init the simulation timeline
// CAREFUL: simulation timeline must be on tape
// Hence moved here
cMdl->init(prd.timeline(), prd.defline());
// Initialize path
initializePath(path);
// Mark the tape straight after initialization
tape.mark();
//
// Init the RNG
cRng->init(cMdl->simDim());
// Allocate workspace
vector<Number> nPayoffs(nPay);
// Gaussian vector
vector<double> gaussVec(cMdl->simDim());
// Results
AADSimulResults results(nPath, nPay, nParam);
// Iterate through paths
for (size_t i = 0; i<nPath; i++)
{
// AAD - 2
// Rewind tape to mark
// parameters stay on tape but the rest is wiped
tape.rewindToMark();
//
// Next Gaussian vector, dimension D
cRng->nextG(gaussVec);
// Generate path, consume Gaussian vector
cMdl->generatePath(gaussVec, path);
// Compute result
prd.payoffs(path, nPayoffs);
// Aggregate
Number result = aggFun(nPayoffs);
// AAD - 3
// Propagate adjoints
result.propagateToMark();
// Store results for the path
results.aggregated[i] = double(result);
convertCollection(
nPayoffs.begin(),
nPayoffs.end(),
results.payoffs[i].begin());
//
}
// AAD - 4
// Mark = limit between pre-calculations and path-wise operations
// Operations above mark have been propagated and accumulated
// We conduct one propagation mark to start
Number::propagateMarkToStart();
//
// Pick sensitivities, summed over paths, and normalize
transform(
params.begin(),
params.end(),
results.risks.begin(),
[nPath](const Number* p) {return p->adjoint() / nPath; });
// Clear the tape
tape.clear();
return results;
}
// Parallel AAD, chapter 12
// Init model and out on tape
inline void initModel4ParallelAAD(
// Inputs
const Product<Number>& prd,
// Cloned model, must have been allocated prior
Model<Number>& clonedMdl,
// Path, also allocated prior
Scenario<Number>& path)
{
// Access to tape
Tape& tape = *Number::tape;
// Rewind tape
tape.rewind();
// Put parameters on tape
// note that also initializes all adjoints
clonedMdl.putParametersOnTape();
// Init the simulation timeline
// CAREFUL: simulation timeline must be on tape
// Hence moved here
clonedMdl.init(prd.timeline(), prd.defline());
// Path
initializePath(path);
// Mark the tape straight after parameters
tape.mark();
//
}
// Parallel version of mcSimulAAD()
template<class F = decltype(defaultAggregator)>
inline AADSimulResults
mcParallelSimulAAD(
const Product<Number>& prd,
const Model<Number>& mdl,
const RNG& rng,
const size_t nPath,
const F& aggFun = defaultAggregator)
{
if (!checkCompatiblity(prd, mdl)) throw runtime_error("Model and product are not compatible");
const size_t nPay = prd.payoffLabels().size();
const size_t nParam = mdl.numParams();
// Allocate results
AADSimulResults results(nPath, nPay, nParam);
// Clear and initialise tape
Number::tape->clear();
auto resetter = setNumResultsForAAD();
// We need one of all these for each thread
// 0: main thread
// 1 to n : worker threads
ThreadPool *pool = ThreadPool::getInstance();
const size_t nThread = pool->numThreads();
// Allocate workspace
// One model clone per thread
vector<unique_ptr<Model<Number>>> models(nThread + 1);
for (auto& model : models)
{
model = mdl.clone();
model->allocate(prd.timeline(), prd.defline());
}
// One scenario per thread
vector<Scenario<Number>> paths(nThread + 1);
for (auto& path : paths)
{
allocatePath(prd.defline(), path);
}
// One vector of payoffs per thread
vector<vector<Number>> payoffs(nThread + 1, vector<Number>(nPay));
// ~workspace
// Tapes for the worker threads
// The main thread has one of its own
vector<Tape> tapes(nThread);
// Model initialized?
// Note we don't use vector<bool>
// because vector<bool> is not thread safe
vector<int> mdlInit(nThread + 1, false);
// Initialize main thread
initModel4ParallelAAD(prd, *models[0], paths[0]);
// Mark main thread as initialized
mdlInit[0] = true;
// Init the RNGs, one pet thread
// One RNG per thread
vector<unique_ptr<RNG>> rngs(nThread + 1);
for (auto& random : rngs)
{
random = rng.clone();
random->init(models[0]->simDim());
}
// One Gaussian vector per thread
vector<vector<double>> gaussVecs
(nThread + 1, vector<double>(models[0]->simDim()));
// Reserve memory for futures
vector<TaskHandle> futures;
futures.reserve(nPath / BATCHSIZE + 1);
// Start
// Same as mcSimul() except we send tasks to the pool
// instead of executing them
size_t firstPath = 0;
size_t pathsLeft = nPath;
while (pathsLeft > 0)
{
size_t pathsInTask = min<size_t>(pathsLeft, BATCHSIZE);
futures.push_back(pool->spawnTask([&, firstPath, pathsInTask]()
{
const size_t threadNum = pool->threadNum();
// Use this thread's tape
// Thread local magic: each thread its own pointer
// Note main thread = 0 is not reset
if (threadNum > 0) Number::tape = &tapes[threadNum - 1];
// Initialize once on each thread
if (!mdlInit[threadNum])
{
// Initialize
initModel4ParallelAAD(prd, *models[threadNum], paths[threadNum]);
// Mark as initialized
mdlInit[threadNum] = true;
}
// Get a RNG and position it correctly
auto& random = rngs[threadNum];
random->skipTo(firstPath);
// And conduct the simulations, exactly same as sequential
for (size_t i = 0; i < pathsInTask; i++)
{
// Rewind tape to mark
// Notice : this is the tape for the executing thread
Number::tape->rewindToMark();
// Next Gaussian vector, dimension D
random->nextG(gaussVecs[threadNum]);
// Path
models[threadNum]->generatePath(
gaussVecs[threadNum],
paths[threadNum]);
// Payoff
prd.payoffs(paths[threadNum], payoffs[threadNum]);
// Propagate adjoints
Number result = aggFun(payoffs[threadNum]);
result.propagateToMark();
// Store results for the path
results.aggregated[firstPath + i] = double(result);
convertCollection(
payoffs[threadNum].begin(),
payoffs[threadNum].end(),
results.payoffs[firstPath + i].begin());
}
// Remember tasks must return bool
return true;
}));
pathsLeft -= pathsInTask;
firstPath += pathsInTask;
}
// Wait and help
for (auto& future : futures) pool->activeWait(future);
// Mark = limit between pre-calculations and path-wise operations
// Operations above mark have been propagated and accumulated
// We conduct one propagation mark to start
// On the main thread's tape
Number::propagateMarkToStart();
// And on the worker thread's tapes
Tape* mainThreadPtr = Number::tape;
for (size_t i = 0; i < nThread; ++i)
{
if (mdlInit[i + 1])
{
// Set tape pointer
Number::tape = &tapes[i];
// On that tape, propagate
Number::propagateMarkToStart();
}
}
// Reset tape to main thread's
Number::tape = mainThreadPtr;
// Sum sensitivities over threads
for (size_t j = 0; j < nParam; ++j)
{
results.risks[j] = 0.0;
for (size_t i = 0; i < models.size(); ++i)
{
if (mdlInit[i]) results.risks[j] += models[i]->parameters()[j]->adjoint();
}
results.risks[j] /= nPath;
}
// Clear the main thread's tape
// The other tapes are cleared on the destruction of the vector of tapes
Number::tape->clear();
return results;
}
// Multi-dimensional AAD, chapter 14
// Rewrite code for the risk reports of multiple payoffs for clarity
struct AADMultiSimulResults
{
AADMultiSimulResults(const size_t nPath, const size_t nPay, const size_t nParam) :
payoffs(nPath, vector<double>(nPay)),
risks(nParam, nPay)
{}
// matrix(0..nPath - 1, 0..nPay - 1) of payoffs, same as mcSimul()
vector<vector<double>> payoffs;
// matrix(0..nParam - 1, 0..nPay - 1) of risk sensitivities
// of all payoffs, averaged over paths
matrix<double> risks;
};
// Serial
inline AADMultiSimulResults
mcSimulAADMulti(
const Product<Number>& prd,
const Model<Number>& mdl,
const RNG& rng,
const size_t nPath)
{
if (!checkCompatiblity(prd, mdl)) throw runtime_error("Model and product are not compatible");
auto cMdl = mdl.clone();
auto cRng = rng.clone();
Scenario<Number> path;
allocatePath(prd.defline(), path);
cMdl->allocate(prd.timeline(), prd.defline());
const size_t nPay = prd.payoffLabels().size();
const vector<Number*>& params = cMdl->parameters();
const size_t nParam = params.size();
Tape& tape = *Number::tape;
tape.clear();
// Set the AAD environment to multi-dimensional with dimension nPay
// Reset to 1D is automatic when resetter exits scope
auto resetter = setNumResultsForAAD(true, nPay);
cMdl->putParametersOnTape();
cMdl->init(prd.timeline(), prd.defline());
initializePath(path);
tape.mark();
cRng->init(cMdl->simDim());
vector<Number> nPayoffs(nPay);
vector<double> gaussVec(cMdl->simDim());
// Allocate multi-dimensional results
// including a matrix(0..nParam - 1, 0..nPay - 1) of risk sensitivities
AADMultiSimulResults results(nPath, nPay, nParam);
for (size_t i = 0; i<nPath; i++)
{
tape.rewindToMark();
cRng->nextG(gaussVec);
cMdl->generatePath(gaussVec, path);
prd.payoffs(path, nPayoffs);
// Multi-dimensional propagation
// client code seeds the tape with the correct boundary conditions
for (size_t j = 0; j < nPay; ++j)
{
nPayoffs[j].adjoint(j) = 1.0;
}
// multi-dimensional propagation over simulation, end to mark
Number::propagateAdjointsMulti(prev(tape.end()), tape.markIt());
convertCollection(
nPayoffs.begin(),
nPayoffs.end(),
results.payoffs[i].begin());
}
// Multi-dimensional propagation over initialization, mark to start
Number::propagateAdjointsMulti(tape.markIt(), tape.begin());
// Pack results
for (size_t i = 0; i < nParam; ++i)
{
for (size_t j = 0; j < nPay; ++j)
{
results.risks[i][j] = params[i]->adjoint(j) / nPath;
}
}
tape.clear();
return results;
}
// Parallel
inline AADMultiSimulResults
mcParallelSimulAADMulti(
const Product<Number>& prd,
const Model<Number>& mdl,
const RNG& rng,
const size_t nPath)
{
if (!checkCompatiblity(prd, mdl)) throw runtime_error("Model and product are not compatible");
const size_t nPay = prd.payoffLabels().size();
const size_t nParam = mdl.numParams();
Number::tape->clear();
auto resetter = setNumResultsForAAD(true, nPay);
ThreadPool *pool = ThreadPool::getInstance();
const size_t nThread = pool->numThreads();
vector<unique_ptr<Model<Number>>> models(nThread + 1);
for (auto& model : models)
{
model = mdl.clone();
model->allocate(prd.timeline(), prd.defline());
}
vector<Scenario<Number>> paths(nThread + 1);
for (auto& path : paths)
{
allocatePath(prd.defline(), path);
}
vector<vector<Number>> payoffs(nThread + 1, vector<Number>(nPay));
vector<Tape> tapes(nThread);
vector<int> mdlInit(nThread + 1, false);
initModel4ParallelAAD(prd, *models[0], paths[0]);
mdlInit[0] = true;
vector<unique_ptr<RNG>> rngs(nThread + 1);
for (auto& random : rngs)
{
random = rng.clone();
random->init(models[0]->simDim());
}
vector<vector<double>> gaussVecs
(nThread + 1, vector<double>(models[0]->simDim()));
AADMultiSimulResults results(nPath, nPay, nParam);
vector<TaskHandle> futures;
futures.reserve(nPath / BATCHSIZE + 1);
size_t firstPath = 0;
size_t pathsLeft = nPath;
while (pathsLeft > 0)
{
size_t pathsInTask = min<size_t>(pathsLeft, BATCHSIZE);
futures.push_back(pool->spawnTask([&, firstPath, pathsInTask]()
{
const size_t threadNum = pool->threadNum();
if (threadNum > 0) Number::tape = &tapes[threadNum - 1];
if (!mdlInit[threadNum])
{
initModel4ParallelAAD(prd, *models[threadNum], paths[threadNum]);
mdlInit[threadNum] = true;
}
auto& random = rngs[threadNum];
random->skipTo(firstPath);
for (size_t i = 0; i < pathsInTask; i++)
{
Number::tape->rewindToMark();
random->nextG(gaussVecs[threadNum]);
models[threadNum]->generatePath(
gaussVecs[threadNum],
paths[threadNum]);
prd.payoffs(paths[threadNum], payoffs[threadNum]);
const size_t n = payoffs[threadNum].size();
for (size_t j = 0; j < n; ++j)
{
payoffs[threadNum][j].adjoint(j) = 1.0;
}
Number::propagateAdjointsMulti(prev(Number::tape->end()), Number::tape->markIt());
convertCollection(
payoffs[threadNum].begin(),
payoffs[threadNum].end(),
results.payoffs[firstPath + i].begin());
}
return true;
}));
pathsLeft -= pathsInTask;
firstPath += pathsInTask;
}
for (auto& future : futures) pool->activeWait(future);
Number::propagateAdjointsMulti(Number::tape->markIt(), Number::tape->begin());
for (size_t i = 0; i < nThread; ++i)
{
if (mdlInit[i + 1])
{
Number::propagateAdjointsMulti(tapes[i].markIt(), tapes[i].begin());
}
}
for (size_t j = 0; j < nParam; ++j) for (size_t k = 0; k < nPay; ++k)
{
results.risks[j][k] = 0.0;
for (size_t i = 0; i < models.size(); ++i)
{
if (mdlInit[i]) results.risks[j][k] += models[i]->parameters()[j]->adjoint(k);
}
results.risks[j][k] /= nPath;
}
Number::tape->clear();
return results;
}