IBM XL C/C++ for Transactional Memory for AIX
A complete programming environment for developing scalable parallel applications using software transactional memory (STM).
Date Posted: May 29, 2008
|
|
 |
 |
|
 |  Transactional memory is a set of declarative constructs that provide data-dependent synchronization for multi-threaded programs. This programming model can be an alternative to conventional synchronization methods such as locks and critical sections. | | |
 |  STM is a run-time library that manages memory accesses executed inside transactions. The run-time library ensures that the memory state of a transaction is visible to other threads only after the transaction commits. The run-time library relies on the compiler to instrument memory references in transactions into STM run-time calls to be monitored by the run-time environment. | | |
 |  In order to write a program using transactional memory language extension, users first write a multi-threaded program using, for example, OpenMP directives or thread Libraries, then mark critical sections using #pragma tm atomic. If transactions are invoked in critical sections, they can be annotated by the attributes of transactional memory function at function declaration.
Following is the source code for the complete program foo.c:
#include <omp.h>
extern int usleep(useconds_t ) __attribute__((tm_func_notrans));
int incre(int sum) __attribute__((tm_func)) ;
int incre(int sum)
{
usleep(1);
return sum+1;
}
int main() {
int sum = 0;
printf("Num of OMP threads: %d\n", omp_get_max_threads());
#pragma omp parallel for
for(int i=0; i <100; i++) {
#pragma tm atomic default(trans)
{
sum = incre(sum);
}
}
printf("sum = %d\n", sum);
}
|
Steps for compiling and executing the source program foo.c are as follows:
% stmxlc –qsmp=omp foo.c -lstm
% setenv OMP_NUM_THREADS 4
% ./a.out
[mycomputer] Num of OMP threads: 4
[mycomputer] sum = 100
|
| | |
 |  This release has the following limitations:
- This release (Version 0.9) does not support non-default installation (NDI). It requires that IBM XL C (or C/C++) for Enterprise Edition for AIX be installed in its default location.
- C++ exceptions-handling occurances in a transaction are not supported. An exception may exit the transaction without properly committing or aborting a transaction.
- In this release, the goto statement in the tm atomic region is supported only in the C++ compiler, not the C compiler.
- It is, in general, unsafe to call libraries inside a transaction.
- Transactional memory might not work properly with other synchronization methods (such as locks).
- Software transactional memory might incur a high overhead for instrumented memory references.
- The STM run-time environment supports weak atomicity.
| | |
 |  Please post all comments, questions, or suggestions to the alphaWorks® forum for IBM XL C/C++ for Transactional Memory for AIX.
We will make every effort to reply to the postings, but we cannot guarantee that every posting will receive a reply. No updates or fixes to the compilers will be provided during the alpha phase, but all reported bugs will be considered for resolution in future releases. | | |
 |  The code block associated with #pragma tm atomic has a single entry but can have multiple exits. That is, you cannot jump into the middle of a transaction, but you can exit a transaction through a continue, break, or return statement. Please note that, as a limitation of this alpha release, the goto statement is allowed only in C++.
| | |
 |  The annotations mark functions that can be called by transactions. The compiler must properly instrument these functions so that their memory references can be monitored by the STM run-time environment.
| | |
 |  These functions must be annotated by the attributes tm_func or tm_func_notrans at the function declaration point.
int foo() __attribute__((tm_func)) ;
int foo()
{
...
}
|
| | |
 |  Both indicate that the function can be called in a transaction, but they differ in whether memory references in the function require compiler instrumentation.
Functions annotated by tm_func require the compiler to instrument its memory references, whereas functions annotated by tm_func_notrans do not.
The programmer guarantees that functions annotated with the attribute tm_func_notrans are safe to call from a transactional memory region without the compiler's instrumenting their memory references.
| | |
 |  Yes.
| | |
 |  Yes. The compiler will replace calls to malloc/free routines to proper STM run-time calls. | | |
 |  In general, it is not safe to call libraries in a transaction: The source codes of library calls are not available, so their memory references cannot be instrumented and monitored. Exceptions can be made for libraries
known to access only local or non-shared data (for example, sin()).
| | |
 |  We provide users with four intrinsic functions to control whether a memory reference is transformed to a STM "read" and "write" barrier.
tm_trans_read() and tm_trans_write() are used to directly instrument references in #pragma tm atomic default(notrans) regions or tm_func_notrans functions.
tm_notrans_read() and tm_notrans_write() are used to tell the compiler not to instrument a memory reference in #pragma tm atomic default(trans) regions or tm_func functions. | | |
 |  Please make sure you have installed the prerequisite software, which is one of these compilers: IBM XL C Enterprise Edition for AIX, Version 9.0, or IBM XL C/C++ Enterprise Edition for AIX, Version 9.0.
| | |
 |  Programs are compiled with stmxlc or stmxlc++ and must be explicitly linked with the STM run-time library. There are two versions of STM run-time library:
- -libstm.a is the standard STM library: % stmxlc foo.c -lstm
- -libstmstat.a is the debug version of the STM library for collecting transaction run-time statistics:
% stmxlc foo.c -lstmstat
| | |
 |  Insert stm_print_stats() call in the program and link the program with the debugging version of STM run-time library -lstmstat. In the following example, run-time statistics are put out to 2 files running directly under - stm_stats_tag01_txn_foo-stats.c_22.out (stats for the transaction at line 22 in foo-stats.c) and stm_stats_tag01_all.out (stats for all transactions in the program).
Following is foo-stat.c:
#include <omp.h>
int incre(int sum) __attribute__((tm_func)) ;
int incre(int sum)
{
sleep(1);
return sum+1;
}
int main() {
int sum = 0;
printf("Num of OMP threads: %d\n", omp_get_max_threads());
#pragma omp parallel for
for(int i=0; i <100; i++) {
#pragma tm atomic default(trans)
{
sum = incre(sum);
}
}
stm_print_stats();
printf("sum = %d\n", sum);
}
|
% stmxlc –qsmp=omp foo-stat.c -lstmstat
% setenv OMP_NUM_THREADS 4
% ./a.out
[mycomputer] Num of OMP threads: 4
[mycomputer] sum = 100
|
| | |
 |  Use the compiler option –qreport to get the information in the listing file. The following is an example of using –qreport when compiling an STM program; the instrumentation report is in the "STM Instrumentation Report Section" part of foo.lst.
% stmxlc -qsmp=omp -qreport foo.c -lstm
|
| | |
 |  The option -qnotm turns off the processing of transactional memory. Under this option, transactional memory pragma and function attributes are ignored. Intrinsic functions tm_trans_read, tm_trans_write, tm_notrans_read, and tm_notrans_write are mapped to regular "read" and "write" operations. | | |
 |  This message warns about un-annotated functions called in lexical transactional scopes. It is highly recommended that programmers pay attention to this warning message and annotate the reported function as either tm_func or tm_func_notrans.
The wording of the warning message might differ between the XL C and XL C/C++ compilers:
- Compiled with XL C compiler: "The function foo is called from a transactional context but its prototype is not annotated with attribute tm_func or tm_func_notrans."
- Compiled with XL C/C++ compiler: "The function 'void foo(int)' should be annotated with tm_func or tm_func_notrans function attribute."
| | |
 |  This message warns about calls through function pointers in lexical transactional scopes. It is highly recommended that programmers pay attention to this warning message and ensure that all targets of the reported function pointer be annotated with transactional function attributes.
The warning message is as follows:
- Compiled with XL C compiler: "A call through a function pointer in a transactional context may invoke a function that is not annotated with attribute tm_func or tm_func_notrans."
- Compiled with XL C/C++ compiler: "User should make sure the function that the function pointer 'fpt' points to should be annotated with transactional memory function attribute."
| | |
 |  This error is possibly caused by a missing STM library during the linking. Here are some messages you might see if –lstm is not used at the link:
- ld: 0711-317 ERROR: Undefined Symbol: .stm_desc
- ld: 0711-317 ERROR: Undefined symbol: .stm_begin
- ld: 0711-317 ERROR: Undefined symbol: .stm_end
| | |
 |  This is a C-only message. The problem is possibly caused by mismatching or incorrect parameter types used in the intrinsics. Refer to the Language extensions and user's guide for the supported data types for these intrinsics.
When using TM intrinsics to access pointer types in C, type casting to (void**) or (void*) is typically needed. For example:
char* p[10];
char* val;
...
tm_trans_write((void**) &p[2], (void*) val);
|
IBM, AIX, and alphaWorks are trademarks of IBM Corporation in the United States, other countries, or both.
Other company, product, or service names may be trademarks or service marks of others.
| |
|
|
 |
|
| |