Title: ITK Workshop
1ITK Workshop
Multi-Threading
2ITK Workshop Multi-Threading
- Multi-Threading
- The portable Multi-Threader
- Mutual Exclusion
- ThreadedGenerateData
- BeforeThreadedGenerateData
- AfterThreadedGenerateData
- SetNumberOfThreads
- The Evil legacy of FORTRAN
3Insight Toolkit - Advanced Course
- The Concept of Multi-Tasking
4The Multi-Tasking Concept
Task A
Task B
Task C
Processor
The Operating System assigns processor time to
each task
5Insight Toolkit - Advanced Course
- Tasks have independent memory spaces
- They cannot communicate with each other unless
they use sockets or files
6Insight Toolkit - Advanced Course
- The Concept of Multi-Threading
7The Multi-Threading Concept
Task A
T2
Processor
T1
T0
A Threading library creates threads and assigns
processor time to each thread
8Insight Toolkit - Advanced Course
- Threads from a common Task have the same memory
space (Shared Memory) - They can simultaneously access variables that
were instantiated before creating the threads.
9The Multi-Threading in Multi-Processors
Task A
T2
Processor 1
T1
T0
Processor 2
Processor 3
Processor 4
10Insight Toolkit Multi-Threading Libraries
- Different Threading libraries are available in
different platforms
- PThreads
- SProc
- Win32 Threads
11Insight Toolkit Multi-Threading Libraries
- ITK Provides a class for hiding the differences
between threading libraries
itkMultiThreader
Win32Threads
PThreads
SProc
12Insight Toolkit - Advanced Course
- Using the itkMultiThreader
- Declare a Callback
- Instantiate an itkMultiThreader
- Set the number of Threads
- Set the callback
- Invoke SingleMethodExecute()
13Using the Multi-Threader
include itkMultiThreader.h int main( int
argc, char argv ) typedef
itkMultiThreader ThreaderType
ThreaderTypePointer threader
ThreaderTypeNew() const unsigned int
numberOfThreads atoi( argv1 )
threader-gtSetNumberOfThreads( numberOfThreads
) threader-gtSetSingleMethod(
ThreaderCallback, NULL ) threader-gtSingleMeth
odExecute()
14Defining the Theader Callback
include itkMultiThreader.h ITK_THREAD_RETURN_T
YPE ThreaderCallback( void arg ) typedef
itkMultiThreaderThreadInfoStruct
ThreadInfoType ThreadInfoType infoStruct
static_castlt ThreadInfoType gt( arg ) const
unsigned int threadId infoStruct-gtThreadID std
cout ltlt Thread ltlt threadId ltlt
stdendl ITK_THREAD_RETURN_TYPE value return
value
15Insight Toolkit - Advanced Course
Run the Multi-Caster for different numbers of
Threads
16Insight Toolkit - Advanced Course
17Insight Toolkit - Advanced Course
- DONT TRY TO WRITE TO SHARED DATA FROM MULTIPLE
THREADS
18Insight Toolkit - Advanced Course
Do the wrong thing on purpose Attempt to write
to a static variable simultaneously from
different Threads
19Defining the WRONG Theader Callback
include itkMultiThreader.h static unsigned
long int Counter 0 ITK_THREAD_RETURN_TYPE
ThreaderCallback( void arg ) typedef
itkMultiThreaderThreadInfoStruct
ThreadInfoType ThreadInfoType infoStruct
static_castlt ThreadInfoType gt( arg )const
unsigned int threadId infoStruct-gtThreadID Cou
nter stdcout ltlt Thread ltlt
threadIdstdcout ltlt Counter ltlt Counter ltlt
stdendl ITK_THREAD_RETURN_TYPE value return
value
20Insight Toolkit - Advanced Course
- IF YOU REALLY REALLY WANT TO PUSH THIS BUTTON
21Insight Toolkit - Advanced Course
Prevent the possible simultaneous access to share
memory by introducion Mutual Exclusion
22Defining the RIGHT Theader Callback
include itkMultiThreader.h include
itkSimpleFastMutexLock.h static unsigned long
int Counter 0 static itkSimpleFastMutexLock
mutex ITK_THREAD_RETURN_TYPE ThreaderCallback(
void arg ) mutex.Lock() Counter
mutex.UnLock() stdcout ltlt Counter ltlt
Counter ltlt stdendl ITK_THREAD_RETURN_TYPE
value return value
23Insight Toolkit - Advanced Course
Use the itkRealTimeClock to measure how fast
(or how slow) the Mutual exclusion
24Timing the Mutual Exclusion Class
include itkSimpleFastMutexLock.h include
itkRealTimeClock.h static unsigned long int
Counter 0 static itkSimpleFastMutexLock
mutex int main() const unsigned int N
400000L itkRealTimeClock clock const
double T1 clock.GetTimestamp() for(unsigned
long i0 i lt N i) mutex.Lock()
Counter mutex.UnLock() const
double TT clock.GetTimestamp() T1
stdcout ltlt Time ltlt TT / N ltlt tdendl
ITK_THREAD_RETURN_TYPE value return value
25Insight Toolkit - Advanced Course
- Writing a Multi-Threading Filter
26Insight Toolkit Filters Hierarchy
itkProcessObject
public MultiThreader GetThreader() private
MultiThreader m_Threader int
m_NumberOfThreads
27Insight Toolkit Filters Hierarchy
itkImageSource
public virtual void BeforeThreadedGenerateData(
) virtual void AfterThreadedGenerateData()
virtual void ThreadedGenerateData(
const OutputImageRegionType region,
int threadId )
28Insight Toolkit GenerateData()
BeforeThreadedGenerateData
TGD
TGD
TGD
TGD
TGD
AfterThreadedGenerateData
29Insight Toolkit - Advanced Course