DO MORE. CODE LESS. Free software for GPU computing on AMD, Intel, and NVIDIA.
examples/blackscholes.cpp
#include "arrayfire.h"
#include "input.h"
#include "math.h"

using namespace af;
using namespace std;

// FIXME
array cnd(array x)
{
        float sqrt2= sqrtf(2.0f);
        array temp = (x > 0.0);
        array y = temp * (erf(x/sqrt2)/2 + 0.5) + (temp - 1.0) * (erf((-x)/sqrt2)/2 - 0.5);
        return y;
}

void black_scholes(array& C, array& P, array& S, array& X, array& R, array& V, const array& T)
{
    // This function computes the call and put option prices based on
    // Black-Scholes Model

    // S = Underlying stock price
    // X = Strike Price
    // R = Risk free rate of interest
    // V = Volatility
    // T = Time to maturity

    array d1_ = log(S / X);
    d1_ = d1_ + (R + (V*V)*0.5) * T;
    array d1 = d1_ / (V* sqrt(T));

    array d2 = d1 - (V*sqrt(T));

    C = S * cnd(d1)  - (X * exp((-R)*T) * cnd(d2));
    P = X * exp((-R)*T) * cnd(-d2) - (S * cnd(-d1));
}

int main(void)
{
    cout << "** ArrayFire Black-Scholes Example **" << endl;
    int N = 4000;

    array array1 = array(C1, N, 1);
    array array2 = array(C2, N, 1);
    array array3 = array(C3, N, 1);
    array array4 = array(C4, N, 1);
    array array5 = array(C5, N, 1);

    size_t dims[] = {array1.m_rows, array1.m_cols};
    cout << "Input Data Size = " << dims[0] << " x " << dims[1] << endl;

    int iter = 5;
    for (int n = 1; n <= 50; n += 5) {

        // Create GPU copies of the data
        array Sg = tile(array1, n, 1);
        array Xg = tile(array2, n, 1);
        array Rg = tile(array3, n, 1);
        array Vg = tile(array4, n, 1);
        array Tg = tile(array5, n, 1);

        size_t dims[] = {Xg.m_rows, Xg.m_cols};
        cout << "Input Data Size = " << dims[0] << " x " << dims[1] << endl;

        // Force compute on the device
        af::sync();
        array Cg, Pg;
        af::timer::start();
        for (int i = 0; i < iter; i++)
            black_scholes(Cg, Pg, Sg,Xg,Rg,Vg,Tg);
        af::sync();
        printf("Mean Computation Time = %0.6f\n\n\n", af::timer::stop());
    }
  std::cout << "hit [enter]...";
    getchar();
    return 0;
}
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines