Injustice - Developers Among Us (SciFiDevCon 2024)
09 intel v_tune_session_13
1. Code Optimization and Performance Tuning Using Intel VTune
Installing Windows XP Professional Using Attended Installation
Objectives
In this session, you will learn to:
Identify the benefits of multithreading
Design applications using threads
Ver. 1.0 Slide 1 of 17
2. Code Optimization and Performance Tuning Using Intel VTune
Installing Windows XP Professional Using Attended Installation
Identifying the Benefits of Multithreading
Multithreading increases the performance of your
application by:
Performing multiple tasks in parallel
Better utilization of system resources
Increasing the speed of your application
Ver. 1.0 Slide 2 of 17
3. Code Optimization and Performance Tuning Using Intel VTune
Installing Windows XP Professional Using Attended Installation
Identifying the Benefits of Multithreading (Contd.)
Thread is a sequential flow of control within a program.
It is a sequence of instructions executed within a program.
Every program consists of at least one thread, called the
main thread.
The main thread is responsible for initializing the programs.
Ver. 1.0 Slide 3 of 17
4. Code Optimization and Performance Tuning Using Intel VTune
Installing Windows XP Professional Using Attended Installation
Just a minute
What is a thread?
Answer:
A thread is a sequential flow of control within a program.
Ver. 1.0 Slide 4 of 17
5. Code Optimization and Performance Tuning Using Intel VTune
Installing Windows XP Professional Using Attended Installation
Identifying the Benefits of Multithreading (Contd.)
The main benefits of threads are:
Increased performance
Better resource utilization
Simpler communication
Ver. 1.0 Slide 5 of 17
6. Code Optimization and Performance Tuning Using Intel VTune
Installing Windows XP Professional Using Attended Installation
Identifying the Benefits of Multithreading (Contd.)
The two inherent conditions of a multithreaded application
are:
► Concurrency Concurrency refers to a situation in
which one or more threads are in
► Parallelism Parallelism refers to the simultaneous
progress simultaneously.
execution of multiple tasks.
In this case, the processor switches
In a multithreaded application, which is
from one thread to another during the
running onof the application. system,
execution a multiprocessor
threads execute in parallel.
Ver. 1.0 Slide 6 of 17
7. Code Optimization and Performance Tuning Using Intel VTune
Installing Windows XP Professional Using Attended Installation
Designing Applications Using Threads
The best time for threading an application is during its
design phase.
During the design phase, you can accommodate all the data
and code restructuring related to threading.
This reduces the overall effort during the application
development.
A program comprising of multiple independent activities can
be redesigned in such a manner so that each activity can
be defined as a separate thread.
This enables you to decompose your work into simple
independent activities and improve the functionality and
performance of your application.
Ver. 1.0 Slide 7 of 17
8. Code Optimization and Performance Tuning Using Intel VTune
Installing Windows XP Professional Using Attended Installation
Designing Applications Using Threads (Contd.)
You can improve the functionality of your application by
assigning different threads to different functions of the
application.
By assigning a separate thread to each function, all the
functions can execute independent of each other.
This approach makes the application more efficient because
threading is easier than switching functions within a serial
code.
Ver. 1.0 Slide 8 of 17
9. Code Optimization and Performance Tuning Using Intel VTune
Installing Windows XP Professional Using Attended Installation
Designing Applications Using Threads (Contd.)
Performance of an application depends upon the
combination of various factors such as speed and utilization
of system resources.
A multithreaded application running on a single processor
system leads to a better utilization of system resources.
A multithreaded application running on a multiprocessor
system leads to better utilization of system resources and
increased speed of the application.
Ver. 1.0 Slide 9 of 17
10. Code Optimization and Performance Tuning Using Intel VTune
Installing Windows XP Professional Using Attended Installation
Just a minute
How does the use of threads improve the functionality of an
application?
Answer:
Threads improve the functionality of an application by
assigning different threads to different functions. This makes it
easier to control the execution of multiple functions within an
application.
Ver. 1.0 Slide 10 of 17
11. Code Optimization and Performance Tuning Using Intel VTune
Installing Windows XP Professional Using Attended Installation
Designing Applications Using Threads (Contd.)
Data decomposition refers to the process of:
Breaking down a program into logical chunks or individual
tasks.
Identifying the dependencies between the tasks.
The two types of decomposition methods are:
► Task decomposition It refers to the process of decomposing
a program on the basis of the functions
► Data decomposition It performs. process of decomposing
it refers to the
an application in such a way so that the
It is also known as functional
same operation is performed
decomposition.
repeatedly for different data.
In this case, you can assign separate
threads to the independent functions in
your program.
Ver. 1.0 Slide 11 of 17
12. Code Optimization and Performance Tuning Using Intel VTune
Installing Windows XP Professional Using Attended Installation
Identifying the Complexities Involved in Multithreaded Applications
In a multithreaded application, you need to take care of
various complexities, which may arise during thread
interaction. These complexities are:
► Race conditions Race conditions occur when the output
of the program depends upon which
► Critical region Critical region refers to those portions
thread reaches a particular block of
of your application that access shared
► Mutual exclusion code first.
Mutual exclusion allows only one
variables.
thread to be executingto different
Race conditions lead in a critical
► Synchronization Synchronization controls the relative
region at a given time.
results every time a program is
order of thread execution and resolves
► Deadlocks executed. refersis executing the code
When a thread to a threads. in which
Deadlock
any conflicts among situation
that accesses a shared resource in a
a thread waits for abased on the can
Synchronization is condition that
criticaloccur. any other thread that
never region,
concept of monitoring.
might desire entry to the critical region
Deadlock halts the execution of your
must wait to access that region.
application preventing it to continue
further.
Ver. 1.0 Slide 12 of 17
13. Code Optimization and Performance Tuning Using Intel VTune
Installing Windows XP Professional Using Attended Installation
Just a minute
List the complexities involved in a multithreaded application.
Answer:
The complexities involved in a multithreaded application are as
follows:
Race conditions
Critical region
Mutual exclusion
Synchronization
Deadlocks
Ver. 1.0 Slide 13 of 17
14. Code Optimization and Performance Tuning Using Intel VTune
Installing Windows XP Professional Using Attended Installation
Activity: Analyzing the Processor Activity During Deadlock
Problem Statement:
John has developed a code in C# in which he acquires a lock
on two resources. However on execution, the application
comes to a halt after some time. He wants to analyze the
processor utilization on his system using the counter monitor
feature of VTune. Help John to accomplish his task.
Ver. 1.0 Slide 14 of 17
15. Code Optimization and Performance Tuning Using Intel VTune
Installing Windows XP Professional Using Attended Installation
Activity: Analyzing the Processor Activity During Deadlock (Contd.)
Solution
To analyze the performance of the application, you need to
perform the following tasks:
1. Configure counter monitor using the counter monitor configuration
wizard.
2. Analyze the processor utilization on the system.
Ver. 1.0 Slide 15 of 17
16. Code Optimization and Performance Tuning Using Intel VTune
Installing Windows XP Professional Using Attended Installation
Summary
In this session, you learned that:
You can improve the speed of your application to a great
extent by using multiple threads in your application.
A thread is a sequential flow of control within a program.
The main benefits of threads are as follows:
• Increased performance
• Better resource utilization
• Simpler communication
• In a multithreaded application, threads run concurrently or in
parallel.
• You can improve the functionality of your application by
assigning different threads to different functions of the
application. These functions may or may not be dependant on
each other.
Ver. 1.0 Slide 16 of 17
17. Code Optimization and Performance Tuning Using Intel VTune
Installing Windows XP Professional Using Attended Installation
Summary (Contd.)
The threads enable you to improve the performance of your
application.
The process of breaking down a program into logical chunks or
individual tasks and identifying the dependencies between
them is referred to as decomposition.
In a multithreaded application, you need to take care of various
complexities that may arise during thread interaction. These
complexities are as follows:
Race condition
Critical region
Mutual exclusion
Synchronization
Deadlock
Ver. 1.0 Slide 17 of 17
Hinweis der Redaktion
Begin the session by sharing the objectives with the students.
Begin the session by sharing the objectives with the students.
Explain the matrix multiplication example in which every element can computed separately by a thread. Also tell the students that improper use of threads can degrade the performance of the application.
Initiate a discussion on the benefits of threads. Tell the students that at a time, the processor can execute only one thread. Next, discuss the concept of time slicing in a threaded application. Explain the concept of time slicing by giving the example of a gaming application as given in the SG. Tell the students that implementing time slicing in a threaded application keeps the processor busy. It gives an impression as if multiple threads are running at the same time.
Discuss the concept of concurrency and parallelism in a threaded application. Tell the students that on a single processor system, threads run concurrently and in a multiprocessor system, threads can run concurrently as well as parallely.
Tell the students that design phase of an application is the best time to incorporate multithreading. This reduces the overall effort in application development process.
Explain the students that a multithreaded application improves the functionality of an application by assigning threads to different functions in the application. You can explain the concept with the help of an example of the Outlook window given in the SG.
Explain that the performance of a multithreaded application varies on a single processor and multiprocessor system. Refer to the SG for the same. Next, explain the students that performance of an application also depends upon various other factors. This may include the way files and databases have been organized. Refer to the SG for the same.
Explain the concept of decomposition of work in an application. Next, explain task decomposition and data decomposition. Refer to the examples given in the SG relating to task and data decomposition.
Initiate a discussion on various complexities involved in a multithreaded application. Ask the students to think of some complexities involved in a multithreaded application. Next, explain various complexities given in the slide. Explain each complexity by giving an example. Refer to the SG for the same.
To demonstrate this activity, you can use the data files provided at the following locations: TIRM Datafiles for Faculty Chapter06 Activity01 Deadlock.zip Deadlock.zip file contains the optimized and the unoptimized codes. The faculty should first show the demonstration of the unoptimized code. After analyzing the processor activity, the faculty should again run the activity using the optimized code. This would enable the students comparing the results between the optimized and the unoptimized code.