Slide 1

Slide 1 text

Software Transactional Memory Marek Kubica TU München 19.07.2011 This work is licensed under the Creative Commons Attribution 3.0 License. Marek Kubica Software Transactional Memory

Slide 2

Slide 2 text

Problem? Concurrent writes in shared memory T1 reads value T1 computes new value T2 reads value T1 writes new value T2 computes new value T2 overwrites value of T1 Marek Kubica Software Transactional Memory

Slide 3

Slide 3 text

How bad is it? An example program in C using pthreads $ ./broken 1 1000 Number = 1000 $ ./broken 2 1000 Number = 997 $ ./broken 15 1000 Number = 2408 $ ./broken 15 1000 Number = 3228 In short: computation results are nearly random. Marek Kubica Software Transactional Memory

Slide 4

Slide 4 text

Solutions Typical real-world solutions Locking (Mutexes, Semaphores, Spinlocks) Message Passing (MPI) Problems Deadlocks (Dining philosopher problem) Complex programs Marek Kubica Software Transactional Memory

Slide 5

Slide 5 text

Software Transactional Memory Concept All writes run in transactions If a value in the transaction was modied, the transaction is rolled back Transactions get repeated if they don't succeed Optimistic model, ecient with infrequent writes Marek Kubica Software Transactional Memory

Slide 6

Slide 6 text

Fixed version (def number (ref 0)) (def add1 (partial + 1)) (def num-threads (Integer/parseInt (first *command-line-args*))) (def increments (Integer/parseInt (second *command-line-args*))) (defn add-number [field times] (dorun (repeatedly times (fn [] (dosync (alter field add1)))))) (def thread (partial add-number number increments)) (printf "Starting %d threads, each with %d increments\n" num-threads increments) (dorun (apply pcalls (repeat num-threads thread))) (printf "Number = %d\n" @number) (shutdown-agents) Marek Kubica Software Transactional Memory

Slide 7

Slide 7 text

Why does it work? Optimistic model Runs computation, tries to write Succeeds or gets rolled back Ecient implementation: use data structures with O(1) rollback Increased concurrency Pessimistic model Wait for lock, no computation Decreased concurrency Marek Kubica Software Transactional Memory

Slide 8

Slide 8 text

Drawbacks Problems of STM Transactions sometimes hard to get right (inconsistent state) Language support not always great Increased bandwidth usage due to retried transactions Marek Kubica Software Transactional Memory

Slide 9

Slide 9 text

Sources & Questions Source code Minied C code on the left. Compile with gcc -o broken broken.c -pthread Questions? And thank you for listening. Marek Kubica Software Transactional Memory