Skip to content

Latest commit

 

History

History
74 lines (56 loc) · 10.5 KB

README.md

File metadata and controls

74 lines (56 loc) · 10.5 KB

Intel Threading Building Blocks (TBB) is a widely-used C++ library for parallel programming. It provides high-level constructs for expressing parallelism and concurrent execution, making it easier for developers to leverage multicore processors and parallel architectures effectively. TBB abstracts the complexity of low-level threading and synchronization mechanisms, allowing developers to focus on algorithm design and productivity.

Here are some key features and components of Intel TBB:

Task-Based Parallelism: TBB introduces the concept of tasks, which represent units of work that can be executed concurrently. Developers can express parallelism by creating task-based algorithms, allowing TBB to manage the scheduling and execution of tasks efficiently.

Parallel Algorithms: TBB offers a collection of parallel algorithms that parallelize common computational tasks, such as parallel_for, parallel_reduce, parallel_scan, and parallel_sort. These algorithms automatically distribute workloads across multiple threads, improving performance on multicore systems.

Concurrency Control: TBB provides thread-safe data structures and synchronization primitives, including concurrent containers (e.g., concurrent_vector, concurrent_queue) and atomic operations (e.g., atomic, atomic_ref), to facilitate safe concurrent access to shared resources.

Parallel Task Scheduler: TBB includes a scalable task scheduler that dynamically adjusts the number of threads based on the available hardware resources and workload characteristics. This adaptive scheduler optimizes resource utilization and load balancing, maximizing parallel performance.

Flow Graph: TBB's flow graph framework enables the creation of dataflow graphs, where nodes represent computational tasks and edges represent data dependencies. Developers can express complex parallel workflows using this graphical model, leveraging TBB's scheduling and synchronization mechanisms for efficient execution.

Compatibility and Portability: TBB is designed to be compatible with various operating systems and platforms, including Windows, Linux, macOS, and different CPU architectures. It integrates seamlessly with popular C++ compilers and development environments, ensuring broad compatibility and portability.

Performance and Scalability: TBB emphasizes performance and scalability, aiming to exploit the full potential of multicore processors and parallel hardware. By leveraging efficient scheduling strategies, fine-grained parallelism, and optimized data structures, TBB enables developers to achieve high-performance parallel execution.

Overall, Intel TBB provides a comprehensive set of tools and abstractions for parallel programming in C++, empowering developers to write scalable and efficient parallel applications with ease. Whether you're developing scientific simulations, multimedia processing, or data analytics applications, TBB offers a powerful framework for harnessing the power of parallelism.

Utilities which uses the power of Intel Threaded Building Blocks

OpenCvRTSPIntelTBBDemo.cpp

This C++ code demonstrates the use of the Intel Threading Building Blocks (TBB) library and the OpenCV library to read and process an RTSP (Real-Time Streaming Protocol) video stream in a parallel manner. Here's a breakdown of the code:

  1. The code includes the necessary header files: <iostream> for input/output operations, <thread> for working with threads, <tbb/tbb.h> for the TBB library, and <opencv2/opencv.hpp> for the OpenCV library.
  2. The processFrame function is defined, which takes a const Mat& (a reference to an OpenCV Mat object representing a frame) as input. This function is responsible for processing the frame, in this case, it simply displays the frame using imshow and waitKey.
  3. The readRTSPStream function is defined, which takes a const string& (a reference to a string) representing the RTSP stream URL as input. This function opens the RTSP stream using VideoCapture, and then continuously reads frames from the stream using cap.read(frame). Each frame is then passed to the processFrame function for processing. 4. The main function is the entry point of the program. It first sets the RTSP stream URL to a placeholder value, which should be replaced with the actual RTSP stream URL.
  4. The code then initializes the TBB task scheduler using tbb::task_scheduler_init init.
  5. The core of the program is the tbb::parallel_for loop, which is used to read the RTSP stream in parallel using multiple threads. The loop iterates over the range of available threads (determined by the default number of threads in the TBB task scheduler) and calls the readRTSPStream function for each thread.

In summary, this code sets up a parallel processing pipeline to read and process an RTSP video stream using the TBB library and the OpenCV library. The tbb::parallel_for loop allows the RTSP stream to be read and processed concurrently, potentially improving the overall performance of the application.

OpenCvRTSPIntelTBBDemo2.cpp

This C++ code is a multi-threaded application that retrieves video frames from multiple RTSP (Real-Time Streaming Protocol) streams and displays them in a window using the OpenCV library. Here's a breakdown of the code:

  1. The code includes the necessary header files for input/output, vectors, threads, mutexes, condition variables, queues, and the OpenCV library.
  2. It declares global variables: - rtsp_urls: a vector to store the RTSP stream URLs. - frame_queue: a concurrent queue to store the video frames. - queue_mutex: a mutex to protect the access to the frame queue. - queue_cv: a condition variable to signal when frames are available in the queue. - stop_threads: a flag to signal the threads to stop.
  3. The grab_frame function is responsible for retrieving frames from a single RTSP stream. It opens the video capture, reads frames, and pushes them into the frame_queue. This function runs in a loop until the stop_threads flag is set.
  4. The display_frames function is responsible for displaying the frames from the frame_queue. It waits for frames to be available in the queue, then retrieves and displays them using OpenCV's imshow and waitKey functions. This function also runs in a loop until the stop_threads flag is set.
  5. The main function: - Loads the RTSP stream URLs from configuration files (or a hardcoded list in this example). - Starts the frame grabbing threads, one for each RTSP stream. - Starts the frame display thread. - Waits for user input to stop the program. - Sets the stop_threads flag to signal the threads to stop. - Waits for all the threads to finish.
  6. The purpose of this code is to provide a multi-threaded solution for retrieving and displaying video frames from multiple RTSP streams simultaneously.
  7. The use of a concurrent queue and condition variables ensures that the frame grabbing and display processes are synchronized and efficient.

RingBufferIntelTBB.cpp

This C++ code demonstrates the use of a concurrent ring buffer, which is a data structure that allows for the storage and retrieval of elements in a circular fashion. The code uses the Intel Threading Building Blocks (TBB) library to implement the concurrent operations. Here's a breakdown of the code:

  1. The RingBuffer class is defined, which has a push method to add elements to the buffer and a pop method to remove elements from the buffer. The buffer is implemented using the tbb::concurrent_bounded_queue class from the TBB library, which provides thread-safe concurrent access to the queue.
  2. In the main function, a RingBuffer of integers is created with a size of 10.
  3. The tbb::parallel_for function is used to create multiple threads (4 in this case) that push items into the ring buffer. Each thread pushes an item with a value equal to its thread index multiplied by 10 (e.g., thread 0 pushes 0, thread 1 pushes 10, etc.).
  4. Another tbb::parallel_for function is used to create multiple threads (again, 4 in this case) that pop items from the ring buffer. Each thread pops an item and prints it to the console. The purpose of this code is to demonstrate the use of a concurrent ring buffer, where multiple threads can safely push and pop items from the buffer without causing race conditions or other concurrency issues. The TBB library provides the necessary synchronization primitives to ensure the thread-safe operation of the buffer.

OpenCVTbbExperiment.cpp

This C++ code demonstrates the usage of OpenCV's parallel processing capabilities using Intel's Threading Building Blocks (TBB) library. Here's a breakdown of the code:

  1. The code includes necessary headers for OpenCV, threading, and chrono (for timing).
  2. The IMAGE_HEIGHT constant is set to 20, which affects how many tasks OpenCV splits the processing into.
  3. The reportThreads() function sleeps for 100 milliseconds to ensure the process reported thread count is up-to-date, then prints the number of threads used by OpenCV.
  4. The runTbb() function creates a grayscale image with a height of IMAGE_HEIGHT and a width of 1000 pixels, fills it with random values, calculates the histogram of the image using OpenCV's calcHist() function, and prints a message indicating that TBB was used. It also calls reportThreads().
  5. The runTbbThread() function calls reportThreads() and then calls runTbb() twice.
  6. In the main() function:
  • The initial number of threads used by OpenCV is printed.
  • The number of threads used by OpenCV is set to 3 using cv::setNumThreads(3).
  • reportThreads() is called to print the updated number of threads.
  • runTbb() is called twice.
  • A new thread is created using std::thread and runTbbThread() is executed in that thread.
  • The main thread waits for the new thread to finish using thread.join().
  • reportThreads() is called again to print the number of threads after the new thread finishes.
  • runTbb() is called twice more. The purpose of this code is to demonstrate how OpenCV can utilize multiple threads for parallel processing, and how the number of threads can be controlled using cv::setNumThreads().

The code creates a separate thread and runs the runTbbThread() function in that thread, which in turn calls runTbb() twice, utilizing the TBB library for parallel processing. Note that the code includes a comment mentioning that calling cv::setNumThreads(3) inside the runTbbThread() function would cause a segmentation fault when runTbb() is called outside that thread. This is likely due to thread safety issues when modifying the number of threads used by OpenCV from multiple threads simultaneously.