Presentation is loading. Please wait.

Presentation is loading. Please wait.

NA-MIC National Alliance for Medical Image Computing ITK Workshop October 5-8, 2005 Multi-Threading.

Similar presentations


Presentation on theme: "NA-MIC National Alliance for Medical Image Computing ITK Workshop October 5-8, 2005 Multi-Threading."— Presentation transcript:

1 NA-MIC National Alliance for Medical Image Computing ITK Workshop October 5-8, 2005 Multi-Threading

2 National Alliance for Medical Image Computing ITK Workshop – Multi-Threading Multi-Threading –The portable Multi-ThreaderThe portable Multi-Threader –Mutual ExclusionMutual Exclusion –ThreadedGenerateDataThreadedGenerateData –BeforeThreadedGenerateDataBeforeThreadedGenerateData –AfterThreadedGenerateDataAfterThreadedGenerateData –SetNumberOfThreadsSetNumberOfThreads –The Evil legacy of FORTRANThe Evil legacy of FORTRAN

3 National Alliance for Medical Image Computing The Concept of Multi-Tasking Insight Toolkit - Advanced Course

4 National Alliance for Medical Image Computing The Multi-Tasking Concept Task A Processor Task BTask C The Operating System assigns processor time to each task

5 National Alliance for Medical Image Computing Tasks have independent memory spaces They cannot communicate with each other unless they use sockets or files Insight Toolkit - Advanced Course

6 National Alliance for Medical Image Computing The Concept of Multi-Threading Insight Toolkit - Advanced Course

7 National Alliance for Medical Image Computing The Multi-Threading Concept Task A Processor A Threading library creates threads and assigns processor time to each thread T0 T1 T2

8 National Alliance for Medical Image Computing Threads from a common Task have the same memory space (Shared Memory) They can simultaneously access variables that were instantiated before creating the threads. Insight Toolkit - Advanced Course

9 National Alliance for Medical Image Computing The Multi-Threading in Multi-Processors Task A Processor 1 T0 T1 T2 Processor 2 Processor 3 Processor 4

10 National Alliance for Medical Image Computing Different Threading libraries are available in different platforms Insight Toolkit – Multi-Threading Libraries PThreads SProc Win32 Threads

11 National Alliance for Medical Image Computing ITK Provides a class for hiding the differences between threading libraries Insight Toolkit – Multi-Threading Libraries itk::MultiThreader Win32 Threads PThreadsSProc

12 National Alliance for Medical Image Computing Using the itk::MultiThreader Insight Toolkit - Advanced Course Declare a Callback Instantiate an itk::MultiThreader Set the number of Threads Set the callback Invoke SingleMethodExecute()

13 National Alliance for Medical Image Computing #include itkMultiThreader.h int main( int argc, char * argv[] ) { typedef itk::MultiThreader ThreaderType; ThreaderType::Pointer threader = ThreaderType::New(); const unsigned int numberOfThreads = atoi( argv[1] ); threader->SetNumberOfThreads( numberOfThreads ); threader->SetSingleMethod( ThreaderCallback, NULL ); threader->SingleMethodExecute(); } Using the Multi-Threader

14 National Alliance for Medical Image Computing Defining the Theader Callback #include itkMultiThreader.h ITK_THREAD_RETURN_TYPE ThreaderCallback( void * arg ) { typedef itk::MultiThreader::ThreadInfoStruct ThreadInfoType; ThreadInfoType * infoStruct = static_cast ( arg ); const unsigned int threadId = infoStruct->ThreadID; std::cout << Thread = << threadId << std::endl; ITK_THREAD_RETURN_TYPE value; return value; }

15 National Alliance for Medical Image Computing Exercise 31a Insight Toolkit - Advanced Course Run the Multi-Caster for different numbers of Threads

16 National Alliance for Medical Image Computing DONT PUSH THIS BUTTON ! Insight Toolkit - Advanced Course

17 National Alliance for Medical Image Computing DONT TRY TO WRITE TO SHARED DATA FROM MULTIPLE THREADS Insight Toolkit - Advanced Course

18 National Alliance for Medical Image Computing Exercise 31b Insight Toolkit - Advanced Course Do the wrong thing on purpose: Attempt to write to a static variable simultaneously from different Threads

19 National Alliance for Medical Image Computing Defining the WRONG Theader Callback #include itkMultiThreader.h static unsigned long int Counter = 0; ITK_THREAD_RETURN_TYPE ThreaderCallback( void * arg ) { typedef itk::MultiThreader::ThreadInfoStruct ThreadInfoType; ThreadInfoType * infoStruct = static_cast ( arg ); const unsigned int threadId = infoStruct->ThreadID; Counter++; std::cout << Thread = << threadId; std::cout << Counter = << Counter << std::endl; ITK_THREAD_RETURN_TYPE value; return value; }

20 National Alliance for Medical Image Computing IF YOU REALLY REALLY WANT TO PUSH THIS BUTTON Insight Toolkit - Advanced Course

21 National Alliance for Medical Image Computing Exercise 31c Insight Toolkit - Advanced Course Prevent the possible simultaneous access to share memory by introducion Mutual Exclusion

22 National Alliance for Medical Image Computing Defining the RIGHT Theader Callback #include itkMultiThreader.h #include itkSimpleFastMutexLock.h static unsigned long int Counter = 0; static itk::SimpleFastMutexLock mutex; ITK_THREAD_RETURN_TYPE ThreaderCallback( void * arg ) { mutex.Lock(); Counter++; mutex.UnLock(); std::cout << Counter = << Counter << std::endl; ITK_THREAD_RETURN_TYPE value; return value; }

23 National Alliance for Medical Image Computing Exercise 31d Insight Toolkit - Advanced Course Use the itk::RealTimeClock to measure how fast (or how slow) the Mutual exclusion

24 National Alliance for Medical Image Computing Timing the Mutual Exclusion Class #include itkSimpleFastMutexLock.h #include itkRealTimeClock.h static unsigned long int Counter = 0; static itk::SimpleFastMutexLock mutex; int main() { const unsigned int N = L; itk::RealTimeClock clock; const double T1 = clock.GetTimestamp(); for(unsigned long i=0; i < N; i++) { mutex.Lock(); Counter++; mutex.UnLock(); } const double TT = clock.GetTimestamp() – T1; std::cout << Time = << TT / N << td::endl; ITK_THREAD_RETURN_TYPE value; return value; }

25 National Alliance for Medical Image Computing Writing a Multi-Threading Filter Insight Toolkit - Advanced Course

26 National Alliance for Medical Image Computing Insight Toolkit – Filters Hierarchy itk::ProcessObject public: MultiThreader * GetThreader(); private: MultiThreader m_Threader int m_NumberOfThreads

27 National Alliance for Medical Image Computing Insight Toolkit – Filters Hierarchy itk::ImageSource public: virtual void BeforeThreadedGenerateData(); virtual void AfterThreadedGenerateData(); virtual void ThreadedGenerateData( const OutputImageRegionType & region, int threadId );

28 National Alliance for Medical Image Computing Insight Toolkit – GenerateData() BeforeThreadedGenerateData AfterThreadedGenerateData TGD

29 National Alliance for Medical Image Computing END Insight Toolkit - Advanced Course


Download ppt "NA-MIC National Alliance for Medical Image Computing ITK Workshop October 5-8, 2005 Multi-Threading."

Similar presentations


Ads by Google