Performing Endian Conversion
The storage in memory of multibyte variables, like longs, floats, reals, and integers, has different byte ordering depending on the system's architecture, which is identified by the endianness of the system. Little Endian byte ordering has the most significant byte ordered last, where Big Endian byte ordering has the most significant byte ordered first. The type of processor used in a system generally indicates the endianness of the system.
Cray Compiler Environment (CCE)
The CCE compiler has a -h byteswapio flag to swap the byte
ordering. This compiler is only available on HPE Cray architectures.
Note: Routines compiled with this option process
all binary files, input or output, as big-endian. To use the Cray CCE
compiler, use the module command on the system to switch to the Cray CCE
compiler environment before compiling.
module swap current_compiler_module cray_compiler_module
Use the module avail command to find actual module names.
Cray CCE also provides access to the FFIO Assign library, which includes the assign command. This command associates options with Fortran unit numbers and file names for use during the library open processing.
For example, in a file in the job execution directory or a project directory,
place the endian-ness of the file(s) to be read and the corresponding unit
number(s). The file is named assign_file, and two big-endian
files are read in at units 1 and 30.
cat > assign_file << EOF
assign -N swap_endian u:1
assign -N swap_endian u:30
EOF
Then, prior to issuing the aprun command in the job script, define the $FILENV environment variable to point to that file, here named assign_file as immediately above:
export FILENV=/path/to/file/assign_file
Read the man page on the assign command for a detailed explanation of its use.
Intel Compiler
The Intel compiler has a -convert big_endian flag to read
and write big-endian files. To use the Intel compiler,
use the module command on the system to switch to the Intel
compiler environment before compiling. For example:
module swap current_compiler_module intel_compiler_module
% ifort -convert big_endian convert_data.f90
GNU Compiler Collection (GNU), NVIDIA, and AMD AOCC Compilers
The GNU, NVIDIA, and AOCC compilers have flags to swap the byte ordering, as follows:
Collection | Flag |
---|---|
GNU | -fconvert=swap |
NVIDIA | -byteswapio |
AOCC | -byteswapio |
Note: Routines compiled with this option will process binary files with opposite byte ordering. Data read from files will have the opposite byte order of the contents of the file once stored in memory. Data written to files will have the opposite byte order of the contents as loaded from memory. To use these flags, use the following commands:
# To use GNU
module swap current_compiler_module gnu_compiler_module
% ftn -fconvert=swap convert_data.f90
# To use NVIDIA
module swap current_compiler_module nvidia_compiler_module
% nvfortran -byteswapio convert_data.f90
# To use AOCC
module swap current_compiler_module aocc_compiler_module
% flang -byteswapio convert_data.f90