mp_misc.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2008-2016 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. //! \addtogroup mp_misc
  16. //! @{
  17. template<typename eT, const bool use_smaller_thresh = false>
  18. struct mp_gate
  19. {
  20. arma_inline
  21. static
  22. bool
  23. eval(const uword n_elem)
  24. {
  25. #if defined(ARMA_USE_OPENMP)
  26. {
  27. const bool length_ok = (is_cx<eT>::yes || use_smaller_thresh) ? (n_elem >= (arma_config::mp_threshold/uword(2))) : (n_elem >= arma_config::mp_threshold);
  28. if(length_ok)
  29. {
  30. if(omp_in_parallel()) { return false; }
  31. }
  32. return length_ok;
  33. }
  34. #else
  35. {
  36. arma_ignore(n_elem);
  37. return false;
  38. }
  39. #endif
  40. }
  41. };
  42. struct mp_thread_limit
  43. {
  44. arma_inline
  45. static
  46. int
  47. get()
  48. {
  49. #if defined(ARMA_USE_OPENMP)
  50. int n_threads = (std::min)(int(arma_config::mp_threads), int((std::max)(int(1), int(omp_get_max_threads()))));
  51. #else
  52. int n_threads = int(1);
  53. #endif
  54. return n_threads;
  55. }
  56. arma_inline
  57. static
  58. bool
  59. in_parallel()
  60. {
  61. #if defined(ARMA_USE_OPENMP)
  62. {
  63. return bool(omp_in_parallel());
  64. }
  65. #else
  66. {
  67. return false;
  68. }
  69. #endif
  70. }
  71. };
  72. //! @}