You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

431 lines
12KB

  1. // -*- C++ -*-
  2. // Copyright (C) 2007-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the terms
  6. // of the GNU General Public License as published by the Free Software
  7. // Foundation; either version 3, or (at your option) any later
  8. // version.
  9. // This library is distributed in the hope that it will be useful, but
  10. // WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file parallel/base.h
  21. * @brief Sequential helper functions.
  22. * This file is a GNU parallel extension to the Standard C++ Library.
  23. */
  24. // Written by Johannes Singler.
  25. #ifndef _GLIBCXX_PARALLEL_BASE_H
  26. #define _GLIBCXX_PARALLEL_BASE_H 1
  27. #include <bits/c++config.h>
  28. #include <bits/stl_function.h>
  29. #include <omp.h>
  30. #include <parallel/features.h>
  31. #include <parallel/basic_iterator.h>
  32. #include <parallel/parallel.h>
  33. // Parallel mode namespaces.
  34. /**
  35. * @namespace std::__parallel
  36. * @brief GNU parallel code, replaces standard behavior with parallel behavior.
  37. */
  38. namespace std _GLIBCXX_VISIBILITY(default)
  39. {
  40. namespace __parallel { }
  41. }
  42. /**
  43. * @namespace __gnu_parallel
  44. * @brief GNU parallel code for public use.
  45. */
  46. namespace __gnu_parallel
  47. {
  48. // Import all the parallel versions of components in namespace std.
  49. using namespace std::__parallel;
  50. }
  51. /**
  52. * @namespace __gnu_sequential
  53. * @brief GNU sequential classes for public use.
  54. */
  55. namespace __gnu_sequential
  56. {
  57. // Import whatever is the serial version.
  58. #ifdef _GLIBCXX_PARALLEL
  59. using namespace std::_GLIBCXX_STD_A;
  60. #else
  61. using namespace std;
  62. #endif
  63. }
  64. namespace __gnu_parallel
  65. {
  66. // NB: Including this file cannot produce (unresolved) symbols from
  67. // the OpenMP runtime unless the parallel mode is actually invoked
  68. // and active, which imples that the OpenMP runtime is actually
  69. // going to be linked in.
  70. inline _ThreadIndex
  71. __get_max_threads()
  72. {
  73. _ThreadIndex __i = omp_get_max_threads();
  74. return __i > 1 ? __i : 1;
  75. }
  76. inline bool
  77. __is_parallel(const _Parallelism __p) { return __p != sequential; }
  78. /** @brief Calculates the rounded-down logarithm of @c __n for base 2.
  79. * @param __n Argument.
  80. * @return Returns 0 for any argument <1.
  81. */
  82. template<typename _Size>
  83. inline _Size
  84. __rd_log2(_Size __n)
  85. {
  86. _Size __k;
  87. for (__k = 0; __n > 1; __n >>= 1)
  88. ++__k;
  89. return __k;
  90. }
  91. /** @brief Encode two integers into one gnu_parallel::_CASable.
  92. * @param __a First integer, to be encoded in the most-significant @c
  93. * _CASable_bits/2 bits.
  94. * @param __b Second integer, to be encoded in the least-significant
  95. * @c _CASable_bits/2 bits.
  96. * @return value encoding @c __a and @c __b.
  97. * @see __decode2
  98. */
  99. inline _CASable
  100. __encode2(int __a, int __b) //must all be non-negative, actually
  101. {
  102. return (((_CASable)__a) << (_CASable_bits / 2)) | (((_CASable)__b) << 0);
  103. }
  104. /** @brief Decode two integers from one gnu_parallel::_CASable.
  105. * @param __x __gnu_parallel::_CASable to decode integers from.
  106. * @param __a First integer, to be decoded from the most-significant
  107. * @c _CASable_bits/2 bits of @c __x.
  108. * @param __b Second integer, to be encoded in the least-significant
  109. * @c _CASable_bits/2 bits of @c __x.
  110. * @see __encode2
  111. */
  112. inline void
  113. __decode2(_CASable __x, int& __a, int& __b)
  114. {
  115. __a = (int)((__x >> (_CASable_bits / 2)) & _CASable_mask);
  116. __b = (int)((__x >> 0 ) & _CASable_mask);
  117. }
  118. //needed for parallel "numeric", even if "algorithm" not included
  119. /** @brief Equivalent to std::min. */
  120. template<typename _Tp>
  121. inline const _Tp&
  122. min(const _Tp& __a, const _Tp& __b)
  123. { return (__a < __b) ? __a : __b; }
  124. /** @brief Equivalent to std::max. */
  125. template<typename _Tp>
  126. inline const _Tp&
  127. max(const _Tp& __a, const _Tp& __b)
  128. { return (__a > __b) ? __a : __b; }
  129. /** @brief Constructs predicate for equality from strict weak
  130. * ordering predicate
  131. */
  132. template<typename _T1, typename _T2, typename _Compare>
  133. class _EqualFromLess : public std::binary_function<_T1, _T2, bool>
  134. {
  135. private:
  136. _Compare& _M_comp;
  137. public:
  138. _EqualFromLess(_Compare& __comp) : _M_comp(__comp) { }
  139. bool operator()(const _T1& __a, const _T2& __b)
  140. { return !_M_comp(__a, __b) && !_M_comp(__b, __a); }
  141. };
  142. /** @brief Similar to std::unary_negate,
  143. * but giving the argument types explicitly. */
  144. template<typename _Predicate, typename argument_type>
  145. class __unary_negate
  146. : public std::unary_function<argument_type, bool>
  147. {
  148. protected:
  149. _Predicate _M_pred;
  150. public:
  151. explicit
  152. __unary_negate(const _Predicate& __x) : _M_pred(__x) { }
  153. bool
  154. operator()(const argument_type& __x)
  155. { return !_M_pred(__x); }
  156. };
  157. /** @brief Similar to std::binder1st,
  158. * but giving the argument types explicitly. */
  159. template<typename _Operation, typename _FirstArgumentType,
  160. typename _SecondArgumentType, typename _ResultType>
  161. class __binder1st
  162. : public std::unary_function<_SecondArgumentType, _ResultType>
  163. {
  164. protected:
  165. _Operation _M_op;
  166. _FirstArgumentType _M_value;
  167. public:
  168. __binder1st(const _Operation& __x, const _FirstArgumentType& __y)
  169. : _M_op(__x), _M_value(__y) { }
  170. _ResultType
  171. operator()(const _SecondArgumentType& __x)
  172. { return _M_op(_M_value, __x); }
  173. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  174. // 109. Missing binders for non-const sequence elements
  175. _ResultType
  176. operator()(_SecondArgumentType& __x) const
  177. { return _M_op(_M_value, __x); }
  178. };
  179. /**
  180. * @brief Similar to std::binder2nd, but giving the argument types
  181. * explicitly.
  182. */
  183. template<typename _Operation, typename _FirstArgumentType,
  184. typename _SecondArgumentType, typename _ResultType>
  185. class __binder2nd
  186. : public std::unary_function<_FirstArgumentType, _ResultType>
  187. {
  188. protected:
  189. _Operation _M_op;
  190. _SecondArgumentType _M_value;
  191. public:
  192. __binder2nd(const _Operation& __x, const _SecondArgumentType& __y)
  193. : _M_op(__x), _M_value(__y) { }
  194. _ResultType
  195. operator()(const _FirstArgumentType& __x) const
  196. { return _M_op(__x, _M_value); }
  197. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  198. // 109. Missing binders for non-const sequence elements
  199. _ResultType
  200. operator()(_FirstArgumentType& __x)
  201. { return _M_op(__x, _M_value); }
  202. };
  203. /** @brief Similar to std::equal_to, but allows two different types. */
  204. template<typename _T1, typename _T2>
  205. struct _EqualTo : std::binary_function<_T1, _T2, bool>
  206. {
  207. bool operator()(const _T1& __t1, const _T2& __t2) const
  208. { return __t1 == __t2; }
  209. };
  210. /** @brief Similar to std::less, but allows two different types. */
  211. template<typename _T1, typename _T2>
  212. struct _Less : std::binary_function<_T1, _T2, bool>
  213. {
  214. bool
  215. operator()(const _T1& __t1, const _T2& __t2) const
  216. { return __t1 < __t2; }
  217. bool
  218. operator()(const _T2& __t2, const _T1& __t1) const
  219. { return __t2 < __t1; }
  220. };
  221. // Partial specialization for one type. Same as std::less.
  222. template<typename _Tp>
  223. struct _Less<_Tp, _Tp>
  224. : public std::less<_Tp> { };
  225. /** @brief Similar to std::plus, but allows two different types. */
  226. template<typename _Tp1, typename _Tp2, typename _Result
  227. = __typeof__(*static_cast<_Tp1*>(0)
  228. + *static_cast<_Tp2*>(0))>
  229. struct _Plus : public std::binary_function<_Tp1, _Tp2, _Result>
  230. {
  231. _Result
  232. operator()(const _Tp1& __x, const _Tp2& __y) const
  233. { return __x + __y; }
  234. };
  235. // Partial specialization for one type. Same as std::plus.
  236. template<typename _Tp>
  237. struct _Plus<_Tp, _Tp, _Tp>
  238. : public std::plus<_Tp> { };
  239. /** @brief Similar to std::multiplies, but allows two different types. */
  240. template<typename _Tp1, typename _Tp2, typename _Result
  241. = __typeof__(*static_cast<_Tp1*>(0)
  242. * *static_cast<_Tp2*>(0))>
  243. struct _Multiplies : public std::binary_function<_Tp1, _Tp2, _Result>
  244. {
  245. _Result
  246. operator()(const _Tp1& __x, const _Tp2& __y) const
  247. { return __x * __y; }
  248. };
  249. // Partial specialization for one type. Same as std::multiplies.
  250. template<typename _Tp>
  251. struct _Multiplies<_Tp, _Tp, _Tp>
  252. : public std::multiplies<_Tp> { };
  253. /** @brief _Iterator associated with __gnu_parallel::_PseudoSequence.
  254. * If features the usual random-access iterator functionality.
  255. * @param _Tp Sequence _M_value type.
  256. * @param _DifferenceTp Sequence difference type.
  257. */
  258. template<typename _Tp, typename _DifferenceTp>
  259. class _PseudoSequenceIterator
  260. {
  261. public:
  262. typedef _DifferenceTp _DifferenceType;
  263. _PseudoSequenceIterator(const _Tp& __val, _DifferenceType __pos)
  264. : _M_val(__val), _M_pos(__pos) { }
  265. // Pre-increment operator.
  266. _PseudoSequenceIterator&
  267. operator++()
  268. {
  269. ++_M_pos;
  270. return *this;
  271. }
  272. // Post-increment operator.
  273. _PseudoSequenceIterator
  274. operator++(int)
  275. { return _PseudoSequenceIterator(_M_pos++); }
  276. const _Tp&
  277. operator*() const
  278. { return _M_val; }
  279. const _Tp&
  280. operator[](_DifferenceType) const
  281. { return _M_val; }
  282. bool
  283. operator==(const _PseudoSequenceIterator& __i2)
  284. { return _M_pos == __i2._M_pos; }
  285. bool
  286. operator!=(const _PseudoSequenceIterator& __i2)
  287. { return _M_pos != __i2._M_pos; }
  288. _DifferenceType
  289. operator-(const _PseudoSequenceIterator& __i2)
  290. { return _M_pos - __i2._M_pos; }
  291. private:
  292. const _Tp& _M_val;
  293. _DifferenceType _M_pos;
  294. };
  295. /** @brief Sequence that conceptually consists of multiple copies of
  296. the same element.
  297. * The copies are not stored explicitly, of course.
  298. * @param _Tp Sequence _M_value type.
  299. * @param _DifferenceTp Sequence difference type.
  300. */
  301. template<typename _Tp, typename _DifferenceTp>
  302. class _PseudoSequence
  303. {
  304. public:
  305. typedef _DifferenceTp _DifferenceType;
  306. // Better cast down to uint64_t, than up to _DifferenceTp.
  307. typedef _PseudoSequenceIterator<_Tp, uint64_t> iterator;
  308. /** @brief Constructor.
  309. * @param __val Element of the sequence.
  310. * @param __count Number of (virtual) copies.
  311. */
  312. _PseudoSequence(const _Tp& __val, _DifferenceType __count)
  313. : _M_val(__val), _M_count(__count) { }
  314. /** @brief Begin iterator. */
  315. iterator
  316. begin() const
  317. { return iterator(_M_val, 0); }
  318. /** @brief End iterator. */
  319. iterator
  320. end() const
  321. { return iterator(_M_val, _M_count); }
  322. private:
  323. const _Tp& _M_val;
  324. _DifferenceType _M_count;
  325. };
  326. /** @brief Compute the median of three referenced elements,
  327. according to @c __comp.
  328. * @param __a First iterator.
  329. * @param __b Second iterator.
  330. * @param __c Third iterator.
  331. * @param __comp Comparator.
  332. */
  333. template<typename _RAIter, typename _Compare>
  334. _RAIter
  335. __median_of_three_iterators(_RAIter __a, _RAIter __b,
  336. _RAIter __c, _Compare __comp)
  337. {
  338. if (__comp(*__a, *__b))
  339. if (__comp(*__b, *__c))
  340. return __b;
  341. else
  342. if (__comp(*__a, *__c))
  343. return __c;
  344. else
  345. return __a;
  346. else
  347. {
  348. // Just swap __a and __b.
  349. if (__comp(*__a, *__c))
  350. return __a;
  351. else
  352. if (__comp(*__b, *__c))
  353. return __c;
  354. else
  355. return __b;
  356. }
  357. }
  358. #if _GLIBCXX_PARALLEL_ASSERTIONS && defined(__glibcxx_assert_impl)
  359. #define _GLIBCXX_PARALLEL_ASSERT(_Condition) __glibcxx_assert_impl(_Condition)
  360. #else
  361. #define _GLIBCXX_PARALLEL_ASSERT(_Condition)
  362. #endif
  363. } //namespace __gnu_parallel
  364. #endif /* _GLIBCXX_PARALLEL_BASE_H */