Docsity

Programming for Multi Core CPUs: Locking and Transactional Memory | CDA 6938, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
School of Electrical Engineering and Computer Science
University of Central Florida
1
Programming for Multi
Programming for Multi-
-Core CPUs:
Core CPUs:
Locking and Transactional Memory
Locking and Transactional Memory
Huiyang Zhou
Slides 1-18 are from Professor Umakishore Ramachandran @ GaTech
2
compute
compute
I/O
I/O result
Needed
(a) Sequential proce ss
compute
thread
I/O result
Needed
(b) Multithreaded process
I/O request
I/O complete
I/O
thread
Example use of threads
Example use of threads -
-1
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Programming for Multi Core CPUs: Locking and Transactional Memory | CDA 6938 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

School of Electrical Engineering and Computer Science University of Central Florida 1

Programming for Multi- Programming for Multi-Core CPUs:Core CPUs:

Locking and Transactional MemoryLocking and Transactional Memory

Huiyang Zhou

Slides 1-18 are from Professor Umakishore Ramachandran @ GaTech

compute

compute

I/O

I/O result Needed

(a) Sequential process

compute thread

I/O result Needed

(b) Multithreaded process

I/O request

I/O complete

I/O thread

Example use of threads - Example use of threads- 1 1

3

Digitizer Tracker Alarm

Example use of threads - Example use of threads- 2 2

Programming Support for ThreadsProgramming Support for Threads

  • creation
    • pthread_create(top-level procedure, args)
  • termination
    • return from top-level procedure
    • explicit kill
  • rendezvous
    • creator can wait for children
      • pthread_join(child_tid)
  • synchronization
    • mutex
    • condition variables

Main thread

thread_create(foo, args)

(a) Before thread creation

main thread

thread_create(foo, args)

(b) After thread creation

foo thread

7

Need for SynchronizationNeed for Synchronization

digitizer()

image_type dig_image;

int tail = 0;

loop {

if (bufavail > 0) {

grab(dig_image);

frame_buf[tail mod MAX]

= dig_image;

tail = tail + 1;

bufavail = bufavail - 1;

tracker() { image_type track_image; int head = 0; loop { if (bufavail < MAX) { track_image = frame_buf[head mod MAX]; head = head + 1; bufavail = bufavail + 1; analyze(track_image); } } }

Problem?

digitizer tracker

bufavail = bufavail – 1; bufavail bufavail = bufavail + 1;

Shared data structure

……

head tail (First valid filled frame in frame_buf)

(First empty spot in frame_buf)

0 frame_buf 99

9

Synchronization PrimitivesSynchronization Primitives

  • lock and unlock
    • mutual exclusion among threads
    • busy-waiting Vs. blocking
    • pthread_mutex_trylock: no blocking
    • pthread_mutex_lock: blocking
    • pthread_mutex_unlock

Fix number 1 – Fix number 1– with lockswith locks

digitizer() { image_type dig_image; int tail = 0; loop { thread_mutex_lock(buflock); if (bufavail > 0) { grab(dig_image); frame_buf[tail mod MAX] = dig_image; tail = tail + 1; bufavail = bufavail - 1; } thread_mutex_unlock(buflock); } }

tracker() ( image_type track_image; int head = 0;

loop { thread_mutex_lock(buflock); if (bufavail < MAX) { track_image = frame_buf[head mod MAX]; head = head + 1; bufavail = bufavail + 1; analyze(track_image); } thread_mutex_unlock(buflock); } } Problem?

13

  • condition variables
    • pthread_cond_wait: block for a signal
    • pthread_cond_signal: signal one waiting thread
    • pthread_cond_broadcast: signal all waiting threads

T1 T

cond_wait (c, m)

blocked cond_signal (c) resumed

T1 T

cond_wait (c, m)

cond_signal (c)

(a) Wait before signal (^) (b) Wait after signal (T1 blocked forever)

Wait and signal with cond Wait and signal withcond varsvars

15

Fix number 4 – Fix number 4– condcond varvar

digitizer() { image_type dig_image; int tail = 0; loop { grab(dig_image); thread_mutex_lock(buflock); if (bufavail == 0) thread_cond_wait(buf_not_full, buflock); thread_mutex_unlock(buflock); frame_buf[tail mod MAX] = dig_image; tail = tail + 1; thread_mutex_lock(buflock); bufavail = bufavail - 1; thread_cond_signal(buf_not_empty); thread_mutex_unlock(buflock); } }

tracker() { image_type track_image; int head = 0; loop { thread_mutex_lock(buflock); if (bufavail == MAX) thread_cond_wait(buf_not_empty, buflock); thread_mutex_unlock(buflock); track_image = frame_buf[head mod MAX]; head = head + 1; thread_mutex_lock(buflock); bufavail = bufavail + 1; thread_cond_signal(buf_not_full); thread_mutex_unlock(buflock); analyze(track_image); } }

This solution is correct so long as there is exactly one producer and one consumer

Gotchas in programming with Gotchasin programming with condcond varsvars

acquire_shared_resource() { thread_mutex_lock(cs_mutex); if (res_state == BUSY) thread_cond_wait (res_not_busy, cs_mutex); res_state = BUSY; thread_mutex_unlock(cs_mutex); } release_shared_resource() { thread_mutex_lock(cs_mutex); res_state = NOT_BUSY; thread_cond_signal(res_not_busy); thread_mutex_unlock(cs_mutex); }

T3 is here

T2 is here

T1 is here

19

Defensive programmingDefensive programming –– retest predicateretest predicate

acquire_shared_resource() { thread_mutex_lock(cs_mutex); T3 is here while (res_state == BUSY) thread_cond_wait (res_not_busy, cs_mutex); T2 is here res_state = BUSY; thread_mutex_unlock(cs_mutex); } release_shared_resource() { thread_mutex_lock(cs_mutex); res_state = NOT_BUSY; T1 is here thread_cond_signal(res_not_buys); thread_mutex_unlock(cs_mutex); }

Defensive programmingDefensive programming –– retest predicateretest predicate

acquire_shared_resource() { thread_mutex_lock(cs_mutex); while (res_state == BUSY) thread_cond_wait (res_not_busy, cs_mutex); T2 is still here (get the lock, fail the condition, wait again) res_state = BUSY; thread_mutex_unlock(cs_mutex); } release_shared_resource() { thread_mutex_lock(cs_mutex); res_state = NOT_BUSY; thread_cond_signal(res_not_buys); thread_mutex_unlock(cs_mutex); } T1 is here

T3 is here (release the lock)

21

Transactional MemoryTransactional Memory

  • Borrow the ‘transaction’ idea from database systems
  • Atomic region
    • All commit or none commit
    • Read set / write set conflict detection
    • Rollback when conflict happens
    • Software implementation using locks
    • Hardware implementation with specialize cache designs

Lock- Lock-based codebased code

digitizer() { image_type dig_image; int tail = 0; loop { thread_mutex_lock(buflock); if (bufavail > 0) { grab(dig_image); frame_buf[tail mod MAX] = dig_image; tail = tail + 1; bufavail = bufavail - 1; } thread_mutex_unlock(buflock); } }

tracker() ( image_type track_image; int head = 0;

loop { thread_mutex_lock(buflock); if (bufavail < MAX) { track_image = frame_buf[head mod MAX]; head = head + 1; bufavail = bufavail + 1; analyze(track_image); } thread_mutex_unlock(buflock); } } Problem?

Challenges of TM Challenges of TM

  • I/O in atomic regions
  • Nested atomic regions
  • Atomic regions conflicting with code in non-atomic

regions

  • Etc.
  • Promising research area with significant challenges