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.

Passing <strong>Fortran</strong> Files through the C Preprocessor<br />

A common programming practice is to pass files through the C preprocessor (cpp).<br />

cpp can include or omit lines from the output file based on user-specified<br />

conditions (“conditional compilation”). It can also per<strong>for</strong>m string substitution<br />

(“macro expansion”).<br />

<strong>XL</strong> <strong>Fortran</strong> can use cpp to preprocess a file be<strong>for</strong>e compiling it. If you are also<br />

using one of the optimizing preprocessors, cpp is called be<strong>for</strong>e any of the other<br />

preprocessors.<br />

To call cpp <strong>for</strong> a particular file, use a file suffix of .F. If you specify the -d option,<br />

each .F file filename.F is preprocessed into an intermediate file Ffilename.f, which is<br />

then compiled. If you do not specify the -d option, the intermediate file name is<br />

/tmpdir/F8xxxxxx, where x is an alphanumeric character and tmpdir is the contents<br />

of the TMPDIR environment variable or, if you have not specified a value <strong>for</strong><br />

TMPDIR, /tmp. You can save the intermediate file by specifying the -d compiler<br />

option; otherwise, the file is deleted. If you only want to preprocess and do not<br />

want to produce object or executable files, specify the -qnoobject option also.<br />

When <strong>XL</strong> <strong>Fortran</strong> uses cpp <strong>for</strong> a file, the preprocessor will emit #line directives<br />

unless you also specify the -d option. The #line directive associates code that is<br />

created by cpp or any other <strong>Fortran</strong> source code generator with input code that<br />

you create. The preprocessor may cause lines of code to be inserted or deleted.<br />

There<strong>for</strong>e, the #line directives that it emits can be useful in error reporting and<br />

debugging, because they identify the source statements found in the preprocessed<br />

code by listing the line numbers that were used in the original source.<br />

The _OPENMP C preprocessor macro can be used to conditionally include code.<br />

This macro is defined when the C preprocessor is invoked and when you specify<br />

the -qsmp=omp compiler option. An example of using this macro follows:<br />

program par_mat_mul<br />

implicit none<br />

integer(kind=8)<br />

integer(kind=8),parameter<br />

integer(kind=8),dimension(N,N)<br />

integer(kind=8)<br />

#ifdef _OPENMP<br />

integer omp_get_num_threads<br />

#endif<br />

::i,j,nthreads<br />

::N=60<br />

::Ai,Bi,Ci<br />

::Sumi<br />

!$OMP<br />

common/data/ Ai,Bi,Ci<br />

threadprivate (/data/)<br />

!$omp parallel<br />

<strong>for</strong>all(i=1:N,j=1:N) Ai(i,j) = (i-N/2)**2+(j+N/2)<br />

<strong>for</strong>all(i=1:N,j=1:N) Bi(i,j) = 3-((i/2)+(j-N/2)**2)<br />

!$omp master<br />

#ifdef _OPENMP<br />

nthreads=omp_get_num_threads()<br />

#else<br />

nthreads=8<br />

#endif<br />

!$omp end master<br />

!$omp end parallel<br />

!$OMP parallel default(private),copyin(Ai,Bi),shared(nthreads)<br />

!$omp do<br />

do i=1,nthreads<br />

call imat_mul(Sumi)<br />

enddo<br />

40 <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!