Correcting Threading Errors with Intel® Thread Checker for Explicit Threads Intel Software College.

Slides:



Advertisements
Similar presentations
Implementing Domain Decompositions Intel Software College Introduction to Parallel Programming – Part 3.
Advertisements

Process A process is usually defined as an instance of a running program and consists of two components: A kernel object that the operating system uses.
Intel Software College Tuning Threading Code with Intel® Thread Profiler for Explicit Threads.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Multi-core Programming Programming with Posix Threads.
INTEL CONFIDENTIAL Deadlock Introduction to Parallel Programming – Part 7.
The Path to Multi-core Tools Paul Petersen. Multi-coreToolsThePathTo 2 Outline Motivation Where are we now What is easy to do next What is missing.
Multi-core Programming Thread Checker. 2 Topics What is Intel® Thread Checker? Detecting race conditions Thread Checker as threading assistant Some other.
1 Tuesday, November 07, 2006 “If anything can go wrong, it will.” -Murphy’s Law.
DISTRIBUTED AND HIGH-PERFORMANCE COMPUTING CHAPTER 7: SHARED MEMORY PARALLEL PROGRAMMING.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
V0.01 © 2009 Research In Motion Limited Introduction to Java Application Development for the BlackBerry Smartphone Trainer name Date.
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for Threads.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
INTEL CONFIDENTIAL Confronting Race Conditions Introduction to Parallel Programming – Part 6.
Threads CNS What is a thread?  an independent unit of execution within a process  a "lightweight process"  an independent unit of execution within.
10/04/2011CS4961 CS4961 Parallel Programming Lecture 12: Advanced Synchronization (Pthreads) Mary Hall October 4, 2011.
Programming Models using Windows* Threads Intel Software College.
Multi-core Programming Thread Profiler. 2 Tuning Threaded Code: Intel® Thread Profiler for Explicit Threads Topics Look at Intel® Thread Profiler features.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Concurrency, Mutual Exclusion and Synchronization.
Multi-core Programming Programming with Windows Threads.
The University of Adelaide, School of Computer Science
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
1. 10/24/ Upon completion of this module, you will be able to: Use Thread Checker to detect and identify a variety of threading correctness issues.
Programming with POSIX* Threads Intel Software College.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Internet Software Development Controlling Threads Paul J Krause.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
Correcting Threading Errors with Intel® Parallel Inspector.
Programming with Windows* Threads Intel Software College.
Lecture 8 Page 1 CS 111 Online Other Important Synchronization Primitives Semaphores Mutexes Monitors.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Barriers and Condition Variables
3/12/2013Computer Engg, IIT(BHU)1 OpenMP-1. OpenMP is a portable, multiprocessing API for shared memory computers OpenMP is not a “language” Instead,
Slides created by: Professor Ian G. Harris Operating Systems  Allow the processor to perform several tasks at virtually the same time Ex. Web Controlled.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Tuning Threaded Code with Intel® Parallel Amplifier.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Chapter 4 – Thread Concepts
Background on the need for Synchronization
Outline Other synchronization primitives
Chapter 4 – Thread Concepts
Other Important Synchronization Primitives
Computer Engg, IIT(BHU)
Intel Software College
Realizing Concurrency using Posix Threads (pthreads)
Intel Software College
Threading And Parallel Programming Constructs
Jonathan Walpole Computer Science Portland State University
Chapter 05. Multithread.
Lecture 2 Part 2 Process Synchronization
Threads Chapter 4.
Concurrency: Mutual Exclusion and Process Synchronization
Concurrency, Processes and Threads
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using Posix Threads (pthreads)
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
26.
CSE 451 Section 1/27/2000.
CS 144 Advanced C++ Programming May 7 Class Meeting
Presentation transcript:

Correcting Threading Errors with Intel® Thread Checker for Explicit Threads Intel Software College

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 2 Intel® Thread Checker Objectives After successful completion of this module you will be able to… Use Thread Checker to detect and identify a variety of threading correctness issues in Windows* threaded applications Determine if library functions are thread-safe

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 3 Intel® Thread Checker Agenda What is Intel® Thread Checker? Detecting race conditions Thread Checker as threading assistant Some other threading errors Checking library thread-safety Other features of Thread Checker

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 4 Intel® Thread Checker Motivation Developing threaded applications can be a complex task New class of problems are caused by the interaction between concurrent threads Data races or storage conflicts More than one thread accesses memory without synchronization Deadlocks Thread waits for an event that will never happen

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 5 Intel® Thread Checker Debugging tool for threaded software Finds threading bugs in Windows*, POSIX*, OpenMP*, and Intel® Threading Building Blocks threaded software Locates bugs quickly that can take days to find using traditional methods and tools Isolates problems, not the symptoms Bug does not have to occur to find it! Plug-in to VTune™ Performance Analyzer Same look, feel, and interface as VTune™ environment

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 6 Intel® Thread Checker Intel® Thread Checker Features Supports several different compilers Intel® C++ and Fortran Compilers, v7 and higher Microsoft* Visual* C++.NET* 2002, 2003 & 2005 Editions Integrated into Microsoft Visual Studio.NET* IDE View (drill-down to) source code for Diagnostics One-click help for diagnostics Possible causes and solution suggestions API for user-defined synchronization primitives

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 7 Intel® Thread Checker Thread Checker: Analysis Dynamic as software runs Data (workload) -driven execution Includes monitoring of: Thread and Sync APIs used Thread execution order Scheduler impacts results Memory accesses between threads Code path must be executed to be analyzed

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 8 Intel® Thread Checker Thread Checker: Before You Start Instrumentation: background Adds calls to library to record information Thread and Sync APIs Memory accesses Increases execution time and size Use small data sets (workloads) Execution time and space is expanded Multiple runs over different paths yield best results Workload selection is important!

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 9 Intel® Thread Checker Workload Guidelines Execute problem code once per thread to be identified Use smallest possible working data set Minimize data set size Smaller image sizes Minimize loop iterations or time steps Simulate minutes rather than days Minimize update rates Lower frames per second Finds threading errors faster!

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 10 Intel® Thread Checker Building for Thread Checker Compile Use dynamically linked thread-safe runtime libraries ( /MD, /MDd ) Generate symbolic information ( /Zi, /ZI, /Z7 ) Disable optimization ( /Od ) Link Preserve symbolic information ( /DEBUG ) Specify relocatable code sections ( /FIXED:NO )

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 11 Intel® Thread Checker Binary Instrumentation Build with supported compiler Running the application Must be run from within Thread Checker Application is instrumented when executed External DLLs are instrumented as used

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 12 Intel® Thread Checker Source Instrumentation More detailed diagnostics Intel® C++ or Fortran Compilers Compile with /Qtcheck Link: c:/Program Files/Intel/VTune/Analyzer/lib Running the application Start in VTune™ environment Start from Windows* command line Data collected in threadchecker.thr results file View results (.thr file) in VTune™ environment Additional DLLs not instrumented or analyzed

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 13 Intel® Thread Checker Intel® Thread Checker Wizard Intel® Thread Profiler Wizard Advanced Activity Configuration Starting Thread Checker 1) Must Select Threading Wizards Intel® Thread Checker Wizard 2) To see these Wizards

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 14 Intel® Thread Checker Thread Checker Diagnostics

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 15 Intel® Thread Checker Diagnostics Grouping

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 16 Intel® Thread Checker Source Code Viewer

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 17 Intel® Thread Checker Diagnostic Help 1) Right-click here... 2) More help!

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 18 Intel® Thread Checker Activity 1a - Potential Energy Build and run serial version Build threaded version Run application in Thread Checker to identify threading problems

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 19 Intel® Thread Checker Dependence Analysis Consider the serial code: Flow dependence between S1 and S2 Value of A updated in S1 is used in S2 Anti dependence between S2 and S3 Value of A is read in S2 before written in S3 Output dependence between S3 and S4 Value of A assigned in S3 must occur before assignment in S4 S1: A = 1.0; S2: B = A ; S3: A = 1/3 * (C – D); S4: A = (B * 3.8) / 2.7;

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 20 Intel® Thread Checker Thread Checker Dependencies Output dependence Write-Write conflict: one thread updates a variable that is subsequently updated by another thread Anti-dependence Read-Write conflict: one thread reads a variable that is subsequently updated by another thread Flow dependence Write-Read conflict: one thread updates a variable that is subsequently read by another thread

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 21 Intel® Thread Checker Race Conditions Execution order is assumed but cannot be guaranteed Concurrent access of same variable by multiple threads Most common error in multithreaded programs May not be apparent at all times

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 22 Intel® Thread Checker Solving Race Conditions Solution: Scope variables to be local to threads When to use Value computed is not used outside parallel region Temporary or “work” variables How to implement OpenMP scoping clauses ( private, shared ) Declare variables within threaded functions Allocate variables on thread stack TLS (Thread Local Storage) API

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 23 Intel® Thread Checker Solving Race Conditions Solution: Control shared access with critical regions When to use Value computed is used outside parallel region Shared value is required by each thread How to implement Mutual exclusion and synchronization Lock, semaphore, event, critical section, atomic… Rule of thumb: Use one lock per data element

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 24 Intel® Thread Checker Windows* HANDLE type Each Windows object is referenced by HANDLE type variables Pointer to kernel objects Thread, process, file, event, mutex, semaphore, etc. Object creation functions return HANDLE Object controlled through its handle Don’t manipulate objects directly

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 25 Intel® Thread Checker Windows* Thread Creation HANDLE CreateThread( LPSECURITY_ATTRIBUTES ThreadAttributes, DWORD StackSize, LPTHREAD_START_ROUTINE StartAddress, LPVOID Parameter, DWORD CreationFlags, LPDWORD ThreadId ); // Out

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 26 Intel® Thread Checker LPTHREAD_START_ROUTINE CreateThread() expects pointer to global function Returns DWORD Calling convention WINAPI Single LPVOID (void *) parameter Thread begins execution of function DWORD WINAPI MyThreadStart(LPVOID p);

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 27 Intel® Thread Checker Destroying Threads Frees OS resources Clean-up if done with thread before program completes Process exit does this for you BOOL CloseHandle(HANDLE hObject);

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 28 Intel® Thread Checker Waiting for a Thread Wait for one object (thread) DWORD WaitForSingleObject( HANDLE hHandle, DWORD dwMilliseconds ); Calling thread waits (blocks) until Time expires Return code used to indicate this Thread exits (handle is signaled) Use INFINITE to wait until thread termination Does not use CPU cycles

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 29 Intel® Thread Checker Waiting for Many Threads Wait for up to 64 objects (threads) DWORD WaitForMultipleObjects( DWORD nCount, CONST HANDLE *lpHandles, // array BOOL fWaitAll, // wait for one or all DWORD dwMilliseconds) Wait for all: fWaitAll==TRUE Wait for any: fWaitAll==FALSE Return value is first array index found

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 30 Intel® Thread Checker Example: Multiple Threads #include const int numThreads = 4; DWORD WINAPI helloFunc(LPVOID arg ) { printf(“Hello Thread\n”); return 0; } main() { HANDLE hThread[numThreads]; for (int i = 0; i < numThreads; i++) hThread[i] = CreateThread(NULL, 0, helloFunc, NULL, 0, NULL ); WaitForMultipleObjects(numThreads, hThread, TRUE, INFINITE); }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 31 Intel® Thread Checker Activity 1b - Potential Energy Fix errors found by Thread Checker

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 32 Intel® Thread Checker Implementation Assistant When implementing threads Obvious shared and private variables can be identified and handled Should you analyze remaining variables for dependencies? What if parallel code is 100’s of lines long? What about variable use in called functions? Can you tell if pointers refer to same memory location? Use Thread Checker as a threading assistant Speculatively insert threading (OpenMP prototype?) Compile and run program in Thread Checker Review diagnostics Update directives and/or restructure Let Thread Checker do the “heavy lifting”

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 33 Intel® Thread Checker Deadlock Caused by thread waiting on some event that will never happen Most common cause is locking hierarchies Always lock and un-lock in the same order Avoid hierarchies if possible DWORD WINAPI threadA(LPVOID arg) { EnterCriticalSection(&L1); EnterCriticalSection(&L2); processA(data1, data2); LeaveCriticalSection(&L2); LeaveCriticalSection(&L1); return(0); } DWORD WINAPI threadB(LPVOID arg) { EnterCriticalSection(&L2); EnterCriticalSection(&L2); EnterCriticalSection(&L1); EnterCriticalSection(&L1); processB(data2, data1) ; processB(data2, data1) ; LeaveCriticalSection(&L1); LeaveCriticalSection(&L1);LeaveCriticalSection(&L2); return(0); return(0);} ThreadA: L1, then L2 ThreadB: L2, then L1

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 34 Intel® Thread Checker Deadlock Add lock per element Lock only elements, not whole array of elements void swap (shape_t A, shape_t B) { lock(a.mutex); lock(b.mutex); // Swap data between A & B unlock(b.mutex); unlock(a.mutex); } typedef struct { // some data things SomeLockType mutex; } shape_t; shape_t Q[1024]; swap(Q[986], Q[34]); Thread 4 swap(Q[34], Q[986]); Thread 1 Grabs mutex 34 Grabs mutex 986

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 35 Intel® Thread Checker Thread Stalls Thread waits for an inordinate amount of time Usually on a resource Commonly caused by dangling locks Be sure threads release all locks held

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 36 Intel® Thread Checker What’s Wrong? int data; DWORD WINAPI threadFunc(LPVOID arg) { int localData; EnterCriticalSection(&lock); if (data == DONE_FLAG) return(1); localData = data; LeaveCriticalSection(&lock); process(local_data); return(0); } Lock never released

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 37 Intel® Thread Checker Windows* Critical Section Lightweight, intra-process only mutex Most useful and most used New type CRITICAL_SECTION cs; Create and destroy operations InitializeCriticalSection(&cs) DeleteCriticalSection(&cs);

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 38 Intel® Thread Checker Windows* Critical Section CRITICAL_SECTION cs ; Attempt to enter protected code EnterCriticalSection(&cs) Blocks if another thread is in critical section Returns when no thread is in critical section Upon exit of critical section LeaveCriticalSection(&cs) Must be from obtaining thread

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 39 Intel® Thread Checker Example: Critical Section #define NUMTHREADS 4 CRITICAL_SECTION g_cs; // why does this have to be global? int g_sum = 0; DWORD WINAPI threadFunc(LPVOID arg ) { int mySum = bigComputation(); EnterCriticalSection(&g_cs); g_sum += mySum; // threads access one at a time LeaveCriticalSection(&g_cs); return 0; } main() { HANDLE hThread[NUMTHREADS]; InitializeCriticalSection(&g_cs); for (int i = 0; i < NUMTHREADS; i++) hThread[i] = CreateThread(NULL,0,threadFunc,NULL,0,NULL); WaitForMultipleObjects(NUMTHREADS, hThread, TRUE, INFINITE); DeleteCriticalSection(&g_cs); }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 40 Intel® Thread Checker Activity 2 - Deadlock Use Intel® Thread Checker to find and correct the potential deadlock problem.

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 41 Intel® Thread Checker Thread Safe Routines All routines called concurrently from multiple threads must be thread safe How to test for thread safety? Use OpenMP and Thread Checker for analysis OpenMP simulator is systematic Use sections to create concurrent execution

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 42 Intel® Thread Checker Thread Safety Example Check for safety issues between Multiple instances of routine1() Instances of routine1() and routine2() Set up sections to test all permutations Still need to provide data sets that exercise relevant portions of code #pragma omp parallel sections { #pragma omp section routine1(&data1); #pragma omp section routine1(&data2); #pragma omp section routine2(&data3); }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 43 Intel® Thread Checker It is better to make a routine reentrant than to add synchronization Avoids potential overhead Two Ways to Ensure Thread Safety Routines can be written to be reentrant Any variables changed by the routine must be local to each invocation Don’t modify globally shared variables Routines can use mutual exclusion to avoid conflicts with other threads If accessing shared variables cannot be avoided What if third-party libraries are not thread safe? Will likely need to control threads access to library

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 44 Intel® Thread Checker Activity 3 – Thread Safety Use OpenMP framework to call library routines concurrently Three library calls = 6 combinations to test A:A, B:B, C:C, A:B, A:C, B:C

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 45 Intel® Thread Checker Instrumentation Levels Instrumentation Level Description Full ImageEach instruction in the module is instrumented to be checked to see if it might generate a diagnostic message. Custom ImageSame as “Full Image” except user can disable selected functions from instrumentation. All FunctionsTurns on full instrumentation for those parts of a module that were compiled with debugging information. Custom FunctionsSame as “All Functions” except user can disable selected functions from instrumentation. API ImportsOnly system API functions that are needed to be instrumented by the tool will be instrumented. No user code is instrumented. Module ImportsDisables instrumentation. This is default on system images, images without base relocations, and images not containing debug information. Higher levels increase memory usage and analysis time, but provide more details Binary instrumentation lowers level from default until successful Manually adjust level of instrumentation to increase speed or control amount of information gathered

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 46 Intel® Thread Checker Large Diagnostics Counts What do you do if you have 5000 diagnostics? Where do you begin debugging? Are all the diagnostic messages equally important/serious? Suggestions for organizing and prioritizing Add “1st Access” column Group by “1st Access” Sort by “Short Description” column

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 47 Intel® Thread Checker Large Diagnostics Counts Add the “1 st Access” column if it not already present

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 48 Intel® Thread Checker Large Diagnostics Counts

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 49 Intel® Thread Checker Large Diagnostics Counts Groups errors reported for the same source line; each group can be seen as the same issue

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 50 Intel® Thread Checker Large Diagnostics Counts Sort on the “Short description”

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 51 Intel® Thread Checker Intel® Thread Checker What’s Been Covered Threading errors are easy to introduce Debugging these errors by traditional techniques is hard Intel® Thread Checker catches these errors Errors do not have to occur to be detected Greatly reduces debugging time Improves robustness of the application

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 52 Intel® Thread Checker