• 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 Java - threads


Multi-threading changed drastically in JDK 5.0, with the addition of a large number of classes and interfaces that provide hihg-quality implementations of the mechanisms that most application programmers will need. Example programs using different APIs. Compilation and execution of java threads programs, programs numerical and non-numerical computations are discussed using different thread APIs and understand Performance issues on multi-core processors.


Example 1.1

Write a Java thread program to compute the sum of n natural numbers

Example 1.2

Write a Java thread program to search the Minimum number in a given array

Example 1.3

Write a Java thread program to Compute Vector Vector Multiplication using block-striped method.

Example 1.4

Write a Java thread program to Compute Matrix into Matrix multiplication

Example 1.5

Write a Java thread program to solve Producer Consumer problem.

Description of Java thread Programs


  Example 1.1 :  Simple Java thread program to compute the sum of n natural numbers
          (Download zipped source code : javathread_program_nsum.java

  • Objective
  • Write a Java program to compute the sum of n natural number

  • Description
  • This program prints the sum of first n natural numbers. This introduces the concept of a Synchronized block. Each thread adds its assigned number to a global variable. When all the threads are done, the global variable will contain the result. It uses a Synchronized block to make sure that only one thread is updating the variable at any given time

  • Input
  • Number of Thread

  • Output
  • Sum of first n natural numbers, n is the number of threads specified

  Example 1.2 :  java thread program to search the minimum number in a given array
          (Download zipped source code : javathread_program_search.java


  • Objective
  • Write a Java thread program to search the Minimum number in a given array.

  • Description
  • This program find minimum element in a given set of element. This introduces the concept of a Synchronized block. Each thread find minimum element in a block of element and compare to global minimum element. When all the threads are done, the global variable will contain the minimum element. It uses a Synchronized block to make sure that only one thread is updating the global minimum variable at any given time

  • Input
  • Number Of Element
    Number Of Thread

  • Output
  • Minimum Element

  Example 1.3 : Write a java program to Compute Vector Vector multiplication using block-striped method.
  (Download zipped source code : javathread_program_vectorvector_blockstrip.java

      
  • Objective
  • To write a Java program to compute the vector-vector multiplication using block striped partitioning for uniform data distribution . Assume that the vectors are of size n and p is number of thread used and n is a multiple of p.

  • Description
  • This is an implementation of Vector-Vector multiplication using the block striped partitioning algorithm. Each thread multiplies the corresponding elements and writes the product into the result vector. A synchronized block is used on the result vector to guarantee atomicity. The thread accesses the elements based on its no which is allocated by the main thread in the order of their creation. As the number of threads and the number of elements is known, the corresponding elements to be accessed can easily be computed

  • Input
  • Size of Vector
    Number of thread

  • Output
  • Dot Product of the given vectors


Example 1.4 : Write a Java thread program to Compute Matrix into Matrix multiplication
(Download zipped source code : javathread_program_matrixmatrix_time_double.java

  • Objective
  • Write a Java thread program to Compute matrix matrix multiplication

  • Description
  • This is an implementation of Matrix-Matrix multiplication algorithm. Number of row equally distributed to each thread. Each thread multiplies the corresponding elements and writes the product into the resultant matrix. The thread accesses the elements of the row based on its rows, which is allocated by the main thread in the order of their creation.

  • Input
  • Number of row and column for first Matrix
    Number of row and column for second Matrix
    Number of thread

  • Output
  • Time taken in matrix multiplication

Example 1.5 : Write a java program to solve producer consumer problem
    (Download zipped source code ; javathread_program_producer_consumer.java


  • Objective
  • Write a Java thread program to illustrate Producer-Consumer problem

  • Description
  • This problem commonly occurs in data flow decomposition in typical distributed computing. Usually, the problem can be decomposed in different ways on target architecture of the computing system, and different tasks can do different work. The most important issue is how the data flow between different tasks requires serious attention from performance point of view. The producer-consumer problem falls in this category in which the program has ability to execute in parallel. In this, the output of one task, the producer, becomes the input to another, the consumer . The two tasks are performed by different threads, and the second one, consumer , cannot start until the producer finishes some portion of its work. This is quite similar to the concept of pipelining in typical Parallel computing Paradigms.

    The producer/consumer problem occurs in several typical scenarios. In one scenario, first task may complete many sub-tasks and decides at some point of time, the work should be given to next task. The next task, which is executed by another thread, cannot start the work until the previous sub-tasks are completed by first task. It is difficult to identify at what point of time the first task completes the work and the delay caused by the first task creates a pause for the second task. After certain point of time, both the tasks can execute in parallel.

    In another scenario, the first task may read of a file and the results of this become the input to next task, which might be threaded. This process introduces substantial delay and the next step cannot begin until the first step completes the reading of file or partial reading of the file is done. It is possible to do new piece of work in the next step and waiting for the completion of first step. The producer-consumer problem has different benefits when decomposing a problem. The dependence created between consumer and producer can cause significant delays if this model is implemented correctly. There are situations in which consumer threads are idling while waiting for producer threads. A performance sensitive design seeks to understand the exact nature of the dependence and diminish the delay it imposes.

    In an ideal scenario, the producer and consumer plan carefully on their interaction. Also, if the consumer is finishing up while the producer is completely done, one thread remains idle while other threads are busy working away.

1.



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.

2.




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.

This program makes use of mutex-locks for establishing a producer-consumer relationship between threads. The producer creates data and inserts them into a shared work -queue .The consumer threads pick up from the shared work- queue and consume it.

Centre for Development of Advanced Computing