ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Parallel.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include <Parallel.h>
9 
10 #ifdef _OPENMP
11 #include <omp.h>
12 #endif
13 
14 #include <CPUInfo.h>
15 #include <Logging.h>
16 
17 #include <cstdlib>
18 #include <string>
19 
20 namespace cloudViewer {
21 namespace utility {
22 
23 static std::string GetEnvVar(const std::string& name) {
24  if (const char* value = std::getenv(name.c_str())) {
25  return std::string(value);
26  } else {
27  return "";
28  }
29 }
30 
32 #ifdef _OPENMP
33  if (!GetEnvVar("OMP_NUM_THREADS").empty() ||
34  !GetEnvVar("OMP_DYNAMIC").empty()) {
35  // See the full list of OpenMP environment variables at:
36  // https://www.openmp.org/spec-html/5.0/openmpch6.html
37  return omp_get_max_threads();
38  } else {
39  // Returns the number of physical cores.
41  }
42 #else
43  (void)GetEnvVar; // Avoids compiler warning.
44  return 1;
45 #endif
46 }
47 
48 bool InParallel() {
49  // TODO: when we add TBB/Parallel STL support to ParallelFor, update this.
50 #ifdef _OPENMP
51  return omp_in_parallel();
52 #else
53  return false;
54 #endif
55 }
56 
57 } // namespace utility
58 } // namespace cloudViewer
std::string name
static CPUInfo & GetInstance()
Definition: CPUInfo.cpp:174
bool InParallel()
Returns true if in an parallel section.
Definition: Parallel.cpp:48
int EstimateMaxThreads()
Estimate the maximum number of threads to be used in a parallel region.
Definition: Parallel.cpp:31
static std::string GetEnvVar(const std::string &name)
Definition: Parallel.cpp:23
Generic file read and write utility for python interface.