From Documentation
Jump to: navigation, search

Reading from c binary files generated by a fortran program

Nick Chepurniy, Ph.D.
e-mail: nickc@sharcnet.ca
HPC Consultant - SHARCNET http://www.sharcnet.ca
Compute Canada http://www.computecanada.org


Overview

Consider the situation where you have a fortran program that carries out intense calculations and then generates a large amount of data for the elements of a matrix.

That data for the matrix needs to be loaded into a PETSc (c-program).

METHODOLOGY

What comes to mind in order to save time and space is to have the fortran program write the data using an unformatted binary file.

That sounds great in theory, but as it turns out each time the fortran write is issued a "record" is written, the record consists of a 4 byte header, then the data, then a trailer that matches the header.

See:

http://local.wasp.uwa.edu.au/~pbourke/dataformats/fortran/

for detailed explanations and examples.

C-program use different procedures for writing out binary files, in fact a much simpler and much more efficient (i.e. no header/trailer markers for each write).

Therefore, the procedure for a c-program to read a fortran generated binary file would be very complicated. Furthermore, the size of the fortran generated binary file would be much larger than one generated by a c-program (because of the header/trailer markers).

Yet, this situation arises in practice and this documents outlines a technique to overcome this difficulties and presents examples illustrating the concepts:

In the tar file: BINARY_FILES_IO.tar you will find preliminary examples in subdirectory SCOPE_of_VARIABLES. These are very simple c-programs illustrating the concepts we need.

The tar file can be found at: Fortran generated binary files read by a c-program

Untar the file BINARY_FILES_IO.tar and follow the commands in the HIST file of subdirectory SCOPE_of_VARIABLES:

       tar xvf BINARY_FILES_IO.tar

In the main directory, i.e. BINARY_FILES_IO you will find a fortran program in file bin_io.f, which generates a binary file. The binary file is generated by making calls to three c-subroutines:

       opn.c
       wrt.c
       cls.c


These c-routines are called from the fortran program and compiled together with the fortran program using the makefile. If your fortran program is LARGE and generates a LARGE amount of date use the makefile makefile_LARGE, by simply copying it into makefile:

       cp makefile_LARGE makefile

then typing:

       make all

The c-subroutine opn.c opens the binary file "bin.14" in binary write mode ("wb"). You can edit opn.c and change "bin.14" to any name you like.

The c-subroutine wrt.c writes all the data to binary file "bin.14" and cls.c closes the "bin.14" file.

Note how the pointer binaryp is defined GLOBALLY in opn.c and then defined as extern in the other two c-routines through the statement:

       extern FILE *binaryp;

All the commands to compile and execute the programs are in the HIST file.

The program read_bin_14.c reads the the binary file "bin.14" and prints the matrix.

Finally, you will find in HIST commands for compiling the c-program io1.c which generates the binary file "bcio" and then reads it back. This is all in c (just for completeness).