alpaka
Abstraction Library for Parallel Kernel Acceleration
OmpSchedule.hpp
Go to the documentation of this file.
1 /* Copyright 2022 Sergei Bastrakov, Bernhard Manfred Gruber
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
7 #include "alpaka/core/Common.hpp"
8 
9 #ifdef _OPENMP
10 # include <omp.h>
11 #endif
12 
13 #include <cstdint>
14 
15 namespace alpaka::omp
16 {
17  //! Representation of OpenMP schedule information: kind and chunk size. This class can be used regardless of
18  //! whether OpenMP is enabled.
19  struct Schedule
20  {
21  //! Schedule kinds corresponding to arguments of OpenMP schedule clause
22  //!
23  //! Kinds also present in omp_sched_t enum have the same integer values.
24  //! It is enum, not enum class, for shorter usage as omp::Schedule::[kind] and to keep interface of 0.6.0.
25  enum Kind
26  {
27  // Corresponds to not setting schedule
29  Static = 1u,
30  Dynamic = 2u,
31  Guided = 3u,
32  // Auto supported since OpenMP 3.0
33 #if defined _OPENMP && _OPENMP >= 200805
34  Auto = 4u,
35 #endif
36  Runtime = 5u
37  };
38 
39  //! Schedule kind.
41 
42  //! Chunk size. Same as in OpenMP, value 0 corresponds to default chunk size. Using int and not a
43  //! fixed-width type to match OpenMP API.
44  int chunkSize;
45 
46  //! Create a schedule with the given kind and chunk size
47  ALPAKA_FN_HOST constexpr Schedule(Kind myKind = NoSchedule, int myChunkSize = 0)
48  : kind(myKind)
49  , chunkSize(myChunkSize)
50  {
51  }
52  };
53 
54  //! Get the OpenMP schedule that is applied when the runtime schedule is used.
55  //!
56  //! For OpenMP >= 3.0 returns the value of the internal control variable run-sched-var.
57  //! Without OpenMP or with OpenMP < 3.0, returns the default schedule.
58  //!
59  //! \return Schedule object.
61  {
62  // Getting a runtime schedule requires OpenMP 3.0 or newer
63 #if defined _OPENMP && _OPENMP >= 200805
64  omp_sched_t ompKind;
65  int chunkSize = 0;
66  omp_get_schedule(&ompKind, &chunkSize);
67  return Schedule{static_cast<Schedule::Kind>(ompKind), chunkSize};
68 #else
69  return Schedule{};
70 #endif
71  }
72 
73  //! Set the OpenMP schedule that is applied when the runtime schedule is used for future parallel regions.
74  //!
75  //! For OpenMP >= 3.0 sets the value of the internal control variable run-sched-var according to the given
76  //! schedule. Without OpenMP or with OpenMP < 3.0, does nothing.
77  //!
78  //! Note that calling from inside a parallel region does not have an immediate effect.
79  ALPAKA_FN_HOST inline void setSchedule(Schedule schedule)
80  {
81  if((schedule.kind != Schedule::NoSchedule) && (schedule.kind != Schedule::Runtime))
82  {
83 #if defined _OPENMP && _OPENMP >= 200805
84  omp_set_schedule(static_cast<omp_sched_t>(schedule.kind), schedule.chunkSize);
85 #endif
86  }
87  }
88 } // namespace alpaka::omp
#define ALPAKA_FN_HOST
Definition: Common.hpp:40
ALPAKA_FN_HOST void setSchedule(Schedule schedule)
Set the OpenMP schedule that is applied when the runtime schedule is used for future parallel regions...
Definition: OmpSchedule.hpp:79
ALPAKA_FN_HOST auto getSchedule()
Get the OpenMP schedule that is applied when the runtime schedule is used.
Definition: OmpSchedule.hpp:60
Representation of OpenMP schedule information: kind and chunk size. This class can be used regardless...
Definition: OmpSchedule.hpp:20
int chunkSize
Chunk size. Same as in OpenMP, value 0 corresponds to default chunk size. Using int and not a fixed-w...
Definition: OmpSchedule.hpp:44
Kind
Schedule kinds corresponding to arguments of OpenMP schedule clause.
Definition: OmpSchedule.hpp:26
Kind kind
Schedule kind.
Definition: OmpSchedule.hpp:40
constexpr ALPAKA_FN_HOST Schedule(Kind myKind=NoSchedule, int myChunkSize=0)
Create a schedule with the given kind and chunk size.
Definition: OmpSchedule.hpp:47