-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel.cl
349 lines (291 loc) · 9.67 KB
/
kernel.cl
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
#define FTYPE double
FTYPE dMax(FTYPE dA, FTYPE dB) {
return dA > dB ? dA : dB;
}
FTYPE RanUnif(long *s)
{
// uniform random number generator
long ix, k1;
FTYPE dRes;
ix = *s;
*s = ix+1;
ix *= 1513517L;
ix %= 2147483647L;
k1 = ix/127773L;
ix = 16807L*( ix - k1*127773L ) - k1 * 2836L;
if (ix < 0) ix = ix + 2147483647L;
//*s = ix;
dRes = (ix * 4.656612875e-10);
return (dRes);
}
__constant FTYPE a[4] = {
2.50662823884,
-18.61500062529,
41.39119773534,
-25.44106049637
};
__constant FTYPE b[4] = {
-8.47351093090,
23.08336743743,
-21.06224101826,
3.13082909833
};
__constant FTYPE c[9] = {
0.3374754822726147,
0.9761690190917186,
0.1607979714918209,
0.0276438810333863,
0.0038405729373609,
0.0003951896511919,
0.0000321767881768,
0.0000002888167364,
0.0000003960315187
};
FTYPE CumNormalInv(FTYPE u)
{
FTYPE x, r;
x = u - 0.5;
if(fabs (x) < 0.42)
{
r = x * x;
r = x * ((( a[3]*r + a[2]) * r + a[1]) * r + a[0])/
((((b[3] * r+ b[2]) * r + b[1]) * r + b[0]) * r + 1.0);
return (r);
}
r = u;
if(x > 0.0) r = 1.0 - u;
r = log(-log(r));
r = c[0] + r * (c[1] + r *
(c[2] + r * (c[3] + r *
(c[4] + r * (c[5] + r * (c[6] + r * (c[7] + r*c[8])))))));
if(x < 0.0) r = -r;
return (r);
}
void HJM_Yield_to_Forward (__local FTYPE *pdForward, int iN, __global FTYPE *pdYield) {
int i;
pdForward[0] = pdYield[0];
for(i = 1; i < iN; i++) pdForward[i] = (i + 1) * pdYield[i] - i * pdYield[i - 1];
}
void HJM_Drifts(__local FTYPE *pdTotalDrift,
__local FTYPE *ppdDrifts,
int iN,
int iFactors,
FTYPE dYears,
__global FTYPE *ppdFactors) {
int i, j, l;
FTYPE ddelt = (FTYPE)(dYears/iN);
FTYPE dSumVol;
for (i = 0; i < iFactors; i++)
ppdDrifts[i * (iN - 1)] = 0.5 * ddelt * (ppdFactors[i * (iN - 1)]) * (ppdFactors[i * (iN - 1)]);
for (i = 0; i < iFactors;i++) {
for (j = 1; j < iN - 1; j++) {
ppdDrifts[i * (iN - 1) + j] = 0;
for(l = 0; l < j; l++) ppdDrifts[i * (iN - 1) + j] -= ppdDrifts[i * (iN - 1) + l];
dSumVol=0;
for(l = 0; l <= j; l++) dSumVol += ppdFactors[i * (iN - 1) + l];
ppdDrifts[i * (iN - 1) + j] += 0.5 * ddelt * dSumVol * dSumVol;
}
}
for(i = 0; i < iN - 1; i++) {
pdTotalDrift[i]=0;
for(j = 0; j < iFactors; j++) pdTotalDrift[i] += ppdDrifts[j * (iN - 1) + i];
}
}
void serialB(__local FTYPE *pdZ, __local FTYPE *randZ, int BLOCKSIZE, int iN, int iFactors)
{
int l, b, j;
for(l = 0; l < iFactors; l++){
for(b = 0; b < BLOCKSIZE; b++){
for (j = 1; j < iN; j++){
pdZ[l * iN * BLOCKSIZE + BLOCKSIZE*j + b]= CumNormalInv(randZ[l * iN * BLOCKSIZE + BLOCKSIZE * j + b]); /* 18% of the total executition time */
}
}
}
}
void HJM_SimPath_Forward_Blocking(__local FTYPE *ppdHJMPath,
int iN,
int iFactors,
FTYPE dYears,
__local FTYPE *pdForward,
__local FTYPE *pdTotalDrift,
__global FTYPE *ppdFactors,
long lRndSeed,
int BLOCKSIZE,
__local FTYPE *pdZ, __local FTYPE *randZ) {
int i, j, l;
int b;
FTYPE dTotalShock;
FTYPE ddelt, sqrt_ddelt;
ddelt = (FTYPE)(dYears/iN);
sqrt_ddelt = sqrt(ddelt);
for(b = 0; b < BLOCKSIZE; b++){
for(j = 0; j < iN; j++){
ppdHJMPath[BLOCKSIZE*j + b] = pdForward[j];
for(i = 1; i < iN; i++) ppdHJMPath[i * iN * BLOCKSIZE + BLOCKSIZE*j + b] = 0;
}
}
for(b = 0; b < BLOCKSIZE; b++){
for(j = 1; j < iN; j++){
for(l = 0; l < iFactors; l++){
randZ[l * iN * BLOCKSIZE + BLOCKSIZE * j + b] = RanUnif(&lRndSeed);
}
}
}
serialB(pdZ, randZ, BLOCKSIZE, iN, iFactors);
for(b = 0; b < BLOCKSIZE; b++){
for(j = 1; j < iN; j++) {
for(l = 0; l < iN - j; l++){
dTotalShock = 0;
for (i = 0; i < iFactors; i++) dTotalShock += ppdFactors[i * (iN - 1) + l] * pdZ[i * iN * BLOCKSIZE + BLOCKSIZE * j + b];
ppdHJMPath[j * iN * BLOCKSIZE + BLOCKSIZE * l + b] = ppdHJMPath[(j - 1) * iN * BLOCKSIZE + BLOCKSIZE * (l + 1) + b] + pdTotalDrift[l] * ddelt + sqrt_ddelt * dTotalShock;
}
}
}
}
void Discount_Factors_Blocking(__local FTYPE *pdDiscountFactors,
int iN,
FTYPE dYears,
__local FTYPE *pdRatePath,
int BLOCKSIZE,
__local FTYPE *pdexpRes) {
int i, j, b;
FTYPE ddelt = (FTYPE)(dYears/iN); //HJM time-step length
//precompute the exponientials
for (j = 0; j < (iN - 1) * BLOCKSIZE; j++) pdexpRes[j] = -pdRatePath[j] * ddelt;
for (j = 0; j < (iN - 1) * BLOCKSIZE; j++) pdexpRes[j] = exp(pdexpRes[j]);
//initializing the discount factor vector
for (i = 0; i < iN * BLOCKSIZE; i++) pdDiscountFactors[i] = 1.0;
for (i = 1; i < iN; i++){
for (b = 0; b < BLOCKSIZE; b++){
for (j = 0; j < i; j++){
pdDiscountFactors[i * BLOCKSIZE + b] *= pdexpRes[j * BLOCKSIZE + b];
}
}
}
}
__kernel void HJM_Swaption_Blocking(__global FTYPE *pdSwaptionPrice,
const FTYPE dStrike,
const FTYPE dCompounding,
const FTYPE dMaturity,
const FTYPE dTenor,
const FTYPE dPaymentInterval,
const int iN,
const int iFactors,
const FTYPE dYears,
__global FTYPE *pdYield,
__global FTYPE *ppdFactors,
const long lRndSeed,
const long lTrials,
int BLOCKSIZE, int tid,
__local FTYPE *ppdHJMPath, __local FTYPE *pdForward, __local FTYPE *ppdDrifts, __local FTYPE *pdTotalDrift,
__local FTYPE *pdDiscountingRatePath, __local FTYPE *pdPayoffDiscountFactors, __local FTYPE *pdSwapRatePath, __local FTYPE *pdSwapDiscountFactors, __local FTYPE *pdSwapPayoffs,
__local FTYPE *pdZ, __local FTYPE *randZ,
__local FTYPE *pdexpRes,
__global FTYPE *partialSum,
__global FTYPE *partialSum2,
const int id)
{
uint globalId = get_global_id(0);
uint globalSize = get_global_size(0);
if(globalId >= lTrials / BLOCKSIZE) return;
int i, b;
long l;
FTYPE ddelt = (FTYPE)(dYears/iN);
int iFreqRatio = (int)(dPaymentInterval/ddelt + 0.5);
FTYPE dStrikeCont;
dStrikeCont = dCompounding == 0 ? dStrike: (1 / dCompounding) * log(1 + dStrike * dCompounding);
int iSwapVectorLength = (int)(iN - dMaturity / ddelt + 0.5);
int iSwapStartTimeIndex;
int iSwapTimePoints;
FTYPE dSwapVectorYears;
FTYPE dSwaptionPayoff;
FTYPE dDiscSwaptionPayoff;
FTYPE dFixedLegValue;
// Accumulators
FTYPE dSumSimSwaptionPrice;
FTYPE dSumSquareSimSwaptionPrice;
// Final returned results
FTYPE dSimSwaptionMeanPrice;
FTYPE dSimSwaptionStdError;
iSwapStartTimeIndex = (int)(dMaturity/ddelt + 0.5); //Swap starts at swaption maturity
iSwapTimePoints = (int)(dTenor/ddelt + 0.5); //Total HJM time points corresponding to the swap's tenor
dSwapVectorYears = (FTYPE)(iSwapVectorLength*ddelt);
for (i = 0; i < iSwapVectorLength; i++) pdSwapPayoffs[i] = 0.0;
for (i = iFreqRatio; i <= iSwapTimePoints; i += iFreqRatio) {
if(i != iSwapTimePoints)
pdSwapPayoffs[i] = exp(dStrikeCont * dPaymentInterval) - 1;
else
pdSwapPayoffs[i] = exp(dStrikeCont * dPaymentInterval);
}
//generating forward curve at t=0 from supplied yield curve
HJM_Yield_to_Forward(pdForward, iN, pdYield);
//computation of drifts from factor volatilities
HJM_Drifts(pdTotalDrift, ppdDrifts, iN, iFactors, dYears, ppdFactors);
dSumSimSwaptionPrice = 0.0;
dSumSquareSimSwaptionPrice = 0.0;
long seed = lRndSeed + iFactors * (iN - 1) * BLOCKSIZE * globalId;
//Simulations begin:
HJM_SimPath_Forward_Blocking(ppdHJMPath, iN, iFactors, dYears, pdForward, pdTotalDrift, ppdFactors, seed, BLOCKSIZE, pdZ, randZ);
for(i = 0; i < iN; i++){
for(b = 0; b < BLOCKSIZE; b++){
pdDiscountingRatePath[BLOCKSIZE*i + b] = ppdHJMPath[i * iN * BLOCKSIZE + b];
}
}
Discount_Factors_Blocking(pdPayoffDiscountFactors, iN, dYears, pdDiscountingRatePath, BLOCKSIZE, pdexpRes);
for(i = 0; i < iSwapVectorLength; i++){
for(b = 0; b < BLOCKSIZE; b++){
pdSwapRatePath[i * BLOCKSIZE + b] = ppdHJMPath[iSwapStartTimeIndex * iN * BLOCKSIZE + i * BLOCKSIZE + b];
}
}
Discount_Factors_Blocking(pdSwapDiscountFactors, iSwapVectorLength, dSwapVectorYears, pdSwapRatePath, BLOCKSIZE, pdexpRes);
FTYPE t1 = 0.0, t2 = 0.0;
for(b = 0; b < BLOCKSIZE; b++){
dFixedLegValue = 0.0;
for(i = 0; i < iSwapVectorLength; i++) dFixedLegValue += pdSwapPayoffs[i] * pdSwapDiscountFactors[i*BLOCKSIZE + b];
dSwaptionPayoff = dMax(dFixedLegValue - 1.0, 0);
dDiscSwaptionPayoff = dSwaptionPayoff * pdPayoffDiscountFactors[iSwapStartTimeIndex*BLOCKSIZE + b];
t1 += dDiscSwaptionPayoff;
t2 += dDiscSwaptionPayoff * dDiscSwaptionPayoff;
}
partialSum[globalId] = t1;
partialSum2[globalId] = t2;
}
__kernel void reduction_sum(__global FTYPE* partialSum,
__global FTYPE* partialSum2,
__local FTYPE* localSums1,
__local FTYPE* localSums2,
__global FTYPE* output1,
__global FTYPE* output2) {
uint globalId = get_global_id(0);
uint globalSize = get_global_size(0);
uint localId = get_local_id(0);
uint groupSize = get_local_size(0);
localSums1[localId] = partialSum[globalId];
localSums2[localId] = partialSum2[globalId];
for(uint stride = groupSize / 2; stride > 0; stride >>= 1) {
barrier(CLK_LOCAL_MEM_FENCE);
if(localId < stride) {
localSums1[localId] += localSums1[localId + stride];
localSums2[localId] += localSums2[localId + stride];
}
}
if(localId == 0) {
output1[get_group_id(0)] = localSums1[0];
output2[get_group_id(0)] = localSums2[0];
}
}
__kernel void non_reduction_sum(__global FTYPE* partialSum,
__global FTYPE* partialSum2,
__global FTYPE* output1,
__global FTYPE* output2,
int len,
long lTrials) {
int i;
FTYPE o1, o2;
for(i = 0; i < len; i++) {
o1 += partialSum[i];
o2 += partialSum2[i];
}
*output1 = o1 / lTrials;
*output2 = sqrt((o2 - o1 * o1 / lTrials) / (lTrials - 1)) / sqrt((FTYPE)lTrials);
}