JeVois  1.22
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
ThreadPool.H
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2020 by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
18#pragma once
19
20#ifdef JEVOIS_PRO
21
22// This code modified from:
23
24// Copyright Mateusz Jaworski 2020 - 2020.
25// Distributed under the Boost Software License, Version 1.0.
26// (See accompanying file LICENSE.md or copy at
27// https://www.boost.org/LICENSE_1_0.txt)
28
29#include <future>
30#include <thread>
31#include <vector>
32#include <stdexcept>
33#include <type_traits>
34
35#include "function2/function2.hpp"
36#include "concurrentqueue/concurrentqueue.h"
37
38#include <sched.h>
39#include <opencv2/core/parallel/parallel_backend.hpp>
40
41namespace jevois
42{
43 //! A thread pool with CPU affinity
44 /*! Thread pool with extra settings to enforce that passed tasks run on some particular cores. Used by JeVois-Pro
45 Platform to enforce that machine vision tasks run on big cores (ARM A73) while non-critical tasks run on slower
46 little cores (ARM A53). \ingroup utils */
48 {
49 public:
50 //! Constructor
51 ThreadPool(unsigned int threads = std::thread::hardware_concurrency(), bool little = false);
52
53 //! Destructor
55
56 //! Execute a function and get a future
57 template<typename Func, typename... Args, std::enable_if_t<std::is_invocable_v<Func&&, Args&&...>, bool> = true>
58 auto execute(Func&& func, Args&&... args) -> std::future<decltype(func(args...))>;
59
60 //! Get the pool size
61 auto getPoolSize() -> size_t;
62
63 private:
64 ThreadPool& operator=(ThreadPool&) = delete;
65 ThreadPool(ThreadPool&) = delete;
66 moodycamel::ConcurrentQueue<fu2::unique_function<void()>> _tasks;
67 std::atomic<unsigned int> _size;
68 std::vector<std::thread> _pool;
69 std::condition_variable _new_task;
70 std::mutex _mtx;
71 bool _exit;
72 };
73
74//! A parallel API to make OpenCV use our thread pool
75/*! The goal is to avoid over-subscribing our cores by having several disjoint parallelization mechanisms: our JeVois
76 ThreadPool, and OpenCV's parallel_for backend (which may be TBB, OpenMP, etc). We can also enforce here that OpenCV
77 tasks will only run on big cores, while the default OpenCV behavior is to use all cores. This can lead to bad delays
78 if little A53 cores are chosen for tome tasks, as they are much slower than the big A73 cores. \ingroup utils */
79 class ParallelForAPIjevois : public cv::parallel::ParallelForAPI
80 {
81 public:
82 //! Constructor from an existing ThreadPool
84
85 //! Virtual destructor for save inheritance
86 virtual ~ParallelForAPIjevois();
87
88 //typedef void (CV_API_CALL *FN_parallel_for_body_cb_t)(int start, int end, void* data);
89
90 //! Run a parallelized OpenCV task
91 virtual void parallel_for(int tasks, cv::parallel::ParallelForAPI::FN_parallel_for_body_cb_t body_callback,
92 void * callback_data) override;
93
94 //! Get some index for the current thread
95 virtual int getThreadNum() const override;
96
97 //! Get number of threads for concurrency (always 4 on JeVois-Pro, unless changed by setNumThreads() here)
98 virtual int getNumThreads() const override;
99
100 //! Set number of threads for OpenCV. NOTE: currently does nothing, we always use 4 big A73 threads
101 virtual int setNumThreads(int nThreads) override;
102
103 //! Get the name: 'jevois'
104 virtual char const * getName() const override;
105
106 protected:
109};
110
111
112}
113
114// Include implementation details
115#include <jevois/Util/details/ThreadPoolImpl.H>
116
117#endif // JEVOIS_PRO
A parallel API to make OpenCV use our thread pool.
Definition ThreadPool.H:80
A thread pool with CPU affinity.
Definition ThreadPool.H:48
auto getPoolSize() -> size_t
Get the pool size.
Definition ThreadPool.C:118
auto execute(Func &&func, Args &&... args) -> std::future< decltype(func(args...))>
Execute a function and get a future.
~ThreadPool()
Destructor.
Definition ThreadPool.C:106
Main namespace for all JeVois classes and functions.
Definition Concepts.dox:2