File: Backend.h

package info (click to toggle)
r-cran-rcppparallel 5.1.7%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544 kB
  • sloc: cpp: 1,852; sh: 19; makefile: 11
file content (84 lines) | stat: -rw-r--r-- 1,582 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

#ifndef __RCPP_PARALLEL_BACKEND__
#define __RCPP_PARALLEL_BACKEND__

#include <cstdlib>
#include <cstring>

extern "C" {
void REprintf(const char*, ...);
}

namespace RcppParallel {
namespace internal {

enum backend_type {
   BACKEND_TBB,
   BACKEND_TINYTHREAD
};

#if RCPP_PARALLEL_USE_TBB

inline backend_type defaultBackend()
{
   return BACKEND_TBB;
}

#else

inline backend_type defaultBackend()
{
   return BACKEND_TINYTHREAD;
}

#endif

inline const char* backendToString(backend_type backend)
{
   switch (backend)
   {
   case BACKEND_TBB:
      return "tbb";
   case BACKEND_TINYTHREAD:
      return "tinythread";
   }

   // shouldn't be reached but need to silence compiler warnings
   return "tbb";
}

inline backend_type backend()
{
   const char* requestedBackend = std::getenv("RCPP_PARALLEL_BACKEND");
   if (requestedBackend == NULL)
   {
      return defaultBackend();
   }
   else if (std::strcmp(requestedBackend, "tbb") == 0)
   {
#if RCPP_PARALLEL_USE_TBB
      return BACKEND_TBB;
#else
      const char* msg =
         "tbb backend is not available; using tinythread instead";
      
      REprintf("%s\n", msg);
      return BACKEND_TINYTHREAD;
#endif
   }
   else if (strcmp(requestedBackend, "tinythread") == 0)
   {
      return BACKEND_TINYTHREAD;
   }
   else
   {
      const char* fmt = "unknown parallel backend '%s'; using '%s' instead\n";
      REprintf(fmt, requestedBackend, backendToString(defaultBackend()));
      return defaultBackend();
   }
}

} // namespace internal
} // namespace RcppParallel

#endif /* __RCPP_PARALLEL_BACKEND__ */