24.05.2014 Views

XL Fortran Enterprise Edition for AIX : User's Guide - IBM

XL Fortran Enterprise Edition for AIX : User's Guide - IBM

XL Fortran Enterprise Edition for AIX : User's Guide - IBM

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

v The result of an application does not depend on the order in which records are<br />

written out or read in.<br />

v Each thread per<strong>for</strong>ms I/O on a different file.<br />

In these cases, results of the I/O operations are independent of the order in which<br />

threads execute. However, you might not get the per<strong>for</strong>mance improvements that<br />

you expect, since the I/O library serializes parallel access to the same logical unit<br />

from multiple threads. Examples of these cases are as follows:<br />

v Each thread per<strong>for</strong>ms I/O on a pre-determined record in a direct-access file:<br />

v<br />

v<br />

do i = 1, 10<br />

write(4, ’(i4)’, rec = i) a(i)<br />

enddo<br />

Each thread per<strong>for</strong>ms I/O on a different part of a stream-access file. Different<br />

I/O statements cannot use the same, or overlapping, areas of a file.<br />

do i = 1, 9<br />

write(4, ’(i4)’, pos = 1 + 5 * (i - 1)) a(i)<br />

! We use 5 above because i4 takes 4 file storage<br />

! units + 1 file storage unit <strong>for</strong> the record marker.<br />

enddo<br />

In the case that each thread operates on a different file, since threads share the<br />

status of the logical units connected to the files, the thread still needs to obtain<br />

the lock on the logical unit <strong>for</strong> either retrieving or updating the status of the<br />

logical unit. However, the thread-safe I/O library allows threads to per<strong>for</strong>m the<br />

data transfer between the logical unit and the I/O list item in parallel. If an<br />

application contains a large number of small I/O requests in a parallel region,<br />

you might not get the expected per<strong>for</strong>mance because of the lock contention.<br />

Consider the following example:<br />

program example<br />

use omp_lib<br />

integer, parameter :: num_of_threads = 4, max = 5000000<br />

character*10 file_name<br />

integer i, file_unit, thread_id<br />

integer, dimension(max, 2 * num_of_threads) :: aa<br />

call omp_set_num_threads(num_of_threads)<br />

!$omp parallel private(file_name, thread_id, file_unit, i) shared(aa)<br />

thread_id = omp_get_thread_num()<br />

file_name = ’file_’<br />

file_name(6:6) = char(ichar(’0’) + thread_id)<br />

file_unit = 10 + thread_id<br />

open(file_unit, file = file_name, status = ’old’, action = ’read’)<br />

do i = 1, max<br />

read(file_unit, *) aa(i, thread_id * 2 + 1), aa(i, thread_id * 2 + 2)<br />

end do<br />

close(file_unit)<br />

!$omp end parallel<br />

end<br />

The I/O library synchronizes retrieving and updating the status of the logical<br />

units while per<strong>for</strong>ming data transfer in parallel. In order to maximize the<br />

parallelism the I/O library provides, it is recommanded to increase the size of<br />

data transfer per I/O request. The do loop, there<strong>for</strong>e, should be rewritten as<br />

follows:<br />

342 <strong>XL</strong> <strong>Fortran</strong> <strong>Enterprise</strong> <strong>Edition</strong> <strong>for</strong> <strong>AIX</strong> : User’s <strong>Guide</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!