Fortran Compiler Tutorial Coarray
Fortran Compiler Tutorial Coarray
Contents
Chapter 1: Tutorial: Coarray Fortran Overview
Introduction to Using Coarray Fortran .......................................................... 3
Calculating the Value of Pi using a Monte Carlo Method................................... 3
Sequential Program ...................................................................................4
Building and Running the Sequential Version ................................................. 5
Modifying the Program to Use Coarrays ........................................................ 5
Building and Running the Coarray Version..................................................... 7
Summary of Coarray Fortran....................................................................... 7
Notices and Disclaimers..............................................................................7
2
Tutorial: Coarray Fortran Overview 1
The coarrays feature of Fortran 2008 provides a Single Program Multiple Data (SPMD) approach to
parallelism, which is integrated into the Fortran language for ease of programming. An application using
coarrays runs the same program, called an image, in parallel, where coarray variables are shared across the
images in a model called Partitioned Global Address Space (PGAS).
This tutorial shows you how to build and run a serial application, and then convert it to run in parallel using
coarrays.
NOTE 32-bit coarrays are deprecated and will be removed in a future release.
About This This tutorial demonstrates writing, building, and running a Fortran application using
Tutorial coarrays.
3
1 Tutorial: Using Coarray Fortran
The area of the circle is πr2, but since the radius (r) is 1, and we know the square’s area is 4 (2x2), the ratio
of the circle’s area to that of the square’s area is π/4. But how do we get the area of the circle? What we can
do is pick random points on the paper and count the points that are within the circle. To make this easier,
we’ll use the center of the square as the origin and generate random values for X and Y coordinates between
zero and 1, so we’re looking at a quarter of the circle/square. If X2+Y2 (distance between the point and the
origin) is less than or equal to 1, the point is within the circle. Count the number of random points that are
within the circle and divide that by the total number of points; the result is π/4. For a more detailed
explanation of this technique, see http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/
compute-pi.html
Sequential Program
Open the sample file: mcpi_sequential.f90 (the sample file is located in the src folder for command-line
builds). The named constants for integer and real kinds are declared using SELECTED_INT_KIND and
SELECTED_REAL_KIND. Select an integer kind that can hold large integers. The num_trials is declared as
the number of trials. It is set to 6,000,000 in this example. The variable total counts the number of points
that are found within the circle.
The Fortran standard intrinsic RANDOM_NUMBER is used to generate the points for testing. The standard does
not say if the random sequence is different for each run of the program, so it is called as RANDOM_SEED with
no arguments. Intel® Fortran uses the time-of-day clock to initialize the random number generator.
The main body of the program is this loop:
! Run the trials. Get a random X and Y and see if the position
! is within a circle of radius 1. If it is, add one to the subtotal
do bigi=1_K_BIGINT,num_trials
call RANDOM_NUMBER(x)
call RANDOM_NUMBER(y)
if ((x*x)+(y*y) <= 1.0_K_SINGLE) total = total + 1_K_BIGINT
end do
At the end of the trials, divide the total by the number of trials and then multiply by four:
! total/num_trials is an approximation of pi/4
print *, "Computed value of pi is",&
REAL(4_K_BIGINT*total,K_DOUBLE)/REAL(num_trials,K_DOUBLE)
NOTE The REAL intrinsic is used to convert the integers to double precision before dividing.
The program includes code to show the elapsed time for the application.
4
Tutorial: Coarray Fortran Overview 1
5
1 Tutorial: Using Coarray Fortran
6
Tutorial: Coarray Fortran Overview 1
1. Execute this code only on image 1.
2. The total (without a coindex) already has the count from image 1, now add in the values from the
other images. Note the [i] coindex.
3. Ensure that the rest of the code is the same as the sequential version.
All of the images exit.
NOTE You can control the number of images through the environment variable:
FOR_COARRAY_NUM_IMAGES.
7
1 Tutorial: Using Coarray Fortran
Performance varies by use, configuration and other factors. Learn more at www.Intel.com/
PerformanceIndex.
Notice revision #20201201
Intel, the Intel logo, Intel Atom, Intel Core, Intel Xeon, Intel Xeon Phi, Pentium, and VTune are trademarks of
Intel Corporation in the U.S. and/or other countries.
*Other names and brands may be claimed as the property of others.
Portions Copyright © 2001, Hewlett-Packard Development Company, L.P.
Microsoft, Windows, and the Windows logo are trademarks, or registered trademarks of Microsoft Corporation
in the United States and/or other countries.
© Intel Corporation.
This software and the related documents are Intel copyrighted materials, and your use of them is governed
by the express license under which they were provided to you (License). Unless the License provides
otherwise, you may not use, modify, copy, publish, distribute, disclose or transmit this software or the
related documents without Intel's prior written permission.
This software and the related documents are provided as is, with no express or implied warranties, other
than those that are expressly stated in the License.