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