Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Content
The Basics Parallel Patterns: Tips & Tricks The Dessert: Debugging Tools
Multithreading Basics
What is Thread?
Has Has
How it Works
OS
slices short CPU time for each thread creates an illusion of simultaneous
That
Executes
execution
Synchronization
Condition synchronization When one thread needs to notify one or more other threads that a specific task has been completed Mutual exclusion When multiple threads access a shared resource in such a way that the resource does not become corrupt
How Do We Do Synchronization?
Critical Sections Conditional Variables
Interlocked functions
Waiting functions
Kernel objects
Timers
One-Time initialization
DeadLocks
Deadlock is a situation where two or more threads are waiting for the other to finish and thus neither ever does
Mutual exclusion. Only a limited number of threads may utilize a resource concurrently. Hold and wait. A thread holding a resource requests access to other resources and waits it. No preemption. Resources are released only voluntarily by the thread holding the resource. Circular wait. Several threads in a chain where each thread waits for a resource that holds the next thread.
Parallel Loop
for (int i = 0; i < n; i++) { }
Shared State
To keep our threads alive and application fast we should follow one of the ways:
Synchronization
Immutability
Isolation
Fork/Join
The problem appears when a set of functions or tasks are executed within a shared address space. The solution is to logically create threads (fork), carry out concurrent computations, and then terminate them after possibly combining results from the computations (join).
Sample:Fork/Join
Fork/Join pattern can be easily implemented via Tasks in .NET4.0
static void MyForkJoin(params Action[] actions) { var tasks = new Task [actions.Length]; for (int i = 0; i < actions.Length; i++) { tasks[i] = Task.Factory.StartNew (actions[i]); } Task.WaitAll(tasks); }
Producer/Consumer Pattern
Queue
Producer/Consumer Pattern
Producer Loops and Consumer Loops communicate through the Queue
MapReduce
Is widely used in: Quick search in databases; databases; Advanced Analytical Queries; Queries;
Sample:MapReduce
Map function void Map( String name, String document){ // name document name // document document content for each word w in document: EmitIntermediate(w, EmitIntermediate(w, "1"); }
Grouping pairs
Reduce function void Reduce( String word, Iterator partialCounts){ partialCounts){ // partialCounts a set of grouped pairs int result = 0; for each word in partialCounts: partialCounts: result += parseInt(v); parseInt(v); Emit(AsString(result)); Emit(AsString(result)); }
Event processed
Event processed
Event generated
Out-of-order events
Pessimistic
Top-down
Bottom-up
Recursive Splitting
Data structure class Tree<T> { Tree<T> public Tree<T> Left, Right; // children Tree<T> public T Data; // data for the node } Sequential approach
public void Process Process<T>(Tree<T> tree, Action Action<T> action) { // Process the current node, then left and right action(tree.Data); Process(tree.Left, action); Process(tree.Right, action); }
Debugging Tools
Intel VTune Performance Analyzer (Intels CPUs only) Collects performance data from system or application: Call Graph && Sampling: Eventbased or Timebased . Intel Thread Checker Two modes of operation: Source && Binary Instrumentation
Or You May Use Task Manager, Process Explorer, Process Viewer, Performance Monitor, etc
GetTickCount() vs GetThreadTimes()
GetTickCount(void) GetThreadTimes( hThread, lpCreationTime, lpExitTime, lpKernelTime, lpUserTime )
Retrieves the number of milliseconds that have elapsed since the system was started Counts all the time, no matter which thread worked at the time
Takes into account that thread mightve been suspended It only produces correct values if each thread would consume all of its time-slice
Performance Counters
Provide information as to how well the OS or an app is performing Can help determine system bottlenecks and fine-tune app performance
Over 40 counters for memory performance objects Over 30 counters for process performance objects
Resume
QUESTIONS ?