barrier.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  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. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #pragma once
  17. #include "intrinsics.h"
  18. #include "sysinfo.h"
  19. #include "atomic.h"
  20. namespace embree
  21. {
  22. /*! system barrier using operating system */
  23. class BarrierSys
  24. {
  25. public:
  26. /*! construction / destruction */
  27. BarrierSys (size_t N = 0);
  28. ~BarrierSys ();
  29. private:
  30. /*! class in non-copyable */
  31. BarrierSys (const BarrierSys& other) DELETED; // do not implement
  32. BarrierSys& operator= (const BarrierSys& other) DELETED; // do not implement
  33. public:
  34. /*! intializes the barrier with some number of threads */
  35. void init(size_t count);
  36. /*! lets calling thread wait in barrier */
  37. void wait();
  38. private:
  39. void* opaque;
  40. };
  41. /*! fast active barrier using atomitc counter */
  42. struct BarrierActive
  43. {
  44. public:
  45. BarrierActive ()
  46. : cntr(0) {}
  47. void reset() {
  48. cntr.store(0);
  49. }
  50. void wait (size_t numThreads)
  51. {
  52. cntr++;
  53. while (cntr.load() != numThreads)
  54. __pause_cpu();
  55. }
  56. private:
  57. std::atomic<size_t> cntr;
  58. };
  59. /*! fast active barrier that does not require initialization to some number of threads */
  60. struct BarrierActiveAutoReset
  61. {
  62. public:
  63. BarrierActiveAutoReset ()
  64. : cntr0(0), cntr1(0) {}
  65. void wait (size_t threadCount)
  66. {
  67. cntr0.fetch_add(1);
  68. while (cntr0 != threadCount) __pause_cpu();
  69. cntr1.fetch_add(1);
  70. while (cntr1 != threadCount) __pause_cpu();
  71. cntr0.fetch_add(-1);
  72. while (cntr0 != 0) __pause_cpu();
  73. cntr1.fetch_add(-1);
  74. while (cntr1 != 0) __pause_cpu();
  75. }
  76. private:
  77. std::atomic<size_t> cntr0;
  78. std::atomic<size_t> cntr1;
  79. };
  80. class LinearBarrierActive
  81. {
  82. public:
  83. /*! construction and destruction */
  84. LinearBarrierActive (size_t threadCount = 0);
  85. ~LinearBarrierActive();
  86. private:
  87. /*! class in non-copyable */
  88. LinearBarrierActive (const LinearBarrierActive& other) DELETED; // do not implement
  89. LinearBarrierActive& operator= (const LinearBarrierActive& other) DELETED; // do not implement
  90. public:
  91. /*! intializes the barrier with some number of threads */
  92. void init(size_t threadCount);
  93. /*! thread with threadIndex waits in the barrier */
  94. void wait (const size_t threadIndex);
  95. private:
  96. volatile unsigned char* count0;
  97. volatile unsigned char* count1;
  98. volatile unsigned int mode;
  99. volatile unsigned int flag0;
  100. volatile unsigned int flag1;
  101. volatile size_t threadCount;
  102. };
  103. }