Debian Codegraph @

src/atlas/parallel/omp/fill.h

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #pragma once
12
13 #include <algorithm>
14 #include <iterator>
15
16 #include "atlas/parallel/omp/omp.h"
17
18 namespace atlas {
19 namespace omp {
20
21 template <typename BidirIt, typename T>
22 void fill(BidirIt begin, BidirIt end, const T& value) {
23 if (atlas_omp_get_max_threads() > 1) {
24 auto size = std::distance(begin, end);
25 atlas_omp_parallel {
26 auto nthreads = atlas_omp_get_num_threads();
27 auto tid = atlas_omp_get_thread_num();
28 auto chunksize = size / nthreads;
29 auto v_begin = begin + chunksize * tid;
30 auto v_end = (tid == nthreads - 1) ? end : v_begin + chunksize;
31 std::fill(v_begin, v_end, value);
32 }
33 }
34 else {
35 std::fill(begin, end, value);
36 }
37 }
38
39 } // namespace omp
40 } // namespace atlas