0

I have found several questions and answers about this, but among "stream", "recl", HDF,etc. I am a bit lost ( I am quite a newbie ). I apologize if there is somewhere a plain answer to my question. This is my problem: I want to insert into an existing Fortran code a "WRITE" statement that produces an unformatted file that I can subsequently read with another post-processing Fortran code that I have written. With portability I mean that I can do this regardless the compiler and compilation flags used and, ideally, between different platforms (computers). Is this possible? How? If not, what are the compromises that I must accept?

If the anwer is supported by a detailed, but not too complicated, explanation, it would be highly appreciated.

p.s. I want to use unformatted files because they are lighter and the I/O operations should be faster than with formatted files. Correct?

Update #1 From a comment it seems that it is not strictly possible to obtain an unformatted file which is portable to different machines. Therefore, let us assume I want to use a single machine. I am using ifort and gfortran. If with Fotran90 is not possbile, I think I can use Fortran2003. For me it is a bit complicated to control the compilation flags used to compile the original code, but if it is necessary I can work to control that aspect too.

Gippo
  • 65
  • 5
  • 2
    In the strictest sense, no this is not possible - different computers may have differing representations of binary numbers so you are going to have to deal with that at the very least. If you do want it in the strictest sense I would suggest looking into HDF5 or netcdf. However if you only want a small set of platforms which are not that different from each other this may well be possible. It would be helpful to add to your question specific examples of machines and compilers in which you are interested. – Ian Bush Apr 25 '21 at 17:10
  • 1
    And yes, unformatted I/O will typically be quicker and lead to smaller files the formatted – Ian Bush Apr 25 '21 at 17:10
  • Thank you for your answer. Ok, let us consider only one machine. Regarding the compiler, I am using gfortran and ifort. Do you mean if it is compatible with Fortran2003 or not? – Gippo Apr 25 '21 at 17:15
  • 1
    Recent versions of gfortran and ifort support fortran 2003, which may, and I stress may, help through stream I/O see https://stackoverflow.com/questions/8751185/unexpected-padding-in-a-fortran-unformatted-file . Personally my recommendation is if you want portable binary I/O look at hdf5 or netcdf, or similar. – Ian Bush Apr 25 '21 at 17:29
  • Ok, thank you, but I don't really know how to use hdf5. Is it like a library that I need to load and then I can use a specific command to write files in that format? – Gippo Apr 25 '21 at 17:36
  • 3
    If you read and write relatively simple data I would indeed use standard Fortran stream I/O. The complex libraries are not necessary for that, but rather for complex structured data of various datatypes on various kinds of grids. Personally, I do not bother and keep my I/O (even MPI parallel I/O) as simple as possible. – Vladimir F Героям слава Apr 25 '21 at 17:57
  • Thank you Vladimir. Yes, I want to write very simple data (array of numbers). Could you give me more details about how to do this I/O stream? – Gippo Apr 25 '21 at 18:11
  • 2
    @Gippo Many tutorials are available and examples can be found even at Stack Overflow. The write and read commands can be as simple as `write(unit) array` and `read(unit) array`. – Vladimir F Героям слава Apr 25 '21 at 19:53
  • 1
    Stream I/O files aren't be totally portable, but the method is easier than HD5 etc, which require a library outside of Fortran. Use access="stream" in the open statement, and simple read/write statements as already described. Files written with stream are also easier to read with other languages, should that become a consideration. – M. S. B. Apr 26 '21 at 00:26

0 Answers0