| 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 |
#include <iostream> |
| 19 |
#include "atlas/runtime/Log.h" |
| 20 |
|
| 21 |
namespace atlas { |
| 22 |
namespace omp { |
| 23 |
|
| 24 |
template <class InputIterator, class OutputIterator> |
| 25 |
OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result) { |
| 26 |
if (atlas_omp_get_max_threads() > 1) { |
| 27 |
auto size = std::distance(first, last); |
| 28 |
atlas_omp_parallel { |
| 29 |
auto nthreads = atlas_omp_get_num_threads(); |
| 30 |
auto tid = atlas_omp_get_thread_num(); |
| 31 |
auto chunksize = size / nthreads; |
| 32 |
auto v_begin = first + chunksize * tid; |
| 33 |
auto v_end = (tid == nthreads - 1) ? last : v_begin + chunksize; |
| 34 |
auto result_begin = result + chunksize * tid; |
| 35 |
std::copy(v_begin, v_end, result_begin); |
| 36 |
} |
| 37 |
return result + size; |
| 38 |
} |
| 39 |
else { |
| 40 |
return std::copy(first, last, result); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
} // namespace omp |
| 45 |
} // namespace atlas |