SECTION 6 Performance Tuning
WHAT IS IN THIS SECTION? Threadsafe Overview Shared Data, Mutex Locks Mutex Example
Threadsafe Overview By default all subroutine computations are executed serially. This is done to avoid conflicts arising from the use of global variables or common data structures. For extensive/lengthy subroutines it can make sense to code subroutines for threadsafe execution. Significant performance gains (wall clock times) are possible. A subroutine declares itself threadsafe via a call to the utility function ADAMS_DECLARE_THREADSAFE()
Shared Data, Mutex Locks Threads & Shared Data: Avoid all situations where data could be shared such as: global/COMMON variables file access Etc. Mutex Locks: Mutex = Mutual Exclusion lock on a global variable. Modern compilers have libraries with mutex functionality: Windows: windows.h provides access to functions like CreateMutex() and WaitForSingleObject() Linux: pthreads.h contains similar functions
Mutex Example Example: modifying a global variable: #include <windows.h> /* use pthreads.h for Linux */ HANDLE ghMutex; /* handle to mutex object */ int gCounter; /* global variable to be shared */ … /* Update global variable using mutex lock: */ WaitForSingleObject(ghMutex, INFINITE); gCounter += 1; ReleaseMutex(ghMutex);