Blender V5.0
parallel_for.h
Go to the documentation of this file.
1// Copyright (c) 2025 libmv authors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to
5// deal in the Software without restriction, including without limitation the
6// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7// sell copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19// IN THE SOFTWARE.
20
21// TBB-compatible implementation of parallel_for() functions.
22//
23// The parallel_for() function applies given functor for every index within the
24// given range. The range might be provided in multiple ways.
25//
26// Supports multiple underlying threading implementations (in the order of
27// preference):
28// - TBB, requires LIBMV_USE_TBB_THREADS to be defined.
29// - OpenMP, requires compiler to use -fopenmp, used when _OPENMP is defined.
30// - Single-threaded fall-back.
31//
32// The function occupies all threads of the default theading pool.
33
34#ifndef LIBMV_THREADING_PARALLEL_FOR_H_
35#define LIBMV_THREADING_PARALLEL_FOR_H_
36
37#if defined(LIBMV_USE_TBB_THREADS)
38# include <tbb/parallel_for.h>
39#endif
40
41namespace libmv {
42
43// Run the function f() for all indices within [first, last).
44template <typename Index, typename Function>
45void parallel_for(const Index first, const Index last, const Function& f) {
46#if defined(LIBMV_USE_TBB_THREADS)
47 tbb::parallel_for(first, last, f);
48#else
49# if defined(_OPENMP)
50# pragma omp parallel for schedule(static)
51# endif
52 for (Index i = first; i < last; ++i) {
53 f(i);
54 }
55#endif
56}
57
58} // namespace libmv
59
60#endif // LIBMV_THREADING_PARALLEL_FOR_H_
void parallel_for(const Index first, const Index last, const Function &f)
i
Definition text_draw.cc:230