5.2. Task Priority#

In this example we compute arrays in a matrix through a compute_array routine. Each task has a priority value equal to the value of the loop variable i at the moment of its creation. A higher priority on a task means that a task is a candidate to run sooner.

The creation of tasks occurs in ascending order (according to the iteration space of the loop) but a hint, by means of the priority clause, is provided to reverse the execution order.

//%compiler: clang
//%cflags: -fopenmp

/*
* name:       task_priority.1
* type:       C
* version:    omp_4.5
*/
void compute_array (float *node, int M);

void compute_matrix (float *array, int N, int M)
{
   int i;
   #pragma omp parallel private(i)
   #pragma omp single
   {
      for (i=0;i<N; i++) {
         #pragma omp task priority(i)
         compute_array(&array[i*M], M);
      }
   }
}
!!%compiler: gfortran
!!%cflags: -fopenmp

! name: task_priority.1
! type: F-free
! version:    omp_4.5
subroutine compute_matrix(matrix, M, N)
   implicit none
   integer :: M, N
   real :: matrix(M, N)
   integer :: i
   interface
      subroutine compute_array(node, M)
      implicit none
      integer :: M
      real :: node(M)
      end subroutine
   end interface
   !$omp parallel private(i)
   !$omp single
   do i=1,N
      !$omp task priority(i)
      call compute_array(matrix(:, i), M)
      !$omp end task
   enddo
   !$omp end single
   !$omp end parallel
end subroutine compute_matrix