#include <iostream>
#include <stdio.h>
#include <cuda.h>
#include <arrayfire.h>
#include "input.h"
using namespace af;
array cnd(const array& x)
{
float sqrt2 = sqrtf(2.0f);
array temp = (x > 0);
array y = mul(temp, (0.5f + erf(x/sqrt2)/2)) + mul((1-temp), (0.5f - erf((-x)/sqrt2)/2));
return y;
}
void black_scholes(array& C, array& P, const array& S, const array& X, const array& R, const array& V, const array& T)
{
array d1 = log(S / X);
d1 = d1 + mul((R + mul(mul(V,V), 0.5)), T);
d1 = d1 / mul(V, sqrt(T));
array d2 = d1 - mul(V, sqrt(T));
array tmp = mul(X, exp(mul(-R, T)));
C = mul(S, cnd(d1)) - mul(tmp, cnd(d2));
P = mul(tmp, cnd(-d2)) - mul(S, cnd(-d1));
}
int main(int argc, char **argv)
{
printf("** Arrayfire Black-Scholes Example **\n"
"** by AccelerEyes **\n\n");
try {
array GC1(4000, 1, C1);
array GC2(4000, 1, C2);
array GC3(4000, 1, C3);
array GC4(4000, 1, C4);
array GC5(4000, 1, C5);
dim4 dims = GC1.dims();
int iter = 5;
for (int n = 1; n <= 50; n += 5) {
array Sg = tile(GC1, n, 1);
array Xg = tile(GC2, n, 1);
array Rg = tile(GC3, n, 1);
array Vg = tile(GC4, n, 1);
array Tg = tile(GC5, n, 1);
dim4 dims = Xg.dims();
printf("Input Data Size = %d x %d\n", dims[0], dims[1]);
af::sync();
array Cg, Pg;
timer::tic();
for (int i = 0; i < iter; i++) {
black_scholes(Cg, Pg, Sg,Xg,Rg,Vg,Tg);
eval(Cg,Pg);
}
af::sync();
printf("Mean GPU Time = %0.6f\n\n\n", timer::toc()/iter);
}
} catch (af::exception& e){
fprintf(stderr, "%s\n", e.what());
}
#ifdef WIN32 // pause in Windows
if (!(argc == 2 && argv[1][0] =='-')) {
printf("hit [enter]...");
getchar();
}
#endif
return 0;
}