Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
parallel_rows.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**
19 * @file parallel_rows.h
20 * @brief Run independent row work with a bounded native thread team.
21 * @details Nested callers fall back to serial execution so inner helpers do not
22 * multiply worker counts and oversubscribe a small processor. Exceptions are
23 * captured on workers and rethrown only after every thread has joined.
24 */
25#ifndef PANGEA_PARALLEL_ROWS_H
26#define PANGEA_PARALLEL_ROWS_H
27
28#include <algorithm>
29#include <atomic>
30#include <exception>
31#include <mutex>
32#include <thread>
33#include <vector>
34
35namespace pangea_survey {
36inline thread_local unsigned parallelRowsDepth = 0;
37
38/**
39 * @brief Apply a row operation in parallel without creating nested teams.
40 * @tparam Function Callable accepting one zero-based row index.
41 * @param[in] rows Number of rows to process; nonpositive values are a no-op.
42 * @param[in] function Independent work for each row.
43 * @param[in] requested Upper bound on workers; hardware availability and row count
44 * further reduce the team.
45 * @throws Any exception thrown by @p function, rethrown after all workers stop.
46 */
47template<class Function>
48void parallelRows(int rows, Function function, unsigned requested = 4) {
49 if (rows <= 0) return;
50 struct DepthScope {
51 DepthScope() { ++parallelRowsDepth; }
52 ~DepthScope() { --parallelRowsDepth; }
53 };
54 const unsigned count = (std::max)(1u, (std::min)(requested, (std::min)(std::thread::hardware_concurrency(),
55 static_cast<unsigned>(rows))));
56 if (count == 1 || parallelRowsDepth != 0) {
57 const DepthScope depth;
58 for (int row = 0; row < rows; ++row) function(row);
59 return;
60 }
61 std::atomic<bool> stop{false};
62 std::atomic<int> next{0};
63 std::mutex mutex;
64 std::exception_ptr error;
65 const auto run = [&] {
66 const DepthScope depth;
67 while (!stop.load(std::memory_order_relaxed)) {
68 const int row = next.fetch_add(1, std::memory_order_relaxed);
69 if (row >= rows) break;
70 try {
71 function(row);
72 } catch (...) {
73 std::lock_guard<std::mutex> lock(mutex);
74 if (!error) error = std::current_exception();
75 stop.store(true, std::memory_order_relaxed);
76 }
77 }
78 };
79 std::vector<std::thread> workers;
80 bool launch_failed = false;
81 try {
82 workers.reserve(count - 1);
83 for (unsigned worker = 1; worker < count; ++worker) workers.emplace_back(run);
84 } catch (...) {
85 launch_failed = true;
86 stop.store(true, std::memory_order_relaxed);
87 }
88 if (!launch_failed) run();
89 for (auto& worker : workers) if (worker.joinable()) worker.join();
90 if (launch_failed && !error) {
91 stop.store(false, std::memory_order_relaxed);
92 run();
93 }
94 if (error) std::rethrow_exception(error);
95}
96}
97#endif
static bool stop(uint32_t session)
End only the matching session, even if its stop packet cannot be sent.
uint8_t row[LWIR_MAX_WIDTH *3U]
Definition lwir_image.c:63
struct jpeg_error_mgr error
Definition lwir_image.c:60
void parallelRows(int rows, Function function, unsigned requested=4)
Apply a row operation in parallel without creating nested teams.
unsigned parallelRowsDepth
static pthread_mutex_t mutex
Definition pose_log.c:72
static size_t count
Definition pose_log.c:79