TRIQS/itertools 1.3.0
C++ range library
Loading...
Searching...
No Matches
omp_chunk.hpp
Go to the documentation of this file.
1// Copyright (c) 2019-2024 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Thomas Hahn, Olivier Parcollet, Nils Wentzell
16
22#pragma once
23
24#include "./itertools.hpp"
25
26#include <omp.h>
27
28namespace itertools {
29
40 template <typename R> auto omp_chunk(R &&rg) {
41 auto total_size = itertools::distance(std::cbegin(rg), std::cend(rg));
42 auto [start_idx, end_idx] = chunk_range(0, total_size, omp_get_num_threads(), omp_get_thread_num());
43 return itertools::slice(std::forward<R>(rg), start_idx, end_idx);
44 }
45
46} // namespace itertools
sliced< R > slice(R &&rg, std::ptrdiff_t start_idx, std::ptrdiff_t end_idx)
Lazy-slice a given range.
Definition slice.hpp:137
std::pair< std::ptrdiff_t, std::ptrdiff_t > chunk_range(std::ptrdiff_t first, std::ptrdiff_t last, long n_chunks, long rank)
Given an integer range [first, last), divide it as equally as possible into N chunks.
Definition utils.hpp:92
std::iterator_traits< Iter1 >::difference_type distance(Iter1 first, Iter2 last)
Calculate the distance between two iterators.
Definition utils.hpp:51
auto omp_chunk(R &&rg)
Distribute a range as evenly as possible across all OMP threads.
Definition omp_chunk.hpp:40
Provides a small subset of the ranges and views from std::ranges.