xref: /dflybsd-src/contrib/gcc-4.7/libstdc++-v3/include/parallel/equally_split.h (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino // -*- C++ -*-
2*e4b17023SJohn Marino 
3*e4b17023SJohn Marino // Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4*e4b17023SJohn Marino //
5*e4b17023SJohn Marino // This file is part of the GNU ISO C++ Library.  This library is free
6*e4b17023SJohn Marino // software; you can redistribute it and/or modify it under the terms
7*e4b17023SJohn Marino // of the GNU General Public License as published by the Free Software
8*e4b17023SJohn Marino // Foundation; either version 3, or (at your option) any later
9*e4b17023SJohn Marino // version.
10*e4b17023SJohn Marino 
11*e4b17023SJohn Marino // This library is distributed in the hope that it will be useful, but
12*e4b17023SJohn Marino // WITHOUT ANY WARRANTY; without even the implied warranty of
13*e4b17023SJohn Marino // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14*e4b17023SJohn Marino // General Public License for more details.
15*e4b17023SJohn Marino 
16*e4b17023SJohn Marino // Under Section 7 of GPL version 3, you are granted additional
17*e4b17023SJohn Marino // permissions described in the GCC Runtime Library Exception, version
18*e4b17023SJohn Marino // 3.1, as published by the Free Software Foundation.
19*e4b17023SJohn Marino 
20*e4b17023SJohn Marino // You should have received a copy of the GNU General Public License and
21*e4b17023SJohn Marino // a copy of the GCC Runtime Library Exception along with this program;
22*e4b17023SJohn Marino // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*e4b17023SJohn Marino // <http://www.gnu.org/licenses/>.
24*e4b17023SJohn Marino 
25*e4b17023SJohn Marino /** @file parallel/equally_split.h
26*e4b17023SJohn Marino  *  This file is a GNU parallel extension to the Standard C++ Library.
27*e4b17023SJohn Marino  */
28*e4b17023SJohn Marino 
29*e4b17023SJohn Marino // Written by Johannes Singler.
30*e4b17023SJohn Marino 
31*e4b17023SJohn Marino #ifndef _GLIBCXX_PARALLEL_EQUALLY_SPLIT_H
32*e4b17023SJohn Marino #define _GLIBCXX_PARALLEL_EQUALLY_SPLIT_H 1
33*e4b17023SJohn Marino 
34*e4b17023SJohn Marino namespace __gnu_parallel
35*e4b17023SJohn Marino {
36*e4b17023SJohn Marino   /** @brief function to split a sequence into parts of almost equal size.
37*e4b17023SJohn Marino    *
38*e4b17023SJohn Marino    *  The resulting sequence __s of length __num_threads+1 contains the
39*e4b17023SJohn Marino    *  splitting positions when splitting the range [0,__n) into parts of
40*e4b17023SJohn Marino    *  almost equal size (plus minus 1).  The first entry is 0, the last
41*e4b17023SJohn Marino    *  one n. There may result empty parts.
42*e4b17023SJohn Marino    *  @param __n Number of elements
43*e4b17023SJohn Marino    *  @param __num_threads Number of parts
44*e4b17023SJohn Marino    *  @param __s Splitters
45*e4b17023SJohn Marino    *  @returns End of __splitter sequence, i.e. @c __s+__num_threads+1 */
46*e4b17023SJohn Marino   template<typename _DifferenceType, typename _OutputIterator>
47*e4b17023SJohn Marino     _OutputIterator
__equally_split(_DifferenceType __n,_ThreadIndex __num_threads,_OutputIterator __s)48*e4b17023SJohn Marino     __equally_split(_DifferenceType __n, _ThreadIndex __num_threads,
49*e4b17023SJohn Marino 		    _OutputIterator __s)
50*e4b17023SJohn Marino     {
51*e4b17023SJohn Marino       _DifferenceType __chunk_length = __n / __num_threads;
52*e4b17023SJohn Marino       _DifferenceType __num_longer_chunks = __n % __num_threads;
53*e4b17023SJohn Marino       _DifferenceType __pos = 0;
54*e4b17023SJohn Marino       for (_ThreadIndex __i = 0; __i < __num_threads; ++__i)
55*e4b17023SJohn Marino 	{
56*e4b17023SJohn Marino 	  *__s++ = __pos;
57*e4b17023SJohn Marino 	  __pos += ((__i < __num_longer_chunks)
58*e4b17023SJohn Marino 		    ? (__chunk_length + 1) : __chunk_length);
59*e4b17023SJohn Marino 	}
60*e4b17023SJohn Marino       *__s++ = __n;
61*e4b17023SJohn Marino       return __s;
62*e4b17023SJohn Marino     }
63*e4b17023SJohn Marino 
64*e4b17023SJohn Marino   /** @brief function to split a sequence into parts of almost equal size.
65*e4b17023SJohn Marino    *
66*e4b17023SJohn Marino    *  Returns the position of the splitting point between
67*e4b17023SJohn Marino    *  thread number __thread_no (included) and
68*e4b17023SJohn Marino    *  thread number __thread_no+1 (excluded).
69*e4b17023SJohn Marino    *  @param __n Number of elements
70*e4b17023SJohn Marino    *  @param __num_threads Number of parts
71*e4b17023SJohn Marino    *  @param __thread_no Number of threads
72*e4b17023SJohn Marino    *  @returns splitting point */
73*e4b17023SJohn Marino   template<typename _DifferenceType>
74*e4b17023SJohn Marino     _DifferenceType
__equally_split_point(_DifferenceType __n,_ThreadIndex __num_threads,_ThreadIndex __thread_no)75*e4b17023SJohn Marino     __equally_split_point(_DifferenceType __n,
76*e4b17023SJohn Marino 			  _ThreadIndex __num_threads,
77*e4b17023SJohn Marino 			  _ThreadIndex __thread_no)
78*e4b17023SJohn Marino     {
79*e4b17023SJohn Marino       _DifferenceType __chunk_length = __n / __num_threads;
80*e4b17023SJohn Marino       _DifferenceType __num_longer_chunks = __n % __num_threads;
81*e4b17023SJohn Marino       if (__thread_no < __num_longer_chunks)
82*e4b17023SJohn Marino 	return __thread_no * (__chunk_length + 1);
83*e4b17023SJohn Marino       else
84*e4b17023SJohn Marino 	return __num_longer_chunks * (__chunk_length + 1)
85*e4b17023SJohn Marino           + (__thread_no - __num_longer_chunks) * __chunk_length;
86*e4b17023SJohn Marino     }
87*e4b17023SJohn Marino }
88*e4b17023SJohn Marino 
89*e4b17023SJohn Marino #endif /* _GLIBCXX_PARALLEL_EQUALLY_SPLIT_H */
90