SMP Basics KeyStone Training Multicore Applications Literature Number: SPRPxxx 1 Definition Symmetric MultiProcessing (SMP): A multiprocessing architecture in which multiple CPUs, residing in one cabinet, share the same memory. SMP systems provide scalability. As business increases, additional CPUs can be added to absorb the increased transaction volume. SMP systems range from two to as many as 32 or more processors. However, if one CPU fails, the entire SMP system is down. Clusters of two or more SMP systems can be used to provide high availability (fault resilience). If one SMP system fails, the others continue to operate. 2 SMP Work A Pool of Resources One of the CPUs boots the system and loads the SMP operating system, which brings the other CPUs online. There is only one instance of the operating system and one instance of an application in memory. The operating system uses the CPUs as a pool of processing resources, all executing simultaneously, either processing data or in an idle loop waiting to do something. Speed Up SMP speeds up the processes that can be overlapped such as running multiple applications simultaneously. If an application is multithreaded, which allows for concurrent operations within the application itself, then SMP can improve the performance of that single application. 3 SMP High Points • A single operating system • An application can be distributed between multiple cores • Memories can be global to all threads or local to each thread • Pthreads provide the parallelism 4 Threads • A process is created by the operating system to run an application. Starting a process requires a great amount of overhead. • A process might have multiple procedures each one is somewhat independent from other procedures. A system that can schedule these procedures in an efficient way is multi-thread system. • A thread is defined as an independent stream of instructions that can be scheduled to run as such by the operating system and requires much less overhead than a process. • POSIX threads (pthreads) is an IEEE standard that defines portable (between different hardware) threads. 5 Pthreads • Pthreads are defined as a set of C language programming types and procedure calls, implemented with a pthread.h header/include file and a thread library. • In terms of performance cost, pthreads require much less resources to be created and managed . • All threads within a process share the same address space. Interthread communication is more efficient and easier than inter-process communication. 6 Parallel pthreads Several common models for threaded programs exist: • Master/slaves: a single thread, the master assigns work to other threads, the slaves. Typically, the master handles all input and parcels out work to the other tasks. At least two forms of the master/slaves model are common: static slaves pool and dynamic slaves pool. • Pipeline: a task is broken into a series of sub-operations, each of which is handled in series, but concurrently, by a different thread. An automobile assembly line best describes this model. • Peer: similar to the master/slave model, but after the main thread creates other threads, it participates in the work. 7 Memory Model • All threads have access to the same global, shared memory. • Threads also have their own private data. • Programmers are responsible for synchronizing access (protecting) globally shared data. Thread Thread Shared memory Private memory Private memory Thread Private memory 8 Memory Protection MUTEX • Mutex is an abbreviation for "mutual exclusion.“ • A mutex variable is a "lock" (a semaphore), protecting access to a shared data resource to prevent “race” conditions. • Using MUTEX ensures that when a thread lock a mutex variable, no other thread can access the same variable until the mutex is released. 9 Condition Variables • Condition variables behave like flags, they enable synchronization between threads based on variable value but without the need to explicitly pull the flag value. • The thread gives resources to other threads until the condition variable is ready. • To prevent race condition (when two threads try to access the same conditional variable at the same time), a condition variable is always used in conjunction with a mutex lock. 10 SMP Demo In MCSDK Release Part of the release at: MCSDK_3_15\mcsdk_linux_3_00_03_15\example-applications\smp_test Need to add: #include <time.h> #include <linux/sched.h> 11