Rookie HPC

Data sharing

C | FORTRAN

linear

Definition

The linear clause is a superset of the private clause; in addition to declaring as private the list item(s) provided, it also indicates that the list item(s) given have a linear relationship with respect to the iteration space of a loop associated with the construct on which the clause appears. The other possible data-sharing policies are shared, private, firstprivate and lastprivate.

Copy

Feedback

linear(linear-list [: step])

Parameters

linear-list
If the linear clause is specified on a for construct, the value of the new list item on each iteration of the associated loop(s) corresponds to the value of the original list item before entering the construct plus the logical number of the iteration times the step. The value corresponding to the sequentially last iteration of the associated loop(s) is assigned to the original list item.
If the linear clause is specified on a simd declarative directive, all list items must be formal parameters of a function that will be invoked concurrently on each simd lane. If no modifier is specified or the val modifier is specified, the value of each list item on each lane corresponds to the value of the list item upon entry to the function plus the logical number of the lane times step.
Possible values:
  • list: the variables, separated by commas, to pass as linear.
  • val(list): equivalent to passing the variables directly; it declares the list item(s) passed as linear.
step [Optional]
The value that the linear variables are increased by at every iteration. If the step is not given, its value is 1.

Example

Copy

Feedback

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

/**
 * @brief Illustrates the OpenMP linear policy.
 * @details This example shows that when a linear variable is passed to a
 * parallelised for loop, the value of that variable is the original value plus 
 * the iteration logical number times the linear-step. After the OpenMP parallel
 * for, the value of the original variable is that of the linear variable at the
 * last iteration.
 **/
int main(int argc, char* argv[])
{
    // Use 4 OpenMP threads
    omp_set_num_threads(4);

    // Variable that will be private
    int val = 1;

    printf("Value of \"val\" before the OpenMP parallel for is %d.\n", val);

    #pragma omp parallel for linear(val:2)
    for(int i = 0; i < 10; i++)
    {
        printf("Thread %d sees \"val\" = %d at iteration %d.\n", omp_get_thread_num(), val, i);
    }

    printf("Value of \"val\" after the OpenMP parallel for is %d.\n", val);

    return EXIT_SUCCESS;
}