Viskores  1.0
RuntimeDeviceConfigurationOpenMP.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_cont_openmp_internal_RuntimeDeviceConfigurationOpenMP_h
19 #define viskores_cont_openmp_internal_RuntimeDeviceConfigurationOpenMP_h
20 
23 
24 #include <viskores/cont/Logging.h>
25 
27 #include <omp.h>
29 
30 namespace viskores
31 {
32 namespace cont
33 {
34 namespace internal
35 {
36 
37 template <>
38 class RuntimeDeviceConfiguration<viskores::cont::DeviceAdapterTagOpenMP>
39  : public viskores::cont::internal::RuntimeDeviceConfigurationBase
40 {
41 public:
42  RuntimeDeviceConfiguration<viskores::cont::DeviceAdapterTagOpenMP>()
43  : HardwareMaxThreads(InitializeHardwareMaxThreads())
44  , CurrentNumThreads(this->HardwareMaxThreads)
45  {
46  }
47 
48  VISKORES_CONT viskores::cont::DeviceAdapterId GetDevice() const override final
49  {
51  }
52 
53  VISKORES_CONT virtual RuntimeDeviceConfigReturnCode SetThreads(
54  const viskores::Id& value) override final
55  {
56  if (omp_in_parallel())
57  {
59  "OpenMP SetThreads: Error, currently in parallel");
60  return RuntimeDeviceConfigReturnCode::NOT_APPLIED;
61  }
62  if (value > 0)
63  {
64  if (value > this->HardwareMaxThreads)
65  {
67  "OpenMP: You may be oversubscribing your CPU cores: "
68  << "process threads available: " << this->HardwareMaxThreads
69  << ", requested threads: " << value);
70  }
71  this->CurrentNumThreads = value;
72  omp_set_num_threads(this->CurrentNumThreads);
73  }
74  else
75  {
76  this->CurrentNumThreads = this->HardwareMaxThreads;
77  omp_set_num_threads(this->CurrentNumThreads);
78  }
79  return RuntimeDeviceConfigReturnCode::SUCCESS;
80  }
81 
82  VISKORES_CONT virtual RuntimeDeviceConfigReturnCode GetThreads(
83  viskores::Id& value) const override final
84  {
85  value = this->CurrentNumThreads;
86  return RuntimeDeviceConfigReturnCode::SUCCESS;
87  }
88 
89  VISKORES_CONT virtual RuntimeDeviceConfigReturnCode GetMaxThreads(
90  viskores::Id& value) const override final
91  {
92  value = this->HardwareMaxThreads;
93  return RuntimeDeviceConfigReturnCode::SUCCESS;
94  }
95 
96 private:
97  VISKORES_CONT viskores::Id InitializeHardwareMaxThreads() const
98  {
99  viskores::Id count = 0;
100 
101  if (omp_in_parallel())
102  {
103  count = omp_get_num_threads();
104  }
105  else
106  {
107  VISKORES_OPENMP_DIRECTIVE(parallel)
108  {
110  ++count;
111  }
112  }
113  return count;
114  }
115 
116  viskores::Id HardwareMaxThreads;
117  viskores::Id CurrentNumThreads;
118 };
119 } // namespace viskores::cont::internal
120 } // namespace viskores::cont
121 } // namespace viskores
122 
123 #endif //viskores_cont_openmp_internal_RuntimeDeviceConfigurationOpenMP_h
viskores::cont::LogLevel::Error
@ Error
Important but non-fatal errors, such as device fail-over.
viskores::cont::LogLevel::Warn
@ Warn
Less important user errors, such as out-of-bounds parameters.
VISKORES_THIRDPARTY_POST_INCLUDE
#define VISKORES_THIRDPARTY_POST_INCLUDE
Definition: Configure.h:200
VISKORES_OPENMP_DIRECTIVE
#define VISKORES_OPENMP_DIRECTIVE(directive)
Definition: FunctorsOpenMP.h:45
viskores::cont::DeviceAdapterTagOpenMP
Tag for a device adapter that uses OpenMP compiler extensions to run algorithms on multiple threads.
Definition: DeviceAdapterTagOpenMP.h:35
DeviceAdapterTagOpenMP.h
viskores::Id
viskores::Int64 Id
Base type to use to index arrays.
Definition: Types.h:235
VISKORES_CONT
#define VISKORES_CONT
Definition: ExportMacros.h:65
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
RuntimeDeviceConfiguration.h
viskores::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:66
VISKORES_LOG_S
#define VISKORES_LOG_S(level,...)
Writes a message using stream syntax to the indicated log level.
Definition: Logging.h:216
Logging.h
Logging utilities.
VISKORES_THIRDPARTY_PRE_INCLUDE
#define VISKORES_THIRDPARTY_PRE_INCLUDE
Definition: Configure.h:199