C/C++ and Fortran

 View Only

Create thread-safe Fortran applications with XL Fortran

By Archive User posted Thu September 08, 2011 11:23 AM


Originally posted by: Xing_Xue

With multicore and manycore machines becoming popular nowadays, more and more applications are using multi-threading to take advantage of the hardware capabilities and get better performance.  Fortran applications are no exception.  This blog provides tips on creating thread-safe Fortran applications with XL Fortran.

  1. Like other languages, protect global and heap variables that may be accessed directly or indirectly by multiple threads.  In Fortran, global variables are common blocks and module variables.  Heap variables are ALLOCATABLE objects, pointer arrays, and automatic variables when compiling with the -qsmallstack=dynlenonheap option.
  2. Use the -qnosave and -qthreaded options to compile.  These options ensure that variables and code created by the XL Fortran compiler are thread-safe.  -qthreaded is the default for the xlf_r, xlf90_r, xlf95_r, and xlf2003_r compiler commands.  -qnosave is the default for all these compiler commands except xlf_r. In addition to -qnosave, if you use the ENTRY statement to have an alternate entry point for a subprogram and the xlf_r command to compile, specify the -qxlf77=nopersistent option explicitly to be thread-safe.
  3. Explicit initialization of a variable that is not in a common block implies the SAVE attribute.  This means that all threads shares the same memory for such variables and therefore, they could potentially be a source of race condition.  In the example code below, variable ivar is explicitly initialized with value 10. As a result, ivar has the SAVE attribute implicitly.
            subroutine sub()
             integer :: ivar = 10
  4. Use xlf_r, xlf90_r, xlf95_r, and xlf2003_r commands to link as these commands automatically link in the thread-safe version of the XLF runtime.

1 view