Transactional Language Constructs for C++ - Justin Gottschlich

crashclappergapΛογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 3 χρόνια και 8 μήνες)

69 εμφανίσεις

Transactional Language Constructs for
C++ (N3341)

Gottschlich et al.





Justin Gottschlich

and the C++ TM Drafting Group

Technical Report Proposal:


Transactional
Language
Constructs (TLC) for
C++

vs.

2

Transactional Language Constructs for
C++ (N3341)

Gottschlich et al.

Motivation


Parallel programming
with locks is hard



Why?


Too few locks


Too many locks


Wrong order


Error condition, acquire / release locks



Locks violate modularity


3

Transactional Language Constructs for
C++ (N3341)

Gottschlich et al.

A Simple Example

int Account::balance()

{


lock(l_);


int val = bal_;


unlock(l_);


return val;

}

class Account

{

public:


void withdraw(int amt)


{


lock(l_);


bal_
-
= amt;


unlock(l_);


}



void deposit(int amt)


{


lock(l_);


bal_ += amt;


unlock(l_);


}

private:


int bal_; lock_type l_;

};

Account chk, sav;


void transfer(int amt)

{


chk.withdraw(amt);


sav.deposit(amt);

}




//
---------------------------------------


// Move $100 from checking to savings



//



// Assume: chk.bal_ = 100, sav.bal_ = 0


//


// S1: chk.bal_ = 100, sav.bal_ = 0


// S2: chk.bal_ = 0, sav.bal_ = 100


//
---------------------------------------



T1 (transfer(100)) T2


------------------

------------------



chk.withdraw(100);


chk.balance(); // 0


sav.balance(); // 0


sav.deposit(100);


time

4

Transactional Language Constructs for
C++ (N3341)

Gottschlich et al.

Now With Transactions

class Account

{

public:


void withdraw(int amt){


__transaction


{ bal_
-
= amt; }


}


void deposit(int amt){


__transaction


{ bal_ += amt; }


}


int balance(){


__transaction


{ return bal_; }


}

private:


int bal_;

};

Account chk, sav;


void
transfer(int
amt)

{


__transaction


{


chk.withdraw(amt
);


sav.deposit(amt
);


}

}


//
---------------------------------------


// Assume: chk.bal_ = 100, sav.bal_ = 0


//


// S1: chk.bal_ = 100, sav.bal_ = 0


// S2: chk.bal_ = 0, sav.bal_ = 100


//
---------------------------------------



T1 T2


----------------

------------------









__transaction


{



transfer(100); chk.balance();


sav.balance();


}



chk.bal_ = 100

sav.bal_ = 0

chk.bal_ = 0

sav.bal_ = 100

time

5

Transactional Language Constructs for
C++ (N3341)

Gottschlich et al.

Achieving Modularity: Part I


Specify
what
is synchronized, not

how







Benefits


Higher level of abstraction; improves
programmability


Separate interface from implementation


Open
-
ended
HW / SW optimizations

__transaction

{


int tmp = y;


y = x;


x = tmp;

}

lock(aLock);


int tmp = y;

y = x;

x = tmp;


unlock(aLock);

6

Transactional Language Constructs for
C++ (N3341)

Gottschlich et al.

Achieving Modularity: Part II


Transactions are
composable









Benefits


Components can be combined together

__transaction

{


foo();


bar();

}

void foo()

{


__transaction


{ /* ... */ }

}

void bar()

{


__transaction


{ /* ... */ }

}

lock(???);

foo();

bar();

unlock(???);

void foo()

{


lock(fooL);


unlock(fooL);

}

void bar()

{


lock(barL);


unlock(barL);

}

7

Transactional Language Constructs for
C++ (N3341)

Gottschlich et al.

Benefits to C++


Adding TLC will:


improve feature set of concurrent libraries


make C++ easier to teach and learn


supply a programming model for future hardware



Follows spirit of C++

8

Transactional Language Constructs for
C++ (N3341)

Gottschlich et al.

TLC Is
Not A Silver Bullet


Bad news:


Synch. exist in legacy code


Managing
memory has a cost


Spatial penalty


Temporal penalty


Need
SW for
unbounded
transactions


Good
news:


TLC works with C++11 concurrency


~
10 years of
optimization research


HTM on the horizon (IBM, Intel, Oracle)


9

Transactional Language Constructs for
C++ (N3341)

Gottschlich et al.


The Draft Specification of
Transactional Language
Constructs for C++

10

Transactional Language Constructs for
C++ (N3341)

Gottschlich et al.

10

Contributors


Ali
-
Reza Adl
-
Tabatabai, Intel

Hans Boehm, HP

Calin Cascaval, IBM

Steve Clamage, Oracle

Robert Geva, Intel

Justin Gottschlich, Intel

Victor Luchangco, Oracle

Virendra Marathe, Oracle

Maged Michael, IBM

Mark Moir, Oracle

Ravi Narayanaswamy, Intel

Clark Nelson, Intel

Yang Ni, Intel

Daniel Nussbaum, Oracle

Torvald Riegel, Red Hat

Tatiana Shpeisman, Intel

Raul Silvera, IBM

Xinmin Tian, Intel

Douglas Walls, Oracle

Adam Welc, Intel

Michael Wong, IBM

Peng Wu, IBM

11

Transactional Language Constructs for
C++ (N3341)

Gottschlich et al.

Brief History


Work started in 2008: IBM, Intel, Sun


August 2009: Released version 1.0


2009: HP and RedHat join


2009
-
2011: Resolving tough 10% of issues



February 2012


Release of specification version 1.1

12

Transactional Language Constructs for
C++ (N3341)

Gottschlich et al.

Existing Partial
Implementations


Intel STM Compiler


Sun Studio Compiler


IBM AlphaWorks Compiler


GCC 4.7



Case studies using Intel STM Compiler


13

Transactional Language Constructs for
C++ (N3341)

Gottschlich et al.

Existing TM Support

Language Support



Chapel


Clojure


Concurrent Haskell


Fortress


X10


Library Support



C


C#


Java


Perl


Python


OCaml


Scala

14

Transactional Language Constructs for
C++ (N3341)

Gottschlich et al.

Why Not a Library?


Annotate each read and write


Preprocessor macros


Notable challenges:


Terminating in non
-
normal ways


Compiler optimizations


Hard to separate impl. from interface

__transaction(tx)

{


int tmp = tx.read(y);


tx.write(y) = tx.read(x);


tx.write(x) = tmp;

}

__transaction

{


int tmp = y;


y = x;


x = tmp;

}

15

Transactional Language Constructs for
C++ (N3341)

Gottschlich et al.

Conclusion


Specification


~4 years / 20+ experts / 5 companies


Four compiler partial reference implementations


Case studies of specification



Adding TLC to C++ will:


improve modularity of concurrent libraries


enable greater functionality


make C++ easier to teach and learn


supply a programming model for future hardware


16

Transactional Language Constructs for
C++ (N3341)

Gottschlich et al.

Thank you! Questions?



Collaborators for this proposal


Hans
Boehm, Victor Luchangco, Maged Michael,
Mark Moir, Clark Nelson, Torvald Riegel, Tatiana
Shpeisman, and Michael Wong


Justin Gottschlich

(justin.e.gottschlich@intel.com)