Mode-1 Multi-Core Memory Allocators OpenMP Intel TBB Pthreads Java - Threads Charm++ Prog. Message Passing (MPI) MPI - OpenMP MPI - Intel TBB MPI - Pthreads Compiler Opt. Features Threads-Perf. Math.Lib. Threads-Prof. & Tools Threads-I/O Perf. PGAS : UPC / CAF / GA Power-Perf. Home






Programming on Multi-Core Processors Using TBB APIs
Example 3.1

Write a TBB program to explain the producer-consumer problem with standered queue .
Example 3.2

Write a TBB program to explain the producer-consumer work queue problem with standered queue.
Example 3.3

Write a TBB program to explain the producer-consumer problem with concurrent queue .
Example 3.4

Write a TBB program to explain the producer-consumer work queue problem with concurrent queue.
Description of TBB Programs
 

Example 3.1 : producer-consumer problem with standered queue using TBB

Download source code : producer_consumer_v1.0 (WinRAR ZIP archive)

  • Objective
  • To explain the producer-consumer problem with standered queue using TBB.

  • Description
  • The producer-consumer problem illustrates the need for synchronization in systems where many processes share a resource. In the problem, two processes share a fixed-size buffer. One process produces item and puts it in the buffer, while the other process consumes item from the buffer. Here, the output of one task, the producer, becomes the input to another, the consumer. In this, the most important issue is how the data flow between different tasks requires serious attention from performance point of view.

    The producer/consumer problem occurs in several typical scenarios. In one scenario, the producer tries to put an item into a full buffer? In this situation, producer threads are idling while waiting for consumer threads. In another scenario, the consumer tries to take an item from an empty buffer? In this situation, consumer threads are idling while waiting for producer threads. In order to synchronize these processes, producer has to be blocked, when the buffer is full, and the consumer has to be blocked, when the buffer is empty.

    This program makes use of mutex-locks for establishing a producer-consumer relationship between threads.

  • Input
  • Number of Items, Number of Producers, Number of Consumers.

  • Output
  • Sequence of Producer and Consumer Items


Example 3.2 : producer-consumer work queue problem with standered queue using TBB

Download source code : producer_consumer_v1.1 (WinRAR ZIP archive)
  • Objective
  • TTo explain the producer-consumer work queue problem with standered queue using TBB

  • Description
  • The producer-consumer work queue problem illustrates the need for synchronization in systems where many processes share a resource. In the problem, two processes share a fixed-size buffer.

    The producer/consumer work queue problem occurs in several typical scenarios. In one scenario, the producer creates tasks and inserts them into a work-queue. The consumer threads pick up tasks from the task queue and execute them. This concept can be called as producer-consumer work queues paradigm. Simple instances of this paradigm in which the task queue can hold only one task, which may be short or long but is typically have bounded size. In this simple model, the producer thread can estimate the time taken for consumer work and insert the new work in a shared buffer.

    In producer-consumer work queue paradigm, the complex model application such as multi-media processing, the different possibilities exist on access to shared buffer. Several possibilities exist in which the producer thread must not overwrite the shared buffer when the previous task has not been picked by a consumer thread. Also, the consumer threads must not pick up tasks until there is something present in the shared data structure and individual consumer threads pick up tasks one at a time. Producer-consumer problem is classic synchronization problem.

    In this example, the task is matrix-matrix multiplication. Here, producer acts as Input Generator for matrix-matrix multiplication. i.e. It will allocates memory for all matrices and assign input to matrices.

    Consumer performs performs matrix multiplication operation and free the memory, that is allocated for matrices.

    This program makes use of mutex-locks for establishing a producer-consumer relationship between threads.

    Number of Items, Number of Producers, Number of Consumers.

  • Output
  • Sequence of matrices and time taken to compute the multiplication.


 

Example 3.3 : producer-consumer problem with concurrent queue using TBB.

Download source code : tbb_producer_consumer_v1.0(WinRAR ZIP archive)
  • Objective
  • To explain the producer-consumer problem with concurrent queue using TBB.

  • Description
  • The producer-consumer problem illustrates the need for synchronization in systems where many processes share a resource. In the problem, two processes share a fixed-size buffer. One process produces item and puts it in the buffer, while the other process consumes item from the buffer. Here, the output of one task, the producer, becomes the input to another, the consumer. In this, the most important issue is how the data flow between different tasks requires serious attention from performance point of view.

    The producer/consumer problem occurs in several typical scenarios. In one scenario, the producer tries to put an item into a full buffer? In this situation, producer threads are idling while waiting for consumer threads. In another scenario, the consumer tries to take an item from an empty buffer? In this situation, consumer threads are idling while waiting for producer threads. In order to synchronize these processes, producer has to be blocked, when the buffer is full, and the consumer has to be blocked, when the buffer is empty.

    In this problem, if multiple threads are producing and consuming concurrently, it is difficult to predict, which is first. The concurrent_queue promises, that if a thread producing multiple values, and another thread consuming those same values, they will be consumed in the same order that they were produced. So, here it is not necessary to use mutex-locks for establishing a producer-consumer relationship between threads.

  • Input
  • Number of Items, Number of Producers, Number of Consumers.

  • Output
  • Sequence of Produced and Consumed Items.


 

Example 3.4 : producer-consumer work queue problem with concurrent queue using TBB

Download source code : tbb_producer_consumer_v1.1(WinRAR ZIP archive)
  • Objective
  • To explain the producer-consumer work queue problem with concurrent queue using TBB

  • Description
  • The producer-consumer work queue problem illustrates the need for synchronization in systems where many processes share a resource. In the problem, two processes share a fixed-size buffer.

    The producer/consumer work queue problem occurs in several typical scenarios. In one scenario, the producer creates tasks and inserts them into a work-queue. The consumer threads pick up tasks from the task queue and execute them. This concept can be called as producer-consumer work queues paradigm. Simple instances of this paradigm in which the task queue can hold only one task, which may be short or long but is typically have bounded size. In this simple model, the producer thread can estimate the time taken for consumer work and insert the new work in a shared buffer.

    In producer-consumer work queue paradigm, the complex model application such as multi-media processing, the different possibilities exist on access to shared buffer. Several possibilities exist in which the producer thread must not overwrite the shared buffer when the previous task has not been picked by a consumer thread. Also, the consumer threads must not pick up tasks until there is something present in the shared data structure and individual consumer threads pick up tasks one at a time. Producer-consumer problem is classic synchronization problem.

    In this example, the task is matrix-matrix multiplication. Here, producer acts as Input Generator for matrix-matrix multiplication. i.e. It will allocates memory for all matrices and assign input to matrices. Consumer performs performs matrix multiplication operation and free the memory, that is allocated for matrices.

    The concurrent_queue promises, that if the producer creates tasks and inserts them into a work-queue. And consumer threads pick up tasks from the task queue and execute them. They will be executed in the same order, that they were inserted in the work-queue. So, here it is not necessary to use mutex-locks for establishing a producer-consumer relationship between threads.

  • Input
  • Number of Items, Number of Producers, Number of Consumers.

  • Output
  • Sequence of matrices and time taken to compute the multiplication.


Centre for Development of Advanced Computing