Offload Programming: Fortran and C Code Examples

This document describes a set of source code examples that are available as part of the Intel® Composer 2013 package. These examples demonstrate the basic concepts of offload programming for Intel® Xeon Phi™ coprocessors and are installed to the following default location on Linux*. The default value for <install_dir> is /opt/intel/composer_xe_2013.

Fortran code examples: <install-dir>/Samples/en_US/Fortran/mic_samples/LEO_Fortran_intro/
C code examples: <install_dir>/Samples/en_US/C++/mic_samples/intro_sampleC/

C Source Code Examples

Each source file contains a similarly named function that is well commented with a description of the program behavior at the beginning of each file. The main program, implemented in the file sampleC_driver.c, calls each function.

Table 1: C Source Code Examples

File Name

Description

sampleC00.c

Uses a special API call to determine if the code is running in offload mode

sampleC01.c

Uses scalars in an offloaded region

sampleC02.c

Uses scalars and named arrays in an offload region

sampleC03.c

Shows how to control data transfers

sampleC04.c

Shows how to use pointers and associated data

sampleC05.c

Receives  output results into a pointer

sampleC06.c

Uses the” __MIC__” macro to write target-specific code

sampleC07.c

Uses the “__MIC__” macro for data reuse on target

sampleC08.c

Shows how to run OpenMP on the target

sampleC09.c

Uses global variables in a function called from an offloaded region

sampleC09_callee.c

This code is called from the offloaded region in sampleC09.c

sampleC10.c

Demonstrates the use of bit-wise copyable structs

sampleC11.c

Shows how to deal with non-bit-wise copyable structs

sampleC12.c

Executes code on multiple target devices

sampleC13.c

Shows how to perform asynchronous offload and data transfers with “offload_transfer” and “offload_wait”

sampleC14.c

Demonstrates the use of the “alloc” and “into” modifiers

sampleC_driver.c

Contains the main program which calls the functions defined in the above files

Fortran Source Code Examples

Each source file contains a similarly named function that is well commented with a description of the program behavior at the beginning of each file. The main program, implemented in the file leoF00_driver.F90, calls each subroutine.

Table 2: Fortran Source Code Examples

File Name

Description

leoF01_scalar.F90

Uses scalars in an offloaded region

leoF02_global_common.inc

Defines COMMON variables used by leoF02_global.F90

leoF02_global.F90

Uses COMMON variables in an offload region

leoF03_various_clauses.F90

Shows how to control data transfers

leoF04_explicit_shape_array.F90

Shows how to use local explicit-shape arrays

leoF05_assumed_shape_array.F90

Uses an assumed-shape array in an offload region

leoF06_assumed_size_array.F90

Uses an assumed-size array in an offload region

leoF07_deferred_shape_array.F90

Uses deferred-shape arrays (Fortran 95/90 pointers)

leoF08_multi_target.F90

Executes code on multiple target devices

leoF09_subprograms_callees.F90

Defines subprograms called in leoF09_subprograms.F90

leoF09_subprograms.F90

Demonstrates various methods for offloading subprograms

leoF10_alloc_into.F90

Shows how to offload static array slices with the “alloc” and “into” modifiers

leoF11_async.F90

Shows how to perform asynchronous offload and data transfers with “offload_transfer” and “offload_wait”

leoF00_driver.F90

Contains the main program which calls the subroutines defined in the above files

Building the Examples

Before you invoke the compiler, use the shell script to set the environment and then check that it was set properly.  This example uses Intel® Fortran Compiler 13.1.0.

source   /opt/intel/composer_xe_2013/bin/compilervars.sh intel64
ifort  –V
Intel(R) Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 13.1.0.146 Build 20130121
Copyright (C) 1985-2013 Intel Corporation.  All rights reserved.

Next, copy the samples folder to your local directory and use the Makefile to build the program. You can then run the program observe the output.

cp -r /opt/intel/composer_xe_2013/Samples/en_US/Fortran/mic_samples/LEO_Fortran_intro/ .
cd LEO_Fortran_intro
make mic
ifort -openmp  -c leoF00_driver.F90 -o leoF00_driver.o
ifort -openmp  -c leoF01_scalar.F90 -o leoF01_scalar.o
ifort -openmp  -c leoF02_global.F90 -o leoF02_global.o
ifort -openmp  -c leoF03_various_clauses.F90 -o leoF03_various_clauses.o
ifort -openmp  -c leoF04_explicit_shape_array.F90 -o leoF04_explicit_shape_array.o
ifort -openmp  -c leoF05_assumed_shape_array.F90 -o leoF05_assumed_shape_array.o
ifort -openmp  -c leoF06_assumed_size_array.F90 -o leoF06_assumed_size_array.o
ifort -openmp  -c leoF07_deferred_shape_array.F90 -o leoF07_deferred_shape_array.o
ifort -openmp  -c leoF08_multi_target.F90 -o leoF08_multi_target.o
ifort -openmp  -c leoF09_subprograms_callees.F90 -o leoF09_subprograms_callees.o
ifort -openmp  -c leoF09_subprograms.F90 -o leoF09_subprograms.o
ifort -openmp  -c leoF10_alloc_into.F90 -o leoF10_alloc_into.o
ifort -openmp  -c leoF11_async.F90 -o leoF11_async.o
ifort -V leoF00_driver.o leoF01_scalar.o leoF02_global.o leoF03_various_clauses.o leoF04_explicit_shape_array.o leoF05_assumed_shape_array.o leoF06_assumed_size_array.o leoF07_deferred_shape_array.o leoF08_multi_target.o leoF09_subprograms_callees.o leoF09_subprograms.o leoF10_alloc_into.o leoF11_async.o -openmp   -o leoF_intro.out
Intel(R) Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 13.1.0.146 Build 20130121
Copyright (C) 1985-2013 Intel Corporation.  All rights reserved.
GNU ld (GNU Binutils) 2.22.52.20120302
GNU ld version 2.20.51.0.2-5.34.el6 20100205
....
Build complete
Run : leoF_intro.out [--help]

Running the Examples

The Makefile will compile and link the examples into an executable named leoF_intro.out.

./leoF_intro.out

System configuration pre-check
    Checking for Intel(R) Xeon Phi(TM) (Target CPU) devices...
    Number of Target devices installed:      2
    Offload sections will execute on:     Target CPU (offload mode)
LEO_Fortran_intro samples started
    PASS leoF01_scalar
    PASS leoF02_global (Case #1)
    PASS leoF02_global (Case #2)
    PASS leoF03_various_clauses (Case #1)
    PASS leoF03_various_clauses (Case #2)
    PASS leoF04_explicit_shape_array
    PASS leoF05_assumed_shape_array (Case #1 - [dim=3])
    PASS leoF05_assumed_shape_array (Case #2 - [dim=3])
    PASS leoF05_assumed_shape_array (Case #3 - [dim=3])
    PASS leoF05_assumed_shape_array (Case #1 - [dim=6])
    PASS leoF05_assumed_shape_array (Case #2 - [dim=6])
    PASS leoF05_assumed_shape_array (Case #3 - [dim=6])
    PASS leoF05_assumed_shape_array (Case #1 - [dim=9])
    PASS leoF05_assumed_shape_array (Case #2 - [dim=9])
    PASS leoF05_assumed_shape_array (Case #3 - [dim=9])
    PASS leoF06_assumed_size_array (Case #1 - [dim=300])
    PASS leoF06_assumed_size_array (Case #2 - [dim=300])
    PASS leoF06_assumed_size_array (Case #3 - [dim=300])
    PASS leoF06_assumed_size_array (Case #1 - [dim=600])
    PASS leoF06_assumed_size_array (Case #2 - [dim=600])
    PASS leoF06_assumed_size_array (Case #3 - [dim=600])
    PASS leoF06_assumed_size_array (Case #1 - [dim=900])
    PASS leoF06_assumed_size_array (Case #2 - [dim=900])
    PASS leoF06_assumed_size_array (Case #3 - [dim=900])
    PASS leoF07_deferred_shape_array (Case #1)
    PASS leoF07_deferred_shape_array (Case #2)
    PASS leoF07_deferred_shape_array (Case #3)
    PASS leoF08_multi_target (Case #1)
    PASS leoF08_multi_target (Case #2)
    PASS leoF08_multi_target (Case #3)
    PASS leoF09_subprograms (Case #1)
    PASS leoF09_subprograms (Case #2)
    PASS leoF09_subprograms (Case #3)
    PASS leoF10_alloc_into (Case #1)
    PASS leoF10_alloc_into (Case #2)
    PASS leoF10_alloc_into (Case #3)
    PASS leoF11_async (Case #1)
    PASS leoF11_async (Case #2)
    PASS leoF11_async (Case #3)
    PASS leoF11_async (Case #4)
LEO_Fortran_intro samples complete

Tags:
Para obter mais informações sobre otimizações de compiladores, consulte Aviso sobre otimizações.