probability

Date:

01-09-2012

NAME

probability, probability_almost_always, probability_almost_never - Specify information used by interprocedure analysis (IPA) and the optimizer to produce faster code sequences

SYNOPSIS

#pragma probability const
#pragma probability_almost_always
#pragma probability_almost_never
!DIR$ PROBABILITY const
!DIR$ PROBABILITY_ALMOST_ALWAYS
!DIR$ PROBABILITY_ALMOST_NEVER
const

An expression that evaluates to a floating point constant at compilation time, where 0.0 <=const<=1.0.

IMPLEMENTATION

Cray Linux Environment (CLE)

DESCRIPTION

The probability, probability_almost_always, and probability_almost_never directives specify information used by IPA and the optimizer to produce faster code sequences. The specified probability is a hint, not a statement of fact. You can also specify almost_never and almost_always by using the values 0.0 and 1.0, respectively.

These directives can appear anywhere executable code is legal.

Each directive applies to the block of code where it appears. It is important to realize that the directive should not be applied to a conditional test directly; rather, it should be used to indicate the relative probability of a then or else branch being executed.

EXAMPLES

Example 1: probability directive

The following example in C/C++ :

    if ( a[i] > b[i] ) {
#pragma probability 0.3
        a[i] = b[i];
    }

This example states that the probability of entering the block of code with the assignment statement is 0.3 or 30%. This also means that a[i] is expected to be greater than b[i] 30% of the time.

Note that the probability directive appears within the conditional block of code, rather than before it. This removes some of the ambiguity that has plagued other implementations that tie the directive directly to the conditional code.

This information is used to guide inlining decisions, branch elimination optimizations, branch hint marking, and the choice of the optimal algorithmic approach to the vectorization of conditional code.

The following GCC-style intrinsic is also accepted when it appears in a conditional test:

__builtin_expect( expr, const )

Example 2: PROBABILITY directive

The following example in Fortran:

                     IF ( A(I) > B(I) ) THEN
!DIR$ PROBABILITY 0.3
                             A(I) = B(I)
                     ENDIF

This example states that the probability of entering the block of code with the assignment statement is 0.3, or 30%. In turn, this means that a(i) is expected to be greater than b(i) 30% of the time as well.

Example 3: probability_almost_never directive

The following example in C/C++ :

if ( __builtin_expect( a[i] > b[i], 0 ) ) {
    a[i] = b[i];
}

is roughly equivalent to:

    if ( a[i] > b[i] ) {
#pragma _CRI probability_almost_never
        a[i] = b[i];
    }

Example 4: PROBABILITY_ALMOST_NEVER directive

The following example in Fortran:

                     DO I = 1,N
                             IF ( A(I) > 0.0 ) THEN
!DIR$ PROBABILITY_ALMOST_NEVER
                                     B(I) = B(I)/A(I) + A(I)/B(I) ! EVALUATE USING SPARSE METHODS
                             ENDIF
                     ENDDO

For vector IF code, probability_almost_never (a probability of very low or < 0.1) will cause the compiler to use the vector gather/scatter methods used for sparse IF vector code, instead of the vector merge methods used for denser IF code.

SEE ALSO

intro_directives(1)