3 // Copyright (C) 2007 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 /** @file parallel/omp_loop_static.h
32 * @brief Parallelization of embarrassingly parallel execution by
33 * means of an OpenMP for loop with static scheduling.
34 * This file is a GNU parallel extension to the Standard C++ Library.
37 // Written by Felix Putze.
39 #ifndef _GLIBCXX_PARALLEL_OMP_LOOP_STATIC_H
40 #define _GLIBCXX_PARALLEL_OMP_LOOP_STATIC_H 1
44 #include <parallel/settings.h>
45 #include <parallel/basic_iterator.h>
47 namespace __gnu_parallel
50 /** @brief Embarrassingly parallel algorithm for random access
51 * iterators, using an OpenMP for loop with static scheduling.
53 * @param begin Begin iterator of element sequence.
54 * @param end End iterator of element sequence.
55 * @param o User-supplied functor (comparator, predicate, adding
57 * @param f Functor to "process" an element with op (depends on
58 * desired functionality, e. g. for std::for_each(), ...).
59 * @param r Functor to "add" a single result to the already processed
60 * elements (depends on functionality).
61 * @param base Base value for reduction.
62 * @param output Pointer to position where final result is written to
63 * @param bound Maximum number of elements processed (e. g. for
65 * @return User-supplied functor (that may contain a part of the result).
67 template<typename RandomAccessIterator
, typename Op
, typename Fu
, typename Red
, typename Result
>
69 for_each_template_random_access_omp_loop_static(RandomAccessIterator begin
,
70 RandomAccessIterator end
,
72 Result base
, Result
& output
,
73 typename
std::iterator_traits
<RandomAccessIterator
>::difference_type bound
)
75 typedef std::iterator_traits
<RandomAccessIterator
> traits_type
;
76 typedef typename
traits_type::difference_type difference_type
;
78 thread_index_t num_threads
= (get_max_threads() < (end
- begin
)) ? get_max_threads() : (end
- begin
);
79 Result
*thread_results
= new Result
[num_threads
];
80 difference_type length
= end
- begin
;
82 for (thread_index_t i
= 0; i
< num_threads
; i
++)
84 thread_results
[i
] = r(thread_results
[i
], f(o
, begin
+i
));
87 #pragma omp parallel num_threads(num_threads)
89 #pragma omp for schedule(static, Settings::workstealing_chunk_size)
90 for (difference_type pos
= 0; pos
< length
; pos
++)
92 thread_results
[omp_get_thread_num()] = r(thread_results
[omp_get_thread_num()], f(o
, begin
+pos
));
96 for (thread_index_t i
= 0; i
< num_threads
; i
++)
98 output
= r(output
, thread_results
[i
]);
101 delete [] thread_results
;
103 // Points to last element processed (needed as return value for
104 // some algorithms like transform).
105 f
.finish_iterator
= begin
+ length
;