Tutorial: SIMD-Enabled Functions

Previous: Array Notation Up: Overview Next: #pragma simd

SIMD-enabled function is a function which can be invoked either on scalar arguments or on array elements in parallel.  SIMD-enabled functions are specified by using the annotation __declspec(vector) or __attribute__((vector)) to indicate that the compiler should generate both scalar and vector versions of the function.

Here's a serial implementation of the Black-Scholes options pricing algorithm derived from code published by Bernt Arne Odegaard, http://finance.bi.no/~bernt/gcc_prog/recipes/recipes/:

double option_price_call_black_scholes(double s, double k, double r,
                                       double sigma, double time);
double option_price_put_black_scholes(double s, double k, double r,
                                      double sigma, double time);

void test_option_price_call_and_put_black_scholes_Serial(
    double S[NUM_OPTIONS], double K[NUM_OPTIONS],
    double r, double sigma,double time[NUM_OPTIONS],
    double call_Serial[NUM_OPTIONS], double put_Serial[NUM_OPTIONS])
{
    for (int i=0; i < NUM_OPTIONS; i++)
    {
        // Invoke calculations for call-options
        call_Serial[i] =
            option_price_call_black_scholes(S[i], K[i], r, sigma, time[i]);

        // Invoke calculations for put-options
        put_Serial[i] =
            option_price_put_black_scholes(S[i], K[i], r, sigma, time[i]);
    }
}

This could be implemented using Array Notation as a SIMD-enabled function:

__declspec(vector)
double option_price_call_black_scholes(double s, double k, double r,
                                       double sigma, double time);
__declspec(vector)
double option_price_put_black_scholes(double s, double k, double r,
                                      double sigma, double time);

void test_option_price_call_and_put_black_scholes_ArrayNotations(
    double S[NUM_OPTIONS], double K[NUM_OPTIONS],
    double r, double sigma,double time[NUM_OPTIONS],
    double call_ArrayNotations[NUM_OPTIONS],
    double put_ArrayNotations[NUM_OPTIONS])
{
    // Call vectored version of the call function optimized for given core's
    // SIMD vector width
    call_ArrayNotations[:] =
        option_price_call_black_scholes(S[:], K[:], r, sigma, time[:]);

    // Call vectored version of the put function optimized for given core's
    // SIMD vector width
    put_ArrayNotations[:] =
        option_price_put_black_scholes(S[:], K[:], r, sigma, time[:]);
}
Previous: Array Notation Up: Overview Next: #pragma simd