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

Slides:



Advertisements
Similar presentations
NA-MIC National Alliance for Medical Image Computing ITK Workshop October 5-8, 2005 Software Design.
Advertisements

NA-MIC National Alliance for Medical Image Computing ITK Workshop October 5-8, 2005 Writing a New ITK Filter.
OpenMP.
MPI Message Passing Interface
Threads Relation to processes Threads exist as subsets of processes Threads share memory and state information within a process Switching between threads.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Threads. Readings r Silberschatz et al : Chapter 4.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
Inter-Process Communication  most OSs provide several abstractions for inter- process communication: message passing, shared memory, etc.  communication.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
CS 241 Section Week #4 (2/19/09). Topics This Section  SMP2 Review  SMP3 Forward  Semaphores  Problems  Recap of Classical Synchronization Problems.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
Operating Systems Chapter 5 Threads. Benefits Responsiveness Resource Sharing Economy Utilization of MP Architectures.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
ITEC 320 C++ Examples.
File ▪ File – Unit of logical storage – Aid in manipulating exact sector of file data ▪ Abstract view of secondary physical storage devices ▪ Without files.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
CS333 Intro to Operating Systems Jonathan Walpole.
Condition Variables u A condition variable is used to wait until a particular condition is true  like in a monitor u Always use condition variables together.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
P4: Multithreaded Programming Zhenxiao Luo CS537 Spring 2010.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
Parallel Processing (CS526) Spring 2012(Week 8).  Shared Memory Architecture  Shared Memory Programming & PLs  Java Threads  Preparing the Environment.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
Threads. Readings r Silberschatz et al : Chapter 4.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads A thread is an alternative model of program execution
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
Threads. Thread A basic unit of CPU utilization. An Abstract data type representing an independent flow of control within a process A traditional (or.
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
2.2 Threads  Process: address space + code execution  There is no law that states that a process cannot have more than one “line” of execution.  Threads:
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
COMP7330/7336 Advanced Parallel and Distributed Computing Thread Basics Dr. Xiao Qin Auburn University
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Introduction to Programming
Auburn University COMP7330/7336 Advanced Parallel and Distributed Computing Thread Basics Dr. Xiao Qin Auburn University.
Threads in C Caryl Rahn.
CS399 New Beginnings Jonathan Walpole.
Threads.
Pointers and References
Dynamic Memory Allocation Reference Variables
Realizing Concurrency using Posix Threads (pthreads)
C Stuff CS 2308.
CS510 Operating System Foundations
Operating System Concepts
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Jonathan Walpole Computer Science Portland State University
Realizing Concurrency using Posix Threads (pthreads)
Pointers and dynamic objects
C Programming - Lecture 5
Functions Reasons Concepts Passing arguments to a function
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Chapter 7: Deadlock CSS503 Systems Programming
Extra C Material Based on material in: The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. 
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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()

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

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; }

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

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

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

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

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; }

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

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

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; }

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

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; }

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

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

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 );

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

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